Skip to content

Commit 92f5f07

Browse files
authored
fix(browser): keep guarded CDP sockets on validated endpoints [AI] (#114506)
* fix(browser): keep guarded CDP sockets on validated endpoints * fix(browser): avoid retrying rate-limited CDP discovery * fix(browser): close Chrome MCP CDP policy gaps * test(browser): match Chrome MCP policy error * test(browser): satisfy transport type and lint checks * fix(browser): narrow guarded CDP transport changes * fix(browser): separate upload filesystem locality * fix(browser): keep strict remote CDP hosts untrusted
1 parent 786c646 commit 92f5f07

52 files changed

Lines changed: 2584 additions & 529 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/tools/browser.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,21 @@ main model can read the screenshot directly.
316316
- 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.
317317
- 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.
318318
- In strict SSRF mode, remote CDP endpoint discovery and `/json/version` probes (`cdpUrl`) are checked too.
319+
- Guarded remote CDP connections now fail closed when the selected driver cannot
320+
keep the approved endpoint bound to the actual socket. Use the regular
321+
`openclaw` driver for Browserless, Browserbase, Notte, or other guarded
322+
remote CDP providers. `existing-session`/Chrome MCP profiles with an explicit
323+
`cdpUrl` or `--browserUrl`/`--wsEndpoint` MCP argument are rejected under the
324+
default strict Browser policy because Chrome MCP cannot carry OpenClaw's
325+
pinned DNS lookup or guarded discovery result across its subprocess boundary.
326+
They remain supported only when private-network Browser access is explicitly
327+
trusted. Otherwise, omit the explicit endpoint and attach Chrome MCP to a
328+
host-local Chrome profile, or switch the profile to the regular driver for
329+
guarded CDP.
330+
- Redirecting CDP discovery to a different authority remains unsupported unless
331+
the active policy explicitly allows that authority change. Revalidating a
332+
returned hostname is not enough; the WebSocket transport must use the endpoint
333+
that passed policy validation.
319334
- 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.
320335
- 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.
321336
- To proxy the managed browser itself, pass explicit Chrome proxy flags through `browser.extraArgs`, such as `--proxy-server=...` or `--proxy-pac-url=...`. Strict SSRF mode blocks explicit browser proxy routing unless private-network browser access is intentionally enabled.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
function decodeUrlUserInfo(value: string): string {
2+
try {
3+
return decodeURIComponent(value);
4+
} catch {
5+
return value;
6+
}
7+
}
8+
9+
/** Merge URL basic-auth credentials into headers without overriding explicit auth. */
10+
export function getHeadersWithAuth(url: string, headers: Record<string, string> = {}) {
11+
const mergedHeaders = { ...headers };
12+
try {
13+
const parsed = new URL(url);
14+
const hasAuthHeader = Object.keys(mergedHeaders).some(
15+
(key) => key.trim().toLowerCase() === "authorization",
16+
);
17+
if (hasAuthHeader) {
18+
return mergedHeaders;
19+
}
20+
if (parsed.username || parsed.password) {
21+
const username = decodeUrlUserInfo(parsed.username);
22+
const password = decodeUrlUserInfo(parsed.password);
23+
const auth = Buffer.from(`${username}:${password}`).toString("base64");
24+
return { ...mergedHeaders, Authorization: `Basic ${auth}` };
25+
}
26+
} catch {
27+
// ignore
28+
}
29+
return mergedHeaders;
30+
}
31+
32+
/** Remove URL userinfo after callers have converted it to an Authorization header. */
33+
export function stripCdpUrlCredentials(url: string): string {
34+
try {
35+
const parsed = new URL(url);
36+
if (!parsed.username && !parsed.password) {
37+
return url;
38+
}
39+
parsed.username = "";
40+
parsed.password = "";
41+
return parsed.toString();
42+
} catch {
43+
return url;
44+
}
45+
}

‎extensions/browser/src/browser/cdp-page-session.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export async function waitForCdpCommittedNavigationUrl(opts: {
143143
signal?: AbortSignal;
144144
timeouts?: CdpActionTimeouts;
145145
}): Promise<string | undefined> {
146-
await assertCdpEndpointAllowed(opts.wsUrl, opts.cdpPolicy, {
146+
const pinned = await assertCdpEndpointAllowed(opts.wsUrl, opts.cdpPolicy, {
147147
source: "discovered",
148148
configuredUrl: opts.configuredCdpUrl,
149149
});
@@ -160,6 +160,7 @@ export async function waitForCdpCommittedNavigationUrl(opts: {
160160
commandTimeoutMs: opts.timeouts?.httpTimeoutMs ?? CDP_TARGET_NAVIGATION_RESULT_TIMEOUT_MS,
161161
handshakeTimeoutMs: opts.timeouts?.handshakeTimeoutMs,
162162
handshakeRetries: 0,
163+
lookup: pinned?.lookup,
163164
},
164165
);
165166
} catch {

‎extensions/browser/src/browser/cdp-reachability-policy.ts‎

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55
* is stricter, so this module scopes the exception to browser control only.
66
*/
77
import type { SsrFPolicy } from "../infra/net/ssrf.js";
8-
import { matchesHostnameAllowlist, normalizeHostname } from "../sdk-security-runtime.js";
8+
import { normalizeHostname } from "../sdk-security-runtime.js";
9+
import { CHROME_MCP_ENDPOINT_FLAGS } from "./chrome-mcp-contracts.js";
910
import type { ResolvedBrowserProfile } from "./config.js";
11+
import { BrowserProfileUnavailableError } from "./errors.js";
1012
import { getBrowserProfileCapabilities } from "./profile-capabilities.js";
11-
import { withExactHostnamePolicy } from "./ssrf-policy-helpers.js";
13+
import { isCdpHostnameTrustedByPolicy, withExactHostnamePolicy } from "./ssrf-policy-helpers.js";
14+
15+
// Synthetic exact-host CDP policies must retain the operator's original intent;
16+
// otherwise Chrome MCP cannot distinguish default control-plane scoping from a
17+
// user-authored restriction that genuinely requires pinned transport.
18+
const cdpControlSourcePolicyByScopedPolicy = new WeakMap<SsrFPolicy, SsrFPolicy>();
1219

1320
function withCdpControlHostname(
1421
profile: ResolvedBrowserProfile,
@@ -19,17 +26,41 @@ function withCdpControlHostname(
1926
if (!ssrfPolicy || !cdpHost) {
2027
return ssrfPolicy;
2128
}
22-
const allowedHostnames = (ssrfPolicy.allowedHostnames ?? [])
23-
.map((pattern) => normalizeHostname(pattern))
24-
.filter((pattern) => pattern && pattern !== "*" && pattern !== "*.");
25-
if (
26-
requireAllowlistMatch &&
27-
allowedHostnames.length > 0 &&
28-
!matchesHostnameAllowlist(cdpHost, allowedHostnames)
29-
) {
29+
if (requireAllowlistMatch && !isCdpHostnameTrustedByPolicy(ssrfPolicy, cdpHost)) {
3030
return ssrfPolicy;
3131
}
32-
return withExactHostnamePolicy(ssrfPolicy, cdpHost);
32+
const scopedPolicy = withExactHostnamePolicy(ssrfPolicy, cdpHost);
33+
cdpControlSourcePolicyByScopedPolicy.set(scopedPolicy, ssrfPolicy);
34+
return scopedPolicy;
35+
}
36+
37+
function hasPolicyEntries(values?: string[]): boolean {
38+
return (values ?? []).some((value) => value.trim().length > 0);
39+
}
40+
41+
function requiresPinnedChromeMcpCdpTransport(cdpPolicy?: SsrFPolicy): boolean {
42+
if (!cdpPolicy) {
43+
return false;
44+
}
45+
const policyIntent = cdpControlSourcePolicyByScopedPolicy.get(cdpPolicy) ?? cdpPolicy;
46+
const hasScopedPolicy =
47+
policyIntent.allowRfc2544BenchmarkRange === true ||
48+
policyIntent.allowIpv6UniqueLocalRange === true ||
49+
hasPolicyEntries(policyIntent.allowedHostnames) ||
50+
hasPolicyEntries(policyIntent.hostnameAllowlist) ||
51+
hasPolicyEntries(policyIntent.allowedOrigins);
52+
return !(
53+
!hasScopedPolicy &&
54+
(policyIntent.dangerouslyAllowPrivateNetwork === true ||
55+
policyIntent.allowPrivateNetwork === true)
56+
);
57+
}
58+
59+
function hasChromeMcpEndpointArg(args?: string[]): boolean {
60+
return (args ?? []).some((arg) => {
61+
const [name] = arg.split("=", 1);
62+
return CHROME_MCP_ENDPOINT_FLAGS.has(name ?? arg);
63+
});
3364
}
3465

3566
export function resolveCdpReachabilityPolicy(
@@ -51,3 +82,19 @@ export function resolveCdpReachabilityPolicy(
5182

5283
/** Alias used by callers that treat reachability and control as one CDP policy. */
5384
export const resolveCdpControlPolicy = resolveCdpReachabilityPolicy;
85+
86+
export function assertChromeMcpCdpTransportAllowed(
87+
profile: ResolvedBrowserProfile,
88+
cdpPolicy?: SsrFPolicy,
89+
): void {
90+
const hasExplicitEndpoint = Boolean(profile.cdpUrl) || hasChromeMcpEndpointArg(profile.mcpArgs);
91+
if (profile.driver !== "existing-session" || !hasExplicitEndpoint) {
92+
return;
93+
}
94+
if (!requiresPinnedChromeMcpCdpTransport(cdpPolicy)) {
95+
return;
96+
}
97+
throw new BrowserProfileUnavailableError(
98+
`Browser profile "${profile.name}" uses Chrome MCP with an explicit CDP endpoint, but the active Browser CDP policy requires OpenClaw to pin the approved endpoint. Chrome MCP cannot carry that pinned transport across its subprocess boundary. Use driver "openclaw" for guarded CDP endpoints, or remove cdpUrl and browserUrl/wsEndpoint mcpArgs from this existing-session profile so Chrome MCP attaches to a host-local Chrome profile.`,
99+
);
100+
}

0 commit comments

Comments
 (0)