WebSocket client: mask outgoing frames (RFC 6455 5.1) - #198
Closed
Vizit0r wants to merge 1 commit into
Closed
Conversation
TCrossWebSocket.FMaskingKey is never initialised, so it stays 0. MakeFrameData treats 0 as "do not mask": the MASK bit is left clear and the payload goes out unmasked. RFC 6455 5.1 requires every client-to-server frame to be masked and says the server MUST close the connection on an unmasked one, so the client does not work against a compliant server out of the box -- the handshake succeeds, then the peer closes as soon as the first frame is sent. Add TCrossWebSocketParser.NewMaskingKey next to NewSecWebSocketKey (same CSPRNG, same failure mode) and seed FMaskingKey from it in the constructor. Callers that assign MaskingKey themselves are unaffected.
Owner
|
Thank you for identifying the issue with clients sending unmasked WebSocket frames by default. This issue has been addressed on This PR was not merged directly because RFC 6455 Sections 5.3 and 10.3 require a client to select a new, unpredictable 32-bit masking key for every frame. The current patch generates the key when The replacement implementation:
The issue is therefore resolved by the replacement implementation, and this PR can be closed as superseded. The source branch does not need to be deleted. Thank you again for the report and the initial implementation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TCrossWebSocket.FMaskingKeyis never initialised, so it stays0unless the caller assignsMaskingKeyby hand. AndMakeFrameDatareads0as "do not mask":RFC 6455 5.1 requires every client-to-server frame to be masked, and says the server MUST close the connection on an unmasked one. So the client does not work against a compliant server out of the box.
The failure is confusing to diagnose, because the handshake is fine:
OnOpenfires (101 +Sec-WebSocket-Acceptvalidated), and only then does the peer drop the connection — right after the first frame is sent. What you see is a connect -> open -> close loop with no error anywhere.Fix
Add
TCrossWebSocketParser.NewMaskingKeynext toNewSecWebSocketKey— same unit, same CSPRNG (Utils.CryptRandom, already in its uses), same failure mode — and seedFMaskingKeyfrom it in the constructor:Callers that assign
MaskingKeythemselves are unaffected.One design note
The key is generated once per
TCrossWebSocket, so a reconnect reuses it. Generating it inOpeninstead would give a fresh key per connection, which is closer to the spirit of 5.3 ("the masking key for a given frame MUST NOT be predicted by the server"). I kept it in the constructor to stay minimal — happy to move it if you prefer.Checked
Compiles clean on Win64 (Delphi 37.0) — no new warnings or hints. Verified live against a WSS endpoint that was previously closing the connection ~50 ms after the first frame: with a non-zero key the server accepts the frame and replies.