Repository navigation
Conversation
- Read the 64-bit payload length as uint64_t and reject a length with the most significant bit set (RFC 6455, 5.2). - Add a maximal received message size (64 MiB by default, SetupWSMaxMessageSize), checked before any payload is buffered and applied to the whole fragmented message. - Do not reserve the receive buffers from the declared length. - Call onWSFrameError(status, message) on a rejected frame: sessions and clients close the connection with 1002 or 1009. - Add unit tests for the frame length and message size checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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.
WebSocket::PrepareReceiveFrametakes the payload length from the frame header as is:reserve()of both receive buffers as soon as the header arrives, before any payload, so a peer that sends only a header makes the other side allocate as much as it declares;header + payloadcan wrap around and the frame is never completed while the buffer keeps growing;(size_t)x << 56, which is undefined behavior there.Changes:
uint64_t. A length with the most significant bit set is rejected (RFC 6455, 5.2).WS_DEFAULT_MAX_MESSAGE_SIZE(64 MiB),ws_max_message_size()andSetupWSMaxMessageSize(). It applies to the whole message, so a fragmented message cannot bypass it. The check runs before any payload is buffered.onWSFrameError(status, message)is called (default:onWSError(message)).WSSession/WSSSessioncallClose(status)andWSClient/WSSClientcallCloseAsync(status)with 1002 (protocol error) or 1009 (message too big). Data received after that is ignored untilClearWSBuffers(), and the synchronousReceive*()methods return.tests/test_ws_frame.cpp: unit tests for valid frames (all length forms, masked and unmasked, fragmented messages), the rejected lengths, the default and configured limits, no up-front allocation, and ignoring data after a rejected frame.Compatibility: valid traffic is parsed exactly as before. A differential test fed the old and the new
PrepareReceiveFramethe same random streams of valid frames (all opcodes, fragmented messages, 7/16/64-bit lengths, up to 200,000 bytes, random chunking) and got the same callbacks. Applications that receive messages above 64 MiB need to raise the limit withSetupWSMaxMessageSize().Tested on Windows (MSVC 2022, Ninja): all 31
cppserver-teststest cases pass.Not in this PR (can be a separate issue): when a frame header is split across reads, the header loops read a fixed number of bytes instead of the missing ones, and the mask is collected from index 0 again, so a header split at some offsets is parsed incorrectly.