Skip to content

Context and page extra HTTP headers replace each other, and context headers drop in cross-site iframes #3459

In review

Description

@EsawAdhana

Before submitting an issue, please:

Environment Information

Stagehand:

  • Language/SDK: TypeScript (the Python SDK goes through the same extension code, so it behaves the same)
  • Stagehand version: 4.4.0 (main at 6f2686d), local Chrome 157, headless

AI Provider:

  • Provider: none needed, this reproduces without a model
  • Model: n/a

Issue Description

context.setExtraHTTPHeaders() and page.setExtraHTTPHeaders() each replace the whole set
of extra headers in Chrome, so whichever one runs last wipes out the other:

- context.setExtraHTTPHeaders({ "x-context": "1" }) then page.setExtraHTTPHeaders({ "x-page": "2" })
  sends only x-page on that page.
- The reverse order sends only x-context.

Separately, with only context headers set, requests made from inside a cross-site iframe
lose them once the iframe has loaded. The iframe's first navigation has the header, but a
fetch the iframe makes afterwards doesn't.

The docs say context headers apply to "every page in the context", and Playwright sends
both sets with the page's value winning on a name clash, so I'd expect both headers in
every case above.

Steps to Reproduce

  1. Set a header on the context and a different one on a page (either order), then navigate.
  2. Or: set only a context header, open a page that embeds a cross-site iframe, and have the iframe make a request after it loads.
  3. Look at the request headers the server receives.

Minimal Reproduction Code

import { localBrowser, Stagehand } from "@browserbasehq/stagehand";

const browser = await localBrowser.launch({
  headless: true,
  args: ["--host-resolver-rules=MAP child.test 127.0.0.1", "--site-per-process"],
});
const stagehand = await Stagehand.create({ browser });
const page = (await stagehand.browser.context.pages())[0];

// Case 1: a server that echoes request headers back
await stagehand.browser.context.setExtraHTTPHeaders({ "x-context": "1" });
await page.setExtraHTTPHeaders({ "x-page": "2" });
await page.goto("http://127.0.0.1:PORT/echo"); // server sees x-page only

// Case 2: page at 127.0.0.1 embeds <iframe src="http://child.test:PORT/child">,
// and the child has a button that calls fetch("/child-fetch")
await page.setExtraHTTPHeaders({});
await page.goto("http://127.0.0.1:PORT/main");
await page.locator("iframe >> #child").click(); // /child-fetch arrives without x-context

Error Messages / Log trace

context then page:     { "x-page": "2" }       (expected x-context and x-page)
page then context:     { "x-context": "1" }    (expected both)
cross-site iframe:     /child loaded with x-context, /child-fetch had none

Screenshots / Videos

n/a

Related Issues

I think the cause is in packages/extension/understudy: BrowserContext.setExtraHTTPHeaders sends only the context's headers to every session, and Page.setExtraHTTPHeaders sends only the page's, while CDP Network.setExtraHTTPHeaders replaces the whole set. For the iframe case, Page.adoptOopifSession sends the page's headers to the adopted session even when they're the default {}, which clears the context headers the session got before it resumed.

I'd be happy to send a PR that sends the merged set (context headers with the page's on top) everywhere, plus integration tests for each case.

Activity

  1. NaaeemMalik commented on Nov 16, 2026

    @NaaeemMalik

    The root cause here is how CDP binds network state across isolated target trees.

    Network.setExtraHTTPHeaders does not merge with prior calls in Chrome, it performs an atomic overwrite of the target header dictionary. To preserve both, Stagehand needs an internal session map combining context headers with page-specific overrides before dispatching the CDP payload.

    For the cross-site iframe issue: Chrome runs cross-origin frames in Out-Of-Process iframes (OOPiFs) with separate RenderProcessHost targets. If Target.setAutoAttach is not listening with waitForDebuggerOnStart for iframe targets, requests dispatched from inside the frame bypass the main page CDP session and never inherit the parent header dictionary. Propagating the merged dictionary inside the Target.attachedToTarget handler will ensure child frames retain the headers without race conditions.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions