fix: drop split CRLFCRLF terminator bytes from the header buffer - #218
fix: drop split CRLFCRLF terminator bytes from the header buffer#218spokodev wants to merge 3 commits into
Conversation
When a part's `\r\n\r\n` header terminator is split across two pushes as `...\r` followed by `\n\r\n`, HeaderParser appended the leading `\r` to the header buffer on the first push, then matched the terminator on the second push without removing it. The stray `\r` stayed attached to the last header value, so e.g. `Content-Type: image/png` was reported as `image/png\r`. This made parsing depend on transport chunking: the same bytes produced a different result depending on where the stream was split, corrupting the last header (mimetype, content-transfer-encoding, `filename*`) of any part whenever the terminator happened to break right after the first CR. Track how many terminator-prefix bytes were buffered on the previous push and trim them before finishing. The count is 0 in the common case, so the slice is a no-op there.
|
Thanks for the detailed writeup — I reproduced the bug and the diagnosis is correct. The whole-chunk path ( One correction to the impact section: this isn't intermittent, it's fully attacker-controlled. Sending the body as two TCP segments split at the terminator CR reproduces it 100% of the time through a real So an app that blocklists on Please fix before this landsThe slice assumes the last Repro —
So on the truncation path the patch swaps one wrong output for another. Guarding on if (found) {
if (!this.maxed) {
this.buffer = this.buffer.slice(0, this.buffer.length - splitTail)
}
this._finish()
return end
}That gives up the branchless property, so please add a Also, Otherwise the change is good: with the patch applied the full suite is green (228 passing, 100% coverage), and an exhaustive two-chunk split of a multipart body is invariant at every split point. |
…s dropped bytes When a header block is truncated at maxHeaderSize (this.maxed), the trailing `\r` of a split CRLFCRLF terminator was never appended, so the last splitTail bytes of the buffer are real header content, not the terminator prefix. Slicing them off then corrupts the final value (e.g. `bar` -> `ba`). Guard the slice on !this.maxed; splitTail is only the buffer's tail when nothing was dropped. Also drop `this.nread -= splitTail` — dead, _finish() sets this.nread = 0 next. Adds a dicer-headerparser case with a small maxHeaderSize to cover the maxed branch and keep coverage at 100%.
|
You're right about the truncation path — once
|
When a part's
\r\n\r\nheader terminator is split across twopush()calls as...\rthen\n\r\n, the parser leaves a stray\ron the last header value of that part.Repro (public API)
A direct
HeaderParserrepro:Content-Type: text/plain\r\n\r\npushed as…text/plain\rthen\n\r\nyields the valuetext/plain\r.Cause (
deps/dicer/lib/HeaderParser.js)On the first push, no terminator is found, so the whole chunk — including the leading
\rof the terminator — is appended tothis.buffer. On the next push the split-terminator branch matches (tailends with a prefix of\r\n\r\n,datastarts with the rest), setsappendEnd = 0and finishes, but never removes the\ralready inthis.buffer._parseHeaderonly strips full\r\nline terminators, so the lone trailing\rstays glued to the last header value.Impact
Parsing becomes dependent on transport chunking: the same bytes give a different result depending on where the TCP/HTTP stream is split. The last header of any part (commonly
Content-Type/mimetype, also content-transfer-encoding and the RFC 5987filename*) intermittently carries a trailing\r. Code that compares or switches on the mimetype, or builds a filename, then gets silently wrong data on a fraction of requests. Pure 1-byte chunking does not trigger it (the tail only ever holds one byte), which is why the existing tests missed it.Fix
Track how many terminator-prefix bytes were buffered on the previous push (
splitTail) and trim them fromthis.bufferbefore finishing.splitTailis0in the common case, so the slice is a no-op there (no new branch, branchless to keep coverage intact).Tests
Added a
dicer-headerparsercase for the...\r|\n\r\nsplit asserting the value has no trailing\r. It fails on the current code and passes with the fix. Full unit suite green (211 passing) with coverage above the configured thresholds; an exhaustive 2-chunk split of the repro body is now invariant at every split point.