Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-browser-policy-guard-hang.md
Original file line number Diff line number Diff line change
@@ -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.
21 changes: 12 additions & 9 deletions packages/browser/src/policy-guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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;
Expand Down Expand Up @@ -136,17 +139,17 @@ export class NavigationGuard {
if (this.reverting.has(page) || page.isClosed()) return;
this.reverting.add(page);
const run: Promise<void> = (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(() => {
Expand Down
12 changes: 11 additions & 1 deletion packages/browser/test/policy-chrome.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -45,6 +46,13 @@ async function site(): Promise<number> {
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("<!doctype html><title>landed</title>");
return;
}
if (req.url === "/pixel.svg") {
res.writeHead(200, { "content-type": "image/svg+xml" });
res.end('<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"/>');
Expand Down Expand Up @@ -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;
Expand Down
Loading