Public: anyone can read this; the crew can change it.
On the check host, a crew sandbox's proxy answers allowed hosts, so the escape test's through-proxy attempts run there #10685
mason/forge:mason/10602-on-the-check-host-a-crew-sandbox-s-proxy → main 3 commits fork
4 files
changed +64
−32
docs/ops/RED-TEAM.md +4 −6
| @@ -42,12 +42,10 @@ forge's check runs it. | ||
| 42 | 42 | | Controls: read and write the own worktree, read the own environ, reach a listener in the own network namespace directly, reach the forge's host:port through the proxy | works | the four "control:" tests | |
| 43 | 43 | |
| 44 | 44 | The controls keep a probe that never ran, or a proxy that refuses |
| 45 | everything, from passing as "all refused". Where the allowed host does not | |
| 46 | answer through the proxy either (the forge's check host today, the same cause | |
| 47 | that keeps `test/crew-sandbox-real.test.ts` out of the gate; #10602 makes them run there), the | |
| 48 | attempts through the proxy and their control are skipped, printing | |
| 49 | `skipped: test/crew-sandbox-escape.test.ts, the attempts through the proxy: | |
| 50 | <reason>`; the direct attempts, the reads and the writes still run. | |
| 45 | everything, from passing as "all refused". `srt` is started without the | |
| 46 | machine's proxy variables (`test/helpers/srt-env.ts`): it chains allowed | |
| 47 | requests through `HTTP_PROXY` from its own environment, which on the check host | |
| 48 | is an egress proxy that cannot reach the test's loopback servers (#10602). The sandbox's relay also answers `407` to a request without the credentials srt puts in `HTTP_PROXY`, and Bun's `fetch` does not send them from the URL, so the probe sends them as `Proxy-Authorization`. | |
| 51 | 49 | |
| 52 | 50 | ## Not done here |
| 53 | 51 |
test/crew-sandbox-escape.test.ts +24 −20
| @@ -37,6 +37,7 @@ import { mkdtemp, rm } from "node:fs/promises"; | ||
| 37 | 37 | import { tmpdir } from "node:os"; |
| 38 | 38 | import { join } from "node:path"; |
| 39 | 39 | import { rgPath } from "@vscode/ripgrep"; |
| 40 | import { srtEnv } from "./helpers/srt-env.ts"; | |
| 40 | 41 | import { claudeSettings } from "../src/crew/claude.ts"; |
| 41 | 42 | import { sandboxAvailable, sandboxPolicy } from "../src/crew/sandbox.ts"; |
| 42 | 43 | |
| @@ -151,7 +152,7 @@ const has = (path: string, needle: string) => { | ||
| 151 | 152 | try { return readFileSync(path, "utf8").includes(needle); } catch { return false; } |
| 152 | 153 | }; |
| 153 | 154 | // The network first, before the shell touches /proc. |
| 154 | const get = async (url: string, proxy?: string) => { | |
| 155 | const get = async (url: string, proxy?: ReturnType<typeof asProxy>) => { | |
| 155 | 156 | try { return await (await fetch(url, { proxy })).text(); } |
| 156 | 157 | catch (e) { return "refused: " + String(e); } |
| 157 | 158 | }; |
| @@ -177,13 +178,30 @@ const direct = (hostname: string, port: number) => | ||
| 177 | 178 | const own = Bun.serve({ hostname: "127.0.0.1", port: 0, fetch: () => new Response("own") }); |
| 178 | 179 | const ownDirect = await direct("127.0.0.1", own.port); |
| 179 | 180 | own.stop(true); |
| 181 | // srt hands the shell its proxy as http://user:pass@localhost:3128 and its | |
| 182 | // relay answers 407 without the credentials; Bun's fetch does not send them | |
| 183 | // from the URL, so they go as a header. IPv4 by name: the relay listens there. | |
| 184 | const asProxy = (value?: string) => { | |
| 185 | if (!value) return undefined; | |
| 186 | const u = new URL(value); | |
| 187 | const auth = u.username | |
| 188 | ? "Basic " + btoa(decodeURIComponent(u.username) + ":" + decodeURIComponent(u.password)) | |
| 189 | : undefined; | |
| 190 | return { url: "http://127.0.0.1:" + u.port, ...(auth ? { headers: { "Proxy-Authorization": auth } } : {}) }; | |
| 191 | }; | |
| 192 | const PROXY = asProxy(process.env.HTTP_PROXY); | |
| 193 | // What is at the proxy's address, for the failure message of the control. | |
| 194 | const proxyDetail = PROXY | |
| 195 | ? JSON.stringify({ HTTP_PROXY: process.env.HTTP_PROXY, tcp4: (await direct("127.0.0.1", 3128)).slice(0, 200), tcp6: (await direct("::1", 3128)).slice(0, 200) }) | |
| 196 | : "no HTTP_PROXY in the sandbox"; | |
| 180 | 197 | // The allowed control first: a refusal must not be what decides it. |
| 181 | const forgeViaProxy = await get("http://127.0.0.1:${forge.port}/", process.env.HTTP_PROXY); | |
| 198 | const viaProxy = await get("http://127.0.0.1:${forge.port}/", PROXY); | |
| 199 | const forgeViaProxy = viaProxy === "forge" ? viaProxy : viaProxy + " " + proxyDetail; | |
| 182 | 200 | const net: Record<string, string> = {}; |
| 183 | 201 | for (const host of ${JSON.stringify(OFF_LIST)}) { |
| 184 | 202 | const url = "http://" + host + ":" + ${outside.port} + "/"; |
| 185 | 203 | net[host + "|direct"] = await direct(host, ${outside.port}); |
| 186 | net[host + "|proxy"] = process.env.HTTP_PROXY ? await get(url, process.env.HTTP_PROXY) : "no proxy"; | |
| 204 | net[host + "|proxy"] = PROXY ? await get(url, PROXY) : "no proxy"; | |
| 187 | 205 | } |
| 188 | 206 | const reads: Record<string, boolean> = {}; |
| 189 | 207 | for (const [key, path] of Object.entries(${JSON.stringify(targets)} as Record<string, string>)) |
| @@ -237,6 +255,7 @@ console.log(JSON.stringify({ | ||
| 237 | 255 | ); |
| 238 | 256 | const proc = Bun.spawn([srt, "--settings", settings, "-c", `'${process.execPath}' probe.ts`], { |
| 239 | 257 | cwd: work, |
| 258 | env: srtEnv(), | |
| 240 | 259 | stdin: "ignore", |
| 241 | 260 | stdout: "pipe", |
| 242 | 261 | stderr: "pipe", |
| @@ -257,21 +276,6 @@ if (nestedReason === undefined) | ||
| 257 | 276 | probeError = e instanceof Error ? e.message : String(e); |
| 258 | 277 | }); |
| 259 | 278 | |
| 260 | /** | |
| 261 | * Where the allowed host cannot be reached through the proxy either, the | |
| 262 | * proxy answers nothing here (the check host today, #10602, as for | |
| 263 | * crew-sandbox-real.test.ts), and a refusal through it would prove nothing: | |
| 264 | * those attempts and their control are skipped, named. | |
| 265 | */ | |
| 266 | const proxyReason = | |
| 267 | nestedReason === undefined && probeError === undefined && report!.forgeViaProxy !== "forge" | |
| 268 | ? `the sandbox's proxy does not answer here, even for an allowed host (#10602): ${report!.forgeViaProxy}` | |
| 269 | : undefined; | |
| 270 | if (proxyReason !== undefined) | |
| 271 | console.warn( | |
| 272 | `skipped: test/crew-sandbox-escape.test.ts, the attempts through the proxy: ${proxyReason}`, | |
| 273 | ); | |
| 274 | ||
| 275 | 279 | describe.skipIf(nestedReason !== undefined)( |
| 276 | 280 | "read attempts from a real sandboxed shell, /proc unmasked", |
| 277 | 281 | () => { |
| @@ -311,7 +315,7 @@ describe.skipIf(nestedReason !== undefined)( | ||
| 311 | 315 | expect(readFileSync(join(work, "own-written.txt"), "utf8")).toBe(CANARY); |
| 312 | 316 | }); |
| 313 | 317 | |
| 314 | test.skipIf(proxyReason !== undefined)( | |
| 318 | test( | |
| 315 | 319 | "control: the shell reaches the forge's host:port through the proxy", |
| 316 | 320 | () => { |
| 317 | 321 | expect(report.forgeViaProxy).toBe("forge"); |
| @@ -340,7 +344,7 @@ describe.skipIf(nestedReason !== undefined)( | ||
| 340 | 344 | expect(report.net[`${host}|direct`]).not.toContain("outside"); |
| 341 | 345 | }); |
| 342 | 346 | |
| 343 | test.skipIf(proxyReason !== undefined)( | |
| 347 | test( | |
| 344 | 348 | `cannot reach ${host}:<port off the allowlist>, through the proxy`, |
| 345 | 349 | () => { |
| 346 | 350 | expect(report.net[`${host}|proxy`]).not.toBe("outside"); |
test/crew-sandbox-real.test.ts +21 −6
| @@ -23,6 +23,7 @@ import { existsSync, mkdirSync, writeFileSync } from "node:fs"; | ||
| 23 | 23 | import { mkdtemp, rm } from "node:fs/promises"; |
| 24 | 24 | import { tmpdir } from "node:os"; |
| 25 | 25 | import { join } from "node:path"; |
| 26 | import { srtEnv } from "./helpers/srt-env.ts"; | |
| 26 | 27 | import { claudeSettings } from "../src/crew/claude.ts"; |
| 27 | 28 | import { sandboxAvailable, sandboxPolicy } from "../src/crew/sandbox.ts"; |
| 28 | 29 | |
| @@ -115,8 +116,22 @@ describe.skipIf(nestedReason !== undefined)("a real sandboxed shell", () => { | ||
| 115 | 116 | ` |
| 116 | 117 | import { existsSync, readFileSync, readdirSync, statSync } from "node:fs"; |
| 117 | 118 | const own = Bun.serve({ hostname: "127.0.0.1", port: 0, fetch: () => new Response("own") }); |
| 118 | const get = async (port: number, proxy?: string) => { | |
| 119 | try { return await (await fetch("http://127.0.0.1:" + port + "/", { proxy })).text(); } | |
| 119 | // srt hands the shell its proxy as http://user:pass@localhost:3128 and its | |
| 120 | // relay answers 407 without the credentials; Bun's fetch does not send them | |
| 121 | // from the URL, so they go as a header. IPv4 by name: the relay listens there. | |
| 122 | const asProxy = (value?: string) => { | |
| 123 | if (!value) return undefined; | |
| 124 | const u = new URL(value); | |
| 125 | const auth = u.username | |
| 126 | ? "Basic " + btoa(decodeURIComponent(u.username) + ":" + decodeURIComponent(u.password)) | |
| 127 | : undefined; | |
| 128 | return { url: "http://127.0.0.1:" + u.port, ...(auth ? { headers: { "Proxy-Authorization": auth } } : {}) }; | |
| 129 | }; | |
| 130 | const get = async (port: number, proxy?: ReturnType<typeof asProxy>) => { | |
| 131 | try { | |
| 132 | const res = await fetch("http://127.0.0.1:" + port + "/", { proxy }); | |
| 133 | return res.ok ? await res.text() : "refused"; | |
| 134 | } | |
| 120 | 135 | catch { return "refused"; } |
| 121 | 136 | }; |
| 122 | 137 | const leaks: string[] = []; |
| @@ -133,8 +148,8 @@ for (const path of ${JSON.stringify(denied)}) walk(path); | ||
| 133 | 148 | console.log(JSON.stringify({ |
| 134 | 149 | own: await get(own.port), |
| 135 | 150 | outside: await get(${outside.port}), |
| 136 | forge: await get(${forge.port}, process.env.HTTP_PROXY), | |
| 137 | viaProxy: await get(${outside.port}, process.env.HTTP_PROXY), | |
| 151 | forge: await get(${forge.port}, asProxy(process.env.HTTP_PROXY)), | |
| 152 | viaProxy: await get(${outside.port}, asProxy(process.env.HTTP_PROXY)), | |
| 138 | 153 | leaks, |
| 139 | 154 | })); |
| 140 | 155 | own.stop(true); |
| @@ -147,7 +162,7 @@ own.stop(true); | ||
| 147 | 162 | writeFileSync(settings, JSON.stringify(runtimeSettings(real, work))); |
| 148 | 163 | const proc = Bun.spawn( |
| 149 | 164 | [srt, "--settings", settings, "-c", `'${process.execPath}' probe.ts`], |
| 150 | { cwd: work, stdin: "ignore", stdout: "pipe", stderr: "pipe" }, | |
| 165 | { cwd: work, env: srtEnv(), stdin: "ignore", stdout: "pipe", stderr: "pipe" }, | |
| 151 | 166 | ); |
| 152 | 167 | const [out, err, code] = await Promise.all([ |
| 153 | 168 | new Response(proc.stdout).text(), |
| @@ -227,7 +242,7 @@ console.log(JSON.stringify({ wrote, read, other })); | ||
| 227 | 242 | writeFileSync(settings, JSON.stringify(runtimeSettings(real, work))); |
| 228 | 243 | const proc = Bun.spawn( |
| 229 | 244 | [srt, "--settings", settings, "-c", `'${process.execPath}' probe.ts`], |
| 230 | { cwd: work, stdin: "ignore", stdout: "pipe", stderr: "pipe" }, | |
| 245 | { cwd: work, env: srtEnv(), stdin: "ignore", stdout: "pipe", stderr: "pipe" }, | |
| 231 | 246 | ); |
| 232 | 247 | const [out, err, code] = await Promise.all([ |
| 233 | 248 | new Response(proc.stdout).text(), |
test/helpers/srt-env.ts added +15
| @@ -0,0 +1,15 @@ | ||
| 1 | /** | |
| 2 | * The environment to start `srt` with in a test: this process's, without any | |
| 3 | * proxy variable. srt takes HTTP_PROXY/HTTPS_PROXY/NO_PROXY from its own | |
| 4 | * environment as a parent proxy and chains every allowed request through it | |
| 5 | * (#10602). On the check host that is the machine's egress proxy, which | |
| 6 | * cannot reach the test's loopback servers, so the sandbox's own proxy | |
| 7 | * answered nothing even for an allowed host. A test of the sandbox's proxy | |
| 8 | * must start it with none above it. | |
| 9 | */ | |
| 10 | export function srtEnv(env: NodeJS.ProcessEnv = process.env): Record<string, string> { | |
| 11 | const out: Record<string, string> = {}; | |
| 12 | for (const [key, value] of Object.entries(env)) | |
| 13 | if (value !== undefined && !/^(https?|all|no|ftp|grpc|rsync)_proxy$/i.test(key)) out[key] = value; | |
| 14 | return out; | |
| 15 | } |
new above
offline