From d58b717419d74774f24661e21469ef3babae63fd Mon Sep 17 00:00:00 2001 From: William Frank Monroy Mamani Date: Mon, 23 Nov 2026 00:36:30 -0600 Subject: [PATCH] fix(browser): the policy guard loads the last allowed page instead of going back A taken-back navigation went back in history, and Chrome restored the page from its back/forward cache, for which Playwright sees no DOMContentLoaded or load. A browser_navigate whose redirect left the allowed domains could then wait the whole 60 s navigation timeout, the MCP client's own limit, and a refused link click took about 15 s. The guard now loads the last allowed page again, waits for its commit only, and bounds each step by the 5 s cap. The Chrome test keeps the redirect's page from loading and runs that step on a tab with no take-back in flight; against the old guard it fails with the CI error. --- .changeset/fix-browser-policy-guard-hang.md | 5 +++++ packages/browser/src/policy-guard.ts | 21 ++++++++++++--------- packages/browser/test/policy-chrome.test.ts | 12 +++++++++++- 3 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 .changeset/fix-browser-policy-guard-hang.md diff --git a/.changeset/fix-browser-policy-guard-hang.md b/.changeset/fix-browser-policy-guard-hang.md new file mode 100644 index 00000000..2404f918 --- /dev/null +++ b/.changeset/fix-browser-policy-guard-hang.md @@ -0,0 +1,5 @@ +--- +"turno-browser": patch +--- + +A tool call whose navigation the policy takes back no longer waits until a timeout. The tab used to go back in history, and Chrome restored the page from its back/forward cache, for which Playwright sees no DOMContentLoaded or load: a `browser_navigate` whose redirect left the allowed domains could wait the full 60 s navigation timeout, which is also the MCP client's limit, so the call failed with "Request timed out", and a click on a refused link took about 15 s. The tab now loads its last allowed page again, and the call returns as soon as it shows. diff --git a/packages/browser/src/policy-guard.ts b/packages/browser/src/policy-guard.ts index 21b7c3b7..7f5273ce 100644 --- a/packages/browser/src/policy-guard.ts +++ b/packages/browser/src/policy-guard.ts @@ -15,8 +15,11 @@ import { type HostPattern, navigationVerdict } from "./policy.js"; * and a click hangs until its timeout.) * 2. A check on every main-frame commit takes the tab back when it shows a page the route did * not stop: a redirect to another host (that request has reached the host), a `data:` or - * `file:` URL, history. The tab goes back to its last allowed page (in history, else loaded - * again); a popup that never showed one is closed, any other tab goes to about:blank. + * `file:` URL, history. The tab loads its last allowed page again; a popup that never showed + * one is closed, any other tab goes to about:blank. Not `goBack`: Chrome restores that page + * from its back/forward cache, and Playwright sees no DOMContentLoaded or load for a restored + * page, so the tool call whose navigation was taken back (a `browser_navigate` waiting for + * DOMContentLoaded, a click waiting for load) waited until its timeout. * * Subresources (scripts, images, XHR) and frames inside a page are not checked. */ @@ -29,7 +32,7 @@ export interface PolicyViolation { target: string; } -/** Longest a tool result waits for a tab being taken back. */ +/** Longest a tool result waits for a tab being taken back, and each step of taking it back. */ const REVERT_WAIT_MS = 5_000; /** How soon after an abort an error page is that abort's (a popup's request has no page yet). */ const ABORT_ERROR_MS = 3_000; @@ -136,17 +139,17 @@ export class NavigationGuard { if (this.reverting.has(page) || page.isClosed()) return; this.reverting.add(page); const run: Promise = (async () => { + // Done once the tab shows the page: a wait for its load would hold the tab's next revert. + const options = { waitUntil: "commit", timeout: REVERT_WAIT_MS } as const; const good = this.lastGood.get(page); if (!good) { if (await page.opener().catch(() => null)) await page.close(); - else if (page.url() !== "about:blank") await page.goto("about:blank"); + else if (page.url() !== "about:blank") await page.goto("about:blank", options); return; } - const back = () => !page.isClosed() && navigationVerdict(this.patterns, page.url()).allowed === true; - await page.goBack({ waitUntil: "domcontentloaded" }).catch(() => null); - // History can be gone or busy with the navigation that brought the tab here: load the page. - if (!back()) await page.goto(good, { waitUntil: "domcontentloaded" }).catch(() => null); - if (!back() && !page.isClosed()) await page.goto("about:blank"); + await page.goto(good, options).catch(() => null); + const back = !page.isClosed() && navigationVerdict(this.patterns, page.url()).allowed === true; + if (!back && !page.isClosed()) await page.goto("about:blank", options); })() .catch(() => {}) .finally(() => { diff --git a/packages/browser/test/policy-chrome.test.ts b/packages/browser/test/policy-chrome.test.ts index 39ec5a76..716e8fb6 100644 --- a/packages/browser/test/policy-chrome.test.ts +++ b/packages/browser/test/policy-chrome.test.ts @@ -28,6 +28,7 @@ beforeEach(() => { afterEach(async () => { await transport?.close().catch(() => {}); await stopSandboxProcesses(sb.home); + server?.closeAllConnections(); await new Promise((r) => (server ? server.close(() => r(undefined)) : r(undefined))); sb.cleanup(); }); @@ -45,6 +46,13 @@ async function site(): Promise { res.end(); return; } + if (req.url === "/landed") { + // Never finishes loading: the tab is taken back before this page's DOMContentLoaded, so + // the call that navigated must not wait for one. + res.writeHead(200, { "content-type": "text/html" }); + res.write("landed"); + return; + } if (req.url === "/pixel.svg") { res.writeHead(200, { "content-type": "image/svg+xml" }); res.end(''); @@ -102,8 +110,10 @@ describe.skipIf(reason)(`policy domain check in Chrome${reason ? ` (skipped: ${r expect(seen).toContain("127.0.0.1 /pixel.svg"); blocked(await call(client, "browser_navigate", { url: `http://127.0.0.1:${port}/direct` })); - blocked(await call(client, "browser_click", { element: "out link", target: "#out" })); + // The redirect before the link, on a tab with no take-back in flight: the hop's page never + // loads, so this call returns only if taking the tab back gives it a page that does. blocked(await call(client, "browser_navigate", { url: `${home}redirect` })); + blocked(await call(client, "browser_click", { element: "out link", target: "#out" })); blocked(await call(client, "browser_click", { element: "pop button", target: "#pop" })); // The route stopped the link, the direct navigation and the popup before they left Chrome;