|
|
@@ -0,0 +1,210 @@ |
| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 20 |
import { existsSync, mkdirSync, writeFileSync } from "node:fs"; |
| 21 |
import { mkdtemp, rm } from "node:fs/promises"; |
| 22 |
import { homedir, tmpdir } from "node:os"; |
| 23 |
import { dirname, join } from "node:path"; |
| 24 |
import { claudeSettings } from "../src/crew/claude.ts"; |
| 25 |
import { sandboxAvailable, sandboxPolicy } from "../src/crew/sandbox.ts"; |
| 26 |
|
| 27 |
const probe = await sandboxAvailable(); |
| 28 |
const srt = join(import.meta.dir, "../node_modules/.bin/srt"); |
| 29 |
const nestedReason = !probe.ok |
| 30 |
? `no sandbox can be made here (nested in a crew sandbox or user namespaces refused): ${probe.reason}` |
| 31 |
: undefined; |
| 32 |
if (nestedReason !== undefined) console.warn(`skipped: ${nestedReason}`); |
| 33 |
else if (Bun.which("rg") === null || !existsSync(srt)) |
| 34 |
throw new Error( |
| 35 |
"a sandbox can be made here but its runtime is missing: install ripgrep (rg) and run bun install", |
| 36 |
); |
| 37 |
|
| 38 |
const CANARY = "the-canary-must-not-be-read"; |
| 39 |
const ENV_CANARY = "forge-redteam-environ-canary"; |
| 40 |
|
| 41 |
interface Report { |
| 42 |
readFiles: Record<string, boolean>; |
| 43 |
environLeaks: string[]; |
| 44 |
writes: Record<string, boolean>; |
| 45 |
net: Record<string, string>; |
| 46 |
worktreeWrite: boolean; |
| 47 |
} |
| 48 |
|
| 49 |
describe.skipIf(nestedReason !== undefined)( |
| 50 |
"escape attempts from a real sandboxed shell, /proc unmasked", |
| 51 |
() => { |
| 52 |
let dataDir: string; |
| 53 |
let report: Report; |
| 54 |
let outside: ReturnType<typeof Bun.serve>; |
| 55 |
let holder: ReturnType<typeof Bun.spawn>; |
| 56 |
const homeMarker = join(homedir(), `.forge-redteam-${process.pid}`); |
| 57 |
|
| 58 |
beforeAll(async () => { |
| 59 |
dataDir = await mkdtemp(join(tmpdir(), "forge-escape-")); |
| 60 |
const work = join(dataDir, "crew/scout/01repo/1"); |
| 61 |
mkdirSync(work, { recursive: true }); |
| 62 |
mkdirSync(join(dataDir, "crew/mason/01repo/3"), { recursive: true }); |
| 63 |
writeFileSync(join(dataDir, "forge.db"), CANARY); |
| 64 |
writeFileSync(join(dataDir, "crew/mason/01repo/3/notes.txt"), CANARY); |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
holder = Bun.spawn(["sleep", "60"], { |
| 69 |
env: { ...process.env, FORGE_REDTEAM_SECRET: ENV_CANARY }, |
| 70 |
stdin: "ignore", |
| 71 |
stdout: "ignore", |
| 72 |
stderr: "ignore", |
| 73 |
}); |
| 74 |
outside = Bun.serve({ |
| 75 |
hostname: "127.0.0.1", |
| 76 |
port: 0, |
| 77 |
fetch: () => new Response("outside"), |
| 78 |
}); |
| 79 |
|
| 80 |
const policy = sandboxPolicy({ |
| 81 |
dataDir, |
| 82 |
agentName: "scout", |
| 83 |
urls: [], |
| 84 |
runId: "01m46abcdefghjkmnpqrstvwxy", |
| 85 |
}); |
| 86 |
const readTargets = [ |
| 87 |
join(dataDir, "forge.db"), |
| 88 |
join(dataDir, "crew/mason/01repo/3/notes.txt"), |
| 89 |
`/proc/self/root${join(dataDir, "forge.db")}`, |
| 90 |
`/proc/1/root${join(dataDir, "forge.db")}`, |
| 91 |
`/proc/${holder.pid}/root${join(dataDir, "forge.db")}`, |
| 92 |
`/proc/self/root${join(dataDir, "crew/mason/01repo/3/notes.txt")}`, |
| 93 |
]; |
| 94 |
const writeTargets = [ |
| 95 |
join(dataDir, "planted"), |
| 96 |
join(dataDir, "crew/mason/01repo/3/planted"), |
| 97 |
`/proc/self/root${join(dataDir, "planted-via-proc")}`, |
| 98 |
`/proc/1/root${join(dataDir, "planted-via-init")}`, |
| 99 |
join(dirname(work), "planted-beside"), |
| 100 |
homeMarker, |
| 101 |
"/etc/forge-redteam-planted", |
| 102 |
]; |
| 103 |
writeFileSync( |
| 104 |
join(work, "probe.ts"), |
| 105 |
` |
| 106 |
import { readFileSync, readdirSync, writeFileSync, existsSync } from "node:fs"; |
| 107 |
const CANARY = ${JSON.stringify(CANARY)}; |
| 108 |
const ENV = ${JSON.stringify(ENV_CANARY)}; |
| 109 |
const readFiles: Record<string, boolean> = {}; |
| 110 |
for (const path of ${JSON.stringify(readTargets)}) { |
| 111 |
try { readFiles[path] = readFileSync(path, "utf8").includes(CANARY); } |
| 112 |
catch { readFiles[path] = false; } |
| 113 |
} |
| 114 |
const environLeaks: string[] = []; |
| 115 |
let pids: string[] = []; |
| 116 |
try { pids = readdirSync("/proc").filter((n) => /^[0-9]+$/.test(n)); } catch {} |
| 117 |
for (const pid of pids) { |
| 118 |
try { if (readFileSync("/proc/" + pid + "/environ", "utf8").includes(ENV)) environLeaks.push(pid); } catch {} |
| 119 |
} |
| 120 |
const writes: Record<string, boolean> = {}; |
| 121 |
for (const path of ${JSON.stringify(writeTargets)}) { |
| 122 |
try { writeFileSync(path, "planted"); writes[path] = true; } catch { writes[path] = false; } |
| 123 |
} |
| 124 |
let worktreeWrite = false; |
| 125 |
try { writeFileSync("ok.txt", "ok"); worktreeWrite = true; } catch {} |
| 126 |
const get = async (url: string, proxy?: string) => { |
| 127 |
try { |
| 128 |
const res = await fetch(url, { proxy, signal: AbortSignal.timeout(4000) }); |
| 129 |
return res.ok ? await res.text() : "refused"; |
| 130 |
} catch { return "refused"; } |
| 131 |
}; |
| 132 |
const proxy = process.env.HTTP_PROXY; |
| 133 |
const net = { |
| 134 |
directLoopback: await get("http://127.0.0.1:${outside.port}/"), |
| 135 |
proxyLoopback: await get("http://127.0.0.1:${outside.port}/", proxy), |
| 136 |
proxyInternet: await get("http://example.com/", proxy), |
| 137 |
directInternet: await get("http://192.0.2.1/"), |
| 138 |
}; |
| 139 |
console.log(JSON.stringify({ readFiles, environLeaks, writes, net, worktreeWrite })); |
| 140 |
`, |
| 141 |
); |
| 142 |
|
| 143 |
const settings = join(dataDir, "srt-settings.json"); |
| 144 |
const real = claudeSettings(policy).sandbox as Record<string, any>; |
| 145 |
writeFileSync( |
| 146 |
settings, |
| 147 |
JSON.stringify({ |
| 148 |
network: { deniedDomains: [], ...real.network }, |
| 149 |
filesystem: { |
| 150 |
denyRead: [], |
| 151 |
denyWrite: [], |
| 152 |
...real.filesystem, |
| 153 |
allowWrite: [work, ...(real.filesystem?.allowWrite ?? [])], |
| 154 |
}, |
| 155 |
}), |
| 156 |
); |
| 157 |
const proc = Bun.spawn( |
| 158 |
[srt, "--settings", settings, "-c", `'${process.execPath}' probe.ts`], |
| 159 |
{ cwd: work, stdin: "ignore", stdout: "pipe", stderr: "pipe" }, |
| 160 |
); |
| 161 |
const [out, err, code] = await Promise.all([ |
| 162 |
new Response(proc.stdout).text(), |
| 163 |
new Response(proc.stderr).text(), |
| 164 |
proc.exited, |
| 165 |
]); |
| 166 |
if (code !== 0) throw new Error(`sandboxed shell exited ${code}: ${err}`); |
| 167 |
report = JSON.parse(out.trim().split("\n").pop() ?? "{}"); |
| 168 |
}, 60_000); |
| 169 |
|
| 170 |
afterAll(async () => { |
| 171 |
holder?.kill(); |
| 172 |
outside?.stop(true); |
| 173 |
await rm(homeMarker, { force: true }); |
| 174 |
await rm("/etc/forge-redteam-planted", { force: true }).catch(() => {}); |
| 175 |
await rm(dataDir, { recursive: true, force: true }); |
| 176 |
}); |
| 177 |
|
| 178 |
test("the shell can write its worktree (the probe ran as intended)", () => { |
| 179 |
expect(report.worktreeWrite).toBe(true); |
| 180 |
}); |
| 181 |
|
| 182 |
test("reads nothing outside the worktree, by path or through /proc/*/root", () => { |
| 183 |
const leaked = Object.entries(report.readFiles) |
| 184 |
.filter(([, read]) => read) |
| 185 |
.map(([path]) => path); |
| 186 |
expect(leaked).toEqual([]); |
| 187 |
}); |
| 188 |
|
| 189 |
test("reads no other process's environment (/proc/*/environ, /proc/1/environ)", () => { |
| 190 |
expect(report.environLeaks).toEqual([]); |
| 191 |
}); |
| 192 |
|
| 193 |
test("writes nothing outside the worktree, by path or through /proc/*/root", () => { |
| 194 |
const wrote = Object.entries(report.writes) |
| 195 |
.filter(([, ok]) => ok) |
| 196 |
.map(([path]) => path); |
| 197 |
expect(wrote).toEqual([]); |
| 198 |
}); |
| 199 |
|
| 200 |
test("reaches no loopback port the policy does not name, direct or by proxy", () => { |
| 201 |
expect(report.net["directLoopback"]).toBe("refused"); |
| 202 |
expect(report.net["proxyLoopback"]).toBe("refused"); |
| 203 |
}); |
| 204 |
|
| 205 |
test("reaches no host beyond the allowlist, direct or by proxy", () => { |
| 206 |
expect(report.net["proxyInternet"]).toBe("refused"); |
| 207 |
expect(report.net["directInternet"]).toBe("refused"); |
| 208 |
}); |
| 209 |
}, |
| 210 |
); |