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>
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>
- 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.