Skip to content

fix: prevent pointer-wrap and oversized-length OOB reads in WebSocket parser#853

Draft
ithewei with Copilot wants to merge 3 commits into
masterfrom
copilot/fix-oversized-body-length-issue
Draft

fix: prevent pointer-wrap and oversized-length OOB reads in WebSocket parser#853
ithewei with Copilot wants to merge 3 commits into
masterfrom
copilot/fix-oversized-body-length-issue

Conversation

Copilot AI commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Attacker-controlled WebSocket frames with extreme 64-bit payload lengths could cause pointer arithmetic to wrap in the C parser, invoking on_frame_body with length ≈ SIZE_MAX and triggering an out-of-bounds read when libhv's C++ layer called append/websocket_parser_decode with that length.

http/websocket_parser.c

  • s_length — RFC 6455 §5.2 enforcement: after accumulating the extended length bytes, reject any frame whose declared length has the MSB set (the spec requires it to be 0). Stops the exploit before the frame ever reaches s_body.

    if (parser->length > ((size_t)~(size_t)0 >> 1)) {
        return GET_NPARSED();
    }
  • s_body — pointer-arithmetic wrap fix: p + parser->require <= end wraps when require is huge. Replaced with the size_t-safe form:

    // before
    if(p + parser->require <= end)
    // after
    if(parser->require <= (size_t)(end - p))

http/WebSocketParser.cpp

  • on_frame_headersize_t→int truncation: int length = parser->length silently truncates large values. Changed to size_t length.

  • on_frame_header — MAX_PAYLOAD_LENGTH guard: returns 1 (halts parsing) for any frame declaring a payload > 16 MB, so on_frame_body is never reached with an attacker-supplied oversized length. This is a defence-in-depth layer on top of the C-level fixes.

Copilot AI added 2 commits July 22, 2026 12:16
- websocket_parser.c s_length: reject 64-bit payload lengths with MSB
  set per RFC 6455 §5.2 before the frame is accepted, preventing
  integer-wrap on the subsequent pointer check
- websocket_parser.c s_body: replace `p + parser->require <= end`
  (pointer-arithmetic wrap on huge require) with the size_t-safe
  `parser->require <= (size_t)(end - p)`
- WebSocketParser.cpp on_frame_header: change `int length` to
  `size_t length` to eliminate the size_t→int truncation; return 1
  (stops parsing) for any frame whose declared length exceeds
  MAX_PAYLOAD_LENGTH (16 MB), so on_frame_body is never reached with
  an attacker-controlled oversized length

Closes #852
Copilot AI changed the title [WIP] Fix oversized body length issue in WebSocketParser fix: prevent pointer-wrap and oversized-length OOB reads in WebSocket parser Jul 22, 2026
Copilot AI requested a review from ithewei July 22, 2026 12:21
@ithewei
ithewei requested a review from Copilot July 23, 2026 03:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens libhv’s WebSocket frame parsing against attacker-controlled oversized payload lengths that could previously lead to pointer-arithmetic wrap and out-of-bounds reads (and related crash/DoS scenarios) across the C parser and the C++ wrapper.

Changes:

  • Adds RFC 6455 §5.2 validation to reject 64-bit payload lengths with the MSB set in the C WebSocket parser.
  • Fixes a pointer-arithmetic wrap hazard in the C parser by switching to a remaining-bytes comparison (require <= end - p).
  • Updates the C++ wrapper to avoid size_t -> int truncation and introduces a maximum payload length guard.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
http/WebSocketParser.cpp Adds size_t-safe length handling and a max payload length rejection guard in the C++ wrapper callbacks.
http/websocket_parser.c Adds RFC length validation and fixes the body-availability check to avoid pointer-wrap issues in the C parser.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread http/websocket_parser.c
Comment on lines +110 to +115
/* RFC 6455 §5.2: the most significant bit of the 64-bit payload
* length MUST be 0. Reject frames that violate this to prevent
* integer-wrap exploits on the pointer check below. */
if (parser->length > ((size_t)~(size_t)0 >> 1)) {
return GET_NPARSED();
}
Comment thread http/WebSocketParser.cpp
Comment on lines +15 to +19
size_t length = parser->length;
if (length > (size_t)MAX_PAYLOAD_LENGTH) {
return 1; // reject oversized frames; stops parsing before s_body is entered
}
size_t reserve_length = length + 1; // safe: length <= MAX_PAYLOAD_LENGTH (16M) so no overflow
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.

WebSocketParser: oversized body length after pointer wrap in vendored websocket_parser (related php-ion#5)

3 participants