content-length header collision → InvalidArgumentError: invalid content-length header when a custom undici global dispatcher is installed
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 85/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Quiet
- Tech stack
- node.js, typescript
Research direction
Start in core.js at APIClient.buildHeaders(), around line 250, and inspect how content-length is added when a body is present. Reproduce the POST /v1/sessions request with a custom undici global dispatcher, then verify that the request succeeds without the invalid header error after the header handling is corrected.
Written by the indexing model from the issue text.
Description
Description
@browserbasehq/sdk@2.16.0's APIClient.buildHeaders() (in core.js) explicitly sets a content-length header on outgoing requests before handing the request to fetch. When the consuming application has installed a custom global dispatcher via undici's setGlobalDispatcher() (e.g. to raise the connect timeout — setGlobalDispatcher(new Agent({ connect: { timeout: ... } }))), undici's Request constructor folds the SDK's caller-supplied content-length header together with its own internally computed one into a comma-joined value (observed: "52, 52"), which fails undici's digit-only header validation.
This crashes every POST /v1/sessions call (session creation) for any consumer that installs a non-default undici dispatcher — a common pattern for adjusting timeouts, proxies, or connection pooling.
Reproduction
import Browserbase from "@browserbasehq/sdk";
import { Agent, setGlobalDispatcher } from "undici";
setGlobalDispatcher(new Agent({ connect: { timeout: 120_000 } }));
const bb = new Browserbase({ apiKey: "..." });
await bb.sessions.create({ projectId: "..." });
Error
FETCH FAILED for URL: https://api.browserbase.com/v1/sessions
CAUSE: InvalidArgumentError: invalid content-length header
at processHeader (undici/lib/core/request.js:514:13)
at new Request (undici/lib/core/request.js:264:11)
...
code: 'UND_ERR_INVALID_ARG'
Root cause
APIClient.buildHeaders() sets reqHeaders['content-length'] = contentLength unconditionally when a body is present (core.js, ~line 250). Under a custom dispatcher, undici's Request constructor doesn't dedupe this against the header it computes itself from the real body length the way the default dispatcher's H1 client path does — the two values get folded together at Request construction time, before undici's own H1-client-level content-length recompute-and-rewrite logic (which would correctly override a single caller-supplied value) ever runs.
Environment
@browserbasehq/sdk@2.16.0undici@8.8.0(also reproduces on8.9.0)- Node 22.14.0
Suggested fix
Don't set content-length explicitly in buildHeaders() — let fetch/undici compute it from the request body, which it does correctly and exactly once. We've verified removing that one assignment resolves the crash with no other side effects.
Workaround
We're currently working around this downstream by monkeypatching APIClient.prototype.buildHeaders at runtime to strip the contentLength argument before it reaches the header-building logic.
- Dominant language
- TypeScript
- Stars
- 64
- Forks
- 17
- Avg merge
- 13m
- Merged PRs (30d)
- 4
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from browserbase/sdk-node
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
browserbase/sdk-node#202 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
browserbase/sdk-node#193 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
browserbase/sdk-node#180 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 66/100
browserbase/sdk-node#218 · 2 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
browserbase/sdk-node#214 ·
All issues in browserbase/sdk-node
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Eynzof/Hermes-CN-Desktop#610 ·
-
bug clawsweeper:linked-pr-open clawsweeper:needs-live-repro clawsweeper:no-new-fix-pr impact:message-loss issue-rating: 🐚 platinum hermit P2 regression
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
calcite-components needs triage refactor
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Esri/calcite-design-system#15203 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
fullcalendar/fullcalendar#8106 ·