fix(extension): merge context and page extra HTTP headers
why
context.setExtraHTTPHeaders() and page.setExtraHTTPHeaders() each send only their own headers, and CDP's Network.setExtraHTTPHeaders replaces the whole set, so whichever one runs last wipes out the other. Separately, when a page adopts a cross-site iframe's session it sends its own headers even when they're the default {}, which clears the context headers that session got before it resumed, so requests the iframe makes after loading go out without them. Fixes #3459.
what changed
Pagekeeps a copy of the context's headers (contextExtraHTTPHeaders) and builds the set to send withmergedExtraHTTPHeaders(): the context's headers with the page's own on top. Header names are compared case-insensitively, so a page header also replaces a context header that only differs in case.Page.setExtraHTTPHeaders()sends the merged set to all of its sessions.BrowserContext.setExtraHTTPHeaders()updates every page's copy and sends each session its owning page's merged set (sessions without a page still get the context headers). A newly created page picks up the current context headers when it's registered.Page.adoptOopifSession()sends the merged set, and skips sending when it's empty so it can't erase the context headers.- One sentence in the context and page reference docs (TypeScript, Python, Go) saying the two sets are sent together and the page's value wins.
No protocol or SDK code changes, so the Python and Go SDKs get this through the extension. The changeset bumps the extension and both SDKs as patches, like other extension-only fixes.
test plan
New integration tests in pageExtraHttpHeaders.test.ts and contextExtraHttpHeaders.test.ts check the request headers a local server actually receives:
- context headers survive setting page headers, and page headers survive setting context headers later;
- a page header wins over a context header with the same name (different casing);
- clearing page headers keeps the context headers;
- in a cross-site iframe (
--site-per-process,child.testmapped to 127.0.0.1, the same setup asactIframeAttachment.test.ts), a fetch the iframe makes after loading still has the context headers, and the page headers too when both are set.
Results:
- On main, the 6 new tests fail (the missing header comes back
undefined) and the 3 existing ones pass. With the fix, all 9 pass. - I ran the new header tests 5 more times together with
iframeNetworkCaptureandactIframeAttachment(12 tests per run), and they passed every time. just checkpasses.pnpm testpasses, including the extension unit tests (1212) and the SDK unit and browser tests.pnpm test:integrationpasses in full (259 tests in 40 files), as do the docs unit tests.- On the Python side, the SDK suite passes in full on Python 3.11, the version in
packages/sdk-python/.python-version(1096 passed, 10 skipped), and so do the core middleware (147), Deep Agents (12 and 61) and catalog cookbook tests. - I also ran the three scenarios end to end through the Python SDK against a local server. On main, the request was missing the context header after setting page headers, and the cross-site iframe's fetch had no headers at all. With this branch, both sets arrive, the page's value wins on a clash, and the iframe keeps the context headers.
One thing this doesn't change: a cross-site iframe's very first navigation only gets the context headers, because its owning page isn't known yet when the session is paused. The page's headers apply from adoption onward, same as before.
I used Claude Code to find this bug, write the change and tests, and run the checks above on my Mac.
🤖 Generated with Claude Code
Summary by cubic
Fixes extra HTTP headers so context.setExtraHTTPHeaders() and page.setExtraHTTPHeaders() send both sets together instead of the last call replacing the other. Also prevents page adoption of a cross-site iframe session from wiping out the context headers that session already had.
What changed
- Pages now send the merged set: context headers with the page's own on top, with the page's value winning on a clash (compared case-insensitively).
context.setExtraHTTPHeaders()updates existing pages and sends each session its owning page's merged set; new pages pick up the current context headers.- Iframe session adoption sends the merged set only when non-empty, so it can't erase the context headers.
- Updated TypeScript, Python, and Go docs to note the two sets are sent together.