|
| 1 | +import test from "node:test" |
| 2 | +import assert from "node:assert/strict" |
| 3 | +import { chmod, lstat, mkdir, mkdtemp, readFile, rm, stat, symlink, writeFile } from "node:fs/promises" |
| 4 | +import { spawnSync } from "node:child_process" |
| 5 | +import path from "node:path" |
| 6 | +import os from "node:os" |
| 7 | + |
| 8 | +const spec = await readFile(new URL("../spec.yaml", import.meta.url), "utf8") |
| 9 | +const startup = spec.match(/ - phase: startup\n user: "0"\n description: Prepare private OpenCode scratch directory for the agent user\n command:\n - sh\n - -c\n - \|\n((?: .*\n)+)/) |
| 10 | +assert.ok(startup, "scratch directory preparation must be wired as a root startup command") |
| 11 | +const body = startup[1].replace(/^ /gm, "") |
| 12 | + |
| 13 | +const idShim = `#!/bin/sh |
| 14 | +[ -n "$FAKE_UID" ] || exit 1 |
| 15 | +[ "$2" = agent ] || exit 1 |
| 16 | +case "$1" in -u) echo "$FAKE_UID" ;; -g) echo "$FAKE_GID" ;; *) exit 1 ;; esac |
| 17 | +` |
| 18 | + |
| 19 | +// Records the requested ownership, then installs with the caller's real ids |
| 20 | +// (the test cannot chown to arbitrary ids without root). |
| 21 | +const installShim = `#!/bin/sh |
| 22 | +echo "$*" >> "$INSTALL_LOG" |
| 23 | +n=$# |
| 24 | +while [ "$n" -gt 0 ]; do |
| 25 | + a=$1; shift; n=$((n - 1)) |
| 26 | + case "$a" in |
| 27 | + -o) shift; n=$((n - 1)); set -- "$@" -o "$REAL_UID" ;; |
| 28 | + -g) shift; n=$((n - 1)); set -- "$@" -g "$REAL_GID" ;; |
| 29 | + *) set -- "$@" "$a" ;; |
| 30 | + esac |
| 31 | +done |
| 32 | +exec /usr/bin/install "$@" |
| 33 | +` |
| 34 | + |
| 35 | +async function fixture(t, { agent = { uid: process.getuid(), gid: process.getgid() } } = {}) { |
| 36 | + const root = await mkdtemp(path.join(os.tmpdir(), "opencode-tmp-test-")) |
| 37 | + t.after(() => rm(root, { recursive: true, force: true })) |
| 38 | + const directory = path.join(root, "opencode") |
| 39 | + const bin = path.join(root, "bin") |
| 40 | + const installLog = path.join(root, "install.log") |
| 41 | + await mkdir(bin) |
| 42 | + await writeFile(path.join(bin, "id"), idShim, { mode: 0o755 }) |
| 43 | + await writeFile(path.join(bin, "install"), installShim, { mode: 0o755 }) |
| 44 | + // Exercise real install/chmod without requiring root or touching /tmp/opencode. |
| 45 | + const command = body.replaceAll("/tmp/opencode", directory) |
| 46 | + const run = () => spawnSync("/bin/sh", ["-c", command], { |
| 47 | + env: { |
| 48 | + PATH: `${bin}:/usr/bin:/bin`, |
| 49 | + INSTALL_LOG: installLog, |
| 50 | + REAL_UID: String(process.getuid()), |
| 51 | + REAL_GID: String(process.getgid()), |
| 52 | + ...(agent ? { FAKE_UID: String(agent.uid), FAKE_GID: String(agent.gid) } : {}), |
| 53 | + }, |
| 54 | + encoding: "utf8", |
| 55 | + }) |
| 56 | + const installCalls = async () => readFile(installLog, "utf8").catch(() => "") |
| 57 | + return { root, directory, run, installCalls } |
| 58 | +} |
| 59 | + |
| 60 | +test("root startup resolves the agent's UID/GID by name without recursive changes", () => { |
| 61 | + assert.match(body, /id -u agent/) |
| 62 | + assert.match(body, /id -g agent/) |
| 63 | + assert.match(body, /install -d -o "\$agent_uid" -g "\$agent_gid" -m 0700 \/tmp\/opencode/) |
| 64 | + assert.doesNotMatch(body, /\b1000\b/) |
| 65 | + assert.doesNotMatch(body, /(?:chown|chmod)\s+-R/) |
| 66 | + assert.ok(spec.indexOf(startup[0]) < spec.indexOf("description: Render current provider inputs")) |
| 67 | +}) |
| 68 | + |
| 69 | +test("startup uses a non-1000 agent uid/gid reported by the sandbox", async (t) => { |
| 70 | + const { directory, run, installCalls } = await fixture(t, { agent: { uid: 2345, gid: 2346 } }) |
| 71 | + const result = run() |
| 72 | + assert.equal(result.status, 0, result.stderr) |
| 73 | + assert.match(await installCalls(), /-d -o 2345 -g 2346 -m 0700 \S+\/opencode/) |
| 74 | + assert.ok((await stat(directory)).isDirectory()) |
| 75 | +}) |
| 76 | + |
| 77 | +test("startup fails closed when the agent user does not exist", async (t) => { |
| 78 | + const { directory, run, installCalls } = await fixture(t, { agent: null }) |
| 79 | + const result = run() |
| 80 | + assert.notEqual(result.status, 0) |
| 81 | + assert.match(result.stderr, /user 'agent' not found/) |
| 82 | + assert.equal(await installCalls(), "") |
| 83 | + await assert.rejects(stat(directory)) |
| 84 | +}) |
| 85 | + |
| 86 | +test("startup creates a private writable directory and is idempotent", async (t) => { |
| 87 | + const { directory, run } = await fixture(t) |
| 88 | + for (let attempt = 0; attempt < 2; attempt++) { |
| 89 | + assert.equal(run().status, 0) |
| 90 | + const info = await stat(directory) |
| 91 | + assert.equal(info.uid, process.getuid()) |
| 92 | + assert.equal(info.gid, process.getgid()) |
| 93 | + assert.equal(info.mode & 0o777, 0o700) |
| 94 | + await writeFile(path.join(directory, "agent-output"), "synthetic output") |
| 95 | + } |
| 96 | +}) |
| 97 | + |
| 98 | +test("startup repairs directory permissions without changing existing contents", async (t) => { |
| 99 | + const { directory, run } = await fixture(t) |
| 100 | + await mkdir(directory) |
| 101 | + const file = path.join(directory, "existing-output") |
| 102 | + await writeFile(file, "preserved") |
| 103 | + await chmod(file, 0o640) |
| 104 | + await chmod(directory, 0o755) |
| 105 | + assert.equal(run().status, 0) |
| 106 | + assert.equal((await stat(directory)).mode & 0o777, 0o700) |
| 107 | + assert.equal(await readFile(file, "utf8"), "preserved") |
| 108 | + assert.equal((await stat(file)).mode & 0o777, 0o640) |
| 109 | +}) |
| 110 | + |
| 111 | +test("startup refuses symlinks without modifying their target", async (t) => { |
| 112 | + const { root, directory, run } = await fixture(t) |
| 113 | + const target = path.join(root, "unrelated") |
| 114 | + await mkdir(target) |
| 115 | + await chmod(target, 0o750) |
| 116 | + await symlink(target, directory) |
| 117 | + const result = run() |
| 118 | + assert.notEqual(result.status, 0) |
| 119 | + assert.match(result.stderr, /refusing symlink/) |
| 120 | + assert.ok((await lstat(directory)).isSymbolicLink()) |
| 121 | + assert.equal((await stat(target)).mode & 0o777, 0o750) |
| 122 | +}) |
| 123 | + |
| 124 | +test("startup refuses a non-directory without replacing it", async (t) => { |
| 125 | + const { directory, run } = await fixture(t) |
| 126 | + await writeFile(directory, "preserved") |
| 127 | + assert.notEqual(run().status, 0) |
| 128 | + assert.equal(await readFile(directory, "utf8"), "preserved") |
| 129 | +}) |
0 commit comments