Repository navigation
Description
A newly attached cross-site iframe can make its first script-driven requests before the page's extra HTTP headers have been applied. Those requests receive only context headers, including the context value when the page overrides the same header.
Follow-up to #3579 / #3460, related to #3459. This is a preexisting startup-ordering gap: the merge fix restores both layers on later requests, but does not cover the interval before iframe adoption.
Reproduction
Use a local HTTP fixture and headless Chromium with:
--site-per-process
--host-resolver-rules=MAP child.test 127.0.0.1,MAP grandchild.test 127.0.0.1
--no-proxy-server
Serve an empty parent document at http://127.0.0.1:PORT/. On the same server, serve this child document at /child, and record the headers received at /api:
<!doctype html>
<script>fetch("/api?phase=immediate")</script>
<button id="later">Fetch later</button>
<script>
document.querySelector("#later").onclick =
() => fetch("/api?phase=later");
</script>With an initialized Stagehand instance:
const context = stagehand.browser.context;
const page = (await context.pages())[0] ?? await context.newPage();
await context.setExtraHTTPHeaders({
"x-context": "context-v1",
"X-Shared": "context-v1",
});
await page.setExtraHTTPHeaders({
"x-page": "page-v1",
"x-shared": "page-v1",
});
await page.goto("http://127.0.0.1:PORT/");
await page.evaluate((src) => {
const iframe = document.createElement("iframe");
iframe.src = src;
document.body.append(iframe);
}, "http://child.test:PORT/child");
await page.locator("iframe >> #later").click();Observed versus expected
Actual requests captured with the merge fix:
| Request | x-context | x-page | x-shared |
|---|---|---|---|
| Immediate iframe fetch | context-v1 | missing | context-v1 |
| Later iframe fetch | context-v1 | page-v1 | page-v1 |
Both requests should receive all three headers, with x-shared: page-v1. The same immediate-fetch failure occurs in a nested cross-site iframe. A nested iframe's initial document request can also miss the page headers.
Verified using real local HTTP servers and headless Chromium 153 on Linux, with the local extension build from #3579 at 57df7d5c4 plus an internal field-only rename. Both single and nested immediate-fetch assertions failed; later-fetch assertions passed. The nine existing header integration tests passed.
The earlier before/after comparison also reproduced the immediate-fetch omission on both 16d099759 and bab439dac. This is not a regression introduced by the header-merge change.
Suggested implementation direction
The current attachment path installs context headers before resuming the child, then resolves its owning page and applies merged headers during adoption. Preserve parent-session ancestry so known owners can supply merged headers before resume, including nested children whose intermediate iframe is not fully adopted yet.
Use the existing command-dispatch barrier before Runtime.runIfWaitingForDebugger; do not wait for CDP command responses that may require resume. Reconcile the latest header state during adoption so delayed work cannot restore stale values or undo an explicit clear.
Acceptance coverage
- The first inline fetch in single and nested cross-site iframes receives both layers, with case-insensitive page precedence.
- Page-only headers, context-only headers, and two pages with different overrides remain isolated.
- Updates and explicit clears during attachment do not leave stale headers.
- Delayed attachment, iframe removal/recreation, and process swaps remain correct.
- The existing deferred-
Network.enable-response fixture still completes without deadlock.
Activity