| 1 | import { execFileSync } from "node:child_process"; |
| 2 | import { readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | import path from "node:path"; |
| 4 | import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 5 | import { startSandbox, type SandboxHandle } from "../src/sandbox.js"; |
| 6 | import { startSandboxPool } from "../src/sandbox-pool.js"; |
| 7 | import { tmp } from "./helpers.js"; |
| 8 | |
| 9 | const SECRET = "sandbox-secret-xyz"; |
| 10 | const TOKEN = "job-token-abc-123"; |
| 11 | let sb: SandboxHandle; |
| 12 | let workRoot: string; |
| 13 | let origin: string; |
| 14 | let base: string; |
| 15 | |
| 16 | async function call(p: string, body?: unknown, auth = SECRET) { |
| 17 | const res = await fetch(`${base}${p}`, { |
| 18 | method: body === undefined ? "GET" : "POST", |
| 19 | headers: { authorization: `Bearer ${auth}`, "content-type": "application/json" }, |
| 20 | body: body === undefined ? undefined : JSON.stringify(body), |
| 21 | }); |
| 22 | return { status: res.status, json: (await res.json()) as Record<string, any> }; |
| 23 | } |
| 24 | |
| 25 | beforeAll(async () => { |
| 26 | workRoot = tmp("sandbox-"); |
| 27 | origin = tmp("sandbox-origin-"); |
| 28 | const env = { ...process.env, GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "t@t", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "t@t" }; |
| 29 | execFileSync("git", ["init", "-q", "-b", "main"], { cwd: origin, env }); |
| 30 | writeFileSync(path.join(origin, "hello.txt"), "hello sandbox\n"); |
| 31 | execFileSync("git", ["add", "."], { cwd: origin, env }); |
| 32 | execFileSync("git", ["commit", "-q", "-m", "init"], { cwd: origin, env }); |
| 33 | execFileSync("git", ["checkout", "-q", "-b", "feature"], { cwd: origin, env }); |
| 34 | writeFileSync(path.join(origin, "feature.txt"), "on feature\n"); |
| 35 | execFileSync("git", ["add", "."], { cwd: origin, env }); |
| 36 | execFileSync("git", ["commit", "-q", "-m", "feat"], { cwd: origin, env }); |
| 37 | execFileSync("git", ["checkout", "-q", "main"], { cwd: origin, env }); |
| 38 | sb = await startSandbox({ port: 0, host: "127.0.0.1", secret: SECRET, workRoot, cloneUrl: `file://${origin}`, ref: "feature", token: TOKEN, maxOutputBytes: 1000 }); |
| 39 | base = `http://127.0.0.1:${sb.port}`; |
| 40 | await sb.ready; |
| 41 | }); |
| 42 | |
| 43 | afterAll(async () => { |
| 44 | await sb?.close(); |
| 45 | rmSync(workRoot, { recursive: true, force: true }); |
| 46 | rmSync(origin, { recursive: true, force: true }); |
| 47 | }); |
| 48 | |
| 49 | describe("sandbox server", () => { |
| 50 | it("reports health without auth and rejects unauthenticated calls", async () => { |
| 51 | const h = await fetch(`${base}/health`); |
| 52 | expect(await h.json()).toEqual({ ok: true, ready: true }); |
| 53 | expect((await call("/exec", { cmd: "true" }, "wrong")).status).toBe(401); |
| 54 | const noAuth = await fetch(`${base}/exec`, { method: "POST", body: "{}" }); |
| 55 | expect(noAuth.status).toBe(401); |
| 56 | }); |
| 57 | |
| 58 | it("cloned the repo at the requested ref", async () => { |
| 59 | const r = await call("/exec", { cmd: "git rev-parse --abbrev-ref HEAD && cat hello.txt feature.txt" }); |
| 60 | expect(r.status).toBe(200); |
| 61 | expect(r.json).toMatchObject({ exitCode: 0, stdout: "feature\nhello sandbox\non feature\n", stderr: "" }); |
| 62 | }); |
| 63 | |
| 64 | it("executes commands with cwd/env, masks secrets and truncates output", async () => { |
| 65 | await call("/exec", { cmd: "mkdir -p sub" }); |
| 66 | const r = await call("/exec", { cmd: 'pwd; echo "v=$MYVAR"; echo "s=$DIFFINERY_SANDBOX_SECRET"; echo err >&2; echo "tok=' + TOKEN + '"; exit 3', cwd: "sub", env: { MYVAR: "x1" } }); |
| 67 | expect(r.json.exitCode).toBe(3); |
| 68 | expect(r.json.stdout).toBe(`${path.join(sb.repoDir, "sub")}\nv=x1\ns=\ntok=***\n`); |
| 69 | expect(r.json.stderr).toBe("err\n"); |
| 70 | const big = await call("/exec", { cmd: "head -c 5000 /dev/zero | tr '\\0' a" }); |
| 71 | expect(big.json.stdout.length).toBe(1000); |
| 72 | expect(big.json.truncated).toBe(true); |
| 73 | }); |
| 74 | |
| 75 | it("times out long commands", async () => { |
| 76 | const r = await call("/exec", { cmd: "sleep 20", timeoutMs: 300 }); |
| 77 | expect(r.json.timedOut).toBe(true); |
| 78 | expect(r.json.exitCode).toBe(124); |
| 79 | }); |
| 80 | |
| 81 | it("reads and writes files (base64)", async () => { |
| 82 | const content = Buffer.from("bin\0ary ✓").toString("base64"); |
| 83 | const w = await call("/files/write", { path: "new/dir/file.bin", content }); |
| 84 | expect(w.json).toMatchObject({ ok: true, size: Buffer.from("bin\0ary ✓").length }); |
| 85 | expect(readFileSync(path.join(sb.repoDir, "new/dir/file.bin"), "utf8")).toBe("bin\0ary ✓"); |
| 86 | const r = await call("/files/read", { path: "new/dir/file.bin" }); |
| 87 | expect(Buffer.from(r.json.content, "base64").toString()).toBe("bin\0ary ✓"); |
| 88 | expect((await call("/files/read", { path: "missing.txt" })).status).toBe(404); |
| 89 | expect((await call("/files/read", {})).status).toBe(400); |
| 90 | expect((await call("/nope", {})).status).toBe(404); |
| 91 | }); |
| 92 | |
| 93 | it("runs background processes: env, secret env masked, output from offsets, exit codes", async () => { |
| 94 | // Built at run time so push protection never sees a key-shaped literal in this file. |
| 95 | const KEY = ["sk", "ant", "api03", "secret-key-value"].join("-"); |
| 96 | const start = await call("/proc/start", { id: "run1", cmd: 'echo "a=$A"; echo "key=$MODEL_KEY"; echo oops >&2; sleep 0.3; echo done; exit 4', env: { A: "1" }, secretEnv: { MODEL_KEY: KEY } }); |
| 97 | expect(start.status).toBe(200); |
| 98 | expect((await call("/proc/start", { id: "run1", cmd: "true" })).status).toBe(409); |
| 99 | let out = ""; |
| 100 | let err = ""; |
| 101 | let so = 0; |
| 102 | let se = 0; |
| 103 | let r; |
| 104 | for (;;) { |
| 105 | r = await call("/proc/read", { id: "run1", stdoutOffset: so, stderrOffset: se }); |
| 106 | out += r.json.stdout; |
| 107 | err += r.json.stderr; |
| 108 | so = r.json.stdoutOffset; |
| 109 | se = r.json.stderrOffset; |
| 110 | if (!r.json.running) break; |
| 111 | await new Promise((x) => setTimeout(x, 50)); |
| 112 | } |
| 113 | expect(out).toBe("a=1\nkey=***\ndone\n"); |
| 114 | expect(err).toBe("oops\n"); |
| 115 | expect(r.json.exitCode).toBe(4); |
| 116 | // The secret is masked everywhere from now on, and other commands don't get it in their environment. |
| 117 | const ex = await call("/exec", { cmd: `echo ${KEY}; echo "env=$MODEL_KEY"` }); |
| 118 | expect(ex.json.stdout).toBe("***\nenv=\n"); |
| 119 | // A finished process can start again under the same id. |
| 120 | expect((await call("/proc/start", { id: "run1", cmd: "echo again" })).status).toBe(200); |
| 121 | expect((await call("/proc/read", { id: "missing" })).status).toBe(404); |
| 122 | expect((await call("/proc/start", { id: "bad id!", cmd: "true" })).status).toBe(409); |
| 123 | }); |
| 124 | |
| 125 | it("kills a background process tree and times one out", async () => { |
| 126 | await call("/proc/start", { id: "long", cmd: "sleep 30 & sleep 30; echo never" }); |
| 127 | expect((await call("/proc/kill", { id: "long" })).json.killed).toBe(true); |
| 128 | let r; |
| 129 | do r = await call("/proc/read", { id: "long" }); |
| 130 | while (r.json.running); |
| 131 | expect(r.json.stdout).toBe(""); |
| 132 | await call("/proc/start", { id: "slow", cmd: "sleep 30", timeoutMs: 200 }); |
| 133 | do r = await call("/proc/read", { id: "slow" }); |
| 134 | while (r.json.running); |
| 135 | expect(r.json).toMatchObject({ exitCode: 124, timedOut: true }); |
| 136 | }); |
| 137 | }); |
| 138 | |
| 139 | describe("sandbox pool", () => { |
| 140 | it("boots sandboxes on demand and routes calls to each by id", async () => { |
| 141 | const pool = await startSandboxPool({ port: 0, secret: "pool-secret", workRoot: tmp("pool-") }); |
| 142 | const p = `http://127.0.0.1:${pool.port}`; |
| 143 | const boot = (auth: string) => fetch(`${p}/boot`, { method: "POST", headers: { authorization: `Bearer ${auth}` }, body: JSON.stringify({ id: "sb1", secret: "s1", env: { DIFFINERY_CLONE_URL: `file://${origin}`, DIFFINERY_REF: "main", FOO: "bar" } }) }); |
| 144 | expect((await boot("wrong")).status).toBe(401); |
| 145 | expect((await boot("pool-secret")).status).toBe(200); |
| 146 | const exec = (auth: string) => fetch(`${p}/s/sb1/exec`, { method: "POST", headers: { authorization: `Bearer ${auth}`, "content-type": "application/json" }, body: JSON.stringify({ cmd: 'cat hello.txt; echo "$FOO"' }) }); |
| 147 | expect((await exec("wrong")).status).toBe(401); |
| 148 | expect(await (await exec("s1")).json()).toMatchObject({ exitCode: 0, stdout: "hello sandbox\nbar\n" }); |
| 149 | expect((await fetch(`${p}/s/nope/exec`, { method: "POST" })).status).toBe(404); |
| 150 | await pool.close(); |
| 151 | }); |
| 152 | }); |
| 153 | |