fix(websocket): enforce MAX_PAYLOAD_LENGTH cap on message accumulation - #875
Open
krleejihyeong wants to merge 1 commit into
Open
fix(websocket): enforce MAX_PAYLOAD_LENGTH cap on message accumulation#875krleejihyeong wants to merge 1 commit into
krleejihyeong wants to merge 1 commit into
Conversation
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.
Summary
In
WebSocketParser.cpp,on_frame_body()appends every received frame'spayload into
wp->messagewith no upper bound. TheMAX_PAYLOAD_LENGTH(16MB) constant is defined but only used as a
reserve()hint inon_frame_header()— it never actually caps accumulation. In addition,on_frame_header()only clearswp->messagewhen the state isWS_FRAME_BEGINorWS_FRAME_FIN, so a stream ofFIN=0(fragmented)frames never triggers a clear and keeps accumulating indefinitely.
Since the WebSocket handshake is an unauthenticated HTTP Upgrade, an
unauthenticated remote attacker can keep a single connection open and
keep sending non-FIN frames to grow the server's memory usage without
bound.
I reproduced this locally: sending 40 x 1MB non-FIN frames over a single
connection grew the server process RSS from a baseline of 4,352 kB to
76,956 kB (+72MB) and counting.
Root cause
Fix
Added a check in
on_frame_body(): if the incoming chunk itself, or theaccumulated message size after appending it, would exceed
MAX_PAYLOAD_LENGTH, the parser callback returns non-zero. This stopswebsocket_parser_executeand the caller (HttpHandler::FeedRecvData)then closes the connection.
Testing
without bound (+72MB and climbing in my test)
the accumulated size would exceed the cap; server RSS stays at
baseline (~4,396 kB) and the process keeps running normally afterward
correctly after the patch