Skip to content

Commit a34b1f6

Browse files
committed
fix(browser): guard all interaction navigations
1 parent 569055c commit a34b1f6

7 files changed

Lines changed: 354 additions & 274 deletions

‎docs/gateway/security/index.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,8 @@ Private/internal destinations stay blocked unless you explicitly opt in.
481481
- Default: `browser.ssrfPolicy.dangerouslyAllowPrivateNetwork` unset, so private/internal/special-use destinations stay blocked. Legacy alias `allowPrivateNetwork` still accepted.
482482
- Opt-in: set `dangerouslyAllowPrivateNetwork: true` to allow those destinations.
483483
- In strict mode, use `hostnameAllowlist` (patterns like `*.example.com`) and `allowedHostnames` (exact host exceptions, including otherwise-blocked names like `localhost`) for explicit exceptions.
484-
- Direct navigation requests are preflight checked. Guarded Playwright hover, drag, and scroll actions intercept policy-denied top-level and subframe document loads before HTTP request bytes, then best-effort re-check the final `http(s)` URL.
485-
- Before each fresh managed Chrome launch, OpenClaw best-effort disables network prediction, suppressing Chromium's observed speculative preconnect for those denied loads. This is defense in depth, not a policy boundary: a browser reused across a control-service restart and other browser backends may not share the hardening. Page routing remains request-level interception, not a network firewall: redirect hops, a popup's first request, Service Worker traffic, and some background/subresource paths can bypass it. Final-URL checks remain detection/quarantine defense; complete prevention requires owner-side egress isolation or a policy-enforcing proxy.
484+
- Direct navigation requests are preflight checked. During the action and bounded post-action grace, guarded Playwright interactions (click, coordinate click, hover, drag, scroll, select, press, type, form fill, and evaluate) intercept policy-denied top-level and subframe document loads before HTTP request bytes, then best-effort re-check the final `http(s)` URL.
485+
- Before each fresh managed Chrome launch, OpenClaw best-effort disables network prediction, suppressing Chromium's observed speculative preconnect for those denied loads. This is defense in depth, not a policy boundary: a browser reused across a control-service restart and other browser backends may not share the hardening. Page routing remains request-level interception, not a network firewall: redirect hops, a popup's first request, Service Worker traffic, page code that runs after the bounded guard window, and some background/subresource paths can bypass it. Final-URL checks remain detection/quarantine defense; complete prevention requires owner-side egress isolation or a policy-enforcing proxy.
486486

487487
```json5
488488
{

‎docs/tools/browser.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ main model can read the screenshot directly.
290290

291291
<Accordion title="SSRF policy">
292292

293-
- Browser navigation and open-tab requests are preflight checked. Guarded Playwright hover, drag, and scroll actions intercept policy-denied top-level and subframe document loads before HTTP request bytes, then best-effort re-check the final `http(s)` URL.
294-
- Before each fresh OpenClaw-managed Chrome launch, OpenClaw best-effort disables network prediction, suppressing Chromium's observed speculative preconnect for those denied loads. This is defense in depth, not a policy boundary: a browser reused across a control-service restart and other browser backends may not share the hardening. Playwright routing is still not a network firewall and does not intercept redirect hops, a popup's first request, Service Worker traffic, or every background/subresource path. Complete egress isolation requires owner-side isolation or a policy-enforcing proxy.
293+
- Browser navigation and open-tab requests are preflight checked. During the action and bounded post-action grace, guarded Playwright interactions (click, coordinate click, hover, drag, scroll, select, press, type, form fill, and evaluate) intercept policy-denied top-level and subframe document loads before HTTP request bytes, then best-effort re-check the final `http(s)` URL.
294+
- Before each fresh OpenClaw-managed Chrome launch, OpenClaw best-effort disables network prediction, suppressing Chromium's observed speculative preconnect for those denied loads. This is defense in depth, not a policy boundary: a browser reused across a control-service restart and other browser backends may not share the hardening. Playwright routing is still not a network firewall and does not intercept redirect hops, a popup's first request, Service Worker traffic, page code that runs after the bounded guard window, or every background/subresource path. Complete egress isolation requires owner-side isolation or a policy-enforcing proxy.
295295
- In strict SSRF mode, remote CDP endpoint discovery and `/json/version` probes (`cdpUrl`) are checked too.
296296
- Gateway/provider `HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY`, and `NO_PROXY` environment variables do not automatically proxy the OpenClaw-managed browser. Managed Chrome launches direct by default so provider proxy settings do not weaken browser SSRF checks.
297297
- OpenClaw-managed local CDP readiness probes and DevTools WebSocket connections bypass the managed network proxy for the exact launched loopback endpoint, so `openclaw browser start` still works when an operator proxy blocks loopback egress.

‎extensions/browser/src/browser/pw-tools-core.browser-ssrf-guard.test.ts‎

Lines changed: 140 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,19 @@ describe("pw-tools-core browser SSRF guards", () => {
199199
browserProxyMode: "explicit-browser-proxy",
200200
}),
201201
},
202+
{
203+
name: "type-submit",
204+
run: async () =>
205+
await interactions.typeViaPlaywright({
206+
cdpUrl: "http://127.0.0.1:18792",
207+
targetId: "tab-1",
208+
ref: "1",
209+
text: "value",
210+
submit: true,
211+
ssrfPolicy: { allowPrivateNetwork: false },
212+
browserProxyMode: "explicit-browser-proxy",
213+
}),
214+
},
202215
{
203216
name: "press",
204217
run: async () =>
@@ -244,7 +257,19 @@ describe("pw-tools-core browser SSRF guards", () => {
244257
browserProxyMode: "explicit-browser-proxy",
245258
}),
246259
},
247-
])("preserves proxy policy for existing $name navigation checks", async ({ run }) => {
260+
{
261+
name: "evaluate-ref",
262+
run: async () =>
263+
await interactions.evaluateViaPlaywright({
264+
cdpUrl: "http://127.0.0.1:18792",
265+
targetId: "tab-1",
266+
ref: "1",
267+
fn: "(el) => Boolean(el)",
268+
ssrfPolicy: { allowPrivateNetwork: false },
269+
browserProxyMode: "explicit-browser-proxy",
270+
}),
271+
},
272+
])("guards $name document requests and preserves proxy policy", async ({ run }) => {
248273
let currentUrl = "https://example.com";
249274
const navigate = vi.fn(async () => {
250275
currentUrl = "https://93.184.216.34/target";
@@ -262,10 +287,19 @@ describe("pw-tools-core browser SSRF guards", () => {
262287
press: navigate,
263288
selectOption: navigate,
264289
setChecked: navigate,
290+
evaluate: navigate,
265291
};
266292

267293
await run();
268294

295+
expect(sessionMocks.withPageNavigationRequestGuard).toHaveBeenCalledWith({
296+
action: expect.any(Function),
297+
onPolicyCheckStarted: expect.any(Function),
298+
onPolicyDenied: expect.any(Function),
299+
page: pageState.page,
300+
ssrfPolicy: { allowPrivateNetwork: false },
301+
browserProxyMode: "explicit-browser-proxy",
302+
});
269303
expect(sessionMocks.assertPageNavigationCompletedSafely).toHaveBeenLastCalledWith({
270304
cdpUrl: "http://127.0.0.1:18792",
271305
page: pageState.page,
@@ -792,6 +826,72 @@ describe("pw-tools-core browser SSRF guards", () => {
792826
});
793827
});
794828

829+
it.each([
830+
{ label: "fill before submit", slowly: false, firstMethod: "fill" as const },
831+
{ label: "click before slow type", slowly: true, firstMethod: "click" as const },
832+
])("stops a multi-step type action after aborting $label", async ({ slowly, firstMethod }) => {
833+
const ctrl = new AbortController();
834+
let firstStepStarted!: () => void;
835+
let releaseFirstStep!: () => void;
836+
const started = new Promise<void>((resolve) => {
837+
firstStepStarted = resolve;
838+
});
839+
const pendingFirstStep = new Promise<void>((resolve) => {
840+
releaseFirstStep = resolve;
841+
});
842+
const click = vi.fn(async () => {});
843+
const fill = vi.fn(async () => {});
844+
const type = vi.fn(async () => {});
845+
const press = vi.fn(async () => {});
846+
const firstStep = vi.fn(() => {
847+
firstStepStarted();
848+
return pendingFirstStep;
849+
});
850+
if (firstMethod === "click") {
851+
click.mockImplementation(firstStep);
852+
} else {
853+
fill.mockImplementation(firstStep);
854+
}
855+
let guardSettled = false;
856+
pageState.page = { url: vi.fn(() => "https://example.com") };
857+
pageState.locator = { click, fill, type, press };
858+
sessionMocks.withPageNavigationRequestGuard.mockImplementationOnce(
859+
async ({
860+
action,
861+
page,
862+
}: {
863+
action: (url: string) => Promise<unknown>;
864+
page: { url: () => string };
865+
}) => {
866+
try {
867+
return await action(page.url());
868+
} finally {
869+
guardSettled = true;
870+
}
871+
},
872+
);
873+
874+
const task = interactions.typeViaPlaywright({
875+
cdpUrl: "http://127.0.0.1:18792",
876+
targetId: "tab-1",
877+
ref: "1",
878+
text: "value",
879+
submit: true,
880+
slowly,
881+
ssrfPolicy: { allowPrivateNetwork: false },
882+
signal: ctrl.signal,
883+
});
884+
885+
await started;
886+
ctrl.abort(new Error("aborted by test"));
887+
await expect(task).rejects.toThrow("aborted by test");
888+
889+
releaseFirstStep();
890+
await vi.waitFor(() => expect(guardSettled).toBe(true));
891+
expect(type).not.toHaveBeenCalled();
892+
expect(press).not.toHaveBeenCalled();
893+
});
894+
795895
it("re-checks select-triggered navigations with the session safety helper", async () => {
796896
let currentUrl = "https://example.com";
797897
pageState.page = { url: vi.fn(() => currentUrl) };
@@ -843,7 +943,42 @@ describe("pw-tools-core browser SSRF guards", () => {
843943
});
844944
});
845945

846-
it("re-checks the current page before evaluating page content", async () => {
946+
it("stops form filling when the first field's request guard denies navigation", async () => {
947+
const fill = vi.fn(async () => {});
948+
const blocked = new Error("blocked field navigation");
949+
blocked.name = "SsrFBlockedError";
950+
pageState.page = { url: vi.fn(() => "https://example.com") };
951+
pageState.locator = { fill };
952+
sessionMocks.withPageNavigationRequestGuard.mockImplementationOnce(
953+
async ({
954+
action,
955+
page,
956+
}: {
957+
action: (url: string) => Promise<unknown>;
958+
page: { url(): string };
959+
}) => {
960+
await action(page.url());
961+
throw blocked;
962+
},
963+
);
964+
965+
await expect(
966+
interactions.fillFormViaPlaywright({
967+
cdpUrl: "http://127.0.0.1:18792",
968+
targetId: "tab-1",
969+
fields: [
970+
{ ref: "1", type: "text", value: "first" },
971+
{ ref: "2", type: "text", value: "second" },
972+
],
973+
ssrfPolicy: { allowPrivateNetwork: false },
974+
}),
975+
).rejects.toThrow("blocked field navigation");
976+
977+
expect(fill).toHaveBeenCalledOnce();
978+
expect(sessionMocks.withPageNavigationRequestGuard).toHaveBeenCalledOnce();
979+
});
980+
981+
it("installs the request guard before evaluating page content", async () => {
847982
const evaluate = vi.fn(async () => "ok");
848983
pageState.page = {
849984
evaluate,
@@ -857,16 +992,9 @@ describe("pw-tools-core browser SSRF guards", () => {
857992
ssrfPolicy: { allowPrivateNetwork: false },
858993
});
859994

860-
expect(sessionMocks.assertPageNavigationCompletedSafely).toHaveBeenCalledWith({
861-
cdpUrl: "http://127.0.0.1:18792",
862-
page: pageState.page,
863-
response: null,
864-
ssrfPolicy: { allowPrivateNetwork: false },
865-
targetId: "tab-1",
866-
});
867-
expect(
868-
sessionMocks.assertPageNavigationCompletedSafely.mock.invocationCallOrder[0],
869-
).toBeLessThan(evaluate.mock.invocationCallOrder[0]);
995+
expect(sessionMocks.withPageNavigationRequestGuard.mock.invocationCallOrder[0]).toBeLessThan(
996+
evaluate.mock.invocationCallOrder[0],
997+
);
870998
});
871999

8721000
it("preserves helper compatibility when no ssrfPolicy is provided", async () => {

‎extensions/browser/src/browser/pw-tools-core.interactions.batch.test.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ describe("batchViaPlaywright", () => {
178178
},
179179
},
180180
{ name: "evaluate", action: { kind: "evaluate", fn: "() => true" } as const },
181-
])("forwards proxy policy to existing batched $name actions", async ({ action }) => {
181+
])("guards batched $name document requests with the proxy policy", async ({ action }) => {
182182
const ssrfPolicy = { dangerouslyAllowPrivateNetwork: false } as const;
183183

184184
const result = await batchViaPlaywright({
@@ -191,6 +191,14 @@ describe("batchViaPlaywright", () => {
191191
});
192192

193193
expect(result).toEqual({ results: [{ ok: true }] });
194+
expect(withPageNavigationRequestGuard).toHaveBeenCalledWith({
195+
action: expect.any(Function),
196+
onPolicyCheckStarted: expect.any(Function),
197+
onPolicyDenied: expect.any(Function),
198+
page,
199+
ssrfPolicy,
200+
browserProxyMode: "explicit-browser-proxy",
201+
});
194202
expect(assertPageNavigationCompletedSafely).toHaveBeenLastCalledWith({
195203
cdpUrl: "http://127.0.0.1:9222",
196204
page,
@@ -210,7 +218,7 @@ describe("batchViaPlaywright", () => {
210218
actions: [
211219
{
212220
kind: "batch",
213-
actions: [{ kind: "hover", ref: "1" }],
221+
actions: [{ kind: "click", ref: "1" }],
214222
},
215223
],
216224
evaluateEnabled: true,

‎extensions/browser/src/browser/pw-tools-core.interactions.evaluate.abort.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ describe("evaluateViaPlaywright (abort)", () => {
146146
await expect(p).rejects.toThrow("blocked by dialog");
147147
expect(forceDisconnectPlaywrightForTarget).not.toHaveBeenCalled();
148148
resolveEval(true);
149-
await Promise.resolve();
150-
expect(markObservedDialogsHandledRemotelyForPage).toHaveBeenCalled();
149+
await vi.waitFor(() => {
150+
expect(markObservedDialogsHandledRemotelyForPage).toHaveBeenCalled();
151+
});
151152
});
152153
});

0 commit comments

Comments
 (0)