Public: anyone can read this; the crew can change it.
A check's sockets live under a short fixed directory, and a rule test bounds its length #15878
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15832-a-check-s-sockets-live-under-a-short-fix → main 0 commits fork 2 threads · 2 unresolved
6 files
changed +356
−32
src/crew/runner.ts +2 −2
| @@ -188,7 +188,7 @@ import { | ||
| 188 | 188 | } from "../core/usage.ts"; |
| 189 | 189 | import { trackAgentRun, untrackAgentRun } from "./running.ts"; |
| 190 | 190 | import { workBegins, workEnds } from "../runs/ending.ts"; |
| 191 | import { installCacheEnv, sandboxPolicy } from "./sandbox.ts"; | |
| 191 | import { installCacheEnv, makeRunTmp, sandboxPolicy } from "./sandbox.ts"; | |
| 192 | 192 | import { runGitAuth } from "./git_auth.ts"; |
| 193 | 193 | import { grantSecrets, needsYou, parseDeclaration, ungrantedSentence, type Grant } from "./grant.ts"; |
| 194 | 194 | import { historyHolds, scrubTree } from "./leak.ts"; |
| @@ -1627,7 +1627,7 @@ async function work( | ||
| 1627 | 1627 | // `<TMPDIR>/claude-<uid>` writable, so TMPDIR has to say the same. |
| 1628 | 1628 | runTmp = sandbox.tmpDir; |
| 1629 | 1629 | if (runTmp !== undefined) { |
| 1630 | mkdirSync(runTmp, { recursive: true }); | |
| 1630 | makeRunTmp(app.config.dataDir, runTmp); | |
| 1631 | 1631 | Object.assign(env, sandboxTmpEnv(runTmp)); |
| 1632 | 1632 | env["TMPDIR"] = runTmp; |
| 1633 | 1633 | } |
src/crew/sandbox.ts +137 −22
| @@ -32,9 +32,9 @@ | ||
| 32 | 32 | * crew start (`sweepRunTmp`). |
| 33 | 33 | */ |
| 34 | 34 | |
| 35 | import { mkdirSync, readdirSync, rmSync, statSync } from "node:fs"; | |
| 35 | import { accessSync, constants, lstatSync, mkdirSync, readdirSync, rmSync, statSync } from "node:fs"; | |
| 36 | 36 | import { homedir, tmpdir } from "node:os"; |
| 37 | import { basename, isAbsolute, join, relative } from "node:path"; | |
| 37 | import { basename, dirname, isAbsolute, join, relative } from "node:path"; | |
| 38 | 38 | import { dropDir } from "../drop/store.ts"; |
| 39 | 39 | import { cacheRoot, crewRoot, installCacheDir } from "./workdir.ts"; |
| 40 | 40 | |
| @@ -178,10 +178,9 @@ export function dataInTemp(data: string): boolean { | ||
| 178 | 178 | return inside(tmpdir(), data); |
| 179 | 179 | } |
| 180 | 180 | |
| 181 | function tempDenied(data: string, tmpDir: string | undefined): string[] { | |
| 182 | const temp = tmpdir(); | |
| 181 | function tempDenied(data: string, tmpDir: string | undefined, temp: string): string[] { | |
| 183 | 182 | const own = tmpDir !== undefined && !inside(data, tmpDir) && inside(temp, tmpDir) ? basename(tmpDir) : undefined; |
| 184 | if (!dataInTemp(data)) { | |
| 183 | if (!inside(temp, data)) { | |
| 185 | 184 | // The run's own temp directory is in it (`runTmpDir`): everything else there is denied. |
| 186 | 185 | return own === undefined ? [temp] : others(temp, own); |
| 187 | 186 | } |
| @@ -220,7 +219,7 @@ const ALWAYS_DENIED = [ | ||
| 220 | 219 | * which stopped mid-run left behind. Only old ones: during a rollout the |
| 221 | 220 | * forge being replaced is still finishing its runs beside this one. |
| 222 | 221 | */ |
| 223 | export function sweepRunTmp(dataDir: string, olderThanMs: number): number { | |
| 222 | export function sweepRunTmp(dataDir: string, olderThanMs: number, roots: readonly string[] = shortRoots()): number { | |
| 224 | 223 | const cutoff = Date.now() - olderThanMs; |
| 225 | 224 | let swept = 0; |
| 226 | 225 | for (const agent of others(crewRoot(dataDir), "")) { |
| @@ -234,7 +233,14 @@ export function sweepRunTmp(dataDir: string, olderThanMs: number): number { | ||
| 234 | 233 | } |
| 235 | 234 | } |
| 236 | 235 | } |
| 237 | for (const dir of others(tmpdir(), "").filter((path) => basename(path).startsWith(RUN_TMP_PREFIX))) { | |
| 236 | const shortRuns = roots.filter((root) => root !== tmpdir()).flatMap((root) => { | |
| 237 | try { | |
| 238 | return others(root, ""); | |
| 239 | } catch { | |
| 240 | return []; // a short root this forge cannot list holds none of its runs | |
| 241 | } | |
| 242 | }); | |
| 243 | for (const dir of [...others(tmpdir(), ""), ...shortRuns].filter((path) => basename(path).startsWith(RUN_TMP_PREFIX))) { | |
| 238 | 244 | try { |
| 239 | 245 | if (statSync(dir).mtimeMs > cutoff) continue; |
| 240 | 246 | rmSync(dir, { recursive: true, force: true }); |
| @@ -247,12 +253,73 @@ export function sweepRunTmp(dataDir: string, olderThanMs: number): number { | ||
| 247 | 253 | } |
| 248 | 254 | |
| 249 | 255 | /** |
| 250 | * What a socket name takes in the run's temp directory: `/claude-socks-<16 hex>.sock`, with room to | |
| 251 | * spare. | |
| 256 | * The longest socket path a run's temp directory gets: Claude Code's bridge sockets, | |
| 257 | * `<tmpDir>/claude-<uid>/claude-socks-<16 hex>.sock` (`shellTmpDir`). | |
| 252 | 258 | */ |
| 253 | const SOCKET_NAME_ROOM = 40; | |
| 259 | export function longestSocketPath(tmpDir: string): string { | |
| 260 | return join(tmpDir, `claude-${process.getuid?.() ?? 0}`, "claude-socks-0123456789abcdef.sock"); | |
| 261 | } | |
| 254 | 262 | /** The longest path a unix socket may have on Linux (`sun_path`, less its NUL). */ |
| 255 | 263 | export const SOCKET_PATH_MAX = 107; |
| 264 | /** | |
| 265 | * The bound a run's temp directory is held to: under `SOCKET_PATH_MAX`, so a socket name a little | |
| 266 | * longer than today's still binds (#13752). | |
| 267 | */ | |
| 268 | export const SOCKET_PATH_BUDGET = 100; | |
| 269 | ||
| 270 | function fits(tmpDir: string): boolean { | |
| 271 | return Buffer.byteLength(longestSocketPath(tmpDir)) <= SOCKET_PATH_BUDGET; | |
| 272 | } | |
| 273 | ||
| 274 | /** | |
| 275 | * The short fixed directories a run's temp directory falls back to when neither the agent's nor | |
| 276 | * the machine's temp leaves its sockets room: `/run/forge`, else `/tmp/forge-<uid>`. Each is this | |
| 277 | * forge's alone (`shortRoot`), never the shared `/tmp` itself, so everything in it but the run's | |
| 278 | * own directory can be denied (`shortRootDenied`). | |
| 279 | */ | |
| 280 | export function shortRoots(): readonly string[] { | |
| 281 | return ["/run/forge", `/tmp/forge-${process.getuid?.() ?? 0}`]; | |
| 282 | } | |
| 283 | ||
| 284 | /** | |
| 285 | * Why `root` is not a directory of this forge's own, or undefined when it is: made if missing, | |
| 286 | * never recursively (a missing parent is a refusal), and an existing entry is checked, never | |
| 287 | * trusted: not a symlink, owned by this uid, closed to everyone else. | |
| 288 | */ | |
| 289 | function unfitRoot(root: string): string | undefined { | |
| 290 | try { | |
| 291 | mkdirSync(root, { mode: 0o700 }); | |
| 292 | } catch (error) { | |
| 293 | if ((error as NodeJS.ErrnoException).code !== "EEXIST") return (error as Error).message; | |
| 294 | } | |
| 295 | try { | |
| 296 | const stat = lstatSync(root); | |
| 297 | if (stat.isSymbolicLink()) return `${root} is a symlink`; | |
| 298 | if (!stat.isDirectory()) return `${root} is not a directory`; | |
| 299 | const uid = process.getuid?.(); | |
| 300 | if (uid !== undefined && stat.uid !== uid) return `${root} is owned by uid ${stat.uid}, not ${uid}`; | |
| 301 | if ((stat.mode & 0o077) !== 0) return `${root} is open to others (mode ${(stat.mode & 0o777).toString(8)})`; | |
| 302 | accessSync(root, constants.W_OK | constants.X_OK); | |
| 303 | return undefined; | |
| 304 | } catch (error) { | |
| 305 | return (error as Error).message; | |
| 306 | } | |
| 307 | } | |
| 308 | ||
| 309 | /** | |
| 310 | * The first of `roots` that is, or is made, a directory of this forge's own: not a symlink, owned | |
| 311 | * by this uid, closed to everyone else. None is: the run is refused (fail closed), never put in a | |
| 312 | * directory another user could have planted or can read. | |
| 313 | */ | |
| 314 | export function shortRoot(roots: readonly string[] = shortRoots()): string { | |
| 315 | const why: string[] = []; | |
| 316 | for (const root of roots) { | |
| 317 | const unfit = unfitRoot(root); | |
| 318 | if (unfit === undefined) return root; | |
| 319 | why.push(unfit); | |
| 320 | } | |
| 321 | throw new SandboxListingError(`no short directory of this forge's own for the run's sockets: ${why.join("; ")}`); | |
| 322 | } | |
| 256 | 323 | |
| 257 | 324 | /** |
| 258 | 325 | * Where a run's temp directory lives: under the agent, unless that leaves |
| @@ -260,22 +327,65 @@ export const SOCKET_PATH_MAX = 107; | ||
| 260 | 327 | * sits under `children/cell-<uuid>`, so there it is the machine's temp |
| 261 | 328 | * directory instead (in a cell, the cell's own `/tmp`). scottishbob, |
| 262 | 329 | * 2026-11-10: every Bash call failed "Failed to create bridge sockets after |
| 263 | * 5 attempts" at a 124-character socket path. | |
| 330 | * 5 attempts" at a 124-character socket path. Inside a hosted forge's check | |
| 331 | * the machine's temp is the check's long scratch path too, so there it is a | |
| 332 | * short fixed directory (`shortRoot`, #13752). | |
| 264 | 333 | */ |
| 265 | export function runTmpDir( | |
| 266 | dataDir: string, | |
| 267 | agentName: string, | |
| 268 | runId: string, | |
| 269 | host: { readonly linux: boolean; readonly temp: string } = { linux: process.platform === "linux", temp: tmpdir() }, | |
| 270 | ): string { | |
| 271 | const own = join(crewRoot(dataDir), agentName, ".t", runId.slice(-12)); | |
| 272 | if (!host.linux || own.length + SOCKET_NAME_ROOM <= SOCKET_PATH_MAX) return own; | |
| 273 | return join(host.temp, `${RUN_TMP_PREFIX}${runId.slice(-12)}`); | |
| 334 | export function runTmpDir(dataDir: string, agentName: string, runId: string, host: TmpHost = machineHost()): string { | |
| 335 | const id = runId.slice(-12); | |
| 336 | const own = join(crewRoot(dataDir), agentName, ".t", id); | |
| 337 | if (!host.linux || fits(own)) return own; | |
| 338 | const temp = join(host.temp, `${RUN_TMP_PREFIX}${id}`); | |
| 339 | if (fits(temp)) return temp; | |
| 340 | return join(host.short?.() ?? shortRoot(host.roots ?? shortRoots()), `${RUN_TMP_PREFIX}${id}`); | |
| 341 | } | |
| 342 | ||
| 343 | /** Where a run's temp directory may go (`runTmpDir`): injectable, so a test names its own. */ | |
| 344 | export interface TmpHost { | |
| 345 | readonly linux: boolean; | |
| 346 | /** The machine's temp directory. */ | |
| 347 | readonly temp: string; | |
| 348 | /** The short roots to try (`shortRoots`). */ | |
| 349 | readonly roots?: readonly string[] | undefined; | |
| 350 | /** The short root, chosen already: no directory is made or checked. */ | |
| 351 | readonly short?: (() => string) | undefined; | |
| 352 | } | |
| 353 | ||
| 354 | /** This machine, as `runTmpDir` sees it. */ | |
| 355 | export function machineHost(): TmpHost { | |
| 356 | return { linux: process.platform === "linux", temp: tmpdir() }; | |
| 357 | } | |
| 358 | ||
| 359 | /** | |
| 360 | * Make the run's temp directory (`runTmpDir`). Under the data directory it is the forge's own | |
| 361 | * tree; anywhere else (a shared `/tmp`, a short root) its name is predictable, so it is made | |
| 362 | * alone and checked like a short root (`unfitRoot`): one planted there first refuses the run. | |
| 363 | */ | |
| 364 | export function makeRunTmp(dataDir: string, dir: string): void { | |
| 365 | if (inside(dataDir, dir)) { | |
| 366 | mkdirSync(dir, { recursive: true }); | |
| 367 | return; | |
| 368 | } | |
| 369 | const unfit = unfitRoot(dir); | |
| 370 | if (unfit !== undefined) throw new SandboxListingError(`the run's temp directory is not this forge's own: ${unfit}`); | |
| 274 | 371 | } |
| 275 | 372 | |
| 276 | 373 | /** A run temp directory in the machine's temp directory (`runTmpDir`) starts so. */ |
| 277 | 374 | const RUN_TMP_PREFIX = "fr-"; |
| 278 | 375 | |
| 376 | /** | |
| 377 | * A run temp directory in a short root (`runTmpDir`): every other entry | |
| 378 | * there is denied, whatever its name. The root is this forge's alone | |
| 379 | * (`shortRoot`), so the listing is short. An entry holding the data | |
| 380 | * directory is the one kept, as in the machine's temp (`tempDenied`). | |
| 381 | */ | |
| 382 | export function shortRootDenied(data: string, tmpDir: string | undefined, temp: string, roots: readonly string[]): string[] { | |
| 383 | if (tmpDir === undefined || inside(data, tmpDir) || inside(temp, tmpDir)) return []; | |
| 384 | const root = dirname(tmpDir); | |
| 385 | if (!roots.includes(root)) return []; | |
| 386 | return others(root, basename(tmpDir)).filter((path) => path !== data && !inside(path, data)); | |
| 387 | } | |
| 388 | ||
| 279 | 389 | /** Whether `path` is inside `dir`. */ |
| 280 | 390 | export function inside(dir: string, path: string): boolean { |
| 281 | 391 | const rel = relative(dir, path); |
| @@ -323,6 +433,8 @@ export function sandboxPolicy(input: { | ||
| 323 | 433 | runId?: string | undefined; |
| 324 | 434 | /** The repository the run is on: its install cache is writable (`installCache`). */ |
| 325 | 435 | repoId?: string | undefined; |
| 436 | /** Where the run's temp directory may go (`runTmpDir`); this machine's by default. */ | |
| 437 | tmpHost?: TmpHost | undefined; | |
| 326 | 438 | }): SandboxPolicy { |
| 327 | 439 | const hosts = new Set<string>(["registry.npmjs.org"]); |
| 328 | 440 | for (const url of input.urls) { |
| @@ -354,10 +466,11 @@ export function sandboxPolicy(input: { | ||
| 354 | 466 | const data = input.dataDir; |
| 355 | 467 | const crew = crewRoot(data); |
| 356 | 468 | const own = join(crew, input.agentName); |
| 469 | const host = input.tmpHost ?? machineHost(); | |
| 357 | 470 | const tmpDir = |
| 358 | 471 | input.runId === undefined |
| 359 | 472 | ? undefined |
| 360 | : runTmpDir(data, input.agentName, input.runId); | |
| 473 | : runTmpDir(data, input.agentName, input.runId, host); | |
| 361 | 474 | const installCache = |
| 362 | 475 | input.repoId === undefined |
| 363 | 476 | ? undefined |
| @@ -376,7 +489,9 @@ export function sandboxPolicy(input: { | ||
| 376 | 489 | ...others(join(own, ".t"), tmpDir === undefined ? "" : basename(tmpDir)), |
| 377 | 490 | // The machine's temp directory: every other session's files, and the |
| 378 | 491 | // forge's own (a cell's sockets, a checkout's index). |
| 379 | ...tempDenied(data, tmpDir), | |
| 492 | ...tempDenied(data, tmpDir, host.temp), | |
| 493 | // A short root's (`shortRoot`): every entry but this run's. | |
| 494 | ...shortRootDenied(data, tmpDir, host.temp, host.roots ?? shortRoots()), | |
| 380 | 495 | // Every repository's install cache, whole and not listed: one created |
| 381 | 496 | // after this policy was built is denied too. This run's own is |
| 382 | 497 | // re-allowed (`readAllow`) and written (`allowWrite`). |
test/crew-sandbox.test.ts +31 −8
| @@ -44,9 +44,12 @@ import { | ||
| 44 | 44 | SandboxListingError, |
| 45 | 45 | sandboxAvailable, |
| 46 | 46 | runTmpDir, |
| 47 | longestSocketPath, | |
| 48 | SOCKET_PATH_BUDGET, | |
| 47 | 49 | sandboxPolicy, |
| 48 | 50 | sweepRunTmp, |
| 49 | 51 | } from "../src/crew/sandbox.ts"; |
| 52 | import { shortTemp } from "./helpers/short-temp.ts"; | |
| 50 | 53 | import { bridgeTmp } from "./helpers/srt.ts"; |
| 51 | 54 | |
| 52 | 55 | describe("the shell sandbox", () => { |
| @@ -616,17 +619,25 @@ describe("a run's temp directory leaves Claude Code's sockets room to bind", () | ||
| 616 | 619 | test.skipIf(process.platform !== "linux")("a child forge's run reads and writes its own temp directory in the machine's, and nothing else there", () => { |
| 617 | 620 | const data = join(tmpdir(), `forge-long-${Date.now()}`, "children", "cell-3ea623d0-14ce-43f3-a6fd-10385eb629a6"); |
| 618 | 621 | mkdirSync(join(data, "crew", "scottishbot"), { recursive: true }); |
| 619 | const neighbour = join(tmpdir(), `fc-neighbour-${Date.now()}`); | |
| 622 | // A machine whose temp is short: the run's temp is there, by name. | |
| 623 | const temp = shortTemp(); | |
| 624 | const neighbour = join(temp, `fc-neighbour-${Date.now()}`); | |
| 620 | 625 | mkdirSync(neighbour, { recursive: true }); |
| 621 | const otherRun = join(tmpdir(), `fr-other${Date.now()}`); | |
| 626 | const otherRun = join(temp, `fr-other${Date.now()}`); | |
| 622 | 627 | mkdirSync(otherRun, { recursive: true }); |
| 623 | const p = sandboxPolicy({ dataDir: data, agentName: "scottishbot", urls: [], runId: "01m71kw4dfp4z8arr359fkygy5" }); | |
| 624 | expect(p.tmpDir).toBe(join(tmpdir(), "fr-arr359fkygy5")); | |
| 628 | const p = sandboxPolicy({ | |
outdated
Tautological: it compares runTmpDir to itself. Assert a concrete path with an injected host. |
||
| 629 | dataDir: data, | |
| 630 | agentName: "scottishbot", | |
| 631 | urls: [], | |
| 632 | runId: "01m71kw4dfp4z8arr359fkygy5", | |
| 633 | tmpHost: { linux: true, temp, roots: [] }, | |
| 634 | }); | |
| 635 | expect(p.tmpDir).toBe(join(temp, "fr-arr359fkygy5")); | |
| 636 | expect(Buffer.byteLength(longestSocketPath(p.tmpDir!))).toBeLessThanOrEqual(SOCKET_PATH_BUDGET); | |
| 625 | 637 | expect(p.denyRead).not.toContain(p.tmpDir); |
| 626 | 638 | expect(p.denyRead).toContain(neighbour); |
| 627 | 639 | expect(p.denyRead).toContain(otherRun); |
| 628 | // The data directory is in the temp directory too: it is not denied whole, | |
| 629 | // so the agent still reaches its own working directory. | |
| 640 | // The agent still reaches its own working directory. | |
| 630 | 641 | const under = (path: string) => p.denyRead.some((d) => path === d || path.startsWith(`${d}/`)); |
| 631 | 642 | expect(under(join(data, "crew", "scottishbot", "01repo", "7"))).toBe(false); |
| 632 | 643 | expect(p.denyRead).toContain(join(data, "forge.db")); |
| @@ -644,9 +655,21 @@ test.skipIf(process.platform !== "linux")("in a long temp directory holding the | ||
| 644 | 655 | for (const dir of ["crew/scout/01repo/7", "crew/mason", "repos"]) mkdirSync(join(data, dir), { recursive: true }); |
| 645 | 656 | mkdirSync(join(temp, "fr-otherrun0001"), { recursive: true }); |
| 646 | 657 | mkdirSync(join(temp, "fc-somecell"), { recursive: true }); |
| 647 | const p = sandboxPolicy({ dataDir: data, agentName: "scout", urls: [], runId: "01m46abcdefghjkmnpqrstvwxy" }); | |
| 658 | const root = join(shortTemp(), "forge"); | |
| 659 | mkdirSync(join(root, "fr-elsewhere00"), { recursive: true, mode: 0o700 }); | |
| 660 | const p = sandboxPolicy({ | |
| 661 | dataDir: data, | |
| 662 | agentName: "scout", | |
| 663 | urls: [], | |
| 664 | runId: "01m46abcdefghjkmnpqrstvwxy", | |
| 665 | tmpHost: { linux: true, temp, roots: [root] }, | |
| 666 | }); | |
| 648 | 667 | const denied = (path: string) => p.denyRead.some((d) => path === d || path.startsWith(`${d}/`)); |
| 649 | expect(p.tmpDir).toBe(join(temp, "fr-kmnpqrstvwxy")); | |
| 668 | // The temp is too long for the run's sockets as well, so the run's temp is the short root's | |
| 669 | // (#13752), and every other entry there is denied. | |
| 670 | expect(p.tmpDir).toBe(join(root, "fr-kmnpqrstvwxy")); | |
| 671 | expect(denied(join(root, "fr-elsewhere00"))).toBe(true); | |
| 672 | expect(Buffer.byteLength(longestSocketPath(p.tmpDir!))).toBeLessThanOrEqual(SOCKET_PATH_BUDGET); | |
| 650 | 673 | expect(denied(join(data, "crew/scout/01repo/7"))).toBe(false); |
| 651 | 674 | expect(denied(p.tmpDir!)).toBe(false); |
| 652 | 675 | expect(denied(join(data, "crew/mason"))).toBe(true); |
test/helpers/short-temp.ts added +23
| @@ -0,0 +1,23 @@ | ||
| 1 | /** | |
| 2 | * A fresh directory with a path short enough for a run's sockets (`runTmpDir`), removed when the | |
| 3 | * test process exits: in `/tmp` where it is writable, else in a sandbox's `/tmp/claude`, else in | |
| 4 | * the machine's temp. A check's temp is too long to stand for "a machine whose temp is short". | |
| 5 | */ | |
| 6 | ||
| 7 | import { mkdirSync, mkdtempSync, rmSync } from "node:fs"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | ||
| 11 | export function shortTemp(): string { | |
| 12 | for (const base of ["/tmp", "/tmp/claude", tmpdir()]) { | |
| 13 | try { | |
| 14 | mkdirSync(base, { recursive: true }); | |
| 15 | const dir = mkdtempSync(join(base, "fq-")); | |
| 16 | process.once("exit", () => rmSync(dir, { recursive: true, force: true })); | |
| 17 | return dir; | |
| 18 | } catch { | |
| 19 | // Not writable here: the next. | |
| 20 | } | |
| 21 | } | |
| 22 | throw new Error("no writable temp directory"); | |
| 23 | } |
test/sandbox-short-root.test.ts added +113
| @@ -0,0 +1,113 @@ | ||
| 1 | /** | |
| 2 | * The short root a run's temp directory falls back to (#13752) is this forge's alone: made | |
| 3 | * without following a planted entry, refused when someone else could read it, and everything in | |
| 4 | * it but the run's own directory denied and swept. | |
| 5 | */ | |
| 6 | ||
| 7 | import { describe, expect, test } from "bun:test"; | |
| 8 | import { chmodSync, existsSync, mkdirSync, statSync, symlinkSync, utimesSync, writeFileSync } from "node:fs"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { makeRunTmp, SandboxListingError, shortRoot, shortRootDenied, shortRoots, sweepRunTmp } from "../src/crew/sandbox.ts"; | |
| 11 | import { shortTemp } from "./helpers/short-temp.ts"; | |
| 12 | ||
| 13 | describe("a short root is this forge's own", () => { | |
| 14 | test("the defaults are /run/forge and a per-user directory in /tmp, never /tmp itself", () => { | |
| 15 | expect(shortRoots()).toEqual(["/run/forge", `/tmp/forge-${process.getuid?.() ?? 0}`]); | |
| 16 | }); | |
| 17 | ||
| 18 | test("a missing root is made closed to everyone else", () => { | |
| 19 | const root = join(shortTemp(), "r"); | |
| 20 | expect(shortRoot([root])).toBe(root); | |
| 21 | expect(statSync(root).mode & 0o777).toBe(0o700); | |
| 22 | }); | |
| 23 | ||
| 24 | test("a symlink, a root open to others or one under a missing parent is passed over for the next", () => { | |
| 25 | const base = shortTemp(); | |
| 26 | const target = join(base, "target"); | |
| 27 | mkdirSync(target, { mode: 0o700 }); | |
| 28 | const link = join(base, "link"); | |
| 29 | symlinkSync(target, link); | |
| 30 | const open = join(base, "open"); | |
| 31 | mkdirSync(open); | |
| 32 | chmodSync(open, 0o755); | |
| 33 | const orphan = join(base, "missing", "root"); | |
| 34 | const good = join(base, "good"); | |
| 35 | expect(shortRoot([link, open, orphan, good])).toBe(good); | |
| 36 | expect(existsSync(join(base, "missing"))).toBe(false); | |
| 37 | }); | |
| 38 | ||
| 39 | test("none fits: the run is refused, never put in a shared directory", () => { | |
| 40 | const base = shortTemp(); | |
| 41 | const file = join(base, "file"); | |
| 42 | writeFileSync(file, ""); | |
| 43 | expect(() => shortRoot([file, join(base, "missing", "root")])).toThrow(SandboxListingError); | |
| 44 | }); | |
| 45 | }); | |
| 46 | ||
| 47 | describe("a run in a short root reads nothing else there", () => { | |
| 48 | test("every sibling is denied whatever its name, the run's own is not", () => { | |
| 49 | const root = join(shortTemp(), "r"); | |
| 50 | for (const name of ["fr-own00000000", "fr-other000000", "fc-cell", "someone-elses-session"]) mkdirSync(join(root, name), { recursive: true }); | |
| 51 | writeFileSync(join(root, "notes.txt"), "private"); | |
| 52 | const own = join(root, "fr-own00000000"); | |
| 53 | const denied = shortRootDenied("/data", own, "/elsewhere", [root]); | |
| 54 | expect(denied.sort()).toEqual( | |
| 55 | ["fc-cell", "fr-other000000", "notes.txt", "someone-elses-session"].map((name) => join(root, name)).sort(), | |
| 56 | ); | |
| 57 | }); | |
| 58 | ||
| 59 | test("a run not in a short root, or in the machine's temp, adds nothing", () => { | |
| 60 | const root = join(shortTemp(), "r"); | |
| 61 | mkdirSync(join(root, "fr-other000000"), { recursive: true }); | |
| 62 | expect(shortRootDenied("/data", "/data/crew/scout/.t/x", "/tmp", [root])).toEqual([]); | |
| 63 | expect(shortRootDenied("/data", join(root, "fr-own"), root, [root])).toEqual([]); | |
| 64 | expect(shortRootDenied("/data", "/somewhere/fr-own", "/tmp", [root])).toEqual([]); | |
| 65 | }); | |
| 66 | ||
| 67 | test("an entry holding the data directory is kept, so the agent reaches its own work", () => { | |
| 68 | const root = join(shortTemp(), "r"); | |
| 69 | const data = join(root, "forge-data"); | |
| 70 | mkdirSync(data, { recursive: true }); | |
| 71 | mkdirSync(join(root, "fr-other000000")); | |
| 72 | expect(shortRootDenied(data, join(root, "fr-own"), "/elsewhere", [root])).toEqual([join(root, "fr-other000000")]); | |
| 73 | }); | |
| 74 | }); | |
| 75 | ||
| 76 | describe("a short root's run directories", () => { | |
| 77 | test("old ones are swept, fresh ones and other entries kept", () => { | |
| 78 | const base = shortTemp(); | |
| 79 | const data = join(base, "data"); | |
| 80 | mkdirSync(join(data, "crew"), { recursive: true }); | |
| 81 | const root = join(base, "r"); | |
| 82 | const old = join(root, "fr-old0000000"); | |
| 83 | const fresh = join(root, "fr-fresh00000"); | |
| 84 | const other = join(root, "keep-me"); | |
| 85 | for (const dir of [old, fresh, other]) mkdirSync(dir, { recursive: true }); | |
| 86 | const longAgo = new Date(Date.now() - 3 * 60 * 60_000); | |
| 87 | utimesSync(old, longAgo, longAgo); | |
| 88 | utimesSync(other, longAgo, longAgo); | |
| 89 | expect(sweepRunTmp(data, 60 * 60_000, [root])).toBeGreaterThanOrEqual(1); | |
| 90 | expect(existsSync(old)).toBe(false); | |
| 91 | expect(existsSync(fresh)).toBe(true); | |
| 92 | expect(existsSync(other)).toBe(true); | |
| 93 | }); | |
| 94 | ||
| 95 | test("a run's directory outside the data is made alone and closed; one planted first refuses the run", () => { | |
| 96 | const base = shortTemp(); | |
| 97 | const dir = join(base, "fr-mine0000000"); | |
| 98 | makeRunTmp("/data", dir); | |
| 99 | expect(statSync(dir).mode & 0o777).toBe(0o700); | |
| 100 | const target = join(base, "theirs"); | |
| 101 | mkdirSync(target, { mode: 0o700 }); | |
| 102 | const planted = join(base, "fr-planted0000"); | |
| 103 | symlinkSync(target, planted); | |
| 104 | expect(() => makeRunTmp("/data", planted)).toThrow(SandboxListingError); | |
| 105 | }); | |
| 106 | ||
| 107 | test("under the data directory it is the forge's own tree, made with its parents", () => { | |
| 108 | const data = join(shortTemp(), "data"); | |
| 109 | const dir = join(data, "crew", "scout", ".t", "abc"); | |
| 110 | makeRunTmp(data, dir); | |
| 111 | expect(existsSync(dir)).toBe(true); | |
| 112 | }); | |
| 113 | }); |
test/sandbox-socket-path.test.ts added +50
| @@ -0,0 +1,50 @@ | ||
| 1 | /** | |
| 2 | * Rule: the sockets a crew run's sandbox binds fit `sun_path` with room to spare, however long | |
| 3 | * the check's scratch path is (#13752: inside a hosted forge a check's path truncated them and the | |
| 4 | * sandbox failed to start). | |
| 5 | */ | |
| 6 | ||
| 7 | import { describe, expect, test } from "bun:test"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { longestSocketPath, runTmpDir, shortRoots, SOCKET_PATH_BUDGET } from "../src/crew/sandbox.ts"; | |
| 10 | ||
| 11 | const runId = "01m71kw4dfp4z8arr359fkygy5"; | |
| 12 | /** A scratch path of exactly `n` characters. */ | |
| 13 | function scratch(n: number): string { | |
| 14 | const base = "/data/runners/local-16/jobs/01m7xvg09v3bfbcfnstw5tj038/tree.home/shard-g2rUah/"; | |
| 15 | return (base + "x".repeat(n)).slice(0, n); | |
| 16 | } | |
| 17 | ||
| 18 | describe("a check's sockets live under a short fixed directory", () => { | |
| 19 | test("the budget is 100 bytes, under Linux's 107", () => { | |
| 20 | expect(SOCKET_PATH_BUDGET).toBe(100); | |
| 21 | }); | |
| 22 | ||
| 23 | test("a 120-character scratch path, as data and temp both, leaves the longest socket within 100 bytes", () => { | |
| 24 | const long = scratch(120); | |
| 25 | expect(long.length).toBe(120); | |
| 26 | for (const root of shortRoots()) { | |
| 27 | const dir = runTmpDir(long, "scout", runId, { linux: true, temp: long, short: () => root }); | |
| 28 | expect(dir).toBe(join(root, "fr-arr359fkygy5")); | |
| 29 | expect(Buffer.byteLength(longestSocketPath(dir))).toBeLessThanOrEqual(100); | |
| 30 | } | |
| 31 | }); | |
| 32 | ||
| 33 | test("the default host's answer fits too, whatever the machine's temp is", () => { | |
| 34 | const dir = runTmpDir(scratch(120), "scout", runId, { linux: true, temp: scratch(120) }); | |
| 35 | expect(Buffer.byteLength(longestSocketPath(dir))).toBeLessThanOrEqual(100); | |
| 36 | }); | |
| 37 | ||
| 38 | test("the OS temp is used only while it is short enough", () => { | |
| 39 | const long = scratch(120); | |
| 40 | expect(runTmpDir(long, "scout", runId, { linux: true, temp: "/tmp", short: () => "/run/forge" })).toBe("/tmp/fr-arr359fkygy5"); | |
| 41 | const edge = scratch(50); | |
| 42 | expect(runTmpDir(long, "scout", runId, { linux: true, temp: edge, short: () => "/run/forge" })).toBe("/run/forge/fr-arr359fkygy5"); | |
| 43 | }); | |
| 44 | ||
| 45 | test("the longest socket path names Claude Code's bridge socket under the shell's own temp", () => { | |
| 46 | expect(longestSocketPath("/run/forge/fr-arr359fkygy5")).toBe( | |
| 47 | `/run/forge/fr-arr359fkygy5/claude-${process.getuid?.() ?? 0}/claude-socks-0123456789abcdef.sock`, | |
| 48 | ); | |
| 49 | }); | |
| 50 | }); |
new above
offline
Only fr-/fc-/claude-<uid> are denied in a shared /tmp: other sessions files stay readable. Deny all siblings of this run directory.