Skip to content

[v6.x] fix(fetch): don't append a duplicate content-length header (#5060) - #5993

Open
f1729 wants to merge 1 commit into
nodejs:v6.xfrom
f1729:backport-fetch-content-length-v6
Open

f1729 wants to merge 1 commit into
nodejs:v6.xfrom
f1729:backport-fetch-content-length-v6

Conversation

@f1729

@f1729 f1729 commented Oct 10, 2026 •

Copy link
Copy Markdown

This relates to...

Backport of the fetch half of #5060 to v6.x. Related: #5500 / #5502.

Rationale

When a request already carries a caller-set Content-Length, undici 6's fetch still appends its own computed value in httpNetworkOrCacheFetch. The two are joined into one header value such as "36, 36".

undici 6's own core accepts that, because parseInt("36, 36") gives 36. But undici 6's fetch is often not dispatched through an undici 6 core: getGlobalDispatcher() reads the shared Symbol.for('undici.globalDispatcher.1'), and that is frequently owned by Node's bundled undici:

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, and fetch skipping the append when the header is already present. Only the validator reached Node's bundled undici, so undici 6 users who set Content-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 by remix-serve with v3_singleFetch) replaces the global fetch with undici 6's. Any library in that process that sets Content-Length, such as arctic in remix-auth-oauth2, breaks on Node 24.18+ and 26. That's how we hit it: an OAuth login broke in production.

Node (bundled undici) undici 6.29.0 fetch + caller-set Content-Length with this patch
24.12.0 (7.16.0) ✅ ✅
24.18.0 (7.28.0) ❌ invalid content-length header ✅
24.21.0 (7.29.1) ❌ ✅
26.11.1 (8.11.2) ❌ ✅

Repro: Node's built-in fetch is used once, so the bundled undici claims the global dispatcher; then undici 6.29.0's fetch is called with an explicit Content-Length.

const http = require('node:http')
const server = http.createServer((req, res) => { req.resume(); req.on('end', () => res.end('ok')) })
server.listen(0, async () => {
  const url = `http://127.0.0.1:${server.address().port}/`
  await fetch(url).then(r => r.text()) // bundled undici claims Symbol.for('undici.globalDispatcher.1')
  const undici6 = require('undici') // 6.29.0
  const body = new TextEncoder().encode('grant_type=authorization_code&code=x')
  try {
    await undici6.fetch(url, { method: 'POST', body, headers: { 'content-length': String(body.byteLength) } })
    console.log('ok')
  } catch (err) {
    console.log(err.cause.message) // "invalid content-length header" on Node 24.18+ / 26
  } finally {
    server.close()
  }
})

#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 .1 directly. Fixing it where the duplicate is produced covers both.

Changes

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

  • fetch no longer sends a duplicated content-length value when the caller sets the header explicitly.

Breaking Changes and Deprecations

N/A

Status

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.
@f1729

f1729 commented Oct 12, 2026

Copy link
Copy Markdown
Author

Hi @trivikr @mcollina 👋 — would one of you be able to take a look when you have a moment? This backports only the fetch half of #5060 to v6.x: the content-length dedupe guard, without the strict validator.

It fixes the invalid content-length header failures when undici 6's fetch is dispatched through Node's bundled undici on Node 24.18+ and Node 26. It's the v6 side of #5500, so it also covers Node 24, which #5502 doesn't. We hit it in production via Remix's installGlobals({ nativeFetch: true }) + arctic OAuth token exchange.

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 v6.x (e.g. run 36106282985): the Node 20 Linux WPT segfault (exit 139), and macOS timeouts in test/fetch/content-length.js and test/client.js.

Thanks!

@mcollina

Copy link
Copy Markdown
Member

Undici v6 is in node 22, undici v7 is in node 24.

@f1729

f1729 commented Oct 12, 2026

Copy link
Copy Markdown
Author

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).

@f1729

f1729 commented Oct 12, 2026 •

Copy link
Copy Markdown
Author

Right — sorry, I could have been clearer. Two paths are affected, and both go through v6's fetch:

  1. Node 22's bundled fetch (v6) + undici v8 imported anywhere in the process, i.e. Importing undici v8 silently breaks Node's bundled fetch via the legacy globalDispatcher bridge (v8's stricter core Request rejects opts the bundled v6 core accepts — e.g. request-set Content-Length) #5500 as reported. The bundled v6 fetch appends the duplicate content-length and the v8 core behind Dispatcher1Wrapper rejects it. This still reproduces on Node 22.21.0 with undici 8.11.2. Once Node 22 picks up a v6 release with this change, it's fixed where the duplicate is produced.
  2. The npm undici@6 package running on Node 24/26. Its fetch dispatches through Node's bundled v7/v8 core via the shared global dispatcher. That's our case: Remix 2's installGlobals({ nativeFetch: true }) installs undici 6's fetch as the global.

I verified both on Node 22.21.0: with v8.11.2 owning the global dispatcher, v6.29.0's fetch with a caller-set Content-Length fails, and the same setup with this patch succeeds. @mcollina

@metcoder95
metcoder95 requested a review from mcollina November 12, 2026 09:59

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants