Skip to content

Stop leading flags from routing installs past the gate - #231

Open
juangaitanv wants to merge 13 commits into
mainfrom
fix/gate-leading-flags
Open

juangaitanv wants to merge 13 commits into
mainfrom
fix/gate-leading-flags

Conversation

@juangaitanv

@juangaitanv juangaitanv commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

What changes

corgea npm <leading flags> <verb> <pkg> no longer runs npm unchecked when npm would read the flags differently from the gate.

  • npm and pnpm read true or false after any bare flag as that flag's value, and npm reads null after a [null, Boolean] flag (--yes, --workspaces). corgea npm --yes null install x now gates x. corgea npm --yes true install x now gates x, and corgea npm --yes true exec x routes like corgea npm exec x.
  • A leading flag the gate does not know may take the next token as its value (--package x). npm hands path, url and array flags the next token even when it looks like a flag (npm --package --registry install x sets package=--registry and installs x), so a dash token forks the reading too, after both unknown and known value flags. uv's clap parser rejects a dash value (error: a value is required for '--directory <DIRECTORY>'), so for uv the fork only over-blocks. When that other reading reaches an install, ci or exec verb, the command is one unverifiable target: it blocks under fail-closed, warns and runs in fail-open mode, and runs under --force. corgea npm --package x install y and --package x exec y no longer run unchecked.

Why

find_subcommand routed on the first non-flag word, skipping a value only for flags in parse::takes_value. npm's parser (nopt) disagrees in two ways, and each disagreement sent an install to the ungated passthrough even with install_gate_fail_closed on. The npm shim calls the same code, so it had the same hole.

Growing the npm flag list would close today's two cases and reopen on the next string flag npm adds. Instead the walk follows every reading a flag of unknown arity allows and refuses when one of them reaches a gated verb, so the hole stays closed without a list of npm value flags. It adds two lists of flags that never take a value, one for npm and one for uv, so common booleans do not fork. An entry that takes a value would reopen the hole. A missing entry makes the command unverifiable: it blocks under fail-closed, and in the default fail-open mode it warns and runs unchecked where main may have gated it (uv --some-unlisted-bool pip install x). uv rejects unknown flags, so its list only has to cover uv's own global booleans. Every entry was checked against uv 0.13.12 to take no value, but no one checked that the list is complete.

Invariants

  • A command whose leading flags could hide a gated verb never spawns the manager unchecked under fail-closed.
  • Leading flags the gate understands route as before: --loglevel silent install x, --version, -- install x, --silent install x, -g install x, -s run ci, --package=x install y.

What can go wrong

  • False positive: an unknown leading flag followed by a word that is itself a gated verb in the other reading is unverifiable (npm --unknown-flag install add). Common npm booleans (-s, -g, -D, --if-present, …) are listed in NPM_BOOLEANS (read by is_boolean_flag) and never fork, so npm -s run ci still runs the ci script. A missing flag makes such a command unverifiable: it blocks fail-closed, and warns and runs unchecked fail-open. A flag that takes a value must never be added to the list.
  • npm --yes true install x (and --yes null install x) also looks up a package named true (or null), because parse.rs reads a word after an unknown flag as a spec. It never under-checks, but under fail-closed a failed lookup of true over-blocks.
  • bun does not read true after a flag (bun --silent true pm bin → Script not found "true", bun 1.4.5), so bun --silent true install x is unverifiable. pnpm 12.25.1 does (pnpm --frozen-lockfile true root prints the root). yarn was not installed to probe; it goes through the same fork rule.
  • Other managers share find_subcommand, so the fix covers them too. Fail-closed probes against fake binaries: pnpm --package x add y, bun --package x add y, yarn --package x add y, pip --resume-retries 3 install y and pip --package x install y all spawned unchecked on main and block on this branch.
  • uv had its own copy of the walk (parse::uv_subcommand), now a thin wrapper over find_subcommand, used both before uv's subcommand and between pip and its verb. uv --python-preference only-system add y (a real uv flag missing from takes_value) ran unchecked on main and blocks here. The listed uv booleans (-q, -qq, --offline, --no-color, …) never fork, so uv --quiet pip install x gates as before. pip counts as a gated uv verb whatever follows, so uv --python-preference only-system pip list also blocks under fail-closed.

Compatibility

No config or migration. Merges cleanly with #229 (git merge-tree --write-tree against #229 head fe61ef9 exits 0; the merged tree was not built). is_gated_verb already counts npm exec/x, so its gated exec stays covered.

Validation

Built binary, fail-closed on, fake npm that records its argv, registry and vuln-api on a closed local port:

Command main 6df322c this PR
corgea npm --yes true install zzevil exit 0, spawned npm --yes true install zzevil checks zzevil, exit 1, only the lockfile-only resolution run
corgea npm --package x install zzevil exit 0, spawned unverifiable --package x, exit 1, nothing spawned
corgea npm --package x exec zzevil exit 0, spawned unverifiable --package x, exit 1, nothing spawned
corgea npm --package --registry install zzevil exit 0, spawned unverifiable --package --registry, exit 1, nothing spawned
corgea npm --yes true exec zzevil exit 0, spawned (routed on true) routes on exec, which main passes through ungated. This case closes with #229, whose exec grammar reports it as unverifiable (read from #229's code, not run)
npm --package x install zzevil via the bash shim exit 0, spawned exit 1, nothing spawned
corgea uv --python-preference only-system add zzevil exit 0, spawned unverifiable, exit 1, nothing spawned
corgea uv pip --python-preference only-system install zzevil exit 0, spawned unverifiable, exit 1, nothing spawned

Not in this PR

Comment thread src/precheck/mod.rs Resolved
@juangaitanv juangaitanv changed the title Stop leading npm flags from routing installs past the gate Stop leading flags from routing installs past the gate Dec 3, 2026
Comment thread src/precheck/mod.rs Resolved
Comment thread src/precheck/uv.rs Resolved
Comment thread src/precheck/mod.rs Resolved
@corgea-security corgea-security added the dennis-reviewed Dennis completed an automated review label Dec 3, 2026

@corgea-security corgea-security left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review risk: 2/5.

The leading-flag walk is fail-closed in the direction that matters: an unknown or dash-valued flag becomes unverifiable whenever any alternate reading lands on a different gated verb, and the npm/uv paths are covered by unit and e2e tests. I do not see a proven bypass, panic, or wrong argv in the diff.

No critical or high-priority changes were found.

@corgea-security corgea-security left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved by Dennis: high policy risk and automated risk 2/5.

@yhoztak yhoztak left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nearly ready to approve. One gap is left in the class this PR closes; details inline.

Comment thread src/precheck/mod.rs Resolved
@juangaitanv
juangaitanv requested a review from yhoztak December 3, 2026 21:24
Comment thread src/precheck/mod.rs Resolved
Comment thread src/precheck/mod.rs Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dennis-reviewed Dennis completed an automated review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants