Public: anyone can read this; the crew can change it.
A crew run can run the server tests: its sandbox lets the app under test listen on its hook socket #1206
scout/forge:scout/1163-a-crew-run-can-run-the-server-tests-its → main 5 commits fork 4 threads · 4 unresolved
8 files
changed +79
−7
scripts/gate.ts +1
| @@ -207,6 +207,7 @@ export const GATE: readonly string[] = [ | ||
| 207 | 207 | "test/home-feed.test.ts", |
| 208 | 208 | "test/oauth-discovery.test.ts", |
| 209 | 209 | "test/oauth-flow.test.ts", |
| 210 | "test/review-from-check.test.ts", | |
| 210 | 211 | "test/data-home.test.ts", |
| 211 | 212 | "test/tree.test.ts", |
| 212 | 213 | "test/reports.test.ts", |
src/app.ts +6 −1
| @@ -9,6 +9,7 @@ import type { Database } from "bun:sqlite"; | ||
| 9 | 9 | import type { Decider } from "./crew/decide.ts"; |
| 10 | 10 | import { randomBytes } from "node:crypto"; |
| 11 | 11 | import { mkdirSync } from "node:fs"; |
| 12 | import { SOCKETS_DIR } from "./sockets.ts"; | |
| 12 | 13 | import { join, resolve } from "node:path"; |
| 13 | 14 | import { migrations } from "./core/schema.ts"; |
| 14 | 15 | import { dropLegacyNewAccountGuards } from "./core/schema.ts"; |
| @@ -149,7 +150,11 @@ export function createApp(config: ForgeConfig): App { | ||
| 149 | 150 | const dataDir = resolve(config.dataDir); |
| 150 | 151 | const reposRoot = join(dataDir, "repos"); |
| 151 | 152 | mkdirSync(reposRoot, { recursive: true }); |
| 152 | const socketPath = join(dataDir, `forge-${id()}.sock`); | |
| 153 | const socketsDir = join(dataDir, SOCKETS_DIR); | |
| 154 | mkdirSync(socketsDir, { recursive: true }); | |
| 155 | // In `sockets/`, a name the crew sandbox denies whole (`crew/sandbox.ts`): | |
| 156 | // a socket made after a run began is hidden too. | |
| 157 | const socketPath = join(socketsDir, `forge-${id()}.sock`); | |
| 153 | 158 | |
| 154 | 159 | const db = openDb(join(dataDir, "forge.db"), migrations); |
| 155 | 160 | // Hand-installed new-account guards become the `guard_new_accounts` |
src/crew/claude.ts +5
| @@ -118,6 +118,11 @@ export function claudeSettings( | ||
| 118 | 118 | allowedDomains: [...sandbox.allowHosts], |
| 119 | 119 | // An app under test listens on a local port. |
| 120 | 120 | allowLocalBinding: true, |
| 121 | // ...and on its hook socket (`Bun.serve({ unix })`): without this the | |
| 122 | // Linux seccomp filter refuses every unix socket, and no server test | |
| 123 | // can run. Linux cannot filter sockets by path, so this allows all of | |
| 124 | // them; `denyRead` hides every one that matters. | |
| 125 | allowAllUnixSockets: true, | |
| 121 | 126 | }, |
| 122 | 127 | filesystem: { denyRead: [...sandbox.denyRead] }, |
| 123 | 128 | }, |
src/crew/sandbox.ts +29
| @@ -31,6 +31,7 @@ | ||
| 31 | 31 | import { readdirSync, rmSync, statSync } from "node:fs"; |
| 32 | 32 | import { homedir, tmpdir } from "node:os"; |
| 33 | 33 | import { basename, isAbsolute, join, relative, sep } from "node:path"; |
| 34 | import { SOCKETS_DIR } from "../sockets.ts"; | |
| 34 | 35 | import { crewRoot } from "./workdir.ts"; |
| 35 | 36 | |
| 36 | 37 | export interface SandboxPolicy { |
| @@ -103,6 +104,30 @@ const HOME_SECRETS = [ | ||
| 103 | 104 | ".claude.json", |
| 104 | 105 | ]; |
| 105 | 106 | |
| 107 | /** | |
| 108 | * Host daemons' sockets outside the clone, hidden by name. Defence in depth | |
| 109 | * only: a run may connect to any unix socket (Linux cannot filter them by | |
| 110 | * path), and this guessed list cannot be complete. The boundary is the | |
| 111 | * forge's own sockets, which live in `SOCKETS_DIR`, denied by a fixed name. | |
outdated
Medium: a guessed list of host paths. It misses |
||
| 112 | */ | |
| 113 | const HOST_SOCKETS = [ | |
| 114 | "/var/run/docker.sock", | |
| 115 | "/run/docker.sock", | |
| 116 | "/var/run/docker", | |
| 117 | "/run/docker", | |
| 118 | "/run/containerd", | |
| 119 | "/run/crio", | |
| 120 | "/var/run/crio", | |
| 121 | "/run/k3s", | |
| 122 | "/run/podman", | |
| 123 | "/run/dbus", | |
| 124 | "/run/systemd", | |
| 125 | "/run/user", | |
| 126 | "/var/run/dbus", | |
| 127 | "/var/run/user", | |
| 128 | "/var/lib/docker", | |
| 129 | ]; | |
| 130 | ||
| 106 | 131 | /** The data directory could not be listed: the run is refused, never left undenied. */ |
| 107 | 132 | export class SandboxListingError extends Error {} |
| 108 | 133 | |
| @@ -160,6 +185,9 @@ const ALWAYS_DENIED = [ | ||
| 160 | 185 | "checks", |
| 161 | 186 | "looks", |
| 162 | 187 | "egress", |
| 188 | SOCKETS_DIR, | |
| 189 | "children", | |
| 190 | "spawn", | |
| 163 | 191 | ]; |
| 164 | 192 | |
| 165 | 193 | /** |
| @@ -242,6 +270,7 @@ export function sandboxPolicy(input: { | ||
| 242 | 270 | // forge's own (a cell's sockets, a checkout's index). |
| 243 | 271 | ...tempDenied(data), |
| 244 | 272 | ]; |
| 273 | deny.push(...HOST_SOCKETS); | |
| 245 | 274 | const home = homedir(); |
| 246 | 275 | for (const secret of HOME_SECRETS) deny.push(join(home, secret)); |
| 247 | 276 | return { |
src/http/server.ts +3 −2
| @@ -61,6 +61,7 @@ import { | ||
| 61 | 61 | import { lstat, readdir, rm } from "node:fs/promises"; |
| 62 | 62 | import { rmSync } from "node:fs"; |
| 63 | 63 | import { join } from "node:path"; |
| 64 | import { SOCKETS_DIR } from "../sockets.ts"; | |
| 64 | 65 | import type { App } from "../app.ts"; |
| 65 | 66 | import { sha256 } from "../kernel/hash.ts"; |
| 66 | 67 | import { |
| @@ -1549,7 +1550,7 @@ function withHeader(response: Response, name: string, value: string): Response { | ||
| 1549 | 1550 | } |
| 1550 | 1551 | } |
| 1551 | 1552 | |
| 1552 | /** The hook sockets a data directory may hold: this forge's, and others'. */ | |
| 1553 | /** The hook sockets a data directory may hold (in `sockets/`): this forge's, and others'. */ | |
| 1553 | 1554 | const SOCKET_FILE = /^forge(?:-[a-z0-9]+)?\.sock$/; |
| 1554 | 1555 | |
| 1555 | 1556 | /** |
| @@ -1561,7 +1562,7 @@ const SOCKET_FILE = /^forge(?:-[a-z0-9]+)?\.sock$/; | ||
| 1561 | 1562 | * EADDRINUSE forever. |
| 1562 | 1563 | */ |
| 1563 | 1564 | async function clearStaleSockets(app: App): Promise<void> { |
| 1564 | const dir = app.config.dataDir; | |
| 1565 | const dir = join(app.config.dataDir, SOCKETS_DIR); | |
| 1565 | 1566 | let names: string[]; |
| 1566 | 1567 | try { |
| 1567 | 1568 | names = (await readdir(dir)).filter((name) => SOCKET_FILE.test(name)); |
src/sockets.ts added +2
| @@ -0,0 +1,2 @@ | ||
| 1 | /** Where the forge's own unix sockets live: one fixed directory the crew sandbox denies whole. */ | |
| 2 | export const SOCKETS_DIR = "sockets"; |
test/crew-sandbox.test.ts +32 −3
| @@ -201,6 +201,31 @@ describe("the shell sandbox denies the whole data directory but the agent's own | ||
| 201 | 201 | const denied = (path: string, deny: readonly string[]) => |
| 202 | 202 | deny.some((d) => path === d || path.startsWith(`${d}/`)); |
| 203 | 203 | |
| 204 | test("the shell may listen on unix sockets, and every socket that matters stays hidden", () => { | |
| 205 | const p = policy(); | |
| 206 | const settings = claudeSettings(p) as { | |
| 207 | sandbox: { network: { allowAllUnixSockets: boolean } }; | |
| 208 | }; | |
| 209 | expect(settings.sandbox.network.allowAllUnixSockets).toBe(true); | |
| 210 | // The forge's hook socket, a cell's socket, the machine's temp directory. | |
| 211 | expect(denied(join(dataDir, "sockets/forge-x.sock"), p.denyRead)).toBe(true); | |
| 212 | expect(denied(join(dataDir, "children/c/app.sock"), p.denyRead)).toBe(true); | |
| 213 | expect(denied(join(dataDir, "spawn/s"), p.denyRead)).toBe(true); | |
| 214 | const cell = join(tmpdir(), "fc-forge-sandbox-neighbour"); | |
| 215 | mkdirSync(cell, { recursive: true }); | |
| 216 | expect(denied(join(cell, "cell.sock"), policy().denyRead)).toBe(true); | |
| 217 | // A host daemon's socket. | |
| 218 | expect(denied("/var/run/docker.sock", p.denyRead)).toBe(true); | |
| 219 | expect(denied("/run/docker.sock", p.denyRead)).toBe(true); | |
| 220 | }); | |
| 221 | ||
| 222 | test("a hook socket made after the policy was taken is still denied (a release's next forge)", () => { | |
| 223 | const p = policy(); | |
| 224 | mkdirSync(join(dataDir, "sockets"), { recursive: true }); | |
| 225 | writeFileSync(join(dataDir, "sockets/forge-y.sock"), ""); | |
| 226 | expect(denied(join(dataDir, "sockets/forge-y.sock"), p.denyRead)).toBe(true); | |
| 227 | }); | |
| 228 | ||
| 204 | 229 | test("every entry of the data directory is denied but crew/, known or not", () => { |
| 205 | 230 | const { denyRead } = policy(); |
| 206 | 231 | for (const entry of [ |
| @@ -310,7 +335,7 @@ describe("the shell sandbox denies the whole data directory but the agent's own | ||
| 310 | 335 | }); |
| 311 | 336 | |
| 312 | 337 | describe("a sandboxed run can run this repository's tests (#1107)", () => { |
| 313 | test("the settings keep the boundary: no unix socket is let through", () => { | |
| 338 | test("the settings let a shell listen on a unix socket, and still deny the forge's own (#1163)", () => { | |
| 314 | 339 | const settings = claudeSettings( |
| 315 | 340 | sandboxPolicy({ |
| 316 | 341 | dataDir: "/tmp/forge-sandbox-test", |
| @@ -318,9 +343,13 @@ describe("a sandboxed run can run this repository's tests (#1107)", () => { | ||
| 318 | 343 | urls: [], |
| 319 | 344 | runId: "01abcdefghijklmnop", |
| 320 | 345 | }), |
| 321 | ).sandbox as { network: Record<string, unknown> }; | |
| 322 | expect(settings.network["allowAllUnixSockets"]).toBeUndefined(); | |
| 346 | ).sandbox as { | |
| 347 | network: Record<string, unknown>; | |
| 348 | filesystem: { denyRead: string[] }; | |
| 349 | }; | |
| 350 | expect(settings.network["allowAllUnixSockets"]).toBe(true); | |
| 323 | 351 | expect(settings.network["allowUnixSockets"]).toBeUndefined(); |
| 352 | expect(settings.filesystem.denyRead).toContain("/tmp/forge-sandbox-test/sockets"); | |
| 324 | 353 | }); |
| 325 | 354 | |
| 326 | 355 | test("in port mode no cell is offered: its proxy socket cannot be bound", async () => { |
test/hardening.test.ts +1 −1
| @@ -91,7 +91,7 @@ bySocket("two forges on one data directory", () => { | ||
| 91 | 91 | test("a socket another forge still answers on is left alone, and this forge boots beside it", async () => { |
| 92 | 92 | const dataDir = await mkdtemp(join(tmpdir(), "forge-harden-share-")); |
| 93 | 93 | const app = createApp({ dataDir, port: 0 }); |
| 94 | const other = join(dataDir, "forge-other.sock"); | |
| 94 | const other = join(dataDir, "sockets", "forge-other.sock"); | |
| 95 | 95 | // A forge that is still alive — a release's old process, draining. |
| 96 | 96 | const child = Bun.spawn( |
| 97 | 97 | [ |
new above
offline
High: this opens connect() to every unix socket, not just listen().
denyReadnames data-directory entries by listing them at launch, so a hook socket created later (the next forge during a release,forge-<newid>.sock) stays visible and connectable.handleHooktrustsactorIdfrom the body. Deny the forge's sockets by a fixed directory name inALWAYS_DENIEDbefore turning this on.