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

Parked opened this
Closed without merging

mason/forge:mason/10602-on-the-check-host-a-crew-sandbox-s-proxy → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +64 −32
docs/ops/RED-TEAM.md +4 −6
@@ -42,12 +42,10 @@ forge's check runs it.
4242 | 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 |
4343
4444 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`.
5149
5250 ## Not done here
5351
test/crew-sandbox-escape.test.ts +24 −20
@@ -37,6 +37,7 @@ import { mkdtemp, rm } from "node:fs/promises";
3737 import { tmpdir } from "node:os";
3838 import { join } from "node:path";
3939 import { rgPath } from "@vscode/ripgrep";
40 import { srtEnv } from "./helpers/srt-env.ts";
4041 import { claudeSettings } from "../src/crew/claude.ts";
4142 import { sandboxAvailable, sandboxPolicy } from "../src/crew/sandbox.ts";
4243
@@ -151,7 +152,7 @@ const has = (path: string, needle: string) => {
151152 try { return readFileSync(path, "utf8").includes(needle); } catch { return false; }
152153 };
153154 // 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>) => {
155156 try { return await (await fetch(url, { proxy })).text(); }
156157 catch (e) { return "refused: " + String(e); }
157158 };
@@ -177,13 +178,30 @@ const direct = (hostname: string, port: number) =>
177178 const own = Bun.serve({ hostname: "127.0.0.1", port: 0, fetch: () => new Response("own") });
178179 const ownDirect = await direct("127.0.0.1", own.port);
179180 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";
180197 // 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;
182200 const net: Record<string, string> = {};
183201 for (const host of ${JSON.stringify(OFF_LIST)}) {
184202 const url = "http://" + host + ":" + ${outside.port} + "/";
185203 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";
187205 }
188206 const reads: Record<string, boolean> = {};
189207 for (const [key, path] of Object.entries(${JSON.stringify(targets)} as Record<string, string>))
@@ -237,6 +255,7 @@ console.log(JSON.stringify({
237255 );
238256 const proc = Bun.spawn([srt, "--settings", settings, "-c", `'${process.execPath}' probe.ts`], {
239257 cwd: work,
258 env: srtEnv(),
240259 stdin: "ignore",
241260 stdout: "pipe",
242261 stderr: "pipe",
@@ -257,21 +276,6 @@ if (nestedReason === undefined)
257276 probeError = e instanceof Error ? e.message : String(e);
258277 });
259278
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
275279 describe.skipIf(nestedReason !== undefined)(
276280 "read attempts from a real sandboxed shell, /proc unmasked",
277281 () => {
@@ -311,7 +315,7 @@ describe.skipIf(nestedReason !== undefined)(
311315 expect(readFileSync(join(work, "own-written.txt"), "utf8")).toBe(CANARY);
312316 });
313317
314 test.skipIf(proxyReason !== undefined)(
318 test(
315319 "control: the shell reaches the forge's host:port through the proxy",
316320 () => {
317321 expect(report.forgeViaProxy).toBe("forge");
@@ -340,7 +344,7 @@ describe.skipIf(nestedReason !== undefined)(
340344 expect(report.net[`${host}|direct`]).not.toContain("outside");
341345 });
342346
343 test.skipIf(proxyReason !== undefined)(
347 test(
344348 `cannot reach ${host}:<port off the allowlist>, through the proxy`,
345349 () => {
346350 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";
2323 import { mkdtemp, rm } from "node:fs/promises";
2424 import { tmpdir } from "node:os";
2525 import { join } from "node:path";
26 import { srtEnv } from "./helpers/srt-env.ts";
2627 import { claudeSettings } from "../src/crew/claude.ts";
2728 import { sandboxAvailable, sandboxPolicy } from "../src/crew/sandbox.ts";
2829
@@ -115,8 +116,22 @@ describe.skipIf(nestedReason !== undefined)("a real sandboxed shell", () => {
115116 `
116117 import { existsSync, readFileSync, readdirSync, statSync } from "node:fs";
117118 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 }
120135 catch { return "refused"; }
121136 };
122137 const leaks: string[] = [];
@@ -133,8 +148,8 @@ for (const path of ${JSON.stringify(denied)}) walk(path);
133148 console.log(JSON.stringify({
134149 own: await get(own.port),
135150 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)),
138153 leaks,
139154 }));
140155 own.stop(true);
@@ -147,7 +162,7 @@ own.stop(true);
147162 writeFileSync(settings, JSON.stringify(runtimeSettings(real, work)));
148163 const proc = Bun.spawn(
149164 [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" },
151166 );
152167 const [out, err, code] = await Promise.all([
153168 new Response(proc.stdout).text(),
@@ -227,7 +242,7 @@ console.log(JSON.stringify({ wrote, read, other }));
227242 writeFileSync(settings, JSON.stringify(runtimeSettings(real, work)));
228243 const proc = Bun.spawn(
229244 [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" },
231246 );
232247 const [out, err, code] = await Promise.all([
233248 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 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs