|
| 1 | +// Live Chrome/CDP proof: blocked redirect openTab must not sticky-adopt. |
| 2 | +import { spawn, type ChildProcess } from "node:child_process"; |
| 3 | +import fs from "node:fs"; |
| 4 | +import http from "node:http"; |
| 5 | +import os from "node:os"; |
| 6 | +import path from "node:path"; |
| 7 | +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; |
| 8 | +import { isLiveTestEnabled } from "../../test-support.js"; |
| 9 | +import * as cdpModule from "./cdp.js"; |
| 10 | +import { |
| 11 | + createTestBrowserRouteContext, |
| 12 | + makeState, |
| 13 | +} from "./server-context.remote-tab-ops.harness.js"; |
| 14 | + |
| 15 | +const LIVE = isLiveTestEnabled() || process.env.OPENCLAW_LIVE_BROWSER_STICKY_PROOF === "1"; |
| 16 | +const CHROME_BIN = |
| 17 | + process.env.OPENCLAW_LIVE_CHROME_BIN?.trim() || |
| 18 | + "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"; |
| 19 | +const describeLive = LIVE && fs.existsSync(CHROME_BIN) ? describe : describe.skip; |
| 20 | + |
| 21 | +function pickLanIPv4(): string { |
| 22 | + for (const entries of Object.values(os.networkInterfaces())) { |
| 23 | + for (const entry of entries ?? []) { |
| 24 | + if (entry.family !== "IPv4" || entry.internal) { |
| 25 | + continue; |
| 26 | + } |
| 27 | + if (entry.address.startsWith("192.168.") || entry.address.startsWith("10.")) { |
| 28 | + return entry.address; |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + throw new Error("no LAN IPv4 address available for redirect proof"); |
| 33 | +} |
| 34 | + |
| 35 | +function delay(ms: number): Promise<void> { |
| 36 | + return new Promise((resolve) => { |
| 37 | + setTimeout(resolve, ms); |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +async function waitForCdp(cdpPort: number, timeoutMs = 15_000): Promise<void> { |
| 42 | + const deadline = Date.now() + timeoutMs; |
| 43 | + while (Date.now() < deadline) { |
| 44 | + try { |
| 45 | + const res = await fetch(`http://127.0.0.1:${cdpPort}/json/version`); |
| 46 | + if (res.ok) { |
| 47 | + return; |
| 48 | + } |
| 49 | + } catch { |
| 50 | + // retry |
| 51 | + } |
| 52 | + await delay(200); |
| 53 | + } |
| 54 | + throw new Error(`Chrome CDP not ready on ${cdpPort}`); |
| 55 | +} |
| 56 | + |
| 57 | +async function waitForTargetUrl( |
| 58 | + cdpPort: number, |
| 59 | + targetId: string, |
| 60 | + predicate: (url: string) => boolean, |
| 61 | + timeoutMs = 10_000, |
| 62 | +): Promise<string> { |
| 63 | + const deadline = Date.now() + timeoutMs; |
| 64 | + while (Date.now() < deadline) { |
| 65 | + const res = await fetch(`http://127.0.0.1:${cdpPort}/json/list`); |
| 66 | + const tabs = (await res.json()) as Array<{ id?: string; url?: string }>; |
| 67 | + const found = tabs.find((tab) => tab.id === targetId); |
| 68 | + const url = found?.url?.trim() ?? ""; |
| 69 | + if (url && predicate(url)) { |
| 70 | + return url; |
| 71 | + } |
| 72 | + await delay(100); |
| 73 | + } |
| 74 | + throw new Error(`timed out waiting for target ${targetId} URL predicate`); |
| 75 | +} |
| 76 | + |
| 77 | +describeLive("browser (live): sticky blocked openTab", () => { |
| 78 | + let chrome: ChildProcess | undefined; |
| 79 | + let redirectServer: http.Server | undefined; |
| 80 | + let userDataDir = ""; |
| 81 | + let cdpPort = 0; |
| 82 | + let lanIp = ""; |
| 83 | + let redirectPort = 0; |
| 84 | + let cdpUrl = ""; |
| 85 | + |
| 86 | + beforeAll(async () => { |
| 87 | + lanIp = pickLanIPv4(); |
| 88 | + redirectServer = await new Promise<http.Server>((resolve, reject) => { |
| 89 | + const server = http.createServer((req, res) => { |
| 90 | + if (req.url?.startsWith("/go")) { |
| 91 | + res.writeHead(302, { Location: "http://127.0.0.1:9/" }); |
| 92 | + res.end("redirect"); |
| 93 | + return; |
| 94 | + } |
| 95 | + res.writeHead(200, { "content-type": "text/plain" }); |
| 96 | + res.end("ok"); |
| 97 | + }); |
| 98 | + server.once("error", reject); |
| 99 | + server.listen(0, lanIp, () => resolve(server)); |
| 100 | + }); |
| 101 | + const addr = redirectServer.address(); |
| 102 | + if (!addr || typeof addr === "string") { |
| 103 | + throw new Error("redirect server missing address"); |
| 104 | + } |
| 105 | + redirectPort = addr.port; |
| 106 | + |
| 107 | + cdpPort = 19_300 + Math.floor(Math.random() * 200); |
| 108 | + cdpUrl = `http://127.0.0.1:${cdpPort}`; |
| 109 | + userDataDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-sticky-proof-")); |
| 110 | + chrome = spawn( |
| 111 | + CHROME_BIN, |
| 112 | + [ |
| 113 | + "--headless=new", |
| 114 | + "--disable-gpu", |
| 115 | + `--remote-debugging-port=${cdpPort}`, |
| 116 | + `--user-data-dir=${userDataDir}`, |
| 117 | + "--no-first-run", |
| 118 | + "--no-default-browser-check", |
| 119 | + "--use-mock-keychain", |
| 120 | + "about:blank", |
| 121 | + ], |
| 122 | + { stdio: "ignore" }, |
| 123 | + ); |
| 124 | + await waitForCdp(cdpPort); |
| 125 | + }, 30_000); |
| 126 | + |
| 127 | + afterAll(async () => { |
| 128 | + vi.restoreAllMocks(); |
| 129 | + if (chrome?.pid) { |
| 130 | + try { |
| 131 | + process.kill(chrome.pid, "SIGTERM"); |
| 132 | + } catch { |
| 133 | + // ignore |
| 134 | + } |
| 135 | + } |
| 136 | + await new Promise<void>((resolve) => { |
| 137 | + if (!redirectServer) { |
| 138 | + resolve(); |
| 139 | + return; |
| 140 | + } |
| 141 | + redirectServer.close(() => { |
| 142 | + resolve(); |
| 143 | + }); |
| 144 | + }); |
| 145 | + try { |
| 146 | + fs.rmSync(userDataDir, { recursive: true, force: true }); |
| 147 | + } catch { |
| 148 | + // Chrome may leave locked extension storage; ignore cleanup. |
| 149 | + } |
| 150 | + }); |
| 151 | + |
| 152 | + it( |
| 153 | + "rejects a LAN→loopback redirect and keeps the previous sticky target", |
| 154 | + { timeout: 60_000 }, |
| 155 | + async () => { |
| 156 | + const state = makeState("openclaw"); |
| 157 | + state.resolved.profiles.openclaw = { |
| 158 | + cdpPort, |
| 159 | + color: "#FF4500", |
| 160 | + }; |
| 161 | + // Allow the LAN redirector host only; final loopback remains blocked. |
| 162 | + state.resolved.ssrfPolicy = { allowedHostnames: [lanIp] }; |
| 163 | + (state.profiles as Map<string, unknown>).set("openclaw", { |
| 164 | + profile: { name: "openclaw" }, |
| 165 | + running: { pid: chrome?.pid ?? 1, proc: { on: () => {} } }, |
| 166 | + lastTargetId: null, |
| 167 | + }); |
| 168 | + |
| 169 | + const openclaw = createTestBrowserRouteContext({ getState: () => state }).forProfile( |
| 170 | + "openclaw", |
| 171 | + ); |
| 172 | + |
| 173 | + const good = await openclaw.openTab("about:blank"); |
| 174 | + expect(good.targetId).toBeTruthy(); |
| 175 | + expect(state.profiles.get("openclaw")?.lastTargetId).toBe(good.targetId); |
| 176 | + |
| 177 | + const redirectUrl = `http://${lanIp}:${redirectPort}/go`; |
| 178 | + // Create the redirected target with real Chrome first and wait until CDP |
| 179 | + // reports the private final URL. openTab then runs the production |
| 180 | + // discovery + assertBrowserNavigationResultAllowed + sticky-adoption path. |
| 181 | + const blocked = await cdpModule.createTargetViaCdp({ |
| 182 | + cdpUrl, |
| 183 | + url: redirectUrl, |
| 184 | + ssrfPolicy: { allowedHostnames: [lanIp] }, |
| 185 | + }); |
| 186 | + const finalUrl = await waitForTargetUrl(cdpPort, blocked.targetId, (url) => |
| 187 | + url.startsWith("http://127.0.0.1"), |
| 188 | + ); |
| 189 | + expect(finalUrl).toMatch(/^http:\/\/127\.0\.0\.1/); |
| 190 | + |
| 191 | + vi.spyOn(cdpModule, "createTargetViaCdp").mockResolvedValueOnce({ |
| 192 | + targetId: blocked.targetId, |
| 193 | + }); |
| 194 | + |
| 195 | + await expect(openclaw.openTab(redirectUrl)).rejects.toThrow(/private|blocked|ssrf/i); |
| 196 | + expect(state.profiles.get("openclaw")?.lastTargetId).toBe(good.targetId); |
| 197 | + expect(state.profiles.get("openclaw")?.lastTargetId).not.toBe(blocked.targetId); |
| 198 | + |
| 199 | + const selected = await openclaw.ensureTabAvailable(); |
| 200 | + expect(selected.targetId).toBe(good.targetId); |
| 201 | + |
| 202 | + console.log( |
| 203 | + JSON.stringify( |
| 204 | + { |
| 205 | + proof: "live-chrome-cdp-sticky-blocked-openTab", |
| 206 | + chrome: "Google Chrome headless CDP", |
| 207 | + redirectRequestHost: "LAN IPv4 (allowlisted)", |
| 208 | + finalUrlHost: "127.0.0.1", |
| 209 | + openTabRejected: true, |
| 210 | + stickyAfterReject: "unchanged-good-target", |
| 211 | + targetlessEnsureTabAvailable: "selected-good-target-not-blocked", |
| 212 | + goodTargetIdPrefix: good.targetId.slice(0, 8), |
| 213 | + blockedTargetIdPrefix: blocked.targetId.slice(0, 8), |
| 214 | + selectedTargetIdPrefix: selected.targetId.slice(0, 8), |
| 215 | + sameStickyTarget: selected.targetId === good.targetId, |
| 216 | + }, |
| 217 | + null, |
| 218 | + 2, |
| 219 | + ), |
| 220 | + ); |
| 221 | + }, |
| 222 | + ); |
| 223 | +}); |
0 commit comments