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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

This pull adds a new test, test/spawn-argv.test.ts, that reads every module under src/ (test files excluded) and names, by file:line, every Bun.spawn, every Bun.spawnSync, every sh -c argv, every Bun.$ template and every childprocess import.

Notes

shipped production is serving it deploy 4ec2c16

opened this

This pull adds a new test, test/spawn-argv.test.ts, that reads every module under src/ (test files excluded) and names, by file:line, every Bun.spawn, every Bun.spawnSync, every sh -c argv, every Bun.$ template and every child_process import. It fails when:

  • a spawn's program is not a string literal, process.execPath or a CONSTANT;
  • an sh -c script contains a value other than a constant;
  • a Bun.$ template or a child_process import appears at all.

A site that must stay is listed in scripts/spawn-argv-allow.json (file, kind, code, why). An entry that no longer matches any site fails the test, and so does one with no real reason. The scan found no case where a request's text reaches a program or a shell script, so no src/ code changed. The 24 entries are of three kinds:

  • Settings from whoever runs the forge: FORGE_AGENT_RUNNER, FORGE_CHROMIUM, and a person's own $EDITOR (found with Bun.which, refused if it has a space).
  • forge.json commands: the shell lines the repository commits (run, tty, check commands), run in the app's cell.
  • Internal pass-throughs: helpers whose only callers pass a literal program (runBounded, run()) or that always start with "git" (serviceCommand). Plus an agent's bash command, which runs only inside bwrap.

What to look at:

  • The four wrappers that put a fixed sh -c script in front of an argv (backgroundArgv, oomFirst, oomArgv, enterArgv). The scan looks through them to the argv inside. The test calls each one with shell-hostile arguments and checks that they come after the script, as "$@", and never inside it. The git wrappers in src/git all build ["git", ...args], so they pass with no entry.
  • How an entry matches its site: a spawn whose argv is an array literal is matched by its program alone ([binary, …]), so changing the arguments doesn't make an entry stale. Other argv is matched by its whole expression, whitespace collapsed.
  • What the scan reads as fixed: fixture tests cover the cases.

What I chose not to do:

  • Trace values back to a request. The scan works on the text of each call site, so a program held in a variable needs a listed reason even when it is harmless.
  • Add the serviceCommand and bashArgv builders to the looked-through wrappers. They are listed in the allow file with their reason instead.
  • Use a separate scripts/ module for the scanner. It lives in the test, which is marked @tree-scan so every fast tier runs it.

Checks run: bun scripts/gate.ts --fix (it rewrapped comments, which are committed), bun x tsc --noEmit, and the 112 files bun scripts/fast.ts named. 1650 passed and 1 failed: test/schema-migrations.test.ts › "two pulls' migrations land in either order…", at 5.9 s under load. That file passes 26/26 when run alone, and no migration is touched here. The keep_plan and estimate tools were not available in this run, so the issue has no plan and no estimate recorded.

To try it: run bun test test/spawn-argv.test.ts. Then add Bun.spawn([req.bin]) or `["sh", "-c", ls ${x}] to any src/` file and see it named by file:line, or delete a line from the allow file and see the site it covered named.

No test: none needed; the change is test-only.

🤖 Generated with Claude Code

Closes #14700

1 earlier comment · 1 event
commented

This pull adds a new test, test/spawn-argv.test.ts, that reads every module under src/ (test files excluded) and names, by file:line, every Bun.spawn, every Bun.spawnSync, every sh -c argv, every Bun.$ template and every child_process import. It fails when:

  • a spawn's program is not a string literal, process.execPath or a CONSTANT;
  • an sh -c script contains a value other than a constant;
  • a Bun.$ template or a child_process import appears at all.

A site that must stay is listed in scripts/spawn-argv-allow.json (file, kind, code, why). An entry that no longer matches any site fails the test, and so does one with no real reason. The scan found no case where a request's text reaches a program or a shell script, so no src/ code changed. The 24 entries are of three kinds:

  • Settings from whoever runs the forge: FORGE_AGENT_RUNNER, FORGE_CHROMIUM, and a person's own $EDITOR (found with Bun.which, refused if it has a space).
  • forge.json commands: the shell lines the repository commits (run, tty, check commands), run in the app's cell.
  • Internal pass-throughs: helpers whose only callers pass a literal program (runBounded, run()) or that always start with "git" (serviceCommand). Plus an agent's bash command, which runs only inside bwrap.

What to look at:

  • The four wrappers that put a fixed sh -c script in front of an argv (backgroundArgv, oomFirst, oomArgv, enterArgv). The scan looks through them to the argv inside. The test calls each one with shell-hostile arguments and checks that they come after the script, as "$@", and never inside it. The git wrappers in src/git all build ["git", ...args], so they pass with no entry.
  • How an entry matches its site: a spawn whose argv is an array literal is matched by its program alone ([binary, …]), so changing the arguments doesn't make an entry stale. Other argv is matched by its whole expression, whitespace collapsed.
  • What the scan reads as fixed: fixture tests cover the cases.

What I chose not to do:

  • Trace values back to a request. The scan works on the text of each call site, so a program held in a variable needs a listed reason even when it is harmless.
  • Add the serviceCommand and bashArgv builders to the looked-through wrappers. They are listed in the allow file with their reason instead.
  • Use a separate scripts/ module for the scanner. It lives in the test, which is marked @tree-scan so every fast tier runs it.

Checks run: bun scripts/gate.ts --fix (it rewrapped comments, which are committed), bun x tsc --noEmit, and the 112 files bun scripts/fast.ts named. 1650 passed and 1 failed: test/schema-migrations.test.ts › "two pulls' migrations land in either order…", at 5.9 s under load. That file passes 26/26 when run alone, and no migration is touched here. The keep_plan and estimate tools were not available in this run, so the issue has no plan and no estimate recorded.

To try it: run bun test test/spawn-argv.test.ts. Then add Bun.spawn([req.bin]) or `["sh", "-c", ls ${x}] to any src/` file and see it named by file:line, or delete a line from the allow file and see the site it covered named.

No test: none needed; the change is test-only.

🤖 Generated with Claude Code

Changes requested — before the latest push

Test-only change; no page or terminal surface, so the experience checks do not apply. The scan, fixtures and wrapper proofs are good. Two gaps keep it from enforcing what #14700 asks:

  1. FIRST FIX: an allow entry matches by file+kind+code text, not by site. matches() and the stale check (sites.some(...)) accept any number of sites. A new Bun.spawn(argv) or ["/bin/sh","-c",command] added to src/apps/process.ts, src/apps/tty.ts, runner_pool.ts, workdir.ts etc. is silently covered by the existing entry, with no reason of its own. Fix: give each entry a count (or require one entry per site, matched in order) and fail when the number of matching sites differs, so a second site needs its own reason. Add a fixture test for it.
  2. The scan is textual and misses aliases: const { spawn } = Bun, const sp = Bun.spawn, Bun"spawn", import { spawn } from "bun", and a re-exported helper. Any of these starts a process with no site named, which is the gap the issue's box 2 is meant to close. Either detect Bun.spawn/Bun.spawnSync used without a call (bare reference not followed by () and \bspawn(Sync)?\b imports from "bun", and name them as sites, with fixtures; or state the limit in the header comment.

Smaller: CONSTANT accepts any ALL_CAPS identifier or dotted path, so REQ.bin would read as fixed; restrict to constants declared as literals in the module, or list the accepted names. sitesIn is exported from a test file only for its own use; drop the export.

No docs/TUI needed for a test-only change; no dependency added.

commented

test/spawn-argv.test.ts scans src/ for every place a process is started. It fails when a program or an sh -c script is built from a value, unless scripts/spawn-argv-allow.json gives that place a reason. This push covers @juniper's review:

  • One reason per site. Each entry now covers exactly one site, or count sites if it says so. The test fails when the number of matching sites in the tree differs from what the list says. So a second identical site in the same file fails until it has a reason of its own, and an entry whose sites are gone fails too. This caught a real case: src/apps/process.ts has two /bin/sh -c command sites, one in runStep (line 391) and one in startProcess (line 734). Both had been covered by one entry; each now has its own reason.
  • Aliases are sites. These are now flagged as their own kind, alias, which always needs a listed reason: Bun.spawn or Bun.spawnSync used without being called (const sp = Bun.spawn, re-exports), Bun[...], { spawn } = Bun, import { spawn… } / import * as / default imports from "bun", and Bun passed around as a value. typeof Bun.spawn in a type and import type … from "bun" are not flagged. The tree has no aliases today.
  • What the scan can't see is stated in the header comment: it reads text, not types, so a process started through globalThis["Bun"], Reflect.get or a name built at run time is missed.
  • CONSTANT is narrower. A capitalised name now counts as fixed only if the same module declares it const as one of:
    • a string or number;
    • an object of those, so a single .field is allowed;
    • a template or + of other such constants;
    • Bun.which of a literal name.

    A method call with only literal arguments on such a constant stays fixed, as in PROXY_PORT.toString(16) inside contain.ts's WRAPPER. REQ.bin, const BIN = req.query.bin, Bun.which(name) and let names are not fixed.

  • sitesIn is no longer exported.

New fixture tests cover the counting, the aliases and the constant rules.

To try it: bun test test/spawn-argv.test.ts passes (14 tests). bun scripts/gate.ts --fix rewrapped one comment, which is committed, and bun x tsc --noEmit is clean. To see the count rule fail, copy any listed site, for example the Bun.spawn(argv, …) in src/crew/workdir.ts. The test then reports listed for 1, found 2 with both line numbers.

This is a test-only change. The desk's hold on releasing trevato/forge doesn't touch it: nothing under src/ changed, and nothing goes out until a person merges.

🤖 Generated with Claude Code

Approved

Earlier asks are met in the head. Each allow entry now covers one site, or count sites if it says so, and a mismatch fails. Spawn aliases are their own alias kind. CONSTANT is narrowed to same-module const literals. The header states what the scan cannot see. I read the diff, not a run: the clone sits at the base and does not have the test file. The change is test-only and touches no page or preview behaviour. Next, non-blocking: (1) move the scanner into its own scripts/ module if a second tree scan wants it; (2) add serviceCommand and bashArgv to the looked-through wrappers so their 3 entries can go.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs