Draft
fix: prevent pointer-wrap and oversized-length OOB reads in WebSocket parser#853
Conversation
- 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
Contributor
There was a problem hiding this comment.
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 -> inttruncation 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 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 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 |
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.
Attacker-controlled WebSocket frames with extreme 64-bit payload lengths could cause pointer arithmetic to wrap in the C parser, invoking
on_frame_bodywithlength ≈ SIZE_MAXand triggering an out-of-bounds read when libhv's C++ layer calledappend/websocket_parser_decodewith that length.http/websocket_parser.cs_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 reachess_body.s_body— pointer-arithmetic wrap fix:p + parser->require <= endwraps whenrequireis huge. Replaced with the size_t-safe form:http/WebSocketParser.cppon_frame_header—size_t→inttruncation:int length = parser->lengthsilently truncates large values. Changed tosize_t length.on_frame_header— MAX_PAYLOAD_LENGTH guard: returns1(halts parsing) for any frame declaring a payload > 16 MB, soon_frame_bodyis never reached with an attacker-supplied oversized length. This is a defence-in-depth layer on top of the C-level fixes.