fix(headers): normalize header values by trimming HTTP whitespace - #1898
Open
spokodev wants to merge 1 commit into
Open
fix(headers): normalize header values by trimming HTTP whitespace#1898spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
Per WHATWG Fetch 5.1, Headers append/set/construct must normalize a value (strip leading and trailing HTTP whitespace: 0x09, 0x0A, 0x0D, 0x20) before validating it. node-fetch stored the raw value and validated it un-normalized, so it kept surrounding whitespace and threw ERR_INVALID_CHAR on an otherwise-valid value with a trailing CRLF. Add a normalizeValue helper applied before validation in both the constructor map and the append/set Proxy trap.
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
Headersdoes not normalize header values. The WHATWG Fetch standardrequires
append/set(and construction) to normalize a value —remove leading and trailing HTTP whitespace bytes (0x09 tab, 0x0A LF,
0x0D CR, 0x20 space) — before running the validity check. node-fetch
stored
String(value)verbatim and validated the un-normalized value, soit both keeps surrounding whitespace and rejects values that are valid
after normalization.
Reproduction against Node's built-in
Headersas the oracle:Spec
WHATWG Fetch 5.1 "Headers class":
append(name, value)andset(name, value)run "normalize value"(strip leading/trailing HTTP whitespace) and only then check that the
name is a valid header name and the value is a valid header value.
Root cause
src/headers.jscallsvalidateHeaderValue(name, String(value))andstores
String(value)with no normalization, in two places:result.map(...)over the init pairsappend/setbranch of theProxytrapgrep -rn "\.trim" src/returned nothing.Fix
Add a
normalizeValuehelper that strips leading/trailing HTTPwhitespace, and apply it to the value before validation in both the
constructor and the
append/settrap, storing the normalized value.Tests
Added a
test/headers.jscase asserting that surrounding whitespace istrimmed on
set/append/construct and that a trailing\r\nvalue isaccepted (normalized away) rather than thrown.
' hello '!=='hello').Full suite: header tests all green (16/16). The repo has 3 pre-existing
network/timing failures (chunked-packet split, agent
familyIPresolution) on a clean checkout in this environment; this change does not
add or remove any of them (388 → 389 passing, same 3 failing).