Public: anyone can read this; the crew can change it.
A test fails when any process is spawned with a shell string or with user text as the program #14727
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/14700-a-test-fails-when-any-process-is-spawned → main 2 commits fork
2 files
changed +593
−0
scripts/spawn-argv-allow.json added +158
| @@ -0,0 +1,158 @@ | ||
| 1 | [ | |
| 2 | { | |
| 3 | "file": "src/apps/contain.ts", | |
| 4 | "kind": "shell", | |
| 5 | "code": "command", | |
| 6 | "why": "the cell's own bwrap argv ends in sh -c with the app's command: forge.json's run or tty line, written by the repository's writers and run confined in the app's cell, never a request's text" | |
| 7 | }, | |
| 8 | { | |
| 9 | "file": "src/apps/process.ts", | |
| 10 | "kind": "spawn", | |
| 11 | "code": "cell?.argv(command) ?? [\"/bin/sh\", \"-c\", command]", | |
| 12 | "why": "a build or check step (runStep): the app's forge.json command in the step's cell (bwrap first) or /bin/sh, both behind backgroundArgv's fixed script" | |
| 13 | }, | |
| 14 | { | |
| 15 | "file": "src/apps/process.ts", | |
| 16 | "kind": "spawn", | |
| 17 | "code": "cell?.argv(command) ?? [\"/bin/sh\", \"-c\", command]", | |
| 18 | "why": "the app's own process (startProcess): its forge.json run command in the app's cell (bwrap first) or /bin/sh; the program is always fixed inside either" | |
| 19 | }, | |
| 20 | { | |
| 21 | "file": "src/apps/process.ts", | |
| 22 | "kind": "shell", | |
| 23 | "code": "command", | |
| 24 | "why": "a build or check step's forge.json command, a shell line by contract (README: forge.json says how to run it), from the repository's committed file, never from a request" | |
| 25 | }, | |
| 26 | { | |
| 27 | "file": "src/apps/process.ts", | |
| 28 | "kind": "shell", | |
| 29 | "code": "command", | |
| 30 | "why": "the app's forge.json run command, a shell line by contract, from the committed file, run in the app's cell when it has one" | |
| 31 | }, | |
| 32 | { | |
| 33 | "file": "src/apps/tty.ts", | |
| 34 | "kind": "shell", | |
| 35 | "code": "command", | |
| 36 | "why": "the app's forge.json tty command, a shell line from the committed file, run in the app's cell when it has one" | |
| 37 | }, | |
| 38 | { | |
| 39 | "file": "src/apps/tty.ts", | |
| 40 | "kind": "spawn", | |
| 41 | "code": "ctty ? [SETSID, \"-c\", ...argv] : argv", | |
| 42 | "why": "argv is the cell's argv or /bin/sh -c for the forge.json tty line (listed above); SETSID is a constant" | |
| 43 | }, | |
| 44 | { | |
| 45 | "file": "src/checks/job_step.ts", | |
| 46 | "kind": "shell", | |
| 47 | "code": "input.commands.join(\" && \")", | |
| 48 | "why": "a check's forge.json commands joined into the cluster job's one shell line, run in the check's own pod; the commands come from the pushed repository, never a request" | |
| 49 | }, | |
| 50 | { | |
| 51 | "file": "src/checks/runner_pool.ts", | |
| 52 | "kind": "spawn", | |
| 53 | "code": "argv", | |
| 54 | "why": "the pool's spawn seam: its one caller hands [\"bun\", options.entry]; tests hand their own" | |
| 55 | }, | |
| 56 | { | |
| 57 | "file": "src/cli/commands/open.ts", | |
| 58 | "kind": "spawn", | |
| 59 | "code": "[opener, …]", | |
| 60 | "why": "opener is \"open\" or \"xdg-open\" by platform, decided on the line above; the URL is one argument" | |
| 61 | }, | |
| 62 | { | |
| 63 | "file": "src/cli/notify.ts", | |
| 64 | "kind": "spawn", | |
| 65 | "code": "args", | |
| 66 | "why": "run() is handed only fixed argv: [\"osascript\", ...] or [\"notify-send\", \"--\", title, body]; the text is arguments" | |
| 67 | }, | |
| 68 | { | |
| 69 | "file": "src/crew/claude.ts", | |
| 70 | "kind": "spawn", | |
| 71 | "code": "present ? backgroundArgv(oomFirst(contained)) : argv", | |
| 72 | "why": "the agent runner's binary is the operator's FORGE_AGENT_RUNNER / FORGE_CLAUDE (default \"claude\"); contained is that argv in the run's cell" | |
| 73 | }, | |
| 74 | { | |
| 75 | "file": "src/crew/claude.ts", | |
| 76 | "kind": "spawn", | |
| 77 | "code": "[binary, …]", | |
| 78 | "why": "the operator's configured agent runner binary (FORGE_AGENT_RUNNER, default \"claude\"), asked its version" | |
| 79 | }, | |
| 80 | { | |
| 81 | "file": "src/crew/claude_usage.ts", | |
| 82 | "kind": "spawn", | |
| 83 | "code": "[input.binary, …]", | |
| 84 | "why": "the operator's configured agent runner binary, asked its usage" | |
| 85 | }, | |
| 86 | { | |
| 87 | "file": "src/crew/openai_runner.ts", | |
| 88 | "kind": "shell", | |
| 89 | "code": "command", | |
| 90 | "why": "an agent's own bash tool command, run only inside bwrap with the run's sandbox binds (bashArgv returns undefined unless the policy is enforced): the agent is meant to run commands" | |
| 91 | }, | |
| 92 | { | |
| 93 | "file": "src/crew/openai_runner.ts", | |
| 94 | "kind": "spawn", | |
| 95 | "code": "argv", | |
| 96 | "why": "runShell is handed bashArgv's confined argv, whose program is the literal \"bwrap\"" | |
| 97 | }, | |
| 98 | { | |
| 99 | "file": "src/crew/plan.ts", | |
| 100 | "kind": "spawn", | |
| 101 | "code": "[options.binary, …]", | |
| 102 | "why": "the operator's configured agent runner binary; every other element is a literal, the prompt one argument" | |
| 103 | }, | |
| 104 | { | |
| 105 | "file": "src/crew/workdir.ts", | |
| 106 | "kind": "spawn", | |
| 107 | "code": "argv", | |
| 108 | "why": "runBounded's one caller hands [\"bun\", \"install\", \"--frozen-lockfile\", \"--ignore-scripts\"]" | |
| 109 | }, | |
| 110 | { | |
| 111 | "file": "src/git/smart-http.ts", | |
| 112 | "kind": "spawn", | |
| 113 | "code": "serviceCommand( service, [\"--stateless-rpc\", ...extraArgs, repoDir], options.pushOptions, held ? undefined : options.bundleUri, )", | |
| 114 | "why": "serviceCommand returns [\"git\", ...config, subcommand, ...args]: the program is the literal \"git\", the service one of two fixed names" | |
| 115 | }, | |
| 116 | { | |
| 117 | "file": "src/git/smart-ssh.ts", | |
| 118 | "kind": "spawn", | |
| 119 | "code": "serviceCommand(service, [dir], options.pushOptions)", | |
| 120 | "why": "serviceCommand returns [\"git\", ...]: the program is the literal \"git\"" | |
| 121 | }, | |
| 122 | { | |
| 123 | "file": "src/looks/capture.ts", | |
| 124 | "kind": "spawn", | |
| 125 | "code": "argv", | |
| 126 | "why": "browserCommand's argv: the operator's FORGE_CHROMIUM path or a chromium found by Bun.which among fixed names" | |
| 127 | }, | |
| 128 | { | |
| 129 | "file": "src/release/devtools.ts", | |
| 130 | "kind": "spawn", | |
| 131 | "code": "argv", | |
| 132 | "why": "browserCommand's argv for the release's own chromium, found as capture.ts finds it" | |
| 133 | }, | |
| 134 | { | |
| 135 | "file": "src/release/small_forge.ts", | |
| 136 | "kind": "spawn", | |
| 137 | "code": "argv", | |
| 138 | "why": "run() is handed only fixed git argv by the release prover, each with the literal \"git\" first" | |
| 139 | }, | |
| 140 | { | |
| 141 | "file": "src/spawn/cell.ts", | |
| 142 | "kind": "spawn", | |
| 143 | "code": "cell.argv(command)", | |
| 144 | "why": "a cell's argv: the literal \"bwrap\" (or \"setsid\") first, the hosted forge's own start command last" | |
| 145 | }, | |
| 146 | { | |
| 147 | "file": "src/tui/screens/external.ts", | |
| 148 | "kind": "spawn", | |
| 149 | "code": "[bin, …]", | |
| 150 | "why": "bin is the person's own $EDITOR resolved by Bun.which, refused when it holds a space, in their own terminal; the file is one argument" | |
| 151 | }, | |
| 152 | { | |
| 153 | "file": "src/tui/term/terminal.ts", | |
| 154 | "kind": "spawn", | |
| 155 | "code": "[opener, …]", | |
| 156 | "why": "opener is \"open\" or \"xdg-open\" by platform, decided on the line above; the URL is one argument" | |
| 157 | } | |
| 158 | ] |
test/spawn-argv.test.ts added +435
| @@ -0,0 +1,435 @@ | ||
| 1 | /** | |
| 2 | * No shelling out with user text, proven from the tree (#14700). Every | |
| 3 | * process `src/` starts is named here by file:line: each `Bun.spawn` and | |
| 4 | * `Bun.spawnSync`, each `sh -c` argv, each `Bun.$` shell template and each | |
| 5 | * `child_process` import, and each alias of the spawn functions (`Bun.spawn` | |
| 6 | * held without a call, `Bun["spawn"]`, `{ spawn } = Bun`, `import { spawn } | |
| 7 | * from "bun"`). A spawn passes an argv array whose program is a literal or a | |
| 8 | * fixed constant; an `sh -c` script is a literal, or a template of fixed | |
| 9 | * constants only. A fixed constant is `process.execPath` or a capitalised | |
| 10 | * name the same module declares `const` as a literal (a string, a number, an | |
| 11 | * object of those, a template of other such constants, or `Bun.which` of a | |
| 12 | * literal name). Anything else (a program held in a variable, a script built | |
| 13 | * from a value) fails with its file:line unless | |
| 14 | * `scripts/spawn-argv-allow.json` lists it with one reason and the `count` of | |
| 15 | * sites that reason covers (1 when absent): a second such site in the same | |
| 16 | * file fails until the list says so, and an entry whose count no longer | |
| 17 | * matches fails too, so the list only says what is true. | |
| 18 | * | |
| 19 | * The limit: the scan reads text, not types. It names every reference to | |
| 20 | * `Bun.spawn` and `Bun.spawnSync` it can see, but a process started through | |
| 21 | * `globalThis["Bun"]`, `Reflect.get`, or a name built at run time is not | |
| 22 | * seen; review holds those. | |
| 23 | * | |
| 24 | * The argv wrappers that put a fixed script in front of a caller's argv | |
| 25 | * (`backgroundArgv`, `oomFirst`, `oomArgv`, `enterArgv`) are looked | |
| 26 | * through, and proven here by calling them: the caller's argv lands after | |
| 27 | * the script, as `"$@"`, never inside it. | |
| 28 | * | |
| 29 | * @tree-scan: reads every module under src/. | |
| 30 | */ | |
| 31 | import { describe, expect, test } from "bun:test"; | |
| 32 | import { readFileSync } from "node:fs"; | |
| 33 | import { join } from "node:path"; | |
| 34 | import { backgroundArgv } from "../src/apps/background.ts"; | |
| 35 | import { enterArgv } from "../src/apps/cgroup.ts"; | |
| 36 | import { oomArgv, oomFirst } from "../src/apps/oom.ts"; | |
| 37 | import allowed from "../scripts/spawn-argv-allow.json"; | |
| 38 | ||
| 39 | const root = join(import.meta.dir, ".."); | |
| 40 | ||
| 41 | /** | |
| 42 | * What a site starts: a spawn's argv, an `sh -c` script, a `Bun.$` template, a `child_process` | |
| 43 | * import, or a spawn function held under another name (`alias`), whose calls the scan cannot | |
| 44 | * follow. | |
| 45 | */ | |
| 46 | type Kind = "spawn" | "shell" | "bun-shell" | "child_process" | "alias"; | |
| 47 | const KINDS: readonly Kind[] = ["spawn", "shell", "bun-shell", "child_process", "alias"]; | |
| 48 | interface Site { | |
| 49 | file: string; | |
| 50 | line: number; | |
| 51 | kind: Kind; | |
| 52 | /** The argv (spawn), the script (shell) or the matched text, whitespace collapsed. */ | |
| 53 | code: string; | |
| 54 | /** Whether the program or script is fixed: a literal or constants only. */ | |
| 55 | fixed: boolean; | |
| 56 | } | |
| 57 | interface Allowed { file: string; kind: Kind; code: string; why: string; count?: number } | |
| 58 | ||
| 59 | /** | |
| 60 | * Wrappers whose result runs the argv they are handed, after a fixed script; the argument index of | |
| 61 | * that argv. | |
| 62 | */ | |
| 63 | const WRAPPERS: Record<string, "first" | "last"> = { backgroundArgv: "first", oomFirst: "first", oomArgv: "last", enterArgv: "last" }; | |
| 64 | const SPAWN = /\bBun\.spawn(?:Sync)?\s*\(/g; | |
| 65 | const SHELL_C = /["'`](?:\/usr)?(?:\/bin\/)?(?:ba|da|z)?sh["'`]\s*,\s*["'`]-c["'`]\s*,\s*/g; | |
| 66 | const BUN_SHELL = /\bBun\.\$\s*`|import\s*\{[^}]*(?<![\w$])\$(?![\w$])[^}]*\}\s*from\s*["']bun["']/g; | |
| 67 | const CHILD_PROCESS = /(?:from\s*|require\s*\(\s*|import\s*\(\s*)["'](?:node:)?child_process["']/g; | |
| 68 | /** | |
| 69 | * The spawn functions held without being called (`const sp = Bun.spawn`, `Bun["spawn"]`), or taken | |
| 70 | * out of `Bun` by name. `typeof Bun.spawn` in a type is not a site. | |
| 71 | */ | |
| 72 | const ALIAS = new RegExp( | |
| 73 | [ | |
| 74 | /(?<!typeof\s*)\bBun\.spawn(?:Sync)?\b(?!\s*[(<])/.source, | |
| 75 | /\bBun\s*\[/.source, | |
| 76 | /\{[^{}]*\bspawn(?:Sync)?\b[^{}]*\}\s*=\s*(?:globalThis\.)?Bun\b/.source, | |
| 77 | /import\s*(?:\{[^}]*\bspawn(?:Sync)?\b[^}]*\}|\*\s*as\s+[\w$]+|[\w$]+)\s*from\s*["']bun["']/.source, | |
| 78 | /[=(,:]\s*(?:globalThis\.)?Bun\s*(?=[;,)\n])/.source, | |
| 79 | ].join("|"), | |
| 80 | "g", | |
| 81 | ); | |
| 82 | /** What a module's constants are: `plain` reads whole, `object` also by one `.field`. */ | |
| 83 | interface Constants { plain: Set<string>; object: Set<string> } | |
| 84 | ||
| 85 | const squash = (s: string): string => s.replace(/\s+/g, " ").trim(); | |
| 86 | const lineAt = (source: string, at: number): number => source.slice(0, at).split("\n").length; | |
| 87 | ||
| 88 | /** Whether `expr` is the running bun, or a constant this module declares as fixed. */ | |
| 89 | function isConstant(text: string, c: Constants): boolean { | |
| 90 | let expr = text; | |
| 91 | if (expr === "process.execPath") return true; | |
| 92 | // A fixed constant read through methods handed only literals (`PORT.toString(16)`) stays fixed. | |
| 93 | const call = /\.[A-Za-z_$][\w$]*\(\s*((?:-?\d[\d_.]*|"[^"\\]*"|'[^'\\]*')(?:\s*,\s*(?:-?\d[\d_.]*|"[^"\\]*"|'[^'\\]*'))*)?\s*\)$/; | |
| 94 | for (let m = call.exec(expr); m && m.index > 0; m = call.exec(expr)) expr = expr.slice(0, m.index); | |
| 95 | const m = /^([A-Z][A-Z0-9_]*)(?:\.([A-Za-z_$][\w$]*))?$/.exec(expr); | |
| 96 | if (!m) return false; | |
| 97 | return m[2] === undefined ? c.plain.has(m[1]!) || c.object.has(m[1]!) : c.object.has(m[1]!); | |
| 98 | } | |
| 99 | ||
| 100 | /** | |
| 101 | * The capitalised `const`s a module declares as fixed: a literal, a template or `+` of fixed | |
| 102 | * constants, an object of literals, or `Bun.which` of a literal name. Read to a fixed point, so a | |
| 103 | * constant built from constants declared below it counts too. | |
| 104 | */ | |
| 105 | function constantsIn(source: string): Constants { | |
| 106 | const decls: [string, string][] = []; | |
| 107 | for (const m of source.matchAll(/(?:^|[\n;])\s*(?:export\s+)?const\s+([A-Z][A-Z0-9_]*)\s*(?::[^=]+)?=\s*/g)) { | |
| 108 | const { text } = readExpr(source, m.index + m[0].length, ";"); | |
| 109 | decls.push([m[1]!, squash(text).replace(/\s+as\s+const$/, "")]); | |
| 110 | } | |
| 111 | const c: Constants = { plain: new Set(), object: new Set() }; | |
| 112 | for (let grew = true; grew; ) { | |
| 113 | grew = false; | |
| 114 | for (const [name, expr] of decls) { | |
| 115 | if (c.plain.has(name) || c.object.has(name)) continue; | |
| 116 | if (/^\{[\s\S]*\}$/.test(expr)) { | |
| 117 | const fields = items(expr, 0, "}"); | |
| 118 | const literal = (v: string): boolean => /^-?\d[\d_.]*$/.test(v) || fixedText(v, c); | |
| 119 | if (fields.length > 0 && fields.every((f) => /^[\w$]+\s*:/.test(f) && literal(f.replace(/^[\w$]+\s*:\s*/, "")))) { | |
| 120 | c.object.add(name); | |
| 121 | grew = true; | |
| 122 | } | |
| 123 | } else if (/^-?\d[\d_.]*$/.test(expr) || /^Bun\.which\(\s*("[\w.-]+"|'[\w.-]+')\s*\)$/.test(expr) || fixedText(expr, c)) { | |
| 124 | c.plain.add(name); | |
| 125 | grew = true; | |
| 126 | } | |
| 127 | } | |
| 128 | } | |
| 129 | return c; | |
| 130 | } | |
| 131 | ||
| 132 | /** Whether `at` sits in a comment: a `//` earlier on its line, or a line of a block comment. */ | |
| 133 | function inComment(source: string, at: number): boolean { | |
| 134 | const start = source.lastIndexOf("\n", at - 1) + 1; | |
| 135 | const before = source.slice(start, at); | |
| 136 | return /^\s*(?:\*|\/\*)/.test(before) || /(?:^|[^:"'`\\])\/\//.test(before); | |
| 137 | } | |
| 138 | ||
| 139 | /** The index just past the string literal opening at `i`. */ | |
| 140 | function pastString(source: string, i: number): number { | |
| 141 | const quote = source[i]; | |
| 142 | for (let j = i + 1; j < source.length; j++) { | |
| 143 | if (source[j] === "\\") j++; | |
| 144 | else if (quote === "`" && source[j] === "$" && source[j + 1] === "{") j = readExpr(source, j + 2, "}").end; | |
| 145 | else if (source[j] === quote) return j + 1; | |
| 146 | } | |
| 147 | return source.length; | |
| 148 | } | |
| 149 | ||
| 150 | /** | |
| 151 | * The expression from `i` up to the first `stops` character at depth zero: | |
| 152 | * brackets and strings are stepped over whole. | |
| 153 | */ | |
| 154 | function readExpr(source: string, i: number, stops: string): { text: string; end: number } { | |
| 155 | let depth = 0; | |
| 156 | for (let j = i; j < source.length; j++) { | |
| 157 | const c = source[j]!; | |
| 158 | if (c === '"' || c === "'" || c === "`") { | |
| 159 | j = pastString(source, j) - 1; | |
| 160 | } else if (depth === 0 && stops.includes(c)) { | |
| 161 | return { text: source.slice(i, j), end: j }; | |
| 162 | } else if ("([{".includes(c)) depth++; | |
| 163 | else if (")]}".includes(c)) depth--; | |
| 164 | } | |
| 165 | return { text: source.slice(i), end: source.length }; | |
| 166 | } | |
| 167 | ||
| 168 | /** The top-level items of a list whose opening bracket is at `open`. */ | |
| 169 | function items(text: string, open: number, close: string): string[] { | |
| 170 | const out: string[] = []; | |
| 171 | let i = open + 1; | |
| 172 | while (i < text.length) { | |
| 173 | const { text: item, end } = readExpr(text, i, `,${close}`); | |
| 174 | if (item.trim() !== "") out.push(item.trim()); | |
| 175 | if (text[end] !== ",") break; | |
| 176 | i = end + 1; | |
| 177 | } | |
| 178 | return out; | |
| 179 | } | |
| 180 | ||
| 181 | /** | |
| 182 | * A literal string with nothing interpolated, or a template / `+` sum of literals and constants | |
| 183 | * only. | |
| 184 | */ | |
| 185 | function fixedText(expr: string, c: Constants): boolean { | |
| 186 | const parts = items(`(${expr})`.replace(/\+/g, ","), 0, ")"); | |
| 187 | // A `+` inside a string was split too; fall back to the whole when the parts do not each read as | |
| 188 | // a string or constant. | |
| 189 | const pieces = parts.length > 1 && parts.every((p) => /^["'`]/.test(p) || isConstant(p, c)) ? parts : [expr.trim()]; | |
| 190 | return pieces.every((p) => { | |
| 191 | if (isConstant(p, c)) return true; | |
| 192 | if (/^"(?:[^"\\]|\\.)*"$|^'(?:[^'\\]|\\.)*'$/.test(p)) return true; | |
| 193 | if (!/^`[\s\S]*`$/.test(p)) return false; | |
| 194 | const holes = [...p.matchAll(/\$\{([^}]*)\}/g)].map((m) => m[1]!.trim()); | |
| 195 | return holes.every((h) => isConstant(h, c)); | |
| 196 | }); | |
| 197 | } | |
| 198 | ||
| 199 | /** Look through a wrapper that runs its argv after a fixed script: the argv it is handed. */ | |
| 200 | function unwrap(expr: string): string { | |
| 201 | let e = expr.trim(); | |
| 202 | for (;;) { | |
| 203 | const m = /^([A-Za-z_$][\w$]*)\s*\(/.exec(e); | |
| 204 | const which = m ? WRAPPERS[m[1]!] : undefined; | |
| 205 | if (!m || !which) return e; | |
| 206 | const open = m[0].length - 1; | |
| 207 | const { end } = readExpr(e, open + 1, ")"); | |
| 208 | if (e.slice(end + 1).trim() !== "") return e; | |
| 209 | const args = items(e, open, ")"); | |
| 210 | const inner = which === "first" ? args[0] : args.at(-1); | |
| 211 | if (inner === undefined) return e; | |
| 212 | e = inner; | |
| 213 | } | |
| 214 | } | |
| 215 | ||
| 216 | /** Whether an argv expression's program (its first element) is fixed. */ | |
| 217 | function fixedProgram(argv: string, c: Constants): boolean { | |
| 218 | if (!argv.startsWith("[")) return false; | |
| 219 | const first = items(argv, 0, "]")[0]; | |
| 220 | if (first === undefined || first.startsWith("...")) return false; | |
| 221 | return isConstant(first, c) || /^"[^"\\]*"$|^'[^'\\]*'$|^`[^`$\\]*`$/.test(first); | |
| 222 | } | |
| 223 | ||
| 224 | /** Every site in one module's text. */ | |
| 225 | function sitesIn(file: string, source: string): Site[] { | |
| 226 | const sites: Site[] = []; | |
| 227 | const c = constantsIn(source); | |
| 228 | for (const m of source.matchAll(SPAWN)) { | |
| 229 | if (inComment(source, m.index)) continue; | |
| 230 | const { text } = readExpr(source, m.index + m[0].length, ",)"); | |
| 231 | const argv = squash(unwrap(text)); | |
| 232 | // An array literal is named by its program alone: the rest are arguments, and need no reason. | |
| 233 | const list = argv.startsWith("[") ? items(argv, 0, "]") : []; | |
| 234 | const code = list.length === 0 ? argv : `[${list[0]}${list.length > 1 ? ", …" : ""}]`; | |
| 235 | sites.push({ file, line: lineAt(source, m.index), kind: "spawn", code, fixed: fixedProgram(argv, c) }); | |
| 236 | } | |
| 237 | for (const m of source.matchAll(SHELL_C)) { | |
| 238 | if (inComment(source, m.index)) continue; | |
| 239 | const { text } = readExpr(source, m.index + m[0].length, ",]"); | |
| 240 | sites.push({ file, line: lineAt(source, m.index), kind: "shell", code: squash(text), fixed: fixedText(text, c) }); | |
| 241 | } | |
| 242 | for (const [kind, pattern] of [["bun-shell", BUN_SHELL], ["child_process", CHILD_PROCESS], ["alias", ALIAS]] as const) | |
| 243 | for (const m of source.matchAll(pattern)) { | |
| 244 | if (inComment(source, m.index)) continue; | |
| 245 | sites.push({ file, line: lineAt(source, m.index), kind, code: squash(m[0]), fixed: false }); | |
| 246 | } | |
| 247 | return sites.sort((a, b) => a.line - b.line); | |
| 248 | } | |
| 249 | ||
| 250 | /** Every site under `src/`, tests aside. */ | |
| 251 | function treeSites(): Site[] { | |
| 252 | const files = [...new Bun.Glob("src/**/*.{ts,tsx,js,mjs,cjs}").scanSync(root)] | |
| 253 | .filter((f) => !/\.test\.[jt]sx?$/.test(f)) | |
| 254 | .sort(); | |
| 255 | return files.flatMap((f) => sitesIn(f, readFileSync(join(root, f), "utf8"))); | |
| 256 | } | |
| 257 | ||
| 258 | const matches = (a: Allowed, s: Site): boolean => a.file === s.file && a.kind === s.kind && squash(a.code) === s.code; | |
| 259 | const at = (s: Site): string => `${s.file}:${s.line} (${s.kind}) ${s.code}`; | |
| 260 | ||
| 261 | /** | |
| 262 | * What is wrong between the sites that need a reason and the list: a site no entry names, or an | |
| 263 | * entry whose `count` (1 when absent, summed over entries naming the same code) is not the number | |
| 264 | * of sites it names. One entry per site by default, so a second site of the same code in the same | |
| 265 | * file needs the list to say so. | |
| 266 | */ | |
| 267 | function audit(sites: Site[], list: Allowed[]): { loose: string[]; miscounted: string[] } { | |
| 268 | const needing = sites.filter((s) => !s.fixed); | |
| 269 | const loose = needing.filter((s) => !list.some((a) => matches(a, s))).map(at); | |
| 270 | const miscounted: string[] = []; | |
| 271 | const seen = new Set<string>(); | |
| 272 | for (const a of list) { | |
| 273 | const key = `${a.file}\0${a.kind}\0${squash(a.code)}`; | |
| 274 | if (seen.has(key)) continue; | |
| 275 | seen.add(key); | |
| 276 | const said = list.filter((b) => matches(b, { file: a.file, kind: a.kind, code: squash(a.code), line: 0, fixed: false })) | |
| 277 | .reduce((n, b) => n + (b.count ?? 1), 0); | |
| 278 | const found = needing.filter((s) => matches(a, s)); | |
| 279 | if (said !== found.length) | |
| 280 | miscounted.push(`${a.file} (${a.kind}) ${a.code}: listed for ${said}, found ${found.length}${found.length > 0 ? ` at line ${found.map((s) => s.line).join(", ")}` : ""}`); | |
| 281 | } | |
| 282 | return { loose, miscounted }; | |
| 283 | } | |
| 284 | ||
| 285 | describe("what the scan reads", () => { | |
| 286 | test("a literal or constant program is fixed; a variable or a template with a value is not", () => { | |
| 287 | const src = [ | |
| 288 | `const SETSID = Bun.which("setsid"); Bun.spawn(["git", ...args], { cwd });`, | |
| 289 | `Bun.spawnSync([process.execPath, "x.ts"]);`, | |
| 290 | `Bun.spawn([SETSID, "-c", ...argv]);`, | |
| 291 | `Bun.spawn(argv, {});`, | |
| 292 | `Bun.spawn([bin, file]);`, | |
| 293 | "Bun.spawn([`${dir}/tool`, x]);", | |
| 294 | `Bun.spawn(backgroundArgv(["bun", "install"]), {});`, | |
| 295 | `Bun.spawn(enterArgv(join(d, "cgroup.procs"), [process.execPath, entry]), {});`, | |
| 296 | `Bun.spawn(backgroundArgv(argv));`, | |
| 297 | ].join("\n"); | |
| 298 | expect(sitesIn("src/a.ts", src).map((s) => [s.line, s.fixed])).toEqual([ | |
| 299 | [1, true], [2, true], [3, true], [4, false], [5, false], [6, false], [7, true], [8, true], [9, false], | |
| 300 | ]); | |
| 301 | expect(sitesIn("src/a.ts", src)[6]!.code).toBe(`["bun", …]`); | |
| 302 | expect(sitesIn("src/a.ts", src)[4]!.code).toBe(`[bin, …]`); | |
| 303 | expect(sitesIn("src/a.ts", src)[3]!.code).toBe(`argv`); | |
| 304 | }); | |
| 305 | ||
| 306 | test("an sh -c script is fixed only when it holds no value", () => { | |
| 307 | const src = [ | |
| 308 | `const NICE = 10; const LEVEL = { io: 7 } as const; const a = ["/bin/sh", "-c", 'exec "$@"', "x", ...argv];`, | |
| 309 | "const b = [\"sh\", \"-c\", `nice -n ${NICE} \"$@\"`];", | |
| 310 | `const c = ["/bin/sh", "-c", "a; " + \`b \${LEVEL.io}\` + "c"];`, | |
| 311 | `const d = ["/bin/sh", "-c", command];`, | |
| 312 | "const e = [\"bash\", \"-c\", `git log ${ref}`];", | |
| 313 | `const f = ["sh", "-c", "echo " + name];`, | |
| 314 | ].join("\n"); | |
| 315 | expect(sitesIn("src/b.ts", src).map((s) => [s.line, s.kind, s.fixed])).toEqual([ | |
| 316 | [1, "shell", true], [2, "shell", true], [3, "shell", true], [4, "shell", false], [5, "shell", false], [6, "shell", false], | |
| 317 | ]); | |
| 318 | }); | |
| 319 | ||
| 320 | test("a capitalised name is fixed only when this module declares it const as a literal", () => { | |
| 321 | const src = [ | |
| 322 | `const PORT = 3128; const DIR = "/run/forge"; const SCRIPT = \`cd \${DIR}; listen \${PORT.toString(16)}\`;`, | |
| 323 | `const BIN = req.query.bin; const LET = Bun.which(name); let LOOSE = "sh";`, | |
| 324 | `Bun.spawn([REQ.bin, x]);`, | |
| 325 | `Bun.spawn([BIN, x]);`, | |
| 326 | `Bun.spawn([LET, x]);`, | |
| 327 | `Bun.spawn([LOOSE, x]);`, | |
| 328 | `Bun.spawn([DIR, x]);`, | |
| 329 | `const s = ["sh", "-c", SCRIPT];`, | |
| 330 | "const t = [\"sh\", \"-c\", `x ${PORT.toString(req.radix)}`];", | |
| 331 | ].join("\n"); | |
| 332 | expect(sitesIn("src/k.ts", src).map((s) => [s.line, s.fixed])).toEqual([ | |
| 333 | [3, false], [4, false], [5, false], [6, false], [7, true], [8, true], [9, false], | |
| 334 | ]); | |
| 335 | }); | |
| 336 | ||
| 337 | test("a spawn function held under another name is a site of its own", () => { | |
| 338 | const src = [ | |
| 339 | `const sp = Bun.spawn;`, | |
| 340 | `const { spawn } = Bun;`, | |
| 341 | `Bun["spawn"](argv);`, | |
| 342 | `import { spawnSync as run } from "bun";`, | |
| 343 | `export const start = Bun.spawnSync;`, | |
| 344 | `const B = Bun;`, | |
| 345 | `let p: ReturnType<typeof Bun.spawn>;`, | |
| 346 | `let q: ReturnType<typeof Bun.spawn<"pipe", "pipe", "pipe">>;`, | |
| 347 | `import type { Socket } from "bun";`, | |
| 348 | `Bun.spawn(["git", "status"]);`, | |
| 349 | ].join("\n"); | |
| 350 | expect(sitesIn("src/l.ts", src).map((s) => [s.line, s.kind])).toEqual([ | |
| 351 | [1, "alias"], [2, "alias"], [3, "alias"], [4, "alias"], [5, "alias"], [6, "alias"], [10, "spawn"], | |
| 352 | ]); | |
| 353 | expect(sitesIn("src/l.ts", src).filter((s) => s.kind === "alias").every((s) => !s.fixed)).toBe(true); | |
| 354 | }); | |
| 355 | ||
| 356 | test("an entry covers as many sites as its count, and a second site needs its own reason", () => { | |
| 357 | const one = `Bun.spawn(argv, {});`; | |
| 358 | const entry: Allowed = { file: "src/m.ts", kind: "spawn", code: "argv", why: "the one caller hands a fixed argv" }; | |
| 359 | expect(audit(sitesIn("src/m.ts", one), [entry])).toEqual({ loose: [], miscounted: [] }); | |
| 360 | const two = `${one}\nBun.spawn(argv, {});`; | |
| 361 | expect(audit(sitesIn("src/m.ts", two), [entry]).miscounted).toEqual(["src/m.ts (spawn) argv: listed for 1, found 2 at line 1, 2"]); | |
| 362 | expect(audit(sitesIn("src/m.ts", two), [entry, { ...entry, why: "the second caller hands a fixed argv too" }]).miscounted).toEqual([]); | |
| 363 | expect(audit(sitesIn("src/m.ts", two), [{ ...entry, count: 2 }]).miscounted).toEqual([]); | |
| 364 | expect(audit(sitesIn("src/m.ts", one), [{ ...entry, count: 2 }]).miscounted).toEqual(["src/m.ts (spawn) argv: listed for 2, found 1 at line 1"]); | |
| 365 | expect(audit([], [entry]).miscounted).toEqual(["src/m.ts (spawn) argv: listed for 1, found 0"]); | |
| 366 | }); | |
| 367 | ||
| 368 | test("a spawn of a shell with a value in its script is caught by both readings", () => { | |
| 369 | const sites = sitesIn("src/c.ts", "Bun.spawn([\"/bin/sh\", \"-c\", `ls ${req.query}`]);"); | |
| 370 | expect(sites.map((s) => [s.kind, s.fixed])).toEqual([["spawn", true], ["shell", false]]); | |
| 371 | }); | |
| 372 | ||
| 373 | test("Bun.$ and child_process are always named; comments and types are not sites", () => { | |
| 374 | const src = [ | |
| 375 | `import { $ } from "bun";`, | |
| 376 | "await Bun.$`ls`;", | |
| 377 | `import { execFile } from "node:child_process";`, | |
| 378 | `const cp = require("child_process");`, | |
| 379 | "// Bun.spawn([bin]) in a comment", | |
| 380 | " * `Bun.spawn({terminal})` in a doc line", | |
| 381 | `let p: ReturnType<typeof Bun.spawn<"pipe", "pipe", "pipe">>;`, | |
| 382 | `const url = "https://x"; Bun.spawn([opener, url]);`, | |
| 383 | ].join("\n"); | |
| 384 | expect(sitesIn("src/d.ts", src).map((s) => [s.line, s.kind, s.fixed])).toEqual([ | |
| 385 | [1, "bun-shell", false], [2, "bun-shell", false], [3, "child_process", false], [4, "child_process", false], [8, "spawn", false], | |
| 386 | ]); | |
| 387 | }); | |
| 388 | }); | |
| 389 | ||
| 390 | describe("the wrappers looked through keep the caller's argv out of the script", () => { | |
| 391 | const argv = ["$(touch /tmp/pwned)", "; rm -rf /", "`id`"]; | |
| 392 | const cases: [string, string[]][] = [ | |
| 393 | ["backgroundArgv", backgroundArgv(argv)], | |
| 394 | ["oomArgv", oomArgv(500, argv, "/proc/self/oom_score_adj")], | |
| 395 | ["oomFirst", oomFirst(argv, 500)], | |
| 396 | ["enterArgv", enterArgv("/sys/fs/cgroup/x/cgroup.procs", argv)], | |
| 397 | ]; | |
| 398 | for (const [name, out] of cases) | |
| 399 | test(`${name}: a fixed sh -c script, then the argv whole as its "$@"`, () => { | |
| 400 | expect(out.slice(0, 2)).toEqual(["/bin/sh", "-c"]); | |
| 401 | for (const word of argv) expect(out[2]).not.toContain(word); | |
| 402 | expect(out.slice(-argv.length)).toEqual(argv); | |
| 403 | expect(out[2]).toContain('exec "$@"'); | |
| 404 | }); | |
| 405 | }); | |
| 406 | ||
| 407 | describe("every process src/ starts", () => { | |
| 408 | const sites = treeSites(); | |
| 409 | const list = allowed as Allowed[]; | |
| 410 | ||
| 411 | test("the scan finds the call sites", () => { | |
| 412 | // A floor, not a count: a scan that stops matching would pass everything. | |
| 413 | expect(sites.filter((s) => s.kind === "spawn").length).toBeGreaterThan(50); | |
| 414 | expect(sites.some((s) => s.kind === "shell")).toBe(true); | |
| 415 | }); | |
| 416 | ||
| 417 | test("each has a fixed program and script, or is listed with its reason", () => { | |
| 418 | expect( | |
| 419 | audit(sites, list).loose, | |
| 420 | "these start a program held in a value, or an sh -c script built from one: pass an argv array whose program is a literal (\"git\", process.execPath) or a CONSTANT, and keep values out of any script (hand them as \"$@\"). Only when it must stay so, list it in scripts/spawn-argv-allow.json with its file, kind, code and one reason", | |
| 421 | ).toEqual([]); | |
| 422 | }); | |
| 423 | ||
| 424 | test("each listed entry names as many sites as it says, and says why", () => { | |
| 425 | for (const a of list) { | |
| 426 | expect(a.why.trim().length, `${a.file} ${a.code}: say why it stays`).toBeGreaterThan(10); | |
| 427 | expect(KINDS).toContain(a.kind); | |
| 428 | if (a.count !== undefined) expect(Number.isInteger(a.count) && a.count >= 1, `${a.file} ${a.code}: count is a whole number from 1`).toBe(true); | |
| 429 | } | |
| 430 | expect( | |
| 431 | audit(sites, list).miscounted, | |
| 432 | "these entries in scripts/spawn-argv-allow.json do not name as many sites as they say: a new site of the same code needs its own reason (or the count raised with a reason that covers it); a gone one, the entry removed or its count lowered", | |
| 433 | ).toEqual([]); | |
| 434 | }); | |
| 435 | }); |
new above
offline