Commit Graph

5 Commits

Author SHA1 Message Date
Fabian @ Blax Software 92fc215151 BF keep the channel tracking cache in step with the handler
Two bugs in the buffered ws_* tracking writes:

- untrackChannelConnection() read array_keys($this->channel_connections)
  before it unset an emptied channel, so ws_active_channels kept listing a
  channel after its last socket unsubscribed or had its subscribe refused.
  The list is now read after the unset.
- flushCacheBuffer() deletes after it writes. When a channel was emptied
  and joined again in the same loop tick, the buffered delete of
  ws_channel_connections_<name> ran after the buffered write and the cache
  showed no sockets for a channel that had one. A buffered write now
  cancels an earlier buffered delete of the same key.

Tests: the last unsubscribe and a refused re-subscribe of the last tracked
socket drop the channel from ws_active_channels; a channel emptied and
joined in one tick keeps the new socket in its cache entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 12:28:37 +01:00
Fabian @ Blax Software 7c9ae46738 BF accept client events only from members of the channel
handleProtocolMessage() relayed every client-* frame to the named channel
before handleChannelSubscriptions() and shouldRejectMessage() ran, with no
membership check. Client messages are on by default, so a socket whose
private or presence subscribe was just refused, or one that never
subscribed, could push frames to every member. The "Subscription not
established" error went out only after the broadcast, and the frame was
then forked to a controller as well.

- A client event is relayed only when the sender is on the channel (or
  tracked there, as shouldRejectMessage() allows). Otherwise the sender
  gets "<event>:error" "Subscription not established".
- Either way the frame ends there: client events no longer reach a child.
- The legacy Server\WebSocketHandler applies the same membership check and
  now honours enable_client_messages, which it ignored.

Members keep whispering on public, private and presence channels; the
Pusher rule that limits client events to private/presence channels is not
applied, so public-channel whispers keep working.

Tests: a non-member (and, for private-, private-encrypted- and presence-,
one whose subscribe was refused) gets the error and reaches no member;
members' client events still reach the others.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 12:27:44 +01:00
Fabian @ Blax Software d95c85a3d9 BF keep unsubscribe frames from joining private/presence channels
shouldRejectMessage() lets every *.unsubscribe frame through, member or not,
and processFullMessage() then handed the frame's channel to
authenticateConnection(). On a connection's first full auth (the first
non-ping frame, the frame after a refused subscribe, any frame after
C:RESET) loadCachedAuth() and ensureUserIsSet() called saveConnection(), so
{"event":"websocket.unsubscribe","channel":"private-x"} put the socket on
private-x, private-encrypted-x or presence-x and it received the channel's
broadcasts. A later unsigned subscribe found hasConnection() true, skipped
the signature check and tracked the socket. The same frame also undid a
legitimate unsubscribe from a public channel.

- processFullMessage() notes whether the socket is on the channel before the
  auth step and takes it off again (Channel::removeConnection(), no leave
  events) if the auth step added it. This also covers consumer overrides
  that call saveConnection() themselves. The channel is still passed in:
  some overrides call $channel->saveConnection() without a null check.
- loadCachedAuth() and ensureUserIsSet() only refresh a stored connection,
  they never add one.
- handleSubscription() checks the signature of a socket that is on a
  private/presence channel without being tracked there (saved by a hook)
  and refuses it on a mismatch. PrivateChannel::verifySubscription() is the
  new public wrapper around verifySignature(). Tracked members still get a
  no-op on a repeated subscribe.

Tests cover an unsubscribe as the first frame, after a refused subscribe,
after an auth reset and through an override that saves the connection
unconditionally, for private-, private-encrypted- and presence-, each
followed by an unsigned subscribe that must be refused; the public-channel
unsubscribe; and the saved-but-untracked socket with and without a
signature.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 12:25:42 +01:00
Fabian @ Blax Software b066d75b33 BF reject private/presence subscribe with invalid signature
handleSubscription() recorded the socket in $channel_connections and in the
buffered ws_channel_connections_* / ws_active_channels cache before
$channel->subscribe() ran, and swallowed the InvalidSignature it threw. On
the same frame shouldRejectMessage() found the entry and called
saveConnection(), and the client got "websocket.subscribe:response Success".
One subscribe with a bad signature put the socket on the private or presence
channel, where it received broadcasts, and WebsocketService listed it.

- Track the socket only after subscribe() succeeds (or it is already on the
  channel).
- A failed private/presence subscribe answers "<event>:error" with the
  exception's message and code (Invalid Signature, 4009), drops any earlier
  tracking of the socket on that channel, takes it off the channel if the
  subscribe had already saved it, and ends the frame before
  shouldRejectMessage() and the Success reply.
- Public channels behave as before.

Tests assert, after the single subscribe frame, the error reply, no channel
membership, no $channel_connections entry and no cache listing, and that a
later message on the channel does not resubscribe the socket.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 11:56:38 +01:00
Fabian @ Blax Software 093bbe3a44 A tests, documentation for helpers and lifecycle
- Introduced `helpers-and-testing.md` to document global helpers and WebsocketService class usage.
- Created `HandlerLifecycleTest.php` to test the full WebSocket handler lifecycle, including connection management, channel subscriptions, and message routing.
- Added `WebsocketServiceTest.php` to validate state tracking methods in WebsocketService, covering user authentication, channel tracking, and broadcast functionality.
2026-04-02 12:44:16 +02:00