diff --git a/.changeset/verify-indirection.md b/.changeset/verify-indirection.md new file mode 100644 index 000000000..b2e0d8ccf --- /dev/null +++ b/.changeset/verify-indirection.md @@ -0,0 +1,48 @@ +--- +"@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; `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`, +`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 "$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`) +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` +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..fe27e5b47 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,15 @@ 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 — including on a +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, 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 @@ -3283,7 +3292,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..774fc1514 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -1404,6 +1404,39 @@ 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, 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 — 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. + +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 — any short-flag cluster with a +`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 +for arguments of a command written plainly, 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..1ab72c420 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,36 @@ 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-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-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-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 @@ -114,3 +145,24 @@ 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 +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 +F-P39 PASS strict cat /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", + "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 new file mode 100644 index 000000000..c3cbac89f --- /dev/null +++ b/internal/core/verify/gate_indirection.go @@ -0,0 +1,322 @@ +package verifycore + +import ( + "regexp" + "slices" + "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" + +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{ + "builtin": true, "command": true, "env": true, "exec": true, "nice": true, + "nohup": true, "stdbuf": true, "time": true, "timeout": true, "xargs": true, + } + cmdShells = map[string]bool{ + "bash": true, "dash": true, "fish": true, "ksh": true, "sh": true, "zsh": true, + } +) + +func checkIndirection(g *gateText) *core.RefusalError { + return indirectionIn(g, 0) +} + +func indirectionIn(g *gateText, depth int) *core.RefusalError { + if word := indirectText(g); word != "" { + return refuse(codeIndirection, word, msgIndirection) + } + if depth == maxSentinelPasses { + return nil + } + for _, body := range substitutionBodies(g.raw) { + if ref := indirectionIn(normalize(body), depth+1); ref != nil { + return ref + } + } + return nil +} + +func indirectText(g *gateText) string { + word := indirectWord(g.qseg) + if word == "" { + return "" + } + if exact := indirectWord(g.qsegExact); exact != "" { + return exact + } + return word +} + +func indirectWord(text string) string { + for _, seg := range segments(text) { + if word := segmentIndirection(seg); word != "" { + return word + } + } + return "" +} + +func commandIndex(words []string) int { + i := 0 + for i < len(words) { + if next, ok := skipRedirect(words, i); ok { + i = next + continue + } + if !reAssignPrefix.MatchString(words[i]) && !cmdKeywords[words[i]] { + return i + } + i++ + } + return i +} + +func segmentIndirection(seg string) string { + words := shellFields(seg) + i := commandIndex(words) + if i >= 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[word] { + return firstIndirect(rest) + } + if cmdShells[word] && hasInlineCommand(rest) { + if hidden := inlineCommandWord(rest); hidden != "" { + return hidden + } + return firstIndirect(rest) + } + if isIndirectWord(words[i]) { + return words[i] + } + 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) { + return word + } + } + 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) +} + +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) + 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 + } + 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); { + 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[a-z]*$`) +) diff --git a/internal/core/verify/gate_indirection_test.go b/internal/core/verify/gate_indirection_test.go new file mode 100644 index 000000000..a4a8bae3f --- /dev/null +++ b/internal/core/verify/gate_indirection_test.go @@ -0,0 +1,169 @@ +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 )`}, + {"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`}, + {"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"`}, + {"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) { + 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) + } +} + +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'`, + `cat = 0 && raw[j] == '\\'; j-- { + n++ + } + return n%2 == 1 +} + +func segments(text string) []string { + var out []string + start := 0 + for i := range len(text) { + if !isSegmentBreak(text, i) { + continue + } + if seg := text[start:i]; seg != "" { + out = append(out, seg) + } + start = i + 1 + } + if seg := text[start:]; seg != "" { + out = append(out, seg) + } + return out +} + +// isSegmentBreak reports a command separator. The `&` of a fd-duplication +// (`2>&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 { 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 a7adaa54e..da208683b 100644 --- a/internal/core/verify/testdata/gate.golden +++ b/internal/core/verify/testdata/gate.golden @@ -232,6 +232,36 @@ 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-R46 verify-indirection +F-R47 verify-indirection +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-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 @@ -280,6 +310,27 @@ F-P26 CLEAN F-P27 CLEAN F-P28 CLEAN F-P29 CLEAN +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 +F-P39 CLEAN +F-P40 CLEAN +F-P41 CLEAN +F-P42 CLEAN +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 @@ -295,6 +346,13 @@ G011 CLEAN G012 CLEAN G013 CLEAN 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 3916d4731..a95da968d 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 -linux/arm64 15480000 -windows/amd64 17430000 -windows/arm64 15550000 +darwin/amd64 17360000 +darwin/arm64 15790000 +linux/amd64 17060000 +linux/arm64 15540000 +windows/amd64 17450000 +windows/arm64 15570000