fix: count empty continuation frames toward the maxFragments limit#2329
Merged
lpinca merged 4 commits intoJun 22, 2026
Merged
Conversation
Previously the maxFragments check in getData() was guarded by `if (data.length)`, so zero-byte continuation frames were never counted against the limit. A malicious peer could send an unlimited number of empty continuation frames without triggering the 'Too many message fragments' error, bypassing the intended DoS guard. Fix by introducing a dedicated `_numFragments` counter that is incremented for every data frame (control frames excluded) before branching on compression or payload length. The counter replaces the two separate `_fragments.length >= _maxFragments` checks (one in the uncompressed path inside getData, one in the decompress callback), giving consistent enforcement across both paths. The counter is reset alongside `_fragments` at the end of each message in dataMessage().
lpinca
requested changes
Jun 22, 2026
Member
There was a problem hiding this comment.
Empty fragments are simply discarded so they cause no memory retention problems. It also does not make sense to send them in the first place.
This allows a remote endpoint to keep the receiver spinning through
startLoopindefinitely, constituting a denial-of-service vector
against any server or client that setsmaxFragments.
This is not a denial of service. The option was added to keep memory usage under control.
Member
|
Thank you anyway. |
Member
|
On second thought this simplifies the code, so I'm good with it. |
lpinca
approved these changes
Jun 22, 2026
lpinca
reviewed
Jun 22, 2026
lpinca
pushed a commit
that referenced
this pull request
Jun 22, 2026
Ensure that empty fragments are counted against the `maxFragments` limit.
lpinca
pushed a commit
that referenced
this pull request
Jun 22, 2026
Ensure that empty fragments are counted against the `maxFragments` limit.
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.
Problem
The
maxFragmentslimit inReceiveris intended to cap the number ofmessage fragments a peer can send before an error is raised with close
code 1008. However, the check was placed inside
if (data.length)ingetData(), so zero-byte continuation frames were never countedagainst the limit.
A peer could send one non-empty opening fragment followed by an
unbounded number of empty (
payload-length = 0) continuation frames(opcode
0x00, FIN = 0). Each frame is parsed ingetInfo()andgetData()with O(1) work per frame, but the limit never fires.This allows a remote endpoint to keep the receiver spinning through
startLoopindefinitely, constituting a denial-of-service vectoragainst any server or client that sets
maxFragments.The same gap existed in the
decompress()callback for compressedfragmented messages.
Fix
Introduce a
_numFragmentscounter (initialized in the constructor andreset in
dataMessage()at the end of every message, alongside_fragments). The counter is incremented once per data frame ingetData(), before branching on whether the frame is compressed orwhether the payload is non-empty. The two previous `_fragments.length
The threshold semantics are preserved: the error fires when the
(_maxFragments + 1)-th frame arrives, matching the previous behaviourfor non-empty frames.
Changes
lib/receiver.js— add_numFragmentscounter; move limit check tocover all data frames regardless of payload length or compression.
test/receiver.test.js— add a test that sendsmaxFragments: 2with one non-empty opening fragment followed by two empty continuation
frames and asserts the second empty frame triggers the error.
Test results