fix: clamp a suffix range whose length exceeds the representation#66
Merged
Merged
Conversation
`bytes=-N` where N is larger than the representation size was rejected as
unsatisfiable instead of using the whole representation:
parse(100, 'bytes=-101') // -1 (expected [{ start: 0, end: 99 }])
parse(100, 'bytes=-500') // -1
RFC 7233 §2.1: "If the selected representation is shorter than the
specified suffix-length, the entire representation is used." The parser
already clamps the analogous last-byte-pos overflow (`bytes=0-9999`); the
suffix form just missed it, letting `start = size - suffixLength` go
negative and get dropped. Clamp that start to 0. `bytes=-0` and an empty
representation stay unsatisfiable.
bjohansebas
approved these changes
Jun 25, 2026
bjohansebas
left a comment
Member
There was a problem hiding this comment.
This change looks good to me, just for the next major version.
blakeembrey
reviewed
Jun 25, 2026
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
A
bytes=-Nsuffix range whose length is larger than the representation size is rejected as unsatisfiable, instead of using the whole representation:Cause
RFC 7233 §2.1: "If the selected representation is shorter than the specified suffix-length, the entire representation is used."
The suffix branch computes
start = size - suffixLength, which goes negative when the suffix is larger than the representation, and the negative start is then dropped by thestart < 0unsatisfiable check:The parser already clamps the analogous overflow for the
first-lastform (if (end > size - 1) end = size - 1, e.g.bytes=0-9999on a 100-byte representation); the suffix form just missed the same clamp.Fix
Clamp the suffix start to
0, so an over-long suffix uses the whole representation.bytes=-0(start becomessize,start > end) and an empty representation stay unsatisfiable.Verification
parse(100, 'bytes=-101')is[{ start: 0, end: 99 }]. It fails onmasterand passes with the fix; the existing suite is unchanged (35 passing).bytes=-0and zero-length representations remain unsatisfiable.