fix: drop split CRLFCRLF terminator bytes from the header buffer#218
Open
spokodev wants to merge 1 commit into
Open
fix: drop split CRLFCRLF terminator bytes from the header buffer#218spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit 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.
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.
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.