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 #1483

Closed opened this
Closed without merging

scout/forge:scout/1163-a-crew-run-can-run-the-server-tests-its → main 5 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
8 files changed +79 −7
scripts/gate.ts +1
@@ -207,6 +207,7 @@ export const GATE: readonly string[] = [
207207 "test/home-feed.test.ts",
208208 "test/oauth-discovery.test.ts",
209209 "test/oauth-flow.test.ts",
210 "test/review-from-check.test.ts",
210211 "test/data-home.test.ts",
211212 "test/tree.test.ts",
212213 "test/reports.test.ts",
src/app.ts +6 −1
@@ -9,6 +9,7 @@ import type { Database } from "bun:sqlite";
99 import type { Decider } from "./crew/decide.ts";
1010 import { randomBytes } from "node:crypto";
1111 import { mkdirSync } from "node:fs";
12 import { SOCKETS_DIR } from "./sockets.ts";
1213 import { join, resolve } from "node:path";
1314 import { migrations } from "./core/schema.ts";
1415 import { dropLegacyNewAccountGuards } from "./core/schema.ts";
@@ -149,7 +150,11 @@ export function createApp(config: ForgeConfig): App {
149150 const dataDir = resolve(config.dataDir);
150151 const reposRoot = join(dataDir, "repos");
151152 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`);
153158
154159 const db = openDb(join(dataDir, "forge.db"), migrations);
155160 // Hand-installed new-account guards become the `guard_new_accounts`
src/crew/claude.ts +5
@@ -118,6 +118,11 @@ export function claudeSettings(
118118 allowedDomains: [...sandbox.allowHosts],
119119 // An app under test listens on a local port.
120120 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,
121126 },
122127 filesystem: { denyRead: [...sandbox.denyRead] },
123128 },
src/crew/sandbox.ts +29
@@ -31,6 +31,7 @@
3131 import { readdirSync, rmSync, statSync } from "node:fs";
3232 import { homedir, tmpdir } from "node:os";
3333 import { basename, isAbsolute, join, relative, sep } from "node:path";
34 import { SOCKETS_DIR } from "../sockets.ts";
3435 import { crewRoot } from "./workdir.ts";
3536
3637 export interface SandboxPolicy {
@@ -103,6 +104,30 @@ const HOME_SECRETS = [
103104 ".claude.json",
104105 ];
105106
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.
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
106131 /** The data directory could not be listed: the run is refused, never left undenied. */
107132 export class SandboxListingError extends Error {}
108133
@@ -160,6 +185,9 @@ const ALWAYS_DENIED = [
160185 "checks",
161186 "looks",
162187 "egress",
188 SOCKETS_DIR,
189 "children",
190 "spawn",
163191 ];
164192
165193 /**
@@ -242,6 +270,7 @@ export function sandboxPolicy(input: {
242270 // forge's own (a cell's sockets, a checkout's index).
243271 ...tempDenied(data),
244272 ];
273 deny.push(...HOST_SOCKETS);
245274 const home = homedir();
246275 for (const secret of HOME_SECRETS) deny.push(join(home, secret));
247276 return {
src/http/server.ts +3 −2
@@ -61,6 +61,7 @@ import {
6161 import { lstat, readdir, rm } from "node:fs/promises";
6262 import { rmSync } from "node:fs";
6363 import { join } from "node:path";
64 import { SOCKETS_DIR } from "../sockets.ts";
6465 import type { App } from "../app.ts";
6566 import { sha256 } from "../kernel/hash.ts";
6667 import {
@@ -1549,7 +1550,7 @@ function withHeader(response: Response, name: string, value: string): Response {
15491550 }
15501551 }
15511552
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'. */
15531554 const SOCKET_FILE = /^forge(?:-[a-z0-9]+)?\.sock$/;
15541555
15551556 /**
@@ -1561,7 +1562,7 @@ const SOCKET_FILE = /^forge(?:-[a-z0-9]+)?\.sock$/;
15611562 * EADDRINUSE forever.
15621563 */
15631564 async function clearStaleSockets(app: App): Promise<void> {
1564 const dir = app.config.dataDir;
1565 const dir = join(app.config.dataDir, SOCKETS_DIR);
15651566 let names: string[];
15661567 try {
15671568 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
201201 const denied = (path: string, deny: readonly string[]) =>
202202 deny.some((d) => path === d || path.startsWith(`${d}/`));
203203
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
204229 test("every entry of the data directory is denied but crew/, known or not", () => {
205230 const { denyRead } = policy();
206231 for (const entry of [
@@ -310,7 +335,7 @@ describe("the shell sandbox denies the whole data directory but the agent's own
310335 });
311336
312337 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)", () => {
314339 const settings = claudeSettings(
315340 sandboxPolicy({
316341 dataDir: "/tmp/forge-sandbox-test",
@@ -318,9 +343,13 @@ describe("a sandboxed run can run this repository's tests (#1107)", () => {
318343 urls: [],
319344 runId: "01abcdefghijklmnop",
320345 }),
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);
323351 expect(settings.network["allowUnixSockets"]).toBeUndefined();
352 expect(settings.filesystem.denyRead).toContain("/tmp/forge-sandbox-test/sockets");
324353 });
325354
326355 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", () => {
9191 test("a socket another forge still answers on is left alone, and this forge boots beside it", async () => {
9292 const dataDir = await mkdtemp(join(tmpdir(), "forge-harden-share-"));
9393 const app = createApp({ dataDir, port: 0 });
94 const other = join(dataDir, "forge-other.sock");
94 const other = join(dataDir, "sockets", "forge-other.sock");
9595 // A forge that is still alive — a release's old process, draining.
9696 const child = Bun.spawn(
9797 [

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs