Skip to content

WebSocket: bound the received frame length and message size - #110

Open
YuryKor wants to merge 1 commit into
chronoxor:mainfrom
YuryKor:ws-frame-length-limit
Open

YuryKor wants to merge 1 commit into
chronoxor:mainfrom
YuryKor:ws-frame-length-limit

Conversation

@YuryKor

@YuryKor YuryKor commented Dec 1, 2026

Copy link
Copy Markdown

WebSocket::PrepareReceiveFrame takes the payload length from the frame header as is:

  • the declared length is passed to 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;
  • a 64-bit length is not validated, so header + payload can wrap around and the frame is never completed while the buffer keeps growing;
  • on 32-bit platforms the 64-bit length is assembled with (size_t)x << 56, which is undefined behavior there.

Changes:

  • The 64-bit length is read as uint64_t. A length with the most significant bit set is rejected (RFC 6455, 5.2).
  • A maximal received message size is added: WS_DEFAULT_MAX_MESSAGE_SIZE (64 MiB), ws_max_message_size() and SetupWSMaxMessageSize(). It applies to the whole message, so a fragmented message cannot bypass it. The check runs before any payload is buffered.
  • The receive buffers are no longer reserved from the declared length: they grow as the payload arrives.
  • On a rejected frame onWSFrameError(status, message) is called (default: onWSError(message)). WSSession/WSSSession call Close(status) and WSClient/WSSClient call CloseAsync(status) with 1002 (protocol error) or 1009 (message too big). Data received after that is ignored until ClearWSBuffers(), and the synchronous Receive*() 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 PrepareReceiveFrame the 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 with SetupWSMaxMessageSize().

Tested on Windows (MSVC 2022, Ninja): all 31 cppserver-tests test 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.

- 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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant