Repository navigation
fix(client): ignore trailing whitespace when parsing Content-Length - #6136
zichen0116 wants to merge 1 commit into
Conversation
1AbdullahButt
left a comment
There was a problem hiding this comment.
Checked this against lib/dispatcher/client-h1.js on main. The digit loop in onHeaderValue folds every byte, so a trailing 0x20 becomes (0x20 - 0x30) = -16 and corrupts the value, as described.
Skipping SP/HTAB anywhere in the value is only safe because llhttp has already validated the field. Its Content-Length states accept trailing OWS but raise "Invalid character in Content-Length" for anything after it (e.g. 5 0), and leading OWS never reaches the callback. That's a reasonable thing to rely on, but it's an implicit contract, so I'd suggest:
- A short comment next to the
continuesaying that llhttp rejects interior whitespace, so only trailing OWS can show up here. - A negative test in
test/issue-6135.jsthat pins that contract, e.g.Content-Length: 5 0should still fail (llhttp parse error, not a silent50). If a future llhttp upgrade or a lenient flag ever relaxes that, this test would catch the silent miscount. - The code also skips HTAB, but the test table only has
' 'and' '. Adding['tab', '\t']would cover the other branch.
The test setup itself looks right. A raw node:net server is needed because node:http would normalise the header, and the global dispatcher is restored in t.after.
Static review; I didn't run the suite here.
Reviewed with help from Claude Code.
| for (let i = 0; i < buf.length; i++) { | ||
| if (buf[i] === 0x20 || buf[i] === 0x09) { | ||
| continue | ||
| } |
There was a problem hiding this comment.
This seems to ignore also whitespaces in the middle, no?
There was a problem hiding this comment.
Good catch — you're right, the first version skipped whitespace anywhere in the loop, so it would have folded 5 0 into 50. I checked what actually reaches the callback: llhttp strips leading OWS and rejects anything after the digits that isn't trailing whitespace (Invalid character in Content-Length), so 5 0 never gets folded — the request fails instead (surfaced as a content-length mismatch, since the parser aborts before the body is read). But relying on that implicitly was fragile, so I restructured it in the latest push: the loop now strips trailing OWS first and then folds digits only, so interior whitespace can't be skipped by construction. There's a comment documenting the llhttp contract, plus a negative test pinning that 5 0 is rejected for both fetch and request.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6136 +/- ##
==========================================
- Coverage 91.67% 91.66% -0.01%
==========================================
Files 113 113
Lines 44041 44044 +3
==========================================
+ Hits 40373 40375 +2
- Misses 3668 3669 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
b089ede to
12b2376
Compare
|
Thanks @1AbdullahButt — all three points are addressed in the latest push (rebased onto current main):
Also restructured per @mcollina's comment: the loop now strips trailing OWS first and folds digits only, so interior whitespace can't be skipped by construction. |
Fixes #6135
A response whose
Content-Lengthvalue carries trailing whitespace (validoptional whitespace per RFC 9112 §5, e.g.
Content-Length: 5as sentby servers that pad the field to a fixed width) fails with
UND_ERR_RES_CONTENT_LENGTH_MISMATCH, even though the body length matches.This is a regression since #5124: before that change the value went through
parseInt(), which ignores trailing whitespace.Root cause: in
lib/dispatcher/client-h1.js,Parser.onHeaderValueaccumulatesthe Content-Length digit by digit, folding every byte into the value. A space
(0x20) contributes -16, so
5followed by spaces becomes a large garbagenumber and the body-length check at message completion fails.
Fix: skip SP (0x20) and HTAB (0x09) while accumulating the value. The Milo
parser path is unaffected: it reads Content-Length from its own parsed event,
not from this loop.
Verification:
Content-Length: 5with bodyhellofailed for bothfetch()andrequest()withUND_ERR_RES_CONTENT_LENGTH_MISMATCH; both pass after.test/issue-6135.js: 4 tests covering a single trailing space andmultiple trailing spaces, through both
fetch()andClient.request()(raw
node:netserver, sincenode:httpnormalizes header values).test/content-length.jsandtest/fetch/content-length.js:27 passed, 0 failed. ESLint clean on both changed files.