Repository navigation
Conversation
Backport of the fetch part of nodejs#5060 to v6.x. When a request already carries a caller-set Content-Length, fetch appended its own computed value, producing e.g. "36, 36". undici 6's own core tolerated that via parseInt, but undici 6's fetch is commonly dispatched through Node's bundled undici (>= 7.28 on Node 24.18+, the v8 Dispatcher1Wrapper on Node 26) via the shared global dispatcher, which rejects it with "invalid content-length header". The strict validator from nodejs#5060 is intentionally not backported.
|
Hi @trivikr @mcollina 👋 — would one of you be able to take a look when you have a moment? This backports only the It fixes the Since this is my first contribution, the workflows here are waiting for approval. In the meantime I ran the same CI on my fork: https://github.com/f1729/undici/actions/runs/38207184877. The new test passes on Linux, macOS and Windows across Node 18–22. The three failing jobs match failures already seen on Thanks! |
|
Undici v6 is in node 22, undici v7 is in node 24. |
|
Update: I re-ran the 3 flaky jobs and they passed, and the Node builds with undici swapped in (Node 20/22) passed too. The fork run is fully green: https://github.com/f1729/undici/actions/runs/38207184877 (plus https://github.com/f1729/undici/actions/runs/38207184210 for the swapped-in builds). |
|
Right — sorry, I could have been clearer. Two paths are affected, and both go through v6's
I verified both on Node 22.21.0: with v8.11.2 owning the global dispatcher, v6.29.0's |
This relates to...
Backport of the
fetchhalf of #5060 tov6.x. Related: #5500 / #5502.Rationale
When a request already carries a caller-set
Content-Length, undici 6'sfetchstill appends its own computed value inhttpNetworkOrCacheFetch. The two are joined into one header value such as"36, 36".undici 6's own core accepts that, because
parseInt("36, 36")gives36. But undici 6'sfetchis often not dispatched through an undici 6 core:getGlobalDispatcher()reads the sharedSymbol.for('undici.globalDispatcher.1'), and that is frequently owned by Node's bundled undici:.1directly..1is theDispatcher1Wrapperbridge into the v8 core.Either way the duplicated value is rejected with
InvalidArgumentError: invalid content-length header(TypeError: fetch failed).On
main, #5060 fixed both halves: the strict validator, andfetchskipping the append when the header is already present. Only the validator reached Node's bundled undici, so undici 6 users who setContent-Length(e.g.arctic's OAuth token requests,aws4fetch,fetch-sparql-endpoint) now fail on current Node.This is not hypothetical. Remix 2's
installGlobals({ nativeFetch: true })(used byremix-servewithv3_singleFetch) replaces the globalfetchwith undici 6's. Any library in that process that setsContent-Length, such asarcticinremix-auth-oauth2, breaks on Node 24.18+ and 26. That's how we hit it: an OAuth login broke in production.fetch+ caller-set Content-LengthRepro: Node's built-in
fetchis used once, so the bundled undici claims the global dispatcher; then undici 6.29.0'sfetchis called with an explicitContent-Length.#5502 addresses the same mismatch from the other side, by collapsing identical repeated values in the v8
Dispatcher1Wrapper. That only helps Node 26 once Node picks it up, and does nothing for Node 24, where the bundled v7 core owns.1directly. Fixing it where the duplicate is produced covers both.Changes
lib/web/fetch/index.js: only append the computedcontent-lengthwhen the header list doesn't already contain one (same change as fix: reject malformed content-length request headers #5060 onmain).test/fetch/content-length.js: backports fix: reject malformed content-length request headers #5060's test. Because undici 6's own core masks the duplicate, it also asserts the exactcontent-lengthvaluefetchhands to the dispatcher. That assertion fails before this change (['5, 5']) and passes after (['5']).The strict content-length validator from #5060 is intentionally not backported, since it would be a breaking change for 6.x.
Features
N/A
Bug Fixes
fetchno longer sends a duplicatedcontent-lengthvalue when the caller sets the header explicitly.Breaking Changes and Deprecations
N/A
Status