From 6cef473283fa9b7923cff383ba4f2bf2d36db7f5 Mon Sep 17 00:00:00 2001 From: Sutu Sebastian Date: Mon, 16 Nov 2026 12:48:38 +0200 Subject: [PATCH 1/6] fix(verify): refuse a command word built from a variable `E=rm; $E -rf ./target` and `E=curl; $E -o ./f http://host/x` passed every gate arm, because the tool word sat in the variable, and the resolved command then ran under `bash -c`. The gate now refuses the shape as `verify-indirection`, quoting the spelling it saw and naming the remedy (write the tool word plainly, or run the step under `verify --sandbox`). Resolution was the other option and is unsound: the value can come from the command's own environment, or from a later or conditional assignment, so a resolver that guesses wrong turns a refusal into a false PASS. Refusing the shape covers the class: the braced, quoted, exported, assignment-prefix, loop, group, subshell, and concatenated spellings close with it, and so do the wrappers that run the word after their flags (`env`, `timeout`, `nice`, `xargs`, `command`, `then`, `do`). Variables in argument position still execute, so the false-positive boundary is unchanged. The arm list in `Check` becomes a slice so the fixed order stays one literal glance and the cognitive-complexity gate stays green. Corpus rows F-R38..F-R45 pin each spelling as strict and F-P30..F-P33 pin the benign neighbours. `SECURITY.md` now says what the README and the manual say: the refusals are a tripwire, `verify --sandbox` is the boundary, and a spelling outside the gate's reach is a gate gap to arm rather than a vulnerability. --- .changeset/verify-indirection.md | 24 ++++ README.md | 2 +- SECURITY.md | 26 ++++- clients/ts/src/refusal-codes.gen.ts | 1 + cmd/verdict/usage.go | 3 +- docs/manual.md | 10 +- docs/troubleshooting.md | 16 +++ evals/cases/usage-doc-gates.sh | 7 +- evals/fixtures/verify-fuzz-corpus.tsv | 15 ++- internal/core/hint.go | 2 + internal/core/refusalcodes.go | 1 + internal/core/testdata/hints.golden | 1 + internal/core/verify/gate.go | 41 +++---- internal/core/verify/gate_fuzz_test.go | 3 +- internal/core/verify/gate_indirection.go | 78 +++++++++++++ internal/core/verify/gate_indirection_test.go | 106 ++++++++++++++++++ internal/core/verify/testdata/gate.golden | 12 ++ 17 files changed, 309 insertions(+), 39 deletions(-) create mode 100644 .changeset/verify-indirection.md create mode 100644 internal/core/verify/gate_indirection.go create mode 100644 internal/core/verify/gate_indirection_test.go diff --git a/.changeset/verify-indirection.md b/.changeset/verify-indirection.md new file mode 100644 index 000000000..32a4c8db2 --- /dev/null +++ b/.changeset/verify-indirection.md @@ -0,0 +1,24 @@ +--- +"@stainless-code/verdict": patch +--- + +The verify pre-exec gate refuses a command word it cannot read: `E=rm; $E -rf +./target` and `E=curl; $E -o ./f http://host/x` passed every arm because the +tool word sat in the variable, and the resolved command then ran. `verify` now +refuses the shape as `verify-indirection`, quoting the spelling it saw and +naming the remedy (write the tool word plainly, or run the step under +`verify --sandbox`). The braced (`${E}`), quoted (`"$E"`), `export`ed, +assignment-prefix, loop (`for E in curl; do $E ...`), and concatenated +(`$A$B`) spellings close with it, and so do the wrappers that run the word +after their flags (`env`, `timeout`, `nice`, `xargs`, `command`, `then`, `do`). +Variables in argument position still execute (`echo $USER`, `PORT=9 curl +http://127.0.0.1:9/$PORT/f`, `$BIN/tool --version`), so the false-positive +boundary is unchanged. + +The gate does not resolve variables — the value can come from the command's own +environment, or from a later or conditional assignment — so it refuses rather +than guessing, and a variable holding a whole command line (`sh -c "$V"`) stays +a documented residual of the tripwire. `SECURITY.md` now says the same thing +the README and the manual say: the refusals are a tripwire, `verify --sandbox` +is the boundary, and a spelling outside the gate's reach is a gate gap to arm, +not a vulnerability. diff --git a/README.md b/README.md index 940a38036..3b20c4ccb 100644 --- a/README.md +++ b/README.md @@ -153,7 +153,7 @@ On the other three hosts, `record`, `report`, `show`, `next`, `triage`, and `thr ## Security -- **The skill runs with your privileges.** `verify` runs the agent's `verifyCmd` through `bash -c` with a 300 second limit. A pre-exec gate refuses destructive, networked, credential-reading, and repo-escaping commands before anything spawns. The gate is a tripwire, not a sandbox. For a hostile PR, `verify --sandbox` enforces the sandbox: `bubblewrap` on Linux (mounts, own PID namespace, no network) and `sandbox-exec` on macOS (no network); elsewhere follow the [recipe](docs/manual.md#hostile-pr-sandbox-recipe). +- **The skill runs with your privileges.** `verify` runs the agent's `verifyCmd` through `bash -c` with a 300 second limit. A pre-exec gate refuses destructive, networked, credential-reading, and repo-escaping commands before anything spawns, and refuses a command word built from a variable (`E=curl; $E -o ./f http://host/x`) as `verify-indirection`. The gate is a tripwire, not a sandbox. For a hostile PR, `verify --sandbox` enforces the sandbox: `bubblewrap` on Linux (mounts, own PID namespace, no network) and `sandbox-exec` on macOS (no network); elsewhere follow the [recipe](docs/manual.md#hostile-pr-sandbox-recipe). - **Review controls come from the base commit.** A PR cannot edit the panel, budget, or knowledge-base rules it is judged by. See [Review controls are base-owned](docs/manual.md#review-controls-are-base-owned). - **Diffs and comments are untrusted input.** The prompts treat them as evidence, never as instructions. - **Secrets stay local.** `GH_TOKEN` never leaves your machine, and provider tokens go only to their own hosts. Nothing phones home: no telemetry, no background updater. diff --git a/SECURITY.md b/SECURITY.md index e90999fd6..29c49ab9e 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -30,11 +30,22 @@ before reporting: many findings are already fixed on the latest tag. In scope (highest priority first): -- The `verify` gate: `verify` executes the agent's claimed proof - (`verifyCmd` via `bash -c`, 300s watchdog, tail recorded into the - patch record). Its pre-exec refusals (destructive, networked, - credential-reading, repo-escaping commands) are a security boundary; - any bypass is a vulnerability. +- `verify --sandbox`, the enforced boundary: mount and namespace isolation + under `bubblewrap` on Linux, network denial under `sandbox-exec` on macOS, + the credential-path hiding, and the `sandbox-unavailable` refusal that never + degrades to an unwrapped run. An escape from that sandbox, or a run that + records `"sandboxed": true` without the enforcement, is a vulnerability. +- The `verify` gate's own contract: `verify` executes the agent's claimed + proof (`verifyCmd` via `bash -c`, 300s watchdog, tail recorded into the + patch record). The pre-exec refusals are a **tripwire, not a boundary**: the + gate refuses the spellings it knows, and a `verifyCmd` spelling it does not + know executes inside whatever boundary you gave it — that ceiling is + documented in the [manual](docs/manual.md#hostile-pr-sandbox-recipe) and a + hit on it is a gate gap to report and arm, not a vulnerability. What is in + scope is the tripwire's own promise: a command that runs after the gate + refused it, a spawn that happens before the refusal, a refusal that names + the wrong reason, or a spelling the docs and the fuzz corpus claim refuses + while it executes. - The installers (`install.sh`, `install.ps1`): archive checksum, cosign verification pinned to the release's published build commit and to a push-triggered run, atomic replace, no execution before verification. A @@ -59,7 +70,10 @@ In scope (highest priority first): head bytes steer a review without the recorded `--allow-head-controls` opt-out is a vulnerability. -Out of scope: documentation typos (open a regular issue), missing +Out of scope: a `verifyCmd` spelling the string gate does not know (the +documented tripwire ceiling above — send it, it gets armed and added to +`evals/fixtures/verify-fuzz-corpus.tsv`), documentation typos (open a regular +issue), missing hardening wishes with no exploit path (open a feature request), and vulnerabilities in `apps/docs` build-time dependencies that never reach the published static site (still reportable, assessed by diff --git a/clients/ts/src/refusal-codes.gen.ts b/clients/ts/src/refusal-codes.gen.ts index ae3a7add4..e4cb520ba 100644 --- a/clients/ts/src/refusal-codes.gen.ts +++ b/clients/ts/src/refusal-codes.gen.ts @@ -226,6 +226,7 @@ export type RefusalCode = | "verify-credential-read" | "verify-destructive" | "verify-failed" + | "verify-indirection" | "verify-networked" | "verify-repo-escape" | "window-too-large" diff --git a/cmd/verdict/usage.go b/cmd/verdict/usage.go index a8633de55..4c3d53b0a 100644 --- a/cmd/verdict/usage.go +++ b/cmd/verdict/usage.go @@ -205,7 +205,8 @@ func usageText() string { " Runs the agent's claimed proof — exit code becomes machine fact. No GitHub I/O.\n" + " Destructive, networked, credential-reading, and repo-escaping commands\n" + " refuse before execution as verify-destructive, verify-networked,\n" + - " verify-credential-read, and verify-repo-escape.\n" + + " verify-credential-read, and verify-repo-escape; a command word built from\n" + + " a variable refuses as verify-indirection.\n" + " doctor [--provider github|gitlab|gitcafe|forgejo|bitbucket|azure] [--json]\n" + " preflight the configured host: its CLI and auth, plus .verdict storage\n" + " writability, hard-fail the run; scopes, git, ps, and API reachability warn\n" + diff --git a/docs/manual.md b/docs/manual.md index 552358c4b..9118e01c6 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -3147,7 +3147,8 @@ step — not the whole review — inside a throwaway sandbox so a malicious command finds no credentials and no route out. The `verify` verb already refuses hostile shapes before spawning anything — destructive (`verify-destructive`), networked (`verify-networked`), credential-reading -(`verify-credential-read`), repo-escaping (`verify-repo-escape`) — each +(`verify-credential-read`), repo-escaping (`verify-repo-escape`), and a +command word built from a variable (`verify-indirection`) — each typed refusal quoting the matched fragment with a `try:` rewrite hint, and the refusal leaves `patch.json` and `.verify.out` untouched. The sandbox below is the second layer for what the static gate cannot see (a plain @@ -3156,7 +3157,9 @@ replacement. The static gate is a tripwire, not a boundary. It matches the spellings it knows, on one command string at a time, and a shape it has not learned -executes. The boundary is the enforced sandbox below: `verify --sandbox` +executes. It does not resolve variables, so it refuses a command word it +cannot read rather than guessing what the variable holds. The boundary is +the enforced sandbox below: `verify --sandbox` runs the command under `bubblewrap` on Linux (the full mount and namespace shape) and under `sandbox-exec` on macOS (no network). A host that can enforce neither refuses `sandbox-unavailable` and runs nothing, so @@ -3283,7 +3286,8 @@ through any of these reopens the mandate discussion. `GEM_HOME`, `CARGO_HOME`, …) including innocent-command hijack; - env-directed fetch destinations (index/registry envs; registry _names_ have no URL to key on); -- dynamic construction (`$'…'`-hex, base64, variable indirection, alias-`!`); +- dynamic construction (`$'…'`-hex, base64, alias-`!`, a variable holding a whole + command line — a bare variable _command word_ refuses as `verify-indirection`); - interpreter-mediated execution (`python|perl|ruby|node|php …`, `curl | python3`); - fetch-then-exec splits across the `/tmp` carve-out (I3); - TOCTOU/meaning-change (symlink swaps, concurrent-run shared state); diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index e5e554306..15dbbfbcb 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -1404,6 +1404,22 @@ Cause: the verification command (or the receipt/head read around it) failed — Fix: fix the command or the evidence named above, then re-run `verify`. +## `verify-indirection` + +Cause: `verifyCmd` invokes a command word the gate cannot read — a plain +variable (`E=curl; $E -o ./f http://host/x`), the braced (`${E}`) or quoted +(`"$E"`) spelling, an `export`, an assignment prefix, or a loop variable. +The value may come from the command's own environment, so the gate refuses +the shape instead of guessing what the variable holds. + +The gate is a tripwire, not a boundary. A variable holding a whole command +line (`sh -c "$V"`, a file written and then run) is a shape it has not +learned. For a boundary, run the step under `verify --sandbox`. + +Fix: write the tool word plainly (`E=curl; $E -o ./f http://host/x` becomes +`curl -o ./f http://host/x`, or the test runner directly) and keep variables +for arguments, then re-run. + ## `verify-networked` Cause: `verifyCmd` fetches the network or pipes to a shell. diff --git a/evals/cases/usage-doc-gates.sh b/evals/cases/usage-doc-gates.sh index 85c397220..0ba202c13 100755 --- a/evals/cases/usage-doc-gates.sh +++ b/evals/cases/usage-doc-gates.sh @@ -10,7 +10,7 @@ if [ "$EVAL_STATUS" -ne 0 ]; then bad "--help exits $EVAL_STATUS" fi -for code in verify-destructive verify-networked verify-credential-read verify-repo-escape; do +for code in verify-destructive verify-networked verify-credential-read verify-repo-escape verify-indirection; do if grep -q "$code" "$EVAL_OUT"; then good "verify usage names $code" else @@ -27,8 +27,9 @@ fi if grep -q "verify-destructive" "$EVAL_ROOT/docs/manual.md" \ && grep -q "verify-networked" "$EVAL_ROOT/docs/manual.md" \ && grep -q "verify-credential-read" "$EVAL_ROOT/docs/manual.md" \ - && grep -q "verify-repo-escape" "$EVAL_ROOT/docs/manual.md"; then - good "manual names the four verify refusal codes" + && grep -q "verify-repo-escape" "$EVAL_ROOT/docs/manual.md" \ + && grep -q "verify-indirection" "$EVAL_ROOT/docs/manual.md"; then + good "manual names the five verify refusal codes" else bad "manual misses a verify refusal code" fi diff --git a/evals/fixtures/verify-fuzz-corpus.tsv b/evals/fixtures/verify-fuzz-corpus.tsv index 46a4ed353..fb3ad0deb 100644 --- a/evals/fixtures/verify-fuzz-corpus.tsv +++ b/evals/fixtures/verify-fuzz-corpus.tsv @@ -7,7 +7,8 @@ # # id stable case id (F-R* refuse, F-X* expected-fail, F-P* pass). # expect what the gate must do: one of verify-destructive, -# verify-networked, verify-credential-read, verify-repo-escape +# verify-networked, verify-credential-read, verify-repo-escape, +# verify-indirection # (refuse with exactly this typed code), or PASS (execute). # For xfail rows expect is aspirational: the code the gate SHOULD # refuse with once the gap is closed. @@ -66,6 +67,14 @@ F-R34 verify-destructive strict tar -Itouch-fz -cf /tmp/verify-fuzz-attached.tar F-R35 verify-destructive strict tar -cIf touch-fz /tmp/verify-fuzz-cluster.tar fz-notes.txt F-R36 verify-networked strict wget -i/tmp/verify-fuzz-wgeturls http://127.0.0.1:9/f F-R37 verify-networked strict wget -qi /tmp/verify-fuzz-wgeturls http://127.0.0.1:9/f +F-R38 verify-indirection strict E=rm; $E -rf ./fz-target +F-R39 verify-indirection strict E=curl; $E -o ./f http://127.0.0.1:9/x +F-R40 verify-indirection strict E=curl; ${E} -o ./f http://127.0.0.1:9/x +F-R41 verify-indirection strict E=curl; "$E" -o ./f http://127.0.0.1:9/x +F-R42 verify-indirection strict export E=curl; $E -o ./f http://127.0.0.1:9/x +F-R43 verify-indirection strict A=curl B=1; $A -o ./f http://127.0.0.1:9/x +F-R44 verify-indirection strict for E in curl; do $E -o ./f http://127.0.0.1:9/x; done +F-R45 verify-indirection strict A=cu B=rl; $A$B -o ./f http://127.0.0.1:9/x F-X01 verify-networked strict wget --remote-name http://127.0.0.1:9/f F-X02 verify-networked strict curl -K /tmp/verify-fuzz-curlcfg http://127.0.0.1:9/ F-X03 verify-networked strict wget -i /tmp/verify-fuzz-wgeturls http://127.0.0.1:9/f @@ -114,3 +123,7 @@ F-P26 PASS strict git --exec-path F-P27 PASS strict tar -czf /tmp/verify-fuzz-ok.tar fz-notes.txt F-P28 PASS strict tar -cf /tmp/verify-fuzz-plain.tar fz-notes.txt F-P29 PASS strict wget -q --version +F-P30 PASS strict printf '%s' $FOO +F-P31 PASS strict $BIN/fz-tool --version +F-P32 PASS strict printf ok --prefix=$FOO/x +F-P33 PASS strict X=$(date); printf hi diff --git a/internal/core/hint.go b/internal/core/hint.go index 99be35a07..d08e84c49 100644 --- a/internal/core/hint.go +++ b/internal/core/hint.go @@ -205,6 +205,8 @@ var hintRules = []HintRule{ {codes: []string{"verify-credential-read"}, hint: "try: rewrite verifyCmd without reading credential paths " + "or piping env into the network, then re-run"}, {codes: []string{"verify-repo-escape"}, hint: "try: rewrite verifyCmd with repo-relative paths (/tmp for scratch), then re-run"}, + {codes: []string{"verify-indirection"}, hint: "try: write the tool word plainly instead of " + + "$VAR, or run the step under verify --sandbox, then re-run"}, {codes: []string{"sandbox-unavailable"}, hint: "try: on Linux install bubblewrap and allow unprivileged " + "user namespaces; on macOS sandbox-exec ships with the OS — or re-run without --sandbox on a command you trust"}, {codes: []string{"agent-missing"}, hint: "try: install the agent CLI (or put it on PATH), or re-run with " + diff --git a/internal/core/refusalcodes.go b/internal/core/refusalcodes.go index 2f1c69114..5b6113d77 100644 --- a/internal/core/refusalcodes.go +++ b/internal/core/refusalcodes.go @@ -249,6 +249,7 @@ var refusalCodes = []string{ "verify-credential-read", "verify-destructive", "verify-failed", + "verify-indirection", "verify-networked", "verify-repo-escape", "window-too-large", diff --git a/internal/core/testdata/hints.golden b/internal/core/testdata/hints.golden index 9ff5e351c..5d48742e7 100644 --- a/internal/core/testdata/hints.golden +++ b/internal/core/testdata/hints.golden @@ -222,6 +222,7 @@ validation-stale try: re-run the validation pass against the current head, or de verify-credential-read try: rewrite verifyCmd without reading credential paths or piping env into the network, then re-run verify-destructive try: rewrite verifyCmd to stay in-repo and non-destructive (run the test runner, not rm/mkfs/dd), then re-run verify-failed try: address the cause above, then re-run the same command +verify-indirection try: write the tool word plainly instead of $VAR, or run the step under verify --sandbox, then re-run verify-networked try: rewrite verifyCmd without network fetches or pipe-to-shell (fetch fixtures first, run offline), then re-run verify-repo-escape try: rewrite verifyCmd with repo-relative paths (/tmp for scratch), then re-run window-too-large try: narrow --since (a shorter span or a later ref), then re-run diff --git a/internal/core/verify/gate.go b/internal/core/verify/gate.go index ba2e26ed2..fc0d4d1c9 100644 --- a/internal/core/verify/gate.go +++ b/internal/core/verify/gate.go @@ -15,35 +15,30 @@ import ( // Check runs the pre-exec allowlist gate over the raw verifyCmd, returning // the typed refusal on the first hostile match and nil when clean. The arm // order is fixed: mid-word substitution, command-position -// substitution, glob/brace fragmentation, then destructive, credential, and -// networked classes, then repo escape. The normalized texts are built once -// and shared by every section. +// substitution, an unreadable command word, glob/brace fragmentation, then +// destructive, credential, and networked classes, then repo escape. The +// normalized texts are built once and shared by every section. func Check(raw string) *core.RefusalError { g := normalize(raw) - if ref := checkSentinel(g); ref != nil { - return ref - } - if ref := checkCommandPosition(g); ref != nil { - return ref - } - if ref := checkGlobFragment(g); ref != nil { - return ref - } - if ref := checkDestructive(g); ref != nil { - return ref - } - if ref := checkCredential(g); ref != nil { - return ref - } - if ref := checkNetworked(g); ref != nil { - return ref - } - if ref := checkRepoEscape(g); ref != nil { - return ref + for _, arm := range gateArms { + if ref := arm(g); ref != nil { + return ref + } } return nil } +var gateArms = []func(*gateText) *core.RefusalError{ + checkSentinel, + checkCommandPosition, + checkIndirection, + checkGlobFragment, + checkDestructive, + checkCredential, + checkNetworked, + checkRepoEscape, +} + const ( codeDestructive = "verify-destructive" codeNetworked = "verify-networked" diff --git a/internal/core/verify/gate_fuzz_test.go b/internal/core/verify/gate_fuzz_test.go index 0ebbda0e0..9e5d19c83 100644 --- a/internal/core/verify/gate_fuzz_test.go +++ b/internal/core/verify/gate_fuzz_test.go @@ -50,7 +50,8 @@ func sameRefusal(a, b *core.RefusalError) bool { func documentedGateCode(code string) bool { switch code { - case "verify-destructive", "verify-networked", "verify-credential-read", "verify-repo-escape": + case "verify-destructive", "verify-networked", "verify-credential-read", "verify-repo-escape", + "verify-indirection": return true } return false diff --git a/internal/core/verify/gate_indirection.go b/internal/core/verify/gate_indirection.go new file mode 100644 index 000000000..2f13a9730 --- /dev/null +++ b/internal/core/verify/gate_indirection.go @@ -0,0 +1,78 @@ +package verifycore + +import ( + "regexp" + "strings" + + "github.com/stainless-code/verdict/internal/core" +) + +const codeIndirection = "verify-indirection" + +const msgIndirection = "a command word built from a variable hides the tool — write the tool " + + "word plainly, or run the step under verify --sandbox" + +// indirectWrappers are the words a command word may follow without being the +// invoked command: the shell keywords that open a command list, and the +// wrappers that run the word after their flags. +var indirectWrappers = map[string]bool{ + "!": true, "(": true, "{": true, "command": true, "do": true, "elif": true, + "else": true, "env": true, "exec": true, "if": true, "nice": true, + "nohup": true, "stdbuf": true, "then": true, "time": true, "timeout": true, + "until": true, "while": true, "xargs": true, +} + +// checkIndirection refuses a command word the gate cannot read: with no tool +// word in the text, every class below is blind to it, and resolving the +// variable statically is unsound (the value can come from the environment or +// a later assignment). Refusing the shape is the whole answer. +func checkIndirection(g *gateText) *core.RefusalError { + word := indirectWord(g.bare) + if word == "" { + return nil + } + if exact := indirectWord(g.csbare); exact != "" { + word = exact + } + return refuse(codeIndirection, word, msgIndirection) +} + +func indirectWord(text string) string { + for _, seg := range commandSegments(text) { + if word := commandWord(seg); isIndirectWord(word) { + return word + } + } + return "" +} + +func commandSegments(text string) []string { + return strings.FieldsFunc(text, func(r rune) bool { + return r == ';' || r == '|' || r == '&' + }) +} + +func commandWord(seg string) string { + for tok := range strings.FieldsSeq(seg) { + if isCommandPrefix(tok) { + continue + } + return tok + } + return "" +} + +func isCommandPrefix(tok string) bool { + return indirectWrappers[tok] || reAssignPrefix.MatchString(tok) || + strings.HasPrefix(tok, "-") || reDigits.MatchString(tok) +} + +func isIndirectWord(word string) bool { + return rePureExpansion.MatchString(strings.Trim(word, "()")) +} + +var ( + reAssignPrefix = regexp.MustCompile(`^[A-Za-z0-9_]+=`) + reDigits = regexp.MustCompile(`^[0-9]+$`) + rePureExpansion = regexp.MustCompile(`^(\$\{[A-Za-z_][A-Za-z0-9_]*\}|\$[A-Za-z_][A-Za-z0-9_]*)+$`) +) diff --git a/internal/core/verify/gate_indirection_test.go b/internal/core/verify/gate_indirection_test.go new file mode 100644 index 000000000..3e670ff05 --- /dev/null +++ b/internal/core/verify/gate_indirection_test.go @@ -0,0 +1,106 @@ +package verifycore_test + +import ( + "context" + "encoding/json" + "strings" + "testing" + + "github.com/stainless-code/verdict/internal/core" + verifycore "github.com/stainless-code/verdict/internal/core/verify" +) + +func patchWithCmd(t *testing.T, cmd string) string { + t.Helper() + raw, err := json.Marshal(map[string]string{"verifyCmd": cmd}) + if err != nil { + t.Fatal(err) + } + return string(raw) +} + +var variableCommandWords = []struct { + name string + cmd string +}{ + {"plain assignment", `E=rm; $E -rf ./target`}, + {"networked tool", `E=curl; $E -o ./f http://host/x`}, + {"braced", `E=curl; ${E} -o ./f http://host/x`}, + {"double quoted", `E=curl; "$E" -o ./f http://host/x`}, + {"export", `export E=curl; $E -o ./f http://host/x`}, + {"two assignments", `A=curl B=1; $A -o ./f http://host/x`}, + {"assignment prefix", `A=curl $A -o ./f http://host/x`}, + {"loop", `for E in curl; do $E -o ./f http://host/x; done`}, + {"group", `{ $E -o ./f http://host/x; }`}, + {"env wrapper", `E=curl; env E=$E $E -o ./f http://host/x`}, + {"timeout wrapper", `E=rm; timeout 5 $E -rf ./target`}, + {"subshell", `E=rm; ( $E -rf ./target )`}, + {"concatenated", `A=cu B=rl; $A$B -o ./f http://host/x`}, +} + +// TestVerifyRefusesVariableCommandWord pins STA-431: a plain assignment plus a +// bare `$VAR` in command position handed the real tool word to the commander. +// The stub records the spawn, so the test fails with the argv a host would +// have executed, and nothing here runs a real tool. +func TestVerifyRefusesVariableCommandWord(t *testing.T) { + for _, tc := range variableCommandWords { + t.Run(tc.name, func(t *testing.T) { wantIndirectionRefusal(t, tc.cmd) }) + } +} + +func wantIndirectionRefusal(t *testing.T, cmd string) { + t.Helper() + bundle(t, patchWithCmd(t, cmd)) + cmdr := &scriptCommander{res: core.Result{ExitCode: 0}} + _, ref := verifycore.Run(context.Background(), + verifyRequest(&collector{}, cmdr, "10", []string{"PATH=/bin"}, true)) + if len(cmdr.got) != 0 { + t.Fatalf("commander spawned %v; the gate must refuse %q before anything runs", + cmdr.got[0].Argv, cmd) + } + if ref == nil || ref.Code != "verify-indirection" { + t.Fatalf("Run refusal = %v, want code verify-indirection", ref) + } +} + +// TestCheckAllowsVariableArguments pins the false-positive boundary: a variable +// in argument position, an assignment prefix, or a path prefix stays clean. +func TestCheckAllowsVariableArguments(t *testing.T) { + benign := []string{ + `echo $USER`, + `echo "$BIN"`, + `PORT=9 curl http://127.0.0.1:9/fixture`, + `FOO=1 printf ok`, + `$BIN/tool --version`, + `X=$(date); printf hi`, + `curl http://127.0.0.1:9/$BRANCH/fixture`, + `printf ok --prefix=$FOO/x`, + `echo ${FOO:-nobody}`, + `sh -c 'printf ok'`, + } + for _, cmd := range benign { + t.Run(cmd, func(t *testing.T) { + if ref := verifycore.Check(cmd); ref != nil { + t.Fatalf("Check(%q) = %s: %q, want CLEAN", cmd, ref.Code, ref.Message) + } + }) + } +} + +// TestCheckVariableCommandWordMessage pins the fragment the refusal quotes and +// the remedy it names: the plain spelling, or --sandbox for a command word the +// gate cannot read. +func TestCheckVariableCommandWordMessage(t *testing.T) { + ref := verifycore.Check(`E=curl; $E -o ./f http://host/x`) + if ref == nil { + t.Fatal("Check = nil, want verify-indirection") + } + want := "refusing verifyCmd: matched '$E' (a command word built from a variable hides " + + "the tool — write the tool word plainly, or run the step under verify --sandbox)" + if ref.Message != want { + t.Fatalf("message = %q\nwant %q", ref.Message, want) + } + if got := verifycore.Check(`E=rm; ${E} -rf ./target`); got == nil || !strings.Contains(got.Message, "'${E}'") { + t.Fatalf("braced message = %v, want it to quote '${E}'", got) + } +} diff --git a/internal/core/verify/testdata/gate.golden b/internal/core/verify/testdata/gate.golden index a7adaa54e..21884a364 100644 --- a/internal/core/verify/testdata/gate.golden +++ b/internal/core/verify/testdata/gate.golden @@ -232,6 +232,14 @@ F-R34 verify-destructive F-R35 verify-destructive F-R36 verify-networked F-R37 verify-networked +F-R38 verify-indirection +F-R39 verify-indirection +F-R40 verify-indirection +F-R41 verify-indirection +F-R42 verify-indirection +F-R43 verify-indirection +F-R44 verify-indirection +F-R45 verify-indirection F-X01 verify-networked F-X02 verify-networked F-X03 verify-networked @@ -280,6 +288,10 @@ F-P26 CLEAN F-P27 CLEAN F-P28 CLEAN F-P29 CLEAN +F-P30 CLEAN +F-P31 CLEAN +F-P32 CLEAN +F-P33 CLEAN G000 CLEAN G001 CLEAN G002 CLEAN From 977af23053cc7743e0b857c8000ad7dde1806d45 Mon Sep 17 00:00:00 2001 From: Sutu Sebastian Date: Mon, 16 Nov 2026 13:07:50 +0200 Subject: [PATCH 2/6] fix(verify): segment on newlines and read wrapper command words Two holes in the new `verify-indirection` arm, both reproduced against the built binary before the fix: * `E=rm\necho ok\n$E -rf ./target` executed and `rm` removed `./target` (`error: null`, exit 0): `flatOf` turns the newline into a space before the gate reads any text, so the second line joined the first command's segment and the command word was `echo`. * `env -u HOME $E`, `timeout 5s $E`, `timeout -s KILL 5 $E`, `xargs -I {} $E` all executed: the arm skipped flags and digits but not a flag's value or a duration suffix, so the command word came back as `HOME`, `5s`, `{}`. Segmentation now reads a pair of texts that keep the newline separator (built only when the command has one, so every single-line verdict is unchanged), and one `segments` helper serves both the indirection arm and the glob arm that had the same flaw: `echo ok\nr? -rf x` now refuses as `verify-destructive` instead of joining the first segment. The wrapper rule is deliberately conservative rather than a per-wrapper flag table: after `env`, `timeout`, `xargs`, `nice`, `nohup`, `stdbuf`, `command`, `time`, or `exec`, any pure variable expansion in that command refuses, because the wrapper's own option grammar decides which later word bash actually runs. So `timeout -s KILL 5 $E` refuses without the gate needing to know that `-s` takes a value, and the cost is pinned in the docs: `timeout 300 make test` stays clean, `timeout 300 make test $TARGET` refuses. Variables in argument position after a command written plainly, `VAR=value cmd`, and `$BIN/tool` stay clean. Corpus rows F-R46..F-R51 and F-P34..F-P38 pin the wrapper spellings and their benign neighbours; the newline spellings live in the Go battery because the TSV forbids embedded newlines. The unspaced `($E -rf ./target)` row now covers the paren trim the reviewer found untested. --- .changeset/verify-indirection.md | 24 +++++-- docs/manual.md | 5 +- docs/troubleshooting.md | 13 ++-- evals/fixtures/verify-fuzz-corpus.tsv | 11 +++ internal/core/verify/gate_destructive.go | 18 ++--- internal/core/verify/gate_golden_test.go | 3 + internal/core/verify/gate_indirection.go | 68 ++++++++++--------- internal/core/verify/gate_indirection_test.go | 27 ++++++-- internal/core/verify/normalize.go | 16 +++++ internal/core/verify/testdata/gate.golden | 14 ++++ 10 files changed, 137 insertions(+), 62 deletions(-) diff --git a/.changeset/verify-indirection.md b/.changeset/verify-indirection.md index 32a4c8db2..e6d355d91 100644 --- a/.changeset/verify-indirection.md +++ b/.changeset/verify-indirection.md @@ -8,17 +8,27 @@ tool word sat in the variable, and the resolved command then ran. `verify` now refuses the shape as `verify-indirection`, quoting the spelling it saw and naming the remedy (write the tool word plainly, or run the step under `verify --sandbox`). The braced (`${E}`), quoted (`"$E"`), `export`ed, -assignment-prefix, loop (`for E in curl; do $E ...`), and concatenated -(`$A$B`) spellings close with it, and so do the wrappers that run the word -after their flags (`env`, `timeout`, `nice`, `xargs`, `command`, `then`, `do`). -Variables in argument position still execute (`echo $USER`, `PORT=9 curl -http://127.0.0.1:9/$PORT/f`, `$BIN/tool --version`), so the false-positive -boundary is unchanged. +assignment-prefix, loop (`for E in curl; do $E ...`), concatenated (`$A$B`), +and own-line (`E=rm\n$E -rf ./target`) spellings close with it, and so do the +wrappers that run a later word (`env -u HOME $E`, `timeout -s KILL 5 $E`, +`timeout 5s $E`, `xargs -I {} $E`, `command`, `nice`, `time`, `nohup`, +`stdbuf`, `exec`, `then`, `do`). Variables in argument position still execute +(`echo $USER`, `PORT=9 curl http://127.0.0.1:9/$PORT/f`, `$BIN/tool --version`), +so the false-positive boundary is unchanged for a command written plainly. The gate does not resolve variables — the value can come from the command's own environment, or from a later or conditional assignment — so it refuses rather than guessing, and a variable holding a whole command line (`sh -c "$V"`) stays -a documented residual of the tripwire. `SECURITY.md` now says the same thing +a documented residual of the tripwire. A wrapper's own option grammar decides +which later word bash runs, so after `env`/`timeout`/`xargs`/`nice`/`command` any +variable in that command refuses: that is the deliberate cost of not modelling +each wrapper's flags, so `timeout 300 make test` stays clean while +`timeout 300 make test $TARGET` refuses. The segmentation the gate reads now +also splits at newlines for every arm that reads a command word, so a glob or +tool word on a later line (`echo ok\nr? -rf x`) refuses instead of joining the +first command's segment. + +`SECURITY.md` now says the same thing the README and the manual say: the refusals are a tripwire, `verify --sandbox` is the boundary, and a spelling outside the gate's reach is a gate gap to arm, not a vulnerability. diff --git a/docs/manual.md b/docs/manual.md index 9118e01c6..f80868666 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -3158,7 +3158,10 @@ replacement. The static gate is a tripwire, not a boundary. It matches the spellings it knows, on one command string at a time, and a shape it has not learned executes. It does not resolve variables, so it refuses a command word it -cannot read rather than guessing what the variable holds. The boundary is +cannot read rather than guessing what the variable holds — including on a +later line of a multi-line `verifyCmd`, and behind a wrapper (`env`, +`timeout`, `xargs`, `nice`, `command`) whose own option grammar decides +which word runs. The boundary is the enforced sandbox below: `verify --sandbox` runs the command under `bubblewrap` on Linux (the full mount and namespace shape) and under `sandbox-exec` on macOS (no network). A host that can diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 15dbbfbcb..89491243c 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -1408,9 +1408,14 @@ Fix: fix the command or the evidence named above, then re-run `verify`. Cause: `verifyCmd` invokes a command word the gate cannot read — a plain variable (`E=curl; $E -o ./f http://host/x`), the braced (`${E}`) or quoted -(`"$E"`) spelling, an `export`, an assignment prefix, or a loop variable. -The value may come from the command's own environment, so the gate refuses -the shape instead of guessing what the variable holds. +(`"$E"`) spelling, an `export`, an assignment prefix, a loop variable, or +the same spelling on its own line or behind a wrapper (`env -u HOME $E`, +`timeout 5s $E`, `xargs -I {} $E`). The value may come from the command's +own environment, so the gate refuses the shape instead of guessing what the +variable holds. A wrapper's own option grammar decides which later word bash +runs, so after `env`/`timeout`/`xargs`/`nice`/`command` any variable in that +command refuses: `timeout 300 make test` stays clean, `timeout 300 make test +$TARGET` does not. The gate is a tripwire, not a boundary. A variable holding a whole command line (`sh -c "$V"`, a file written and then run) is a shape it has not @@ -1418,7 +1423,7 @@ learned. For a boundary, run the step under `verify --sandbox`. Fix: write the tool word plainly (`E=curl; $E -o ./f http://host/x` becomes `curl -o ./f http://host/x`, or the test runner directly) and keep variables -for arguments, then re-run. +for arguments of a command written plainly, then re-run. ## `verify-networked` diff --git a/evals/fixtures/verify-fuzz-corpus.tsv b/evals/fixtures/verify-fuzz-corpus.tsv index fb3ad0deb..fe11f68dc 100644 --- a/evals/fixtures/verify-fuzz-corpus.tsv +++ b/evals/fixtures/verify-fuzz-corpus.tsv @@ -75,6 +75,12 @@ F-R42 verify-indirection strict export E=curl; $E -o ./f http://127.0.0.1:9/x F-R43 verify-indirection strict A=curl B=1; $A -o ./f http://127.0.0.1:9/x F-R44 verify-indirection strict for E in curl; do $E -o ./f http://127.0.0.1:9/x; done F-R45 verify-indirection strict A=cu B=rl; $A$B -o ./f http://127.0.0.1:9/x +F-R46 verify-indirection strict env -u HOME $E -rf ./fz-target +F-R47 verify-indirection strict timeout 5s $E -rf ./fz-target +F-R48 verify-indirection strict timeout -s KILL 5 $E -rf ./fz-target +F-R49 verify-indirection strict xargs -I {} $E -rf ./fz-target +F-R50 verify-indirection strict command $E -rf ./fz-target +F-R51 verify-indirection strict nice -n 5 $E -rf ./fz-target F-X01 verify-networked strict wget --remote-name http://127.0.0.1:9/f F-X02 verify-networked strict curl -K /tmp/verify-fuzz-curlcfg http://127.0.0.1:9/ F-X03 verify-networked strict wget -i /tmp/verify-fuzz-wgeturls http://127.0.0.1:9/f @@ -127,3 +133,8 @@ F-P30 PASS strict printf '%s' $FOO F-P31 PASS strict $BIN/fz-tool --version F-P32 PASS strict printf ok --prefix=$FOO/x F-P33 PASS strict X=$(date); printf hi +F-P34 PASS strict timeout 300 printf ok +F-P35 PASS strict env CI=1 printf ok +F-P36 PASS strict xargs -n1 echo ok +F-P37 PASS strict { echo $USER; } +F-P38 PASS strict if true; then echo $USER; fi diff --git a/internal/core/verify/gate_destructive.go b/internal/core/verify/gate_destructive.go index 7a4a7e032..16b4b0deb 100644 --- a/internal/core/verify/gate_destructive.go +++ b/internal/core/verify/gate_destructive.go @@ -55,7 +55,7 @@ func checkCommandPosition(g *gateText) *core.RefusalError { } func checkGlobFragment(g *gateText) *core.RefusalError { - segw := segWords(g.bare) + segw := segWords(g.seg) if !hasGlobWord(segw) { return nil } @@ -95,18 +95,10 @@ func isBareBracket(w string) bool { return w == "[" || w == "[[" || w == "{" } -func segWords(bare string) []string { - parts := strings.Split(bare, ";") - var segs []string - for _, p := range parts { - segs = append(segs, strings.Split(p, "|")...) - } - var amp []string - for _, s := range segs { - amp = append(amp, strings.Split(s, "&")...) - } - out := make([]string, 0, len(amp)) - for _, seg := range amp { +func segWords(text string) []string { + segs := segments(text) + out := make([]string, 0, len(segs)) + for _, seg := range segs { if m := reSegWord.FindStringSubmatch(seg); m != nil { out = append(out, m[segWordIdx]) continue diff --git a/internal/core/verify/gate_golden_test.go b/internal/core/verify/gate_golden_test.go index 1cab3302b..fb88e0095 100644 --- a/internal/core/verify/gate_golden_test.go +++ b/internal/core/verify/gate_golden_test.go @@ -186,6 +186,9 @@ func gateOnlyExtras() [][2]string { `curl http://127.0.0.1:9/fixture 2>/dev/null`, `ping -c1 127.0.0.1 >/dev/null`, `rsync -a notes.txt /tmp/verify-allowlist-rsync-probe`, + "echo ok\n$E -rf ./target", + "E=rm\necho ok\n$E -rf ./target", + "echo ok\r\n$E -rf ./target", } var out [][2]string for i, c := range cmds { diff --git a/internal/core/verify/gate_indirection.go b/internal/core/verify/gate_indirection.go index 2f13a9730..8779f5c57 100644 --- a/internal/core/verify/gate_indirection.go +++ b/internal/core/verify/gate_indirection.go @@ -12,67 +12,71 @@ const codeIndirection = "verify-indirection" const msgIndirection = "a command word built from a variable hides the tool — write the tool " + "word plainly, or run the step under verify --sandbox" -// indirectWrappers are the words a command word may follow without being the -// invoked command: the shell keywords that open a command list, and the -// wrappers that run the word after their flags. -var indirectWrappers = map[string]bool{ - "!": true, "(": true, "{": true, "command": true, "do": true, "elif": true, - "else": true, "env": true, "exec": true, "if": true, "nice": true, - "nohup": true, "stdbuf": true, "then": true, "time": true, "timeout": true, - "until": true, "while": true, "xargs": true, -} +var ( + cmdKeywords = map[string]bool{ + "!": true, "(": true, "{": true, "do": true, "then": true, "else": true, + "elif": true, "if": true, "while": true, "until": true, + } + cmdWrappers = map[string]bool{ + "command": true, "env": true, "exec": true, "nice": true, "nohup": true, + "stdbuf": true, "time": true, "timeout": true, "xargs": true, + } +) -// checkIndirection refuses a command word the gate cannot read: with no tool -// word in the text, every class below is blind to it, and resolving the -// variable statically is unsound (the value can come from the environment or -// a later assignment). Refusing the shape is the whole answer. func checkIndirection(g *gateText) *core.RefusalError { - word := indirectWord(g.bare) + word := indirectWord(g.seg) if word == "" { return nil } - if exact := indirectWord(g.csbare); exact != "" { + if exact := indirectWord(g.segExact); exact != "" { word = exact } return refuse(codeIndirection, word, msgIndirection) } func indirectWord(text string) string { - for _, seg := range commandSegments(text) { - if word := commandWord(seg); isIndirectWord(word) { + for _, seg := range segments(text) { + if word := segmentIndirection(seg); word != "" { return word } } return "" } -func commandSegments(text string) []string { - return strings.FieldsFunc(text, func(r rune) bool { - return r == ';' || r == '|' || r == '&' - }) +func segmentIndirection(seg string) string { + words := strings.Fields(seg) + i := 0 + for i < len(words) && (reAssignPrefix.MatchString(words[i]) || cmdKeywords[words[i]]) { + i++ + } + if i == len(words) { + return "" + } + // A wrapper's own option grammar decides which later word bash runs, so + // after one every expansion refuses rather than being guessed apart. + if cmdWrappers[strings.Trim(words[i], "()")] { + return firstIndirect(words[i+1:]) + } + if isIndirectWord(words[i]) { + return words[i] + } + return "" } -func commandWord(seg string) string { - for tok := range strings.FieldsSeq(seg) { - if isCommandPrefix(tok) { - continue +func firstIndirect(words []string) string { + for _, word := range words { + if isIndirectWord(word) { + return word } - return tok } return "" } -func isCommandPrefix(tok string) bool { - return indirectWrappers[tok] || reAssignPrefix.MatchString(tok) || - strings.HasPrefix(tok, "-") || reDigits.MatchString(tok) -} - func isIndirectWord(word string) bool { return rePureExpansion.MatchString(strings.Trim(word, "()")) } var ( reAssignPrefix = regexp.MustCompile(`^[A-Za-z0-9_]+=`) - reDigits = regexp.MustCompile(`^[0-9]+$`) rePureExpansion = regexp.MustCompile(`^(\$\{[A-Za-z_][A-Za-z0-9_]*\}|\$[A-Za-z_][A-Za-z0-9_]*)+$`) ) diff --git a/internal/core/verify/gate_indirection_test.go b/internal/core/verify/gate_indirection_test.go index 3e670ff05..e0a4cf954 100644 --- a/internal/core/verify/gate_indirection_test.go +++ b/internal/core/verify/gate_indirection_test.go @@ -35,7 +35,16 @@ var variableCommandWords = []struct { {"env wrapper", `E=curl; env E=$E $E -o ./f http://host/x`}, {"timeout wrapper", `E=rm; timeout 5 $E -rf ./target`}, {"subshell", `E=rm; ( $E -rf ./target )`}, + {"subshell unspaced", `E=rm; ($E -rf ./target)`}, {"concatenated", `A=cu B=rl; $A$B -o ./f http://host/x`}, + {"newline", "echo ok\n$E -rf ./target"}, + {"newline after assignment", "E=rm\necho ok\n$E -rf ./target"}, + {"crlf", "echo ok\r\n$E -rf ./target"}, + {"env wrapper flag value", `env -u HOME $E -rf ./target`}, + {"timeout duration suffix", `timeout 5s $E -rf ./target`}, + {"timeout signal flag", `timeout -s KILL 5 $E -rf ./target`}, + {"xargs replace flag", `xargs -I {} $E -rf ./target`}, + {"command wrapper", `command $E -rf ./target`}, } // TestVerifyRefusesVariableCommandWord pins STA-431: a plain assignment plus a @@ -63,8 +72,6 @@ func wantIndirectionRefusal(t *testing.T, cmd string) { } } -// TestCheckAllowsVariableArguments pins the false-positive boundary: a variable -// in argument position, an assignment prefix, or a path prefix stays clean. func TestCheckAllowsVariableArguments(t *testing.T) { benign := []string{ `echo $USER`, @@ -77,6 +84,13 @@ func TestCheckAllowsVariableArguments(t *testing.T) { `printf ok --prefix=$FOO/x`, `echo ${FOO:-nobody}`, `sh -c 'printf ok'`, + `timeout 300 make test`, + `env CI=1 go test ./...`, + `xargs -n1 echo ok`, + `{ echo $USER; }`, + `! echo $USER`, + `if true; then echo $USER; fi`, + "echo ok\nprintf $FOO", } for _, cmd := range benign { t.Run(cmd, func(t *testing.T) { @@ -87,9 +101,12 @@ func TestCheckAllowsVariableArguments(t *testing.T) { } } -// TestCheckVariableCommandWordMessage pins the fragment the refusal quotes and -// the remedy it names: the plain spelling, or --sandbox for a command word the -// gate cannot read. +func TestCheckRefusesGlobWordAfterNewline(t *testing.T) { + wantRefusal(t, "echo ok\nr? -rf x", "verify-destructive", + "refusing verifyCmd: matched 'r?' (glob metacharacters in command position expand "+ + "into a hidden tool word — write the tool word plainly)") +} + func TestCheckVariableCommandWordMessage(t *testing.T) { ref := verifycore.Check(`E=curl; $E -o ./f http://host/x`) if ref == nil { diff --git a/internal/core/verify/normalize.go b/internal/core/verify/normalize.go index cf985baa2..6d27a9774 100644 --- a/internal/core/verify/normalize.go +++ b/internal/core/verify/normalize.go @@ -13,6 +13,8 @@ type gateText struct { scan string bare string csbare string + seg string + segExact string adj string deepSubst bool unreduced bool @@ -27,9 +29,23 @@ func normalize(raw string) *gateText { g.csbare = bareOf(g.flat, true) g.adj, g.deepSubst = adjOf(g.scan) g.unreduced = gluedSpan(g.adj) + // The flattened texts lose the newline command separator, and the gate + // reads one command per line: keep an unflattened pair for segmentation. + g.seg, g.segExact = g.bare, g.csbare + if strings.ContainsAny(raw, "\n\r") { + g.seg = bareOf(scanOf(readcommon.ASCIILower(raw)), false) + g.segExact = bareOf(scanOf(raw), true) + } return g } +// segments splits a command text at the shell's command separators. +func segments(text string) []string { + return strings.FieldsFunc(text, func(r rune) bool { + return r == ';' || r == '|' || r == '&' || r == '\n' || r == '\r' + }) +} + func flatOf(raw string) string { var sb strings.Builder sb.Grow(len(raw)) diff --git a/internal/core/verify/testdata/gate.golden b/internal/core/verify/testdata/gate.golden index 21884a364..dc3abeee2 100644 --- a/internal/core/verify/testdata/gate.golden +++ b/internal/core/verify/testdata/gate.golden @@ -240,6 +240,12 @@ F-R42 verify-indirection F-R43 verify-indirection F-R44 verify-indirection F-R45 verify-indirection +F-R46 verify-indirection +F-R47 verify-indirection +F-R48 verify-indirection +F-R49 verify-indirection +F-R50 verify-indirection +F-R51 verify-indirection F-X01 verify-networked F-X02 verify-networked F-X03 verify-networked @@ -292,6 +298,11 @@ F-P30 CLEAN F-P31 CLEAN F-P32 CLEAN F-P33 CLEAN +F-P34 CLEAN +F-P35 CLEAN +F-P36 CLEAN +F-P37 CLEAN +F-P38 CLEAN G000 CLEAN G001 CLEAN G002 CLEAN @@ -307,6 +318,9 @@ G011 CLEAN G012 CLEAN G013 CLEAN G014 CLEAN +G015 verify-indirection +G016 verify-indirection +G017 verify-indirection M000 verify-destructive MU000 verify-destructive MB000 verify-destructive From 4683de99dbd2b5ef7a7afaf55fc4561cefa5da0b Mon Sep 17 00:00:00 2001 From: Sutu Sebastian Date: Mon, 16 Nov 2026 13:17:49 +0200 Subject: [PATCH 3/6] chore(size): raise linux/arm64 budget to 15 540 000 Measured linux/arm64 at 15 466 656 bytes against the 15 480 000 baseline: 13 KB of headroom, under the house 70 KB floor for that platform. The next 10 kB multiple is 15 540 000, leaving 73 344 bytes. Every other line already clears its floor on this machine (go1.27.4, the version CI resolves): darwin/amd64 9 808, darwin/arm64 22 014, linux/amd64 8 672, windows/amd64 8 688, windows/arm64 9 776. --- scripts/size-baseline.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/size-baseline.txt b/scripts/size-baseline.txt index 3916d4731..e20d720cd 100644 --- a/scripts/size-baseline.txt +++ b/scripts/size-baseline.txt @@ -2,6 +2,6 @@ deps 0 darwin/amd64 17340000 darwin/arm64 15770000 linux/amd64 17040000 -linux/arm64 15480000 +linux/arm64 15540000 windows/amd64 17430000 windows/arm64 15550000 From eee99c214cc1dc1550e6410d9b59e9afd1ea9eed Mon Sep 17 00:00:00 2001 From: Sutu Sebastian Date: Mon, 16 Nov 2026 13:37:09 +0200 Subject: [PATCH 4/6] fix(verify): read past redirections, substitutions and inline shells MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Round 2 of the STA-431 review found the same class in three more shapes, each reproduced against the built binary first (`E=echo; …` executed, `error: null`): a leading redirection before the command word (`/dev/null $E`, `2>&1 $E`, `<<> f $E`), a command substitution (`echo $($E pwned)`, backticks, `cat <($E pwned)`), and `builtin $E pwned` — plus `sh -c "$E pwned"`, which no arm read at all. The arm now skips leading redirections the way it skips assignments — an operator alone (`>`) consumes its target word, an operator with an attached target (`>/dev/null`, `2>&1`) consumes only itself — so `2>&1 $E` reaches `$E`. `segments` no longer breaks at the `&` of `>&`/`<&`/`&>`, which is what a fd-duplication is rather than a command separator; the glob arm shares that fix. Substitution bodies are extracted from the raw command (balanced `$( )`, backticks, `<(`/`>(` process substitution, `$((` arithmetic skipped) and judged as their own command position, recursively, so `$($E pwned)` refuses and `$(date)` stays clean. Depth stops at the sentinel pass cap, where the existing `nested-substitution` refusal already fires. `builtin` joins the wrapper set. Shells join only when they carry an inline command (`-c`, `-ec`), so `sh -c "$E pwned"` refuses while `bash ./fz-missing.sh $TARGET` stays clean. `eval $E` and `sudo $E` already refused as `verify-destructive` before this arm; a test now pins that, so a later move into the wrapper set cannot reroute the code silently. Corpus rows F-R52..F-R63 (including the two `verify-destructive` pins) and F-P39..F-P44 (`cat &1`, `>> f`) are skipped to reach the command +word, and a substitution body (`$($E pwned)`, backticks, `cat <($E pwned)`) is +judged as its own command position. `SECURITY.md` now says the same thing the README and the manual say: the refusals are a tripwire, `verify --sandbox` diff --git a/docs/manual.md b/docs/manual.md index f80868666..8b8e33200 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -3159,9 +3159,11 @@ The static gate is a tripwire, not a boundary. It matches the spellings it knows, on one command string at a time, and a shape it has not learned executes. It does not resolve variables, so it refuses a command word it cannot read rather than guessing what the variable holds — including on a -later line of a multi-line `verifyCmd`, and behind a wrapper (`env`, -`timeout`, `xargs`, `nice`, `command`) whose own option grammar decides -which word runs. The boundary is +later line of a multi-line `verifyCmd`, after a leading redirection, inside a +substitution (`$(...)`, backticks, `<(...)`), and behind a wrapper (`builtin`, +`command`, `env`, `exec`, `nice`, `nohup`, `stdbuf`, `time`, `timeout`, +`xargs`, or a shell carrying `-c`) whose own option grammar decides which word +runs. The boundary is the enforced sandbox below: `verify --sandbox` runs the command under `bubblewrap` on Linux (the full mount and namespace shape) and under `sandbox-exec` on macOS (no network). A host that can diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 89491243c..c5a65a101 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -1408,14 +1408,20 @@ Fix: fix the command or the evidence named above, then re-run `verify`. Cause: `verifyCmd` invokes a command word the gate cannot read — a plain variable (`E=curl; $E -o ./f http://host/x`), the braced (`${E}`) or quoted -(`"$E"`) spelling, an `export`, an assignment prefix, a loop variable, or -the same spelling on its own line or behind a wrapper (`env -u HOME $E`, -`timeout 5s $E`, `xargs -I {} $E`). The value may come from the command's -own environment, so the gate refuses the shape instead of guessing what the -variable holds. A wrapper's own option grammar decides which later word bash -runs, so after `env`/`timeout`/`xargs`/`nice`/`command` any variable in that -command refuses: `timeout 300 make test` stays clean, `timeout 300 make test -$TARGET` does not. +(`"$E"`) spelling, an `export`, an assignment prefix, a loop variable, the +same spelling on its own line, after a leading redirection (`&1 $E`, `>> f $E`), or inside a substitution (`echo $($E pwned)`, backticks, +`cat <($E pwned)`), whose body is judged as its own command. The value may +come from the command's own environment, so the gate refuses the shape instead +of guessing what the variable holds. + +A wrapper's own option grammar decides which later word bash runs, so after +`builtin`, `command`, `env`, `exec`, `nice`, `nohup`, `stdbuf`, `time`, +`timeout`, or `xargs` any variable in that command refuses: `timeout 300 make +test` stays clean, `timeout 300 make test $TARGET` does not. A shell counts +the same way when it carries an inline command (`sh -c`, `bash -c`, `bash -ec`): +`sh -c "$E pwned"` refuses. `eval` and `sudo` refuse earlier, as +`verify-destructive`, whatever their arguments. The gate is a tripwire, not a boundary. A variable holding a whole command line (`sh -c "$V"`, a file written and then run) is a shape it has not diff --git a/evals/fixtures/verify-fuzz-corpus.tsv b/evals/fixtures/verify-fuzz-corpus.tsv index fe11f68dc..7010272d7 100644 --- a/evals/fixtures/verify-fuzz-corpus.tsv +++ b/evals/fixtures/verify-fuzz-corpus.tsv @@ -81,6 +81,18 @@ F-R48 verify-indirection strict timeout -s KILL 5 $E -rf ./fz-target F-R49 verify-indirection strict xargs -I {} $E -rf ./fz-target F-R50 verify-indirection strict command $E -rf ./fz-target F-R51 verify-indirection strict nice -n 5 $E -rf ./fz-target +F-R52 verify-indirection strict E=echo; /dev/null $E pwned +F-R54 verify-indirection strict E=echo; 2>&1 $E pwned +F-R55 verify-indirection strict E=echo; <<> fz-notes.txt $E pwned +F-R57 verify-indirection strict E=echo; echo $($E pwned) +F-R58 verify-indirection strict E=echo; echo `$E pwned` +F-R59 verify-indirection strict E=echo; cat <($E pwned) +F-R60 verify-indirection strict E=echo; builtin $E pwned +F-R61 verify-indirection strict E=echo; sh -c "$E pwned" +F-R62 verify-destructive strict E=echo; eval $E pwned +F-R63 verify-destructive strict E=echo; sudo $E pwned F-X01 verify-networked strict wget --remote-name http://127.0.0.1:9/f F-X02 verify-networked strict curl -K /tmp/verify-fuzz-curlcfg http://127.0.0.1:9/ F-X03 verify-networked strict wget -i /tmp/verify-fuzz-wgeturls http://127.0.0.1:9/f @@ -138,3 +150,9 @@ F-P35 PASS strict env CI=1 printf ok F-P36 PASS strict xargs -n1 echo ok F-P37 PASS strict { echo $USER; } F-P38 PASS strict if true; then echo $USER; fi +F-P39 PASS strict cat = len(words) { return "" } + rest := words[i+1:] + word := strings.Trim(words[i], "()") // A wrapper's own option grammar decides which later word bash runs, so // after one every expansion refuses rather than being guessed apart. - if cmdWrappers[strings.Trim(words[i], "()")] { - return firstIndirect(words[i+1:]) + if cmdWrappers[word] || cmdShells[word] && hasInlineCommand(rest) { + return firstIndirect(rest) } if isIndirectWord(words[i]) { return words[i] @@ -72,11 +109,95 @@ func firstIndirect(words []string) string { return "" } +func hasInlineCommand(words []string) bool { + return slices.ContainsFunc(words, reInlineCommand.MatchString) +} + func isIndirectWord(word string) bool { return rePureExpansion.MatchString(strings.Trim(word, "()")) } +func skipRedirect(words []string, i int) (int, bool) { + op := reRedirect.FindString(words[i]) + if op == "" { + return i, false + } + if len(op) < len(words[i]) { + return i + 1, true + } + return i + 2, true +} + +func substitutionBodies(raw string) []string { + var out []string + for i := 0; i < len(raw); i++ { + body, end, ok := substitutionAt(raw, i) + if !ok { + continue + } + out = append(out, body) + i = end + } + return out +} + +func substitutionAt(raw string, i int) (string, int, bool) { + switch raw[i] { + case '$': + return dollarBody(raw, i) + case '<', '>': + return processBody(raw, i) + case '`': + return backtickBody(raw, i) + } + return "", 0, false +} + +func dollarBody(raw string, i int) (string, int, bool) { + if !parenAt(raw, i+1) || parenAt(raw, i+2) { + return "", 0, false + } + return parenBody(raw, i+2) +} + +func processBody(raw string, i int) (string, int, bool) { + if !parenAt(raw, i+1) { + return "", 0, false + } + return parenBody(raw, i+2) +} + +func backtickBody(raw string, i int) (string, int, bool) { + end := strings.IndexByte(raw[i+1:], '`') + if end < 0 { + return "", 0, false + } + return raw[i+1 : i+1+end], i + end + 1, true +} + +func parenAt(raw string, i int) bool { + return i < len(raw) && raw[i] == '(' +} + +func parenBody(raw string, start int) (string, int, bool) { + depth := 1 + for i := start; i < len(raw); i++ { + switch raw[i] { + case '(': + depth++ + case ')': + depth-- + if depth == 0 { + return raw[start:i], i, true + } + } + } + return "", 0, false +} + var ( reAssignPrefix = regexp.MustCompile(`^[A-Za-z0-9_]+=`) rePureExpansion = regexp.MustCompile(`^(\$\{[A-Za-z_][A-Za-z0-9_]*\}|\$[A-Za-z_][A-Za-z0-9_]*)+$`) + reRedirect = regexp.MustCompile(`^[0-9]*(<<<|<<|<>|>>|>\||>&|<&|&>>|&>|<|>)`) + reInlineCommand = regexp.MustCompile(`^-[a-z]*c$`) ) diff --git a/internal/core/verify/gate_indirection_test.go b/internal/core/verify/gate_indirection_test.go index e0a4cf954..26140a051 100644 --- a/internal/core/verify/gate_indirection_test.go +++ b/internal/core/verify/gate_indirection_test.go @@ -45,12 +45,20 @@ var variableCommandWords = []struct { {"timeout signal flag", `timeout -s KILL 5 $E -rf ./target`}, {"xargs replace flag", `xargs -I {} $E -rf ./target`}, {"command wrapper", `command $E -rf ./target`}, + {"builtin wrapper", `E=echo; builtin $E pwned`}, + {"redirect in", `E=echo; /dev/null $E pwned`}, + {"redirect fd dup", `E=echo; 2>&1 $E pwned`}, + {"redirect here string", `E=echo; <<> fz-notes.txt $E pwned`}, + {"command substitution", `E=echo; echo $($E pwned)`}, + {"command substitution nested", `E=echo; echo $(echo $($E pwned))`}, + {"backtick substitution", "E=echo; echo `$E pwned`"}, + {"process substitution", `E=echo; cat <($E pwned)`}, + {"shell inline command", `E=echo; sh -c "$E pwned"`}, + {"shell inline command clustered", `E=echo; bash -ec "$E pwned"`}, } -// TestVerifyRefusesVariableCommandWord pins STA-431: a plain assignment plus a -// bare `$VAR` in command position handed the real tool word to the commander. -// The stub records the spawn, so the test fails with the argv a host would -// have executed, and nothing here runs a real tool. func TestVerifyRefusesVariableCommandWord(t *testing.T) { for _, tc := range variableCommandWords { t.Run(tc.name, func(t *testing.T) { wantIndirectionRefusal(t, tc.cmd) }) @@ -84,6 +92,13 @@ func TestCheckAllowsVariableArguments(t *testing.T) { `printf ok --prefix=$FOO/x`, `echo ${FOO:-nobody}`, `sh -c 'printf ok'`, + `cat &1`) or a redirect (`&> f`) is not one: it belongs to its redirect. +func isSegmentBreak(text string, i int) bool { + switch text[i] { + case ';', '|', '\n', '\r': + return true + case '&': + if i > 0 && (text[i-1] == '>' || text[i-1] == '<') { + return false + } + return i+1 >= len(text) || text[i+1] != '>' + } + return false } func flatOf(raw string) string { diff --git a/internal/core/verify/testdata/gate.golden b/internal/core/verify/testdata/gate.golden index dc3abeee2..e6fcfbc5d 100644 --- a/internal/core/verify/testdata/gate.golden +++ b/internal/core/verify/testdata/gate.golden @@ -246,6 +246,18 @@ F-R48 verify-indirection F-R49 verify-indirection F-R50 verify-indirection F-R51 verify-indirection +F-R52 verify-indirection +F-R53 verify-indirection +F-R54 verify-indirection +F-R55 verify-indirection +F-R56 verify-indirection +F-R57 verify-indirection +F-R58 verify-indirection +F-R59 verify-indirection +F-R60 verify-indirection +F-R61 verify-indirection +F-R62 verify-destructive +F-R63 verify-destructive F-X01 verify-networked F-X02 verify-networked F-X03 verify-networked @@ -303,6 +315,12 @@ F-P35 CLEAN F-P36 CLEAN F-P37 CLEAN F-P38 CLEAN +F-P39 CLEAN +F-P40 CLEAN +F-P41 CLEAN +F-P42 CLEAN +F-P43 CLEAN +F-P44 CLEAN G000 CLEAN G001 CLEAN G002 CLEAN diff --git a/scripts/size-baseline.txt b/scripts/size-baseline.txt index e20d720cd..b2ac05faf 100644 --- a/scripts/size-baseline.txt +++ b/scripts/size-baseline.txt @@ -1,7 +1,7 @@ deps 0 -darwin/amd64 17340000 -darwin/arm64 15770000 -linux/amd64 17040000 +darwin/amd64 17350000 +darwin/arm64 15790000 +linux/amd64 17050000 linux/arm64 15540000 -windows/amd64 17430000 -windows/arm64 15550000 +windows/amd64 17440000 +windows/arm64 15560000 From 5f41a881bcbd924363e4497dd9b35912729dd6f7 Mon Sep 17 00:00:00 2001 From: Sutu Sebastian Date: Mon, 16 Nov 2026 13:54:48 +0200 Subject: [PATCH 5/6] fix(verify): read inline-shell flags, quoted parens and arithmetic suffixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three advisories from the round-3 review, each reproduced against the built binary before the change. `sh -cu "$E pwned"` executed: `reInlineCommand` required `c` to end the cluster. Any cluster carrying a `c` is an inline command (`-c`, `-ec`, `-cu`), so the flag pattern is `^-[a-z]*c[a-z]*$`. `echo $($E "(")` executed: `parenBody` counted parens inside quotes, so the quoted `(` never closed and the whole substitution body was skipped. The body scan now skips `'…'` and `"…"` spans (honoring `\"` inside double quotes) and `\`-escaped bytes, so parens inside quotes no longer move the depth — the body is `$E "("`, its command word is `$E`, and it refuses. `$((…))` arithmetic stays out of the scan, now pinned by a benign case so the guard cannot be dropped silently. Corpus: F-R64 (`sh -cu`), F-R65 (`$($E "(")`) strict; F-P45 (`echo $(( $X + 1 ))`), F-P46 (`echo $(printf "(%s)" x)`) pass. An unbalanced body is no longer reachable as a bypass: bash parses the whole `-c` string before running any of it, so a substitution that never closes executes nothing. --- .changeset/verify-indirection.md | 5 +- docs/troubleshooting.md | 7 ++- evals/fixtures/verify-fuzz-corpus.tsv | 4 ++ internal/core/verify/gate_indirection.go | 61 ++++++++++++++++--- internal/core/verify/gate_indirection_test.go | 4 ++ internal/core/verify/testdata/gate.golden | 4 ++ 6 files changed, 70 insertions(+), 15 deletions(-) diff --git a/.changeset/verify-indirection.md b/.changeset/verify-indirection.md index 10c318cbf..ad230e184 100644 --- a/.changeset/verify-indirection.md +++ b/.changeset/verify-indirection.md @@ -24,14 +24,15 @@ which later word bash runs, so after `builtin`/`command`/`env`/`exec`/`nice`/ `nohup`/`stdbuf`/`time`/`timeout`/`xargs` any variable in that command refuses: that is the deliberate cost of not modelling each wrapper's flags, so `timeout 300 make test` stays clean while `timeout 300 make test $TARGET` -refuses. A shell carrying an inline command (`sh -c "$E pwned"`, `bash -ec`) +refuses. A shell carrying an inline command (`sh -c "$E pwned"`, `bash -ec`, `sh -cu`) counts the same way; `eval` and `sudo` refuse earlier as `verify-destructive`. The segmentation the gate reads now also splits at newlines for every arm that reads a command word, so a glob or tool word on a later line (`echo ok\nr? -rf x`) refuses instead of joining the first command's segment. Leading redirections (`&1`, `>> f`) are skipped to reach the command word, and a substitution body (`$($E pwned)`, backticks, `cat <($E pwned)`) is -judged as its own command position. +judged as its own command position, read quote-aware (`$($E "(")` closes where +bash closes it) with `$((…))` arithmetic left alone. `SECURITY.md` now says the same thing the README and the manual say: the refusals are a tripwire, `verify --sandbox` diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index c5a65a101..6aa6c16f4 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -1411,7 +1411,8 @@ variable (`E=curl; $E -o ./f http://host/x`), the braced (`${E}`) or quoted (`"$E"`) spelling, an `export`, an assignment prefix, a loop variable, the same spelling on its own line, after a leading redirection (`&1 $E`, `>> f $E`), or inside a substitution (`echo $($E pwned)`, backticks, -`cat <($E pwned)`), whose body is judged as its own command. The value may +`cat <($E pwned)`), whose body is judged as its own command — quotes included, +so `$($E "(")` is read the way bash reads it. The value may come from the command's own environment, so the gate refuses the shape instead of guessing what the variable holds. @@ -1419,8 +1420,8 @@ A wrapper's own option grammar decides which later word bash runs, so after `builtin`, `command`, `env`, `exec`, `nice`, `nohup`, `stdbuf`, `time`, `timeout`, or `xargs` any variable in that command refuses: `timeout 300 make test` stays clean, `timeout 300 make test $TARGET` does not. A shell counts -the same way when it carries an inline command (`sh -c`, `bash -c`, `bash -ec`): -`sh -c "$E pwned"` refuses. `eval` and `sudo` refuse earlier, as +the same way when it carries an inline command — any short-flag cluster with a +`c` in it (`sh -c`, `bash -c`, `bash -ec`, `sh -cu`): `sh -c "$E pwned"` refuses. `eval` and `sudo` refuse earlier, as `verify-destructive`, whatever their arguments. The gate is a tripwire, not a boundary. A variable holding a whole command diff --git a/evals/fixtures/verify-fuzz-corpus.tsv b/evals/fixtures/verify-fuzz-corpus.tsv index 7010272d7..a830e8488 100644 --- a/evals/fixtures/verify-fuzz-corpus.tsv +++ b/evals/fixtures/verify-fuzz-corpus.tsv @@ -93,6 +93,8 @@ F-R60 verify-indirection strict E=echo; builtin $E pwned F-R61 verify-indirection strict E=echo; sh -c "$E pwned" F-R62 verify-destructive strict E=echo; eval $E pwned F-R63 verify-destructive strict E=echo; sudo $E pwned +F-R64 verify-indirection strict E=echo; sh -cu "$E pwned" +F-R65 verify-indirection strict E=echo; echo $($E "(") F-X01 verify-networked strict wget --remote-name http://127.0.0.1:9/f F-X02 verify-networked strict curl -K /tmp/verify-fuzz-curlcfg http://127.0.0.1:9/ F-X03 verify-networked strict wget -i /tmp/verify-fuzz-wgeturls http://127.0.0.1:9/f @@ -156,3 +158,5 @@ F-P41 PASS strict echo `date` F-P42 PASS strict builtin echo ok F-P43 PASS strict diff <(sort fz-notes.txt) <(sort fz-notes.txt) F-P44 PASS strict bash ./fz-missing.sh $TARGET +F-P45 PASS strict echo $(( $X + 1 )) +F-P46 PASS strict echo $(printf "(%s)" x) diff --git a/internal/core/verify/gate_indirection.go b/internal/core/verify/gate_indirection.go index 2e51986a3..a1cfbf913 100644 --- a/internal/core/verify/gate_indirection.go +++ b/internal/core/verify/gate_indirection.go @@ -181,23 +181,64 @@ func parenAt(raw string, i int) bool { func parenBody(raw string, start int) (string, int, bool) { depth := 1 - for i := start; i < len(raw); i++ { - switch raw[i] { - case '(': - depth++ - case ')': - depth-- - if depth == 0 { - return raw[start:i], i, true - } + for i := start; i < len(raw); { + next, delta, ok := parenStep(raw, i) + if !ok { + return "", 0, false + } + depth += delta + i = next + if depth == 0 { + return raw[start : next-1], next - 1, true } } return "", 0, false } +func parenStep(raw string, i int) (int, int, bool) { + skip, ok := skipQuoted(raw, i) + if !ok { + return 0, 0, false + } + if skip > i { + return skip + 1, 0, true + } + switch raw[i] { + case '(': + return i + 1, 1, true + case ')': + return i + 1, -1, true + } + return i + 1, 0, true +} + +func skipQuoted(raw string, i int) (int, bool) { + switch raw[i] { + case '\\': + return i + 1, true + case '\'', '"': + return quotedEnd(raw, i) + } + return i, true +} + +func quotedEnd(raw string, i int) (int, bool) { + quote := raw[i] + for j := i + 1; j < len(raw); j++ { + if raw[j] == '\\' && quote == '"' { + j++ + continue + } + if raw[j] == quote { + return j, true + } + } + return i, false +} + var ( reAssignPrefix = regexp.MustCompile(`^[A-Za-z0-9_]+=`) rePureExpansion = regexp.MustCompile(`^(\$\{[A-Za-z_][A-Za-z0-9_]*\}|\$[A-Za-z_][A-Za-z0-9_]*)+$`) reRedirect = regexp.MustCompile(`^[0-9]*(<<<|<<|<>|>>|>\||>&|<&|&>>|&>|<|>)`) - reInlineCommand = regexp.MustCompile(`^-[a-z]*c$`) + reInlineCommand = regexp.MustCompile(`^-[a-z]*c[a-z]*$`) ) diff --git a/internal/core/verify/gate_indirection_test.go b/internal/core/verify/gate_indirection_test.go index 26140a051..db4b295bf 100644 --- a/internal/core/verify/gate_indirection_test.go +++ b/internal/core/verify/gate_indirection_test.go @@ -57,6 +57,8 @@ var variableCommandWords = []struct { {"process substitution", `E=echo; cat <($E pwned)`}, {"shell inline command", `E=echo; sh -c "$E pwned"`}, {"shell inline command clustered", `E=echo; bash -ec "$E pwned"`}, + {"shell inline command clustered suffix", `E=echo; sh -cu "$E pwned"`}, + {"quoted paren in substitution", `E=echo; echo $($E "(")`}, } func TestVerifyRefusesVariableCommandWord(t *testing.T) { @@ -95,6 +97,8 @@ func TestCheckAllowsVariableArguments(t *testing.T) { `cat Date: Mon, 16 Nov 2026 14:12:44 +0200 Subject: [PATCH 6/6] fix(verify): join continuations, keep quoted words, judge inline shells MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Seven findings from the PR #167 review, five of them real bypasses or false refusals reproduced against the built binary first. Backslash-newline continuations (`echo \` + newline + `"$USER"`) refused as `verify-indirection`: bash reads the pair as one command, while `normalize` kept the raw newline, so the argument started a segment of its own. The segmentation texts are now built from the command with each *unescaped* continuation removed; an escaped backslash before the newline still leaves a real separator. Quoted words lost their boundaries: `E=printf; LABEL="hello world" $E ok` and `E=printf; >"out file" $E ok` both executed, because de-quoting the text before splitting it put `world` in command position. The arm now reads a quote-preserving, unflattened text (`qseg`/`qsegExact`) with a quote-aware field splitter that strips the quotes but keeps the word whole, and `$'…'` stays a quoted literal rather than a `$`-sigil word. Single-quoted arguments are literals: `awk '{print $($NF)}' notes.txt` refused because the substitution scan read `$NF` out of the awk program. A single-quoted span is now skipped there, so only real substitutions are judged. A shell's inline command is judged as its own command: `sh -c "$E pwned"` refuses by finding `$E` in the command string rather than by refusing every variable after a shell, and `sh -c "echo $E"` stays clean. Command position now reads the segmented text instead of the flattened one, whose newline-to-space rewrite hid a later line from the `^|[;|&{]` anchor: `echo ok` + newline + `$(echo rm) -rf x` was accepted and now refuses. The remedy is honest: the gate runs before the sandbox wrapper, so `verify --sandbox` refuses a variable command word the same way. The hint and the refusal message drop the sandbox retry, and the tripwire passage stops offering `sh -c "$V"` (a shape the gate now refuses) for `sh "$V"`. Corpus: F-R66, F-R67 strict; F-P47..F-P50 pass (`awk`, a quoted assignment prefix, a quoted argument, `sh -c 'echo $TARGET'`). size: darwin/amd64 17 346 608, darwin/arm64 15 764 530, linux/amd64 17 047 712, linux/arm64 15 466 656, windows/amd64 17 436 672, windows/arm64 15 554 560 — four lines fell under their headroom floor, so each moves to the next 10 kB multiple (17.36M, 17.06M, 17.45M, 15.57M). --- .changeset/verify-indirection.md | 32 ++++--- docs/manual.md | 3 +- docs/troubleshooting.md | 17 ++-- evals/fixtures/verify-fuzz-corpus.tsv | 6 ++ internal/core/hint.go | 2 +- internal/core/testdata/hints.golden | 2 +- internal/core/verify/gate_destructive.go | 17 +++- internal/core/verify/gate_golden_test.go | 4 + internal/core/verify/gate_indirection.go | 90 +++++++++++++++++-- internal/core/verify/gate_indirection_test.go | 22 ++++- internal/core/verify/normalize.go | 42 ++++++++- internal/core/verify/testdata/gate.golden | 10 +++ scripts/size-baseline.txt | 8 +- 13 files changed, 216 insertions(+), 39 deletions(-) diff --git a/.changeset/verify-indirection.md b/.changeset/verify-indirection.md index ad230e184..b2e0d8ccf 100644 --- a/.changeset/verify-indirection.md +++ b/.changeset/verify-indirection.md @@ -6,8 +6,8 @@ The verify pre-exec gate refuses a command word it cannot read: `E=rm; $E -rf ./target` and `E=curl; $E -o ./f http://host/x` passed every arm because the tool word sat in the variable, and the resolved command then ran. `verify` now refuses the shape as `verify-indirection`, quoting the spelling it saw and -naming the remedy (write the tool word plainly, or run the step under -`verify --sandbox`). The braced (`${E}`), quoted (`"$E"`), `export`ed, +naming the remedy (write the tool word plainly; `verify --sandbox` refuses it +too, because the gate runs before the sandbox wrapper). The braced (`${E}`), quoted (`"$E"`), `export`ed, assignment-prefix, loop (`for E in curl; do $E ...`), concatenated (`$A$B`), and own-line (`E=rm\n$E -rf ./target`) spellings close with it, and so do the wrappers that run a later word (`env -u HOME $E`, `timeout -s KILL 5 $E`, @@ -18,21 +18,29 @@ so the false-positive boundary is unchanged for a command written plainly. The gate does not resolve variables — the value can come from the command's own environment, or from a later or conditional assignment — so it refuses rather -than guessing, and a variable holding a whole command line (`sh -c "$V"`) stays -a documented residual of the tripwire. A wrapper's own option grammar decides +than guessing, and a variable holding a whole command line (`sh "$V"`) stays a +documented residual of the tripwire. A wrapper's own option grammar decides which later word bash runs, so after `builtin`/`command`/`env`/`exec`/`nice`/ `nohup`/`stdbuf`/`time`/`timeout`/`xargs` any variable in that command refuses: that is the deliberate cost of not modelling each wrapper's flags, so `timeout 300 make test` stays clean while `timeout 300 make test $TARGET` refuses. A shell carrying an inline command (`sh -c "$E pwned"`, `bash -ec`, `sh -cu`) -counts the same way; `eval` and `sudo` refuse earlier as `verify-destructive`. -The segmentation the gate reads now also splits at newlines for every arm that -reads a command word, so a glob or tool word on a later line (`echo ok\nr? -rf -x`) refuses instead of joining the first command's segment. Leading -redirections (`&1`, `>> f`) are skipped to reach the command -word, and a substitution body (`$($E pwned)`, backticks, `cat <($E pwned)`) is -judged as its own command position, read quote-aware (`$($E "(")` closes where -bash closes it) with `$((…))` arithmetic left alone. +has that command string judged as its own command position, so +`sh -c "echo $E"` stays clean; `eval` and `sudo` refuse earlier as +`verify-destructive`. +The three arms that read a command word — glob fragments, indirection and +command position — now read the segmented text, which splits at newlines, so a +later line is a command position of its own: `echo ok\nr? -rf x` and +`echo ok\n$(echo rm) -rf x` refuse instead of joining the first segment. (The +whole-string greps already saw every line.) A `\`-newline continuation is one +command and joins before segmentation, so `echo \` + newline + `"$USER"` stays +clean. Leading redirections (`&1`, `>> f`) are skipped to reach +the command word; a quoted word stays one word, so `LABEL="hello world" $E ok` +and `>"out file" $E` refuse rather than putting `world` in command position; a +substitution body (`$($E pwned)`, backticks, `cat <($E pwned)`) is judged as its +own command position — quote-aware, so `$($E "(")` closes where bash closes it — +while `$((…))` arithmetic and single-quoted literals (`awk '{print $($NF)}'`) +are left alone. `SECURITY.md` now says the same thing the README and the manual say: the refusals are a tripwire, `verify --sandbox` diff --git a/docs/manual.md b/docs/manual.md index 8b8e33200..fe27e5b47 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -3164,7 +3164,8 @@ substitution (`$(...)`, backticks, `<(...)`), and behind a wrapper (`builtin`, `command`, `env`, `exec`, `nice`, `nohup`, `stdbuf`, `time`, `timeout`, `xargs`, or a shell carrying `-c`) whose own option grammar decides which word runs. The boundary is -the enforced sandbox below: `verify --sandbox` +the enforced sandbox below, which runs after this gate — a refused spelling +refuses with or without `--sandbox`: `verify --sandbox` runs the command under `bubblewrap` on Linux (the full mount and namespace shape) and under `sandbox-exec` on macOS (no network). A host that can enforce neither refuses `sandbox-unavailable` and runs nothing, so diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 6aa6c16f4..774fc1514 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -1421,12 +1421,17 @@ A wrapper's own option grammar decides which later word bash runs, so after `timeout`, or `xargs` any variable in that command refuses: `timeout 300 make test` stays clean, `timeout 300 make test $TARGET` does not. A shell counts the same way when it carries an inline command — any short-flag cluster with a -`c` in it (`sh -c`, `bash -c`, `bash -ec`, `sh -cu`): `sh -c "$E pwned"` refuses. `eval` and `sudo` refuse earlier, as -`verify-destructive`, whatever their arguments. - -The gate is a tripwire, not a boundary. A variable holding a whole command -line (`sh -c "$V"`, a file written and then run) is a shape it has not -learned. For a boundary, run the step under `verify --sandbox`. +`c` in it (`sh -c`, `bash -c`, `bash -ec`, `sh -cu`): the inline command string +is judged as its own command, so `sh -c "$E pwned"` refuses while +`sh -c "echo $E"` stays clean. `eval` and `sudo` refuse earlier, as +`verify-destructive`, whatever their arguments. A quoted word stays one word: +`LABEL="hello world" $E ok` and `>"out file" $E` refuse, because quote removal +would otherwise put `world` in command position. + +The gate is a tripwire, not a boundary, and it runs before the sandbox +wrapper: `verify --sandbox` refuses an indirection the same way, so the remedy +is to write the command plainly. A variable holding a whole command line +(`sh "$V"`, a file written and then run) is a shape the gate has not learned. Fix: write the tool word plainly (`E=curl; $E -o ./f http://host/x` becomes `curl -o ./f http://host/x`, or the test runner directly) and keep variables diff --git a/evals/fixtures/verify-fuzz-corpus.tsv b/evals/fixtures/verify-fuzz-corpus.tsv index a830e8488..1ab72c420 100644 --- a/evals/fixtures/verify-fuzz-corpus.tsv +++ b/evals/fixtures/verify-fuzz-corpus.tsv @@ -95,6 +95,8 @@ F-R62 verify-destructive strict E=echo; eval $E pwned F-R63 verify-destructive strict E=echo; sudo $E pwned F-R64 verify-indirection strict E=echo; sh -cu "$E pwned" F-R65 verify-indirection strict E=echo; echo $($E "(") +F-R66 verify-indirection strict E=printf; LABEL="hello world" $E ok +F-R67 verify-indirection strict E=printf; >"out file" $E ok F-X01 verify-networked strict wget --remote-name http://127.0.0.1:9/f F-X02 verify-networked strict curl -K /tmp/verify-fuzz-curlcfg http://127.0.0.1:9/ F-X03 verify-networked strict wget -i /tmp/verify-fuzz-wgeturls http://127.0.0.1:9/f @@ -160,3 +162,7 @@ F-P43 PASS strict diff <(sort fz-notes.txt) <(sort fz-notes.txt) F-P44 PASS strict bash ./fz-missing.sh $TARGET F-P45 PASS strict echo $(( $X + 1 )) F-P46 PASS strict echo $(printf "(%s)" x) +F-P47 PASS strict awk '{print $($NF)}' fz-notes.txt +F-P48 PASS strict LABEL="hello world" printf ok +F-P49 PASS strict echo "hello $USER" +F-P50 PASS strict sh -c 'echo $TARGET' diff --git a/internal/core/hint.go b/internal/core/hint.go index d08e84c49..61abab68b 100644 --- a/internal/core/hint.go +++ b/internal/core/hint.go @@ -206,7 +206,7 @@ var hintRules = []HintRule{ "or piping env into the network, then re-run"}, {codes: []string{"verify-repo-escape"}, hint: "try: rewrite verifyCmd with repo-relative paths (/tmp for scratch), then re-run"}, {codes: []string{"verify-indirection"}, hint: "try: write the tool word plainly instead of " + - "$VAR, or run the step under verify --sandbox, then re-run"}, + "$VAR, then re-run"}, {codes: []string{"sandbox-unavailable"}, hint: "try: on Linux install bubblewrap and allow unprivileged " + "user namespaces; on macOS sandbox-exec ships with the OS — or re-run without --sandbox on a command you trust"}, {codes: []string{"agent-missing"}, hint: "try: install the agent CLI (or put it on PATH), or re-run with " + diff --git a/internal/core/testdata/hints.golden b/internal/core/testdata/hints.golden index 5d48742e7..9508e1097 100644 --- a/internal/core/testdata/hints.golden +++ b/internal/core/testdata/hints.golden @@ -222,7 +222,7 @@ validation-stale try: re-run the validation pass against the current head, or de verify-credential-read try: rewrite verifyCmd without reading credential paths or piping env into the network, then re-run verify-destructive try: rewrite verifyCmd to stay in-repo and non-destructive (run the test runner, not rm/mkfs/dd), then re-run verify-failed try: address the cause above, then re-run the same command -verify-indirection try: write the tool word plainly instead of $VAR, or run the step under verify --sandbox, then re-run +verify-indirection try: write the tool word plainly instead of $VAR, then re-run verify-networked try: rewrite verifyCmd without network fetches or pipe-to-shell (fetch fixtures first, run offline), then re-run verify-repo-escape try: rewrite verifyCmd with repo-relative paths (/tmp for scratch), then re-run window-too-large try: narrow --since (a shorter span or a later ref), then re-run diff --git a/internal/core/verify/gate_destructive.go b/internal/core/verify/gate_destructive.go index 16b4b0deb..9ce030a85 100644 --- a/internal/core/verify/gate_destructive.go +++ b/internal/core/verify/gate_destructive.go @@ -31,22 +31,22 @@ func checkSentinel(g *gateText) *core.RefusalError { } func checkCommandPosition(g *gateText) *core.RefusalError { - if frag := grepO(reCmdPosDest, g.bare); frag != "" { + if frag := segmentFrag(reCmdPosDest, g.seg); frag != "" { return refuse(codeDestructive, frag, cmdPosBuilds+ "destructive tool word — write the tool word plainly") } - if frag := grepO(reCmdPosNet, g.bare); frag != "" { + if frag := segmentFrag(reCmdPosNet, g.seg); frag != "" { return refuse(codeNetworked, frag, cmdPosBuilds+ "networked tool word — fetch fixtures first, run offline") } - if frag := grepO(reCmdPosDestBt, g.scan); frag != "" { + if frag := segmentFrag(reCmdPosDestBt, g.qseg); frag != "" { return refuse(codeDestructive, frag, cmdPosBuilds+ "destructive tool word — write the tool word plainly") } - if frag := grepO(reCmdPosNetBt, g.scan); frag != "" { + if frag := segmentFrag(reCmdPosNetBt, g.qseg); frag != "" { return refuse(codeNetworked, frag, cmdPosBuilds+ "networked tool word — fetch fixtures first, run offline") @@ -54,6 +54,15 @@ func checkCommandPosition(g *gateText) *core.RefusalError { return nil } +func segmentFrag(re *regexp.Regexp, text string) string { + for _, seg := range segments(text) { + if frag := grepO(re, seg); frag != "" { + return frag + } + } + return "" +} + func checkGlobFragment(g *gateText) *core.RefusalError { segw := segWords(g.seg) if !hasGlobWord(segw) { diff --git a/internal/core/verify/gate_golden_test.go b/internal/core/verify/gate_golden_test.go index fb88e0095..5d17f415f 100644 --- a/internal/core/verify/gate_golden_test.go +++ b/internal/core/verify/gate_golden_test.go @@ -189,6 +189,10 @@ func gateOnlyExtras() [][2]string { "echo ok\n$E -rf ./target", "E=rm\necho ok\n$E -rf ./target", "echo ok\r\n$E -rf ./target", + "echo \\\n \"$USER\"", + `awk '{print $($NF)}' notes.txt`, + "echo ok\n$(echo rm) -rf ./fz-target", + "echo ok\n$(echo curl) -o ./f", } var out [][2]string for i, c := range cmds { diff --git a/internal/core/verify/gate_indirection.go b/internal/core/verify/gate_indirection.go index a1cfbf913..c3cbac89f 100644 --- a/internal/core/verify/gate_indirection.go +++ b/internal/core/verify/gate_indirection.go @@ -10,8 +10,7 @@ import ( const codeIndirection = "verify-indirection" -const msgIndirection = "a command word built from a variable hides the tool — write the tool " + - "word plainly, or run the step under verify --sandbox" +const msgIndirection = "a command word built from a variable hides the tool — write the tool word plainly" var ( cmdKeywords = map[string]bool{ @@ -47,11 +46,11 @@ func indirectionIn(g *gateText, depth int) *core.RefusalError { } func indirectText(g *gateText) string { - word := indirectWord(g.seg) + word := indirectWord(g.qseg) if word == "" { return "" } - if exact := indirectWord(g.segExact); exact != "" { + if exact := indirectWord(g.qsegExact); exact != "" { return exact } return word @@ -82,7 +81,7 @@ func commandIndex(words []string) int { } func segmentIndirection(seg string) string { - words := strings.Fields(seg) + words := shellFields(seg) i := commandIndex(words) if i >= len(words) { return "" @@ -91,7 +90,13 @@ func segmentIndirection(seg string) string { word := strings.Trim(words[i], "()") // A wrapper's own option grammar decides which later word bash runs, so // after one every expansion refuses rather than being guessed apart. - if cmdWrappers[word] || cmdShells[word] && hasInlineCommand(rest) { + if cmdWrappers[word] { + return firstIndirect(rest) + } + if cmdShells[word] && hasInlineCommand(rest) { + if hidden := inlineCommandWord(rest); hidden != "" { + return hidden + } return firstIndirect(rest) } if isIndirectWord(words[i]) { @@ -100,6 +105,60 @@ func segmentIndirection(seg string) string { return "" } +func shellFields(text string) []string { + var out []string + var cur strings.Builder + inWord := false + for i := 0; i < len(text); { + next, breaking := shellStep(&cur, text, i) + if breaking && inWord { + out = append(out, cur.String()) + cur.Reset() + inWord = false + } else if !breaking { + inWord = true + } + i = next + } + if inWord { + out = append(out, cur.String()) + } + return out +} + +func shellStep(cur *strings.Builder, text string, i int) (int, bool) { + switch text[i] { + case ' ', '\t', '\v', '\f': + return i + 1, true + case '$': + if i+1 < len(text) && (text[i+1] == '\'' || text[i+1] == '"') { + return appendQuoted(cur, text, i+1) + 1, false + } + cur.WriteByte(text[i]) + return i + 1, false + case '\'', '"': + return appendQuoted(cur, text, i) + 1, false + } + cur.WriteByte(text[i]) + return i + 1, false +} + +func appendQuoted(cur *strings.Builder, text string, i int) int { + quote := text[i] + for j := i + 1; j < len(text); j++ { + if text[j] == '\\' && quote == '"' && j+1 < len(text) { + j++ + cur.WriteByte(text[j]) + continue + } + if text[j] == quote { + return j + } + cur.WriteByte(text[j]) + } + return len(text) +} + func firstIndirect(words []string) string { for _, word := range words { if isIndirectWord(word) { @@ -109,6 +168,15 @@ func firstIndirect(words []string) string { return "" } +func inlineCommandWord(words []string) string { + for i, word := range words { + if reInlineCommand.MatchString(word) && i+1 < len(words) { + return indirectWord(words[i+1]) + } + } + return "" +} + func hasInlineCommand(words []string) bool { return slices.ContainsFunc(words, reInlineCommand.MatchString) } @@ -149,10 +217,20 @@ func substitutionAt(raw string, i int) (string, int, bool) { return processBody(raw, i) case '`': return backtickBody(raw, i) + case '\'': + return literalBody(raw, i) } return "", 0, false } +func literalBody(raw string, i int) (string, int, bool) { + end, ok := quotedEnd(raw, i) + if !ok { + return "", 0, false + } + return "", end, true +} + func dollarBody(raw string, i int) (string, int, bool) { if !parenAt(raw, i+1) || parenAt(raw, i+2) { return "", 0, false diff --git a/internal/core/verify/gate_indirection_test.go b/internal/core/verify/gate_indirection_test.go index db4b295bf..a4a8bae3f 100644 --- a/internal/core/verify/gate_indirection_test.go +++ b/internal/core/verify/gate_indirection_test.go @@ -59,6 +59,10 @@ var variableCommandWords = []struct { {"shell inline command clustered", `E=echo; bash -ec "$E pwned"`}, {"shell inline command clustered suffix", `E=echo; sh -cu "$E pwned"`}, {"quoted paren in substitution", `E=echo; echo $($E "(")`}, + {"single-quoted paren in substitution", `E=printf; echo "$($E '(')"`}, + {"continuation into command word", "E=rm; \\\n$E -rf ./target"}, + {"quoted assignment value", `E=printf; LABEL="hello world" $E ok`}, + {"quoted redirect target", `E=printf; >"out file" $E ok`}, } func TestVerifyRefusesVariableCommandWord(t *testing.T) { @@ -99,6 +103,10 @@ func TestCheckAllowsVariableArguments(t *testing.T) { `echo $(date)`, `echo $(printf "(%s)" x)`, `echo $(( $X + 1 ))`, + "echo \\\n \"$USER\"", + `awk '{print $($NF)}' fz-notes.txt`, + `LABEL="hello world" printf ok`, + `echo "hello $USER"`, "echo `date`", `diff <(sort a) <(sort b)`, `builtin echo ok`, @@ -126,6 +134,18 @@ func TestCheckRefusesGlobWordAfterNewline(t *testing.T) { "into a hidden tool word — write the tool word plainly)") } +func TestCheckRefusesCommandPositionAfterNewline(t *testing.T) { + wantRefusal(t, "echo ok\n$(echo rm) -rf ./fz-target", "verify-destructive", + "refusing verifyCmd: matched '$(echo rm)' (command substitution in command position "+ + "builds a destructive tool word — write the tool word plainly)") + wantRefusal(t, "echo ok\n$(echo curl) -o ./f", "verify-networked", + "refusing verifyCmd: matched '$(echo curl)' (command substitution in command position "+ + "builds a networked tool word — fetch fixtures first, run offline)") + if ref := verifycore.Check("echo ok\n$(echo date)"); ref != nil { + t.Fatalf("Check = %s: %q, want CLEAN", ref.Code, ref.Message) + } +} + func TestCheckRefusesEvalAndSudoBeforeTheWrapperRule(t *testing.T) { wantRefusal(t, `E=echo; eval $E pwned`, "verify-destructive", "refusing verifyCmd: matched ' eval ' (eval with non-literal args refuses — fail closed)") @@ -139,7 +159,7 @@ func TestCheckVariableCommandWordMessage(t *testing.T) { t.Fatal("Check = nil, want verify-indirection") } want := "refusing verifyCmd: matched '$E' (a command word built from a variable hides " + - "the tool — write the tool word plainly, or run the step under verify --sandbox)" + "the tool — write the tool word plainly)" if ref.Message != want { t.Fatalf("message = %q\nwant %q", ref.Message, want) } diff --git a/internal/core/verify/normalize.go b/internal/core/verify/normalize.go index ab8833dea..21b0e1983 100644 --- a/internal/core/verify/normalize.go +++ b/internal/core/verify/normalize.go @@ -16,6 +16,8 @@ type gateText struct { csbare string seg string segExact string + qseg string + qsegExact string adj string deepSubst bool unreduced bool @@ -32,15 +34,49 @@ func normalize(raw string) *gateText { g.adj, g.deepSubst = adjOf(g.scan) g.unreduced = gluedSpan(g.adj) // The flattened texts lose the newline command separator, and the gate - // reads one command per line: keep an unflattened pair for segmentation. + // reads one command per line: keep unflattened texts for segmentation — + // a de-quoted pair for whole-text greps, a quote-preserving pair for the + // arms that must keep one shell word one word. + joined := joinContinuations(raw) g.seg, g.segExact = g.bare, g.csbare + g.qseg, g.qsegExact = g.scan, scanOf(raw) if strings.ContainsAny(raw, "\n\r") { - g.seg = bareOf(scanOf(readcommon.ASCIILower(raw)), false) - g.segExact = bareOf(scanOf(raw), true) + g.seg = bareOf(scanOf(readcommon.ASCIILower(joined)), false) + g.segExact = bareOf(scanOf(joined), true) + g.qseg = scanOf(readcommon.ASCIILower(joined)) + g.qsegExact = scanOf(joined) } return g } +// joinContinuations drops each unescaped backslash-newline pair: bash reads it +// as a line continuation, not as a command separator. +func joinContinuations(raw string) string { + if !strings.Contains(raw, "\\\n") { + return raw + } + var sb strings.Builder + sb.Grow(len(raw)) + i := 0 + for i < len(raw) { + if raw[i] == '\\' && i+1 < len(raw) && raw[i+1] == '\n' && !escapedBackslash(raw, i) { + i += 2 + continue + } + sb.WriteByte(raw[i]) + i++ + } + return sb.String() +} + +func escapedBackslash(raw string, i int) bool { + n := 0 + for j := i - 1; j >= 0 && raw[j] == '\\'; j-- { + n++ + } + return n%2 == 1 +} + func segments(text string) []string { var out []string start := 0 diff --git a/internal/core/verify/testdata/gate.golden b/internal/core/verify/testdata/gate.golden index 54aca8d10..da208683b 100644 --- a/internal/core/verify/testdata/gate.golden +++ b/internal/core/verify/testdata/gate.golden @@ -260,6 +260,8 @@ F-R62 verify-destructive F-R63 verify-destructive F-R64 verify-indirection F-R65 verify-indirection +F-R66 verify-indirection +F-R67 verify-indirection F-X01 verify-networked F-X02 verify-networked F-X03 verify-networked @@ -325,6 +327,10 @@ F-P43 CLEAN F-P44 CLEAN F-P45 CLEAN F-P46 CLEAN +F-P47 CLEAN +F-P48 CLEAN +F-P49 CLEAN +F-P50 CLEAN G000 CLEAN G001 CLEAN G002 CLEAN @@ -343,6 +349,10 @@ G014 CLEAN G015 verify-indirection G016 verify-indirection G017 verify-indirection +G018 CLEAN +G019 CLEAN +G020 verify-destructive +G021 verify-networked M000 verify-destructive MU000 verify-destructive MB000 verify-destructive diff --git a/scripts/size-baseline.txt b/scripts/size-baseline.txt index b2ac05faf..a95da968d 100644 --- a/scripts/size-baseline.txt +++ b/scripts/size-baseline.txt @@ -1,7 +1,7 @@ deps 0 -darwin/amd64 17350000 +darwin/amd64 17360000 darwin/arm64 15790000 -linux/amd64 17050000 +linux/amd64 17060000 linux/arm64 15540000 -windows/amd64 17440000 -windows/arm64 15560000 +windows/amd64 17450000 +windows/arm64 15570000