Skip to content

fix(security): refuse a mint whose verb comes from a quoted substitution - #37077

Open
jeeshofone wants to merge 1 commit into
kirodotdev:mainfrom
jeeshofone:fix/36961-mint-subst-quote
Open

jeeshofone wants to merge 1 commit into
kirodotdev:mainfrom
jeeshofone:fix/36961-mint-subst-quote

Conversation

@jeeshofone

@jeeshofone jeeshofone commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: The credential-mint guard refuses kirocrew token when the subcommand comes from a command substitution whose body holds a ' inside double quotes.

is_denied("T=$(: \"'\"; echo token); kirocrew $T") returns None on main, and so does the ${T} variant. Bash reads that ' as a plain character, so the substitution prints token and kirocrew token runs. Main's resolver reads dequoted tokens and treats the lone ' as an open quote. Its program scan then runs past the substitution's real close into the use site and binds $T to kirocrew, so the use resolves to kirocrew kirocrew. That has no verb, so the mint is allowed.

Why it matters

The mint prints a signed dashboard access URL. An agent that gets one past the guard escalates out of its own session.

Not a goal

  • Computing what a substitution prints. The floor binds on the verb or the program a substitution body NAMES, never on whether bash would capture it. A body that computes the verb without naming it (printf %s%s tok en, echo tokem | tr m n) stays allowed, as on main.
  • Changing main's reader. Main's resolution and its verdict are kept exactly as they are, including where main over-reads (see the residual table).
  • Refusing the mint where it actually RUNS rather than in this static text reader (Design Review's first note). Binding on a named verb or program is an approximation by construction, and moving the decision to execution is a separate, larger change. It is a follow-up, not this PR.
  • Distinguishing sudo --edit / sudo -e from sudo -E. sudo --edit/-e with a mint-shaped operand is refused, because the floor lowercases before the walk and cannot tell -e (sudoedit, runs no program) from -E (--preserve-env, which does run the command); refusing is the safe side, and threading original-case text through the lowercasing layer is a change to the floor's input contract that does not belong here.

Not covered (same verdict as main)

The subcommand-from-substitution reading applies only when the command's program token (argv[0], after resolution) is the product CLI (the Security Scope Review / F3 rule). A product name carried as another program's argument keeps main's verdict. The computed-verb and here-string residuals (see "Not a goal" and the residual table) stay allowed, identical to origin/main.

An assignment inside a case arm or a { …; } command group — case x in x) T=$(: "'"; echo token);; esac; kirocrew $T, { T=$(: "'"; echo token); }; kirocrew $T — is allowed on origin/main and here. The raw word walk treats a {/( group as a non-current scope (main's resolver has no scope notion), so a substitution-valued assignment inside one is not read; the same holds for an uncalled function body (f(){ T=$(...); }, function f { ...; }). Catching these needs per-construct scope tracking that repeatedly mis-read exotic shell grammar in review, so the r26 scope tracking is reverted to the r25 depth-based reading and these are left exactly as origin/main (see the follow-up issue).

A leading clobber redirect — 2>| /dev/null kirocrew "$(printf token)" — is allowed on origin/main and here. _program_positions reads dequoted tokens, where the clobber operator's >| is byte-identical to a quoted > beside a pipe, so it cannot tell them apart without the raw text's quote state; >| behaviour is therefore left unchanged from origin/main. A quote-aware fix belongs in its own PR (follow-up issue filed with the trigger, the dequoted-walk limitation, and the fix shape).

A wrapper or command keyword in front of the CLI (env kirocrew "$(printf token)", sudo, command, exec, nohup, time, timeout 5, !, if …; then) is now COVERED, not a residual. An earlier round declared it out of scope on the rationale that "the floor has no shared wrapper-unwrapping resolver to reuse"; that was wrong — argv_rules._program_index already steps over the same _WRAPPERS table and command keywords. The wrapper table and that skip now live in shell_normalizer_walk (_WRAPPERS, _program_index), and _program_positions reuses them (one definition, no second copy), so kirocrew behind a wrapper takes program position and the quoted-substitution mint is refused. The widening is only the mint shape: a wrapper running a harmless subcommand (env kirocrew status, sudo kirocrew doctor) still keeps main's allow, and the plain-verb form (env kirocrew token) was already refused on both main and head by the position-agnostic bare-verb reading.

A command built of never-closing command substitutions — a=$(: #); a=$(: #); …; kirocrew doctor, where each # swallows the ) so the substitution never closes — makes the BASELINE reader (_resolve_local_assignments → _mint_verb_in_substitution → _substitution_bodies) hand each of N frames the whole remainder and rescan it per assignment: O(n³) in the command length. This is PRE-EXISTING on origin/main: is_denied takes main 10.7s at a ~2 KiB command (reps=200). This head is slower only because r30's fail-closed reading (below) pairs every never-closing assignment, so the gate is 15.3s at 2 KiB; both main and this head still exceed 180s at ~8 KiB because of main's reader. The line is also syntactically incomplete in bash (an unterminated substitution never runs). This PR does not change main's reader; the alternative pass it adds is NOT the dominant cost — its share of the gate is flat across sizes (gate 2.3s / 15.3s / 112.7s and alt-pass share 29% / 31% / 31% at 1 / 2 / 4 KiB), the same order as the baseline, with main's reader still dominating (pinned by test_the_alternative_pass_is_a_small_share_of_the_gate, bound < 50%). A cost shortcut that skipped the remainder read for unproven bodies was tried and reverted — it dropped refusals bash closes (a folded-newline literal F=$(: # )⏎printf …); $F token, and the lettered-glob self-program spellings kirocre[w]/k* that only the alternative reads, with no baseline backstop), now pinned refused by test_a_bash_closable_unprovable_body_mint_stays_refused. The baseline cubic is tracked as a main-side DoS in #39617.

Residual (over-refusal, fail-closed). F=$(: "'"; printf %s ./kirocre[w]; bash -c "$F token"); true is REFUSED here, allowed on origin/main and on the pushed head 571d9e9e07f4:

ref verdict
origin/main allowed
pushed 571d9e9e07f4 allowed
this head REFUSED

Mechanism: the pre-existing lone-quote tokenizer defect mis-surfaces the initializer's own nested bash -c as a top-level carrier, so the program-only carry seeds the binding into its own initializer — where $F is in fact unset while that RHS runs, so bash never mints. This is fail-closed (a refusal of a non-executable command, the direction the repo's floors err), and a clean exclusion needs the quote boundary the defect obscures; it is left as a residual and pinned REFUSED by test_the_self_initializer_carry_is_a_documented_over_refusal so a later flip is visible.

What changed (motivation → approach → change)

Root cause: a lone dequoted quote stops main's quote-aware close walk from finding the substitution's real close. The program scan then over-reads into the use site and binds the use-site program.

Approach: keep main's resolution untouched and add a second resolution. The command is refused if EITHER resolution refuses.

  • Baseline: _resolve_local_assignments is main's code, unchanged. Its binding and verdict stand.
  • Alternative (shell_normalizer._alternative_mint_overrides, _raw_substitution_mint_name; the raw word walk _raw_word_scopes lives in shell_normalizer_walk): for the assignments the baseline itself binds, read each one's own substitution body from the folded raw source, where its quotes still close. Only an assignment that sets a variable in this shell is read. That means one at a command's prefix position (no program yet in its argv) and at top level in the raw text: not inside a subshell, a substitution, a brace group or a function body (glued f(){ ... } included), and not an argument such as echo T=$(...). Main's resolver has no notion of scope, so this check is what keeps the alternative's binding in the right frame. Anything the check cannot classify is left unpaired and keeps main's binding. The body comes from the same quote-aware readers the baseline uses (_matching_close_paren, _matching_close_backtick), and is tokenized as a command, so a quoted "it's token" stays one operand. A protected program named anywhere in it wins (product name, by-name kill program, or a glob whose basename has a letter, such as kirocre[w] or kiro*; a bare * or test/* is a filename pattern), else the verb, else nothing. Each assignment takes its own reading, so a later T=$(echo doctor) does not inherit an earlier T's verb. A name in a ${...} prefix or in a later command is never read as the body's own.
  • Union (_shell_payload_walk): when the alternative binds anything, the walk appends a second frame resolved with those bindings and descends that frame's nested payloads too, CARRYING the frame's bindings (the values and the program-only set) into the inner walk and resolving each program-only name at the INNER payload's program positions. So a program-only binding reaches a mint nested in a bash -c/sh -c or wrapper payload: F=$(: "'"; printf %s ./kirocre[w]); bash -c "$F token" (also sh -c, and behind env/sudo/timeout 5) is refused, because $F is at the -c script's own program position though it was a -c operand in the parent; a data operand (bash -c "echo $F") stays unexpanded (the fork GPT lane's r28 F1). The baseline frame is never dropped or rewritten, so the floor refuses a superset of what main refuses. A command with no substitution-valued assignment builds no second frame and pays nothing. The alternative reads one body per assignment, so many substitution assignments cost what main already pays (test_the_alternative_reads_each_body_once).

Dead code from earlier rounds is gone: _substitution_body_span, _substitution_quote_unbalanced, _unbalanced_substitution_binding and the quote-blind paren walk.

Program-position scope (Security Scope Review / F3). The subcommand-from-substitution reading fires only when the self name is the command's program word (argv[0], after resolution), so a product name carried as another program's argument keeps main's verdict: a repo slug (gh ... --repo kirodotdev/KiroCrew --body "$(gh auth token)"), a secret/service/container name (kubectl ... --from-literal=X="$(...)", security ... -s kirocrew ... -w "$(...)", docker run --name kirocrew ... -e X="$(...)"), or a glob-bound variable used as an operand (TESTS=$(git ls-files 'test/*crew*'); pytest $TESTS -k token). A new _program_positions (in shell_normalizer_walk, re-exported) marks argv[0] with the same command-boundary walk as _argv_programs, skipping a leading redirection (which the shell strips from argv, so >/dev/null kirocrew "$(printf token)" still reads the CLI as the program). The alternative's own prefix test — whether a substitution-valued assignment precedes any program in its command — walks that same redirect-aware walk rather than _argv_programs, which reads a leading fd-prefixed redirect (2>, 1>&2, &>, {fd}>) as a program named 2/1/&/{fd}; so 2>/dev/null T=$(: "'"; echo token); kirocrew $T keeps the assignment at prefix and is refused, as the plain >/dev/null form already was. That prefix test runs only in the alternative pass, so the baseline is unchanged from main. _program_positions recognises a redirection only when the token BEGINS with one (_push_token_redirection: an fd number / & / {name} prefix then the operator), so a program with a redirect GLUED ON (kirocrew>/dev/null "$(printf token)", kirocrew>&2 ...) keeps program position and its basename resolves to the CLI, and the mint is refused; a digit glued to the word (kirocrew2>/dev/null) is part of the program name, so bash runs kirocrew2 and it stays allowed, as on main. _program_positions finds the program through the shared _program_index walk (relocated to shell_normalizer_walk with the _WRAPPERS table), so it also steps over wrappers and command keywords (env, sudo, exec, nohup, timeout, !, if): a kirocrew behind one of them takes program position and the quoted-substitution mint is refused, while a harmless wrapped subcommand keeps main's allow. The alternative resolution honours a program-name binding only at a program-position use, through one gate applied to all three reference forms (plain $VAR, transform ${V:0}, indirect ${!V}) at both the use site and the alias site, with the program-only provenance re-derived from each variable's CURRENT binding alone and never inherited from an earlier one: a shared _set_program_only adds the program-only flag for a program-name binding and discards it for anything else, called at every binding site (override, append, computed program/verb, literal), so a variable bound to the program name and then rebound to the mint verb loses the flag and the operand gate resolves it (the fork GPT lane's F1). The transform/indirect resolution added at the alias site runs only in the alternative pass, so the baseline stays unchanged from main. _program_basename and _argv_programs were moved whole into shell_normalizer_walk (re-exported) to keep shell_normalizer under its 2,000-line cap; both are pure relocations.

Appends and empty assignments (+=). A += append and an empty assignment read the same in the baseline and the alternative pass: += concatenates from the variable's current binding and an empty assignment records nothing, exactly as origin/main. A += that assembles the verb from known literals is refused by that concatenation on both main and head (P=tok; P+=en; kirocrew $P, P+=token, P=; P+=token, P=kiro; P+=crew; $P token). A += whose base or tail is unknown — an unset name, or an opaque substitution such as P+=$($Q) — has no literal to concatenate, so neither pass isolates the verb and the command keeps main's verdict: allowed. The guard's target is a mint whose subcommand comes from a substitution, not one assembled across an opaque append. An earlier round modelled the opaque append (a fail-closed verb binding) and a hand-copied CLI subcommand tree to refuse these spellings; that machinery is removed here, because it refused rows origin/main allows and made the mint-name read quadratic. The mint-name read is now linear: one lookup per $-use into the precomputed program positions, with no rescan of the resolved prefix.

Why five security modules change

_program_basename is defined in shell_normalizer_walk, but _EXPORTS named shell_normalizer as its home. So a patch of security._program_basename wrote the reader's re-export, while shell_normalizer_walk._argv_programs read its own binding, and test_security_split_patch_reach failed (Backend Tests shard 5). _EXPORTS now names shell_normalizer_walk, and every read goes there: shell_normalizer, argv_floor_self, argv_rules, rm_floor and s3_direction (plus acp/batch_guard.py, the one reader outside the package). Each call site changes only the module it reads the name from. The function is the same, and the floor's verdicts are the same: deny_diff 0, and 0 flips in either direction over the 20,736-command oracle corpus against the previous head.

Also in the diff (from earlier review rounds, unchanged here):

  • argv_floor_self._is_credential_mint reads a quoted "$(...)" or backtick argument that names the verb where it can be the subcommand. It applies behind a product-name-spelling program word, but not to a redirect target or an interpreter-flag operand, and a glob-only word like * stays allowed.
  • argv_floor.py lists _REDIRECT_OP_PREFIX in the argv_floor_self mirror manifest.
  • The refusal note names the way out: write the verb as a plain word outside $( ) or backticks, or move the mint out of the substituted variable.

docs/system-specs/modules/security.md describes the two resolutions.

ssh self-connection floor: cased-twin judging (for the ssh floor's owner)

argv_floor_ssh._judge_ssh_to_self now judges a de-duplicated lowered frame under each DISTINCT cased twin and refuses if any twin refuses — so sudo -E/-e and -H/-h, which lowercase to one frame, are each judged — replacing the earlier "ambiguous frame → None" reading that was fail-open where the lowercase option takes a value; this is a behaviour change in the ssh floor flagged here for its owner (Design lane note).

Backwards compatibility

This change only adds refusals. Each row below was measured with is_denied on the parent commit and on this head.

Newly refused (main allows; bash runs the mint in each):

row main here
T=$(: "'"; echo token); kirocrew $T (also ${T}, a trailing : "'", and bash -c "kirocrew $T") allowed REFUSED
P=$(echo kirocrew); P=$(: "'"; echo token); kirocrew $P (a var bound to the program name then rebound to the verb; also the three-rebinding chain) allowed REFUSED
2>/dev/null T=$(: "'"; echo token); kirocrew $T (a leading redirection stripped from argv; also 1>&2, &>, {fd}> forms — the plain >/dev/null form was already refused) allowed REFUSED
kirocrew>/dev/null "$(printf token)" (a redirection glued onto the program word; also >&2 and a file target) allowed REFUSED
env kirocrew "$(printf token)" (a wrapper/keyword in front of the CLI; also sudo, command, exec, nohup, time, timeout 5, !, if …; then) allowed REFUSED
T=${U:+"'"}$(printf token); kirocrew $T, T=${U-"'"}$(printf token); kirocrew $T allowed REFUSED
T=$(: "'"; echo 'kirocre[w]'); $T token, T=$(: "'"; echo k*); $T token allowed REFUSED
T=$(: ")"; printf token); kirocrew $T (quoted ) in the body) allowed REFUSED
T=`: "'"; echo token`; kirocrew $T (backtick body) allowed REFUSED
python -m kiro_crew "$(printf token)" allowed REFUSED

Refused on main and still refused here (the review triggers):

  • T=`printf %s token`; kirocrew $T; echo "it's done"
  • T=$(: "')"; echo kirocrew); $T token
  • T=$(: "')"; echo pkill); $T -f kirocrew
  • T=$(: "'"; : token; echo kirocrew); $T token (decoy) and its >/dev/null sibling
  • kirocrew "$(printf token)" and its backtick form

Residuals, with the same verdict as main:

row verdict why
T=${KIROCREW:+"'"}$(printf doctor); $T token refused, as main Pre-existing main over-read. Main's reader binds kirocrew from the ${...} prefix name. The alternative reads only the body (printf doctor), so it binds nothing and adds no refusal. The refusal is main's, kept as is.
echo T=$(echo token); kirocrew $T refused, as main Pre-existing main over-binding. Main's resolver binds the argument token t=$(echo token) although bash never assigns it, so kirocrew $T reads as kirocrew token. The alternative pairs nothing for an argument, so the refusal is main's.
T=$(: "'"; echo token; : kirocrew); kirocrew $T allowed, as main The body names both the program and the verb. Program wins, so $T binds kirocrew, while bash prints token. A text-reader limit, covered by the Not-a-goal follow-up.
python -m kiro_crew "$(cat <<<token)" and the here-string corpus allowed, as main The verb arrives on stdin.
T=$(echo "it's token"); kirocrew chat -m "$T" allowed, as main The verb is inside a quoted phrase, and bash runs kirocrew chat.
T=$(echo "it's ok"); grep token f; kirocrew $T, T=$(git log --format="%an's"); git grep token; kirocrew chat -m "$T", T=`echo done`; kirocrew $T; echo "it's" allowed, as main The verb appears only in a later command or not at all.
T=doctor; (true; T=$(: "'"; echo token)); kirocrew $T, echo T=$(: "'"; echo token); kirocrew $T, X=$(T=$(: "'"; echo token)); kirocrew $T, f() { T=$(: "'"; echo token); }; kirocrew $T, T=doctor; f(){ T=$(: "'"; echo token); }; kirocrew $T allowed, as main The assignment is in a subshell, an argument, a nested substitution or an uncalled function body, so it sets nothing in this shell and bash runs kirocrew doctor or an unset $T.
F=$(ls test/*); pytest $F -k token allowed, as main A bare glob word in the body is a filename pattern, not the program.
kirocrew2>/dev/null "$(printf token)" allowed, as main A digit glued to the program word is part of it — bash runs kirocrew2, not the CLI — so it is not a mint.
coproc worker kirocrew "$(printf token)" allowed, as main With a simple command, bash reads the word after coproc as the program (a coproc name needs a compound command, verified on bash 5.2), so worker runs, not the CLI. The no-name coproc kirocrew "$(…)" runs the CLI and is refused.
P=$(echo kirocrew); P=; P+=token; kirocrew $P, P=; P+=$(: "'"; echo token); kirocrew $P, X=$(echo kirocrew); P=; P+=$($Q); kirocrew $P, P+=$(true), OPTS+=x; kirocrew $OPTS allowed, as main An opaque or unknown-base += has no literal to concatenate, so neither the baseline nor the alternative pass isolates the verb. These return to main's verdict; the round-22/23 machinery (a fail-closed verb binding plus a hand-copied CLI subcommand tree) that refused them is removed.
X=$(echo kirocrew); P=; P+=$($Q); kirocrew pod exec w $P, … env kirocrew $P, … kirocrew --json $P, … bash -c "kirocrew $P" allowed, as main Same unknown-base +=; allowed in every position (wrapper, redirect, flag, pod/pod exec, module, nested shell, alias), matching main.
U=; U+=$(: "'"; echo token); kirocrew $U allowed, as main An append onto an unset name has no literal to concatenate, so the verb is never isolated; main's verdict stands.

Repo sweep: 3,215 real (non-test) lines that put the verb inside $( ) or backticks were each evaluated in a fresh process under the parent commit and this head. There were zero stable flips in either direction. Two lines, docs/system-specs/modules/memory-skills-hooks.md:4361 and src/kiro_crew/sandbox_aliases.py:109, read refused-to-allowed in one run. In ten isolated trials per tree, both changed from run to run in main and in this head, because their verdict comes from the exfil host probe, which this diff does not touch.

Oracle corpus, both directions: 20,736 commands (each of the 6,912 bodies as python -m kiro_crew "$(BODY)", T=$(: "'"; BODY); kirocrew $T and T=$(BODY); kirocrew $T), run through is_denied on the parent commit and this head: 8,524 allowed-to-refused (the refusals this PR adds) and 0 refused-to-allowed.

Generated-corpus trade (unchanged): of 6,912 bodies, 4,577 name the verb while bash captures nothing (FAIL_CLOSED_DELTA). 86 are allowed although bash captures the verb, because the body computes it without naming it (COMPUTED_VERB_RESIDUAL). 120 name the verb only inside a here-string the static floor cannot read (FLOOR_HERESTRING_RESIDUAL, allowed as on main).

Tests

test/test_security_shell_grammar.py::TestMintVerbFromAQuotedSubstitution:

  • test_the_mint_is_refused covers every row in the newly-refused table, the six review triggers, the decoy and its siblings, and the quoted-substitution-at-subcommand rows.
  • test_main_residuals_stay_allowed covers the allowed residuals above.
  • test_an_opaque_append_rides_along_as_main_allows covers the opaque / unknown-base += rows (empty-reset, opaque tails, OPTS+=x, and the wrapper / pod exec / module / alias family) that keep main's allow.
  • test_a_wrapper_with_a_harmless_operand_stays_allowed covers a wrapper / keyword in front of the CLI running a non-mint subcommand (env kirocrew status, sudo kirocrew doctor, ! kirocrew status, if kirocrew status; then …), so the wrapper widening is only the mint shape. The wrapper-mint rows themselves (env kirocrew "$(printf token)" …) are in test_the_mint_is_refused.
  • test_the_operand_resolution_is_linear_per_use pins the per-$-use resolution as linear (interpreter-work counts, not a clock) at sizes whose command text runs past the 8 KiB classified cap, so no per-token rescan of the resolved prefix can return.
  • test_an_argument_shaped_assignment_keeps_mains_refusal pins the balanced echo T=$(echo token) row: the alternative builds no frame, and the verdict stays main's (refused).
  • test_the_alternative_reads_the_body_not_the_value_prefix pins two facts: the alternative binds nothing from ${KIROCREW:+...}, and the verdict stays main's (refused).
  • test_the_union_only_adds_frames (curated corpus) and test_the_union_adds_frames_on_every_repo_substitution_line (every git grep substitution line in the repo) run each command with the alternative switched off and on. Switched off, the walk is main's resolver unchanged. Both tests assert that the frames with it off are a subset of the frames with it on.

test/test_security_capture_oracle.py::test_every_command_main_refuses_this_head_refuses takes every oracle body in both assignment forms. For each, it asserts that if the floor refuses with the alternative off, it also refuses with it on.

Two tests previously hit CI's 120s per-test timeout on f9e9a73f07 (shard 4 and shard 5); both are now bounded so each instance stays well under it. test_the_union_adds_frames_on_every_repo_substitution_line is 14.79s on f9e9a73f07 and 14.65s here — this PR's logic does not change its per-line cost, and the lines are pure in-process frame walks with no subprocess to batch — so it is sharded into 8 parametrized instances, max 3.97s each locally. test_every_command_main_refuses_this_head_refuses is 41.75s as a single test and is likewise sharded into 8, max 6.22s each. Every shard keeps the identical assertion and per-line/per-command attribution, and the shards partition the corpus. The shard-4 oracle trade test test_the_trade_against_bash_is_counted was separately batched (39.14s → 7.05s). test_the_floor_binds_on_the_verbs_presence is likewise sharded into 8 (max 1.6s each locally, from ~12s unsharded) because the fail-closed rule raised the gate's cost on the pathological corpus and it timed out on shard 4 of head 3d406deee885; the proof it runs is unchanged, and each shard pins its own slice's here-string residual via a structural _is_herestring_body predicate, with the 120-body total pinned by test_the_here_string_residual_total_is_pinned.

With the alternative changed to replace the baseline frame instead of appending one, these property tests fail: 6 of 13, including the oracle corpus test.

The grammar module pins the floor's network seams (_own_address_probe_stays_local and _resolved_host_verdict), as the denied-command suites do. Both changed test modules pass under KIROCREW_NET_STRICT=1 with no off-loopback call.

Proof: prove.py PROVEN on head a460e18, via test/test_security_capture_oracle.py::test_the_floor_binds_on_the_verbs_presence and the quoted-substitution mint pins in test/test_security_shell_grammar.py::TestMintVerbFromAQuotedSubstitution::test_the_mint_is_refused (including the F1 rebinding trigger), among others.

Checks: black, isort, flake8 and mypy are clean on the diff. The 87 security, deny, argv, shell and floor test files pass, and the full facade/bindings suite (test/*facade*.py test/*_bindings.py test/test_harness_parity.py) passes, with the split-patch, batch-guard and security suites. The budget row is +480, this diff's own net under src/kiro_crew/security/*.py (merge-base diff). pre_push_gate --base main ran 26 checks with 0 failures. The nesting test from #37964 passes unchanged on this base.

ENV_ASSIGNMENT_RE is re-imported at the shell_normalizer facade. Round 20 carried its only in-module use into shell_normalizer_walk with the argv walk and dropped the now-unused import; but it is a frozen public name of that facade's inventory, so the re-export is restored for importers (test_campaign_facades_base_public_names).

Whole-corpus monotonicity sweep (main vs head over the 20,736 synthetic mint-shaped probes the oracle generates — each body wrapped in the kirocrew $T / python -m kiro_crew "$(...)" templates): 8,524 go allowed-to-refused, which are smuggled mints this change now catches, not benign commands that stopped working; 0 go refused-to-allowed, which is the monotonicity guarantee that head refuses a superset of what main refuses.

The per-frame cost invariant (test_denied_commands_security_cost.py::...::test_the_memo_is_declared_inside_the_frame_loop) is kept for _judge_ssh_to_self: the cased-twin pairing added for the ssh union-refuse is preprocessing that builds the judged pairs, so its loop variable is lowered, not tokens — it is not a second frame loop, and the per-frame disqualified memo stays inside the one frame loop.

Manual verification

N/A. Unit coverage is sufficient: the guard is a pure function, exercised directly with each issue command.

Related Issues

Fixes #36961

Sibling sweep

Swept: git grep -n "_protected_name_in_substitution|_mint_verb_in_substitution|_alternative_mint_overrides|_shell_payload_walk" -- src/
Sibling: src/kiro_crew/security/shell_normalizer.py -- changed here (the alternative resolution and the union frame)
Sibling: src/kiro_crew/security/argv_floor_self.py -- changed here (reads the verb in a quoted substitution); it consumes the walk's frames, so it sees the union
Sibling: src/kiro_crew/security/argv_rules.py -- consumes the same walk; unchanged
Sibling: src/kiro_crew/security/argv_floor.py -- mirror manifest updated here

Pattern harvest

Rule candidate: to harden a static reader without risking a regression, keep the old resolution, add a second one, and refuse if either refuses. Then test monotonicity as frame inclusion with the addition switched off and on, instead of arguing it case by case.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributed under the project's Apache-2.0 license.

Takeover

This PR touches src/kiro_crew/security/* from a fork, and the Sensitive Change Review gate refuses fork PRs on that path by design — a fork PR cannot pass it, and nothing posted here clears it (the same hold as #35356 and #36072). The route to green is a maintainer takeover: a branch on this repository that keeps the author with Co-authored-by and writes Supersedes #37077, runs the real sandbox, and drives that PR green. Every other check and lane on this head is green (local gate 26/0, prove PROVEN, semgrep 0, deny_diff 0, oracle 0 refused-on-main→allowed; both pre-push review lanes APPROVE), so the takeover is a cherry-pick.

@jeeshofone
jeeshofone requested review from a team and KiroCrewCaptain as code owners November 14, 2026 04:27
@jeeshofone
jeeshofone requested a review from buluoray November 14, 2026 04:27
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: checking Automated validation is still running labels Nov 14, 2026
@KiroCrewCaptain

KiroCrewCaptain commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

Code change approver recommendation: A person should decide

Why: the decider thinks the change is probably fine but estimates a 14% chance that this change introduces a defect, above the 12% limit for approving on its own.

I'm approving this PR. It only adds refusals, and the path main uses is unchanged byte for byte.

Resolver (shell_normalizer.py). _resolve_local_assignments takes two new keyword arguments, ambiguous and verb_at. With their defaults (None and -1) both new branches are dead code, so the main frame is computed exactly as before.

Payload walk. It now adds extra frames, but only in one case: some substitution on the line, read with its quotes, has an echo/printf that prints only the verb.

  • In each extra frame, one ambiguous assignment is bound to the verb. Re-tokenizing the same source gives the same token indices, so the binding lands on the right assignment.
  • Past 8 such assignments the walk adds a kirocrew token frame, so the line is refused rather than allowed.
  • The extra frames reuse the same source text. I checked every consumer: the push gate's _shell_payload_sources, argv_rules._commands, _self_frames and the self-kill _locate. For each, a duplicate source or an extra argv can only re-check something or add a refusal; none of them can allow anything new.

Floor (argv_floor_self.py). _is_credential_mint now reads a quoted substitution ar

An automated recommendation, not a review: a maintainer still decides. It approves on its own only when the estimated chance of a defect is at most 12% and the change has at most 400 changed lines. Decider v3c (217fe607) · commit 73646b3355 · 254 changed lines. If this recommendation is wrong, reply here: replies are how the approver is evaluated.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention merge conflict Branch has merge conflicts with its base — author must resolve before merge and removed readiness: checking Automated validation is still running labels Nov 14, 2026
@jeeshofone
jeeshofone force-pushed the fix/36961-mint-subst-quote branch from 29667d7 to 32d18e4 Compare November 14, 2026 12:23
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Nov 14, 2026
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — ✅ PASS

Premise-level review of a3e3871308c598a7fc94381bf1acb24fec5dd95c via the fork AI-review pipeline — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

First-Principles-Verdict: PASS

Nothing blocks. The fix works by adding a second resolver next to main's dequoted body readers instead of fixing them, so the security floor now carries two resolutions for good.

Notes

  • Item 1 — symptom-level: the description says the cause is "a lone dequoted quote stops main's quote-aware close walk". Those readers are _protected_name_in_substitution and _mint_verb_in_substitution (shell_normalizer.py:1026/:1116), and all 3 of their call sites are in _resolve_local_assignments. The PR leaves them as they are and adds _raw_substitution_mint_name plus a whole second pass. Subtraction: switch those 3 call sites to the raw-body read, then delete alternative=, mint_overrides, seed_values/seed_program_only/bindings_out and the carry-keyed seen.
  • Item 4 — rides along: the ssh-to-self twin pairing exists only because "the lowercased walk gains an ALTERNATIVE frame… so the two walks can differ in length". That comes from item 1's second pass. Subtraction: it goes away once item 1 is collapsed onto one reader.
  • Item 2 — rides along: the kirocrew "$(…token…)" subcommand branch in _is_credential_mint covers a substitution with no lone quote. The Goal doesn't name that defect, and the description only asserts it.

What this change ships

Inventory (8 items) — 4 justified

Intent: stop an agent from getting kirocrew token past the guard when the verb comes from a substituted variable that contains a lone '. FIX.

  1. T=$(: "'"; echo token); kirocrew $T is now refused, read through a second resolution — symptom-level
  2. kirocrew "$(printf token)", including behind env/sudo, is now refused — rides along
  3. Variables bound to a program name resolve only where they are argv[0] — justified
  4. The ssh-to-self floor judges each cased twin frame on its own — rides along
  5. Program-only bindings carry into nested bash -c payloads — justified
  6. A line with unbalanced grouping is read fail-closed — justified
  7. The wrapper table, _program_basename and _argv_programs moved into shell_normalizer_walk — justified
  8. The deny-note text and the security spec name the new refusal — justified

[FIRST-PRINCIPLES-REVIEWED] a3e3871

Description read

[DESCRIPTION-READ] c90d4baf8c85801fd17234f7d261c20670747e965b8e140a156850d4998cd500

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — ⏭️ skipped

Revision a3e3871308c598a7fc94381bf1acb24fec5dd95c touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of a3e3871308c598a7fc94381bf1acb24fec5dd95c via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks: the bypass is real, the second resolution can only add refusals, and every reader of the moved wrapper table and program-index helper was updated in this patch.

Notes

  • Each round adds another text-reader approximation: a second resolver, a fail-closed rule for unbalanced lines, carried bindings, and pairing frames by case. The follow-up that refuses the mint where it runs is what closes this class; give it a filed issue rather than leaving it as prose under "Not a goal".
  • The alternative pass makes the pre-existing O(n³) worst case on the synchronous gate worse (10.7s → 15.3s at 2 KiB). Before merge, link Credential-mint reader is O(n^3) on never-closing command substitutions (synchronous PreToolUse gate stall) #39617 from the pinning test so the measured ratio has an owner.

[DESIGN-REVIEWED] a3e3871

Description read

[DESCRIPTION-READ] 5c07f91c148d3d652141f8c34ae9671d751682845fd8a7cba690410396c39c19

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions github-actions Bot removed the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Nov 14, 2026
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

Reviewed a3e3871308c598a7fc94381bf1acb24fec5dd95c via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] a3e3871

@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

Reviewed a3e3871308c598a7fc94381bf1acb24fec5dd95c via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] a3e3871

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Nov 14, 2026
@jeeshofone
jeeshofone force-pushed the fix/36961-mint-subst-quote branch from 32d18e4 to 8d0de57 Compare November 14, 2026 13:19
@jeeshofone

Copy link
Copy Markdown
Contributor Author

Both GPT 6.1 blockers on 32d18e4 are fixed in 8d0de57.

Unsuccessful verb scans bypass the reading cap (shell_normalizer.py:1237): every verb scan now counts toward _MAX_VERB_READINGS, whether or not it finds the verb. Each scan reads only that assignment's own substitution (_mint_verb_in_own_substitution) instead of reparsing every later substitution. Past the cap, the walk refuses the line. Measured locally: 400 A=$(printf kirocrew) assignments take 1.1s, and 200 ambiguous ones take 0.4s. Test: TestMintVerbFromAQuotedSubstitution::test_assignments_that_never_print_the_verb_still_count_against_the_cap, which fails on 32d18e4.

Message operands classified as mint verbs (argv_floor_self.py:425): the quoted-substitution check now runs only before the first non-flag operand, the only place a substitution can supply the subcommand. kirocrew chat -m "$(printf 'Explain /api token authentication')" is allowed again, and kirocrew "$(printf token)" and kirocrew -v "$(printf token)" are still refused. Test: the {name} chat -m ... case in test_a_substitution_without_the_verb_stays_allowed, which fails on 32d18e4.

@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Nov 14, 2026
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Security Scope Review (fork) — ✅ PASS

Which legitimate operations does 3d406deee88514479e010f8ce598a978368f9027 newly refuse? Model proposes, scripts/deny_diff.py decides — updated in place on each push. A script-confirmed regression blocks PR readiness.

adjudicated, zero confirmed regressions

Scope-Verdict: PASS

Reason: No regression is script-confirmed yet: the harness had not handed back any verdict for the 22 candidates when this review was written. The tightening is narrow. It fires only when the product CLI is the command's argv[0] and a quoted substitution naming the verb sits before the first operand, or when a top-level assignment's substitution body names the verb or a protected program. The ssh-to-self cased-twin pairing change is probed only through neighbours.

TIGHTENING: credential-mint argv floor (self-protection mint predicate, sensitive-bash tier) -- it refuses a mint whose subcommand comes from a substitution, either directly or through an assigned variable. Two hunks: and program_positions[i] and ("$(" in later or "" in later) and _shell_normalizer._mint_verb_in_substitution([later], 0): return Truein_is_credential_mint (kirocrew "$(printf token)"); and the alternative resolution in _shell_payload_walk_uncached, alt = _self_tokens(source, alternative=True, ...), which binds T=$(: "'"; echo token); kirocrew $T` from the raw, quote-aware body.

ADJUDICATED: 22 candidates, 0 confirmed newly-refused (no verdicts had been handed back when this review was written)

PLATFORM COVERAGE: posix (17 rows) covers what the diff assumes about bash: $( )/backtick syntax, NAME=$(...) prefix and export assignments, the sudo -E/-H wrapper skip in _program_positions, and fd-prefixed and glued redirects (2>"$(mktemp)", 2>/dev/null;). It also covers the console-script and python -m kiro_crew program forms, and the nested bash -c carry of program-only bindings. windows (5 rows) covers the .exe and drive-letter \ spellings of the gated program and PowerShell's $( ) subexpression next to the CLI. That syntax is lexically the same opener the new branch keys on. It also covers a $env: credential assignment, which _LOCAL_ASSIGN_RE does not read, and the & $kc call-operator counterpart of command -v. Not covered: cmd.exe for /f ... do set assignment, since the diff adds no cmd-specific reading.

UNADJUDICATED: Two boundaries are argued in prose only, with no verdict. (1) _raw_substitution_mint_name matches a protected program by plain substring (prog in word over _KILL_BY_NAME_PROGRAMS, and _SELF_NAME_RE.search(word)). Any body word containing the product name, such as a product-named worktree path, binds the variable as a program-only kirocrew. The program-position gate should keep operand uses allowed. The WT=$(dirname .../kirocrew-wt-fix/...) and TESTS=$(git ls-files 'test/*token*') rows probe this, but a program-position use of such a variable, like $ROOT/.venv/bin/python ..., is an argued-safe case I did not separately confirm. (2) _program_positions deliberately leaves the sudoedit rule out on lowered text. So sudo -e kirocrew "$(...)", where every word is a file to edit, is read as running the CLI. That is a documented fail-closed narrowing with no real legitimate operation behind it, so no row was written.

[SCOPE-REVIEWED] 3d406de

Security scope review -- adjudicated candidates

  • macos-latest (platform posix): 17 classified, 0 newly refused, 16 still allowed.
  • ubuntu-latest (platform posix): 17 classified, 0 newly refused, 16 still allowed.
  • windows-latest (platform windows): 5 classified, 0 newly refused, 3 still allowed.

No candidate flipped from allowed to refused.

@github-actions github-actions Bot added merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Nov 14, 2026
@jeeshofone

Copy link
Copy Markdown
Contributor Author

The Opus 5.5, GPT 6.1 and Security Scope blockers on 8d0de57 are fixed in e6dd2cc.

Nine read-only captures refused as a mint (Opus shell_normalizer.py:1241, and the Security Scope rows such as V=$(kirocrew --version); S=$(kirocrew status); ...): verb readings, and with them the cap's fallback frame, are now built only when some substitution on the line, read with its quotes, ends in an echo/printf of the verb alone (_prints_only_the_verb). A line of $(kirocrew ...) captures keeps main's single reading and stays allowed, on one line or as a multi-line script. Tests: the two A{i}=$({name} status {i}) cases in test_a_substitution_without_the_verb_stays_allowed, which fail on 8d0de57.

Chat message read as the verb (GPT shell_normalizer.py:1697): T=$(echo "it's token"); kirocrew chat -m "$T" prints one word that is not the verb, so no verb reading is added and the command stays allowed. Test: the T=$(echo "it's {verb}"); {name} chat -m "$T" case in the same test, which fails on 8d0de57. A value that is exactly the verb (T=$(printf token); kirocrew chat -m "$T") is refused, as it is on main.

The ${U:+"'"}$(printf token) and ${U-"'"}$(printf token) shapes reported on #36961 are refused and pinned in test_the_mint_is_refused.

@jeeshofone
jeeshofone force-pushed the fix/36961-mint-subst-quote branch from 8d0de57 to e6dd2cc Compare November 14, 2026 21:02
@jeeshofone
jeeshofone force-pushed the fix/36961-mint-subst-quote branch from 6b10ef7 to ee3f14b Compare November 16, 2026 13:25
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Nov 16, 2026
@jeeshofone

Copy link
Copy Markdown
Contributor Author

Round-16 review reply — how each lane's concern was resolved on the current head (ee3f14b700, one commit on origin/main).

First Principles — no emulator. The reverted approach modelled what a substitution would print (a bash-output emulator), and that machinery produced repeated real-mint regressions. It is gone. What ships is a single bounded rule in _resolve_local_assignments: when a computed assignment's substitution carries an unbalanced quote, _unbalanced_substitution_binding reads only the substitution body — bounded by a plain paren count, not a quote-aware reader — and binds the protected program the body names (else the verb, else nothing). Nothing computes substitution output; the floor still binds on a name being present, never on what bash would capture.

Opus — redirect depth. The redirect-skip continue in argv_floor_self updates paren depth before it continues, so $(...) nesting stays balanced across the skip; and it skips the end-of-argv break only when the stripped redirect leaves no argv-ending remainder — a glued separator (2>/dev/null;, 2>&1|grep) still ends the scan, and a separate target token (> f;) is not treated as a redirect word, so it still breaks.

GPT r18 — benign over-refusal. When the body named neither a program nor the verb, the empty binding used to fall through to _protected_name_in_substitution, whose quote-aware walk over-reads a later command's program name and manufactured a false refusal of a benign command. The unbalanced branch is now authoritative: it binds the body-scoped name when there is one and otherwise binds nothing and leaves the use unresolved — it never falls through to the over-reading scan. Regression test added.

GPT r19 — lettered-glob gap. The body scan matched only the literal product name, so a glob that bash expands to the product (echo 'kirocre[w]', echo k*) was missed and $T token minted. The body scan now also runs the glob-aware _is_self_program and binds the canonical program for a glob that could expand to it, so those rows refuse. Regression test added.

Review harness. The local lanes were extracted from the base commit's CI review workflows by local_review.py and run on the final head; both APPROVE (opus claude-opus-5.5; gpt gpt-5.6-sol, standing in for the CI-pinned gpt-6.1-sol, which is unavailable on this backend — one minor below). pre_push_gate --base main is 24/0; a per-line sweep of every tracked line that puts the verb in a substitution shows zero real-line flips; the capture oracle pins the floor's verb-presence contract and the bash-measured fail-closed trade. The known residuals (balanced quoted ), here-strings, computed/glued verbs, backtick bodies, and a module-spelling assembled in a variable) are the spellings main also allows and are listed in the PR description.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Nov 16, 2026
@jeeshofone
jeeshofone force-pushed the fix/36961-mint-subst-quote branch from ee3f14b to 2467648 Compare November 16, 2026 20:16
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Nov 16, 2026
@jeeshofone

Copy link
Copy Markdown
Contributor Author

Round 18 on head 246764816c: one commit on 0637f3ebba. The resolver is restructured so that it can only add refusals to main's verdicts, and a property test checks that over the oracle corpus and the repo. Replies per finding follow, each naming its pin.

Design

The new branch is no longer patched. Its old pieces are deleted: _substitution_body_span, _substitution_quote_unbalanced, _unbalanced_substitution_binding and the quote-blind paren walk.

  • Baseline: main's _resolve_local_assignments, unchanged, with main's binding and verdict.
  • Alternative: for each NAME=$(...) or backtick assignment, the variable is bound to what the substitution's OWN body names. The body is read from the raw source, where its quotes close, using the readers main already has (_substitution_bodies(raw_source=True), and _matching_close_backtick for a backtick body). A protected program wins, else the verb, else nothing.
  • Union: _shell_payload_walk appends the alternative frame and descends its nested payloads. The command is refused if either frame is a mint. The baseline frame is never dropped, so the head cannot un-refuse anything main refuses.

Monotonicity proof, in the PR:

  • test_every_command_main_refuses_this_head_refuses covers all 6,912 oracle bodies in both assignment forms. Its check: when the floor refuses with the alternative off (main), it also refuses with it on.
  • test_the_union_adds_frames_on_every_repo_substitution_line covers every git grep substitution line. It asserts that main's frames are a subset of the head's frames.
  • With the alternative changed to replace the baseline frame instead of appending one, 6 of the 13 property tests fail, including the oracle one.

Opus finding on ee3f14b (comment 6877448080): backtick body with a later apostrophe (T=`printf %s token`; kirocrew $T; echo "it's done")

Fixed by construction. Main's baseline refuses it and is kept, so the head refuses it too.
Pin: test_the_mint_is_refused row T=`printf %s token`; {_N} $T; echo "it's done". Measured: main REFUSE, head REFUSE.

GPT F2 on ee3f14b (comment 6877469524): trailing apostrophe (T=$(: "'"; echo token); kirocrew $T; : "'")

Fixed by construction. The alternative reads only the substitution's own body, and nothing after its close, so the count of later quotes is irrelevant. It binds T=token, which gives kirocrew token, which is refused.
Pin: the core rows in test_the_mint_is_refused, plus test_the_union_only_adds_frames. Measured: main allow, head REFUSE.

GPT F1 on ee3f14b (comment 6877469524): quoted ) (T=$(: "')"; echo kirocrew); $T token, and ...echo pkill); $T -f kirocrew)

Fixed by construction. Main's baseline refuses both and is kept. The alternative does not close the body at the quoted ) either.
Pin: the decoy and program rows in test_the_mint_is_refused. Measured: main REFUSE, head REFUSE for both.

GPT (local lane on da453deafb): ${KIROCREW:+...} prefix name bound as the program (false refusal)

Fixed by construction, with main parity. The alternative reads the substitution's body (printf doctor), never the ${...} prefix, so it binds nothing and adds no refusal. The command is still refused, but by main's own reader, which over-reads to the prefix name. That over-read is pre-existing. This round does not change it, and the PR body lists it as a residual.
Pin: test_the_alternative_reads_the_body_not_the_value_prefix. It asserts that _alternative_mint_overrides returns {} for that command and that the verdict stays main's (refused).

Opus (local lane on da453deafb, non-blocking): T=$(: "'"; echo token; : kirocrew); kirocrew $T

Pre-existing and unchanged: main allows it and the head allows it. The body names both the program and the verb. Program wins, so the binding is kirocrew while bash prints token. This is a limit of reading the text, and it is the Design Review note-1 follow-up named under Not a goal. It is listed as a residual in the body.

Opus (local lane on 92b1f2be9a): APPROVE, no findings

The lane listed three shapes it could not probe, because the shell gate refused its probe strings. I measured them with is_denied on main and on the head:

  • F=$(grep -c token log); kirocrew chat -m $F, its quoted form, and kirocrew $F: REFUSE on both.
  • C=$(which kirocrew); $C status: allow on both. $C token: REFUSE on both.
  • Backtick assignments: T=`printf token`; kirocrew $T REFUSE on both. T=`echo hi`; kirocrew $T allow on both. T=`grep -l token *.py`; kirocrew chat -m "$T" REFUSE on both.

One added refusal turned up: T=$(: "'"; grep -c token log); kirocrew chat -m "$T". It is allowed on main and refused on the head. Bash prints a count, so this one does not mint. Its balanced twin F=$(grep -c token log); kirocrew chat -m "$F" is already refused on main, because main's fail-closed rule binds a verb named in a substitution whether or not bash captures it. That is the FAIL_CLOSED_DELTA trade. The head now gives the unbalanced spelling the same verdict as the balanced one, and it adds no allow.

GPT (local lane on 92b1f2be9a): added test cases reach the network (D10)

Fixed at the cause. test_security_shell_grammar.py called is_denied without pinning the floor's two network seams, unlike the denied-command suites. The first such verdict in a process seeds the own-host set, which does a UDP route probe and an own-hostname lookup. A glob at program position (* "$(printf token)") can also be ssh, so the gate resolves the next word with DNS. The module now:

  • imports _own_address_probe_stays_local from denied_commands_security_helpers, as the test_denied_commands_security_* modules do;
  • stubs _resolved_host_verdict to "not this machine", as test_deny_gate_call_sharing does.

Pin: with KIROCREW_NET_STRICT=1, test_security_shell_grammar.py and test_security_capture_oracle.py pass with no off-loopback call (267 passed), both alone with -n0 and under xdist. Before the fix, the same run failed on an own-hostname lookup, and then on name lookup 'token' for * "$(printf token)".

GPT (local lane on 324d0ceb34): bare monkeypatch.undo() dropped the autouse fixtures' patches (D11)

Fixed at the cause. The oracle property test now toggles the alternative inside monkeypatch.context(), so leaving the scope undoes only that one patch. The grammar property helper also used a hand-rolled save-and-restore of the module attribute; it now uses pytest.MonkeyPatch.context() as well.
Pin: the mutation check still fails 6 of 13 property tests with the scoped toggles, including the oracle one, so the toggles still switch the alternative off. Both modules pass under KIROCREW_NET_STRICT=1 and KIROCREW_PKG_ATTR_STRICT=1 (267 passed).

Opus (local lane on 324d0ceb34): APPROVE, no findings

It measured 18 commands on main and the head. The only differences were the intended additions: the core trigger, and kirocrew 2>/dev/null "$(printf token)".

GPT (local lane on 6a55ee2e02): APPROVE, no findings

It ran on gpt-5.6-sol, because the pinned gpt-6.1-sol is not on this backend.

Opus (local lane on 6a55ee2e02): APPROVE; cost note fixed at the cause

The lane timed a command made of N aI=$(echo x); segments followed by kirocrew status. Main was already superlinear, and the head took about twice as long (200 segments: main 10.8 s, head 21.4 s; I reproduced both numbers).

Cause: _raw_substitution_mint_name called _substitution_bodies on the whole remainder of the line after each assignment. That read every later body again, so it was quadratic in the number of assignments.

Fix: it now reads only the assignment's own body, with the same closers _substitution_bodies uses (_matching_close_paren for $(...), _matching_close_backtick for a backtick body, the whole remainder when a close is unproven). At 200 segments the head is back to main's time: 10.85 s against 10.80 s. Every row in the body table keeps its verdict.

Pin: test_the_alternative_reads_each_body_once checks that the work grows linearly at 100, 200 and 300 assignments. It counts interpreter instructions rather than seconds, so it holds on any runner, and it fails on the old code.

The lane's second note was that an override applies to every assignment of that name. I measured it. The only difference from main is an added refusal on T=$(: "'"; echo token); T=$(echo status); kirocrew $T. Its balanced twin T=$(echo token); T=$(echo status); kirocrew $T is already refused on main. A literal reassignment (T=status) is allowed on both.

GPT (local lane on 9372c7c0b2): an earlier override survived a later reassignment

Fixed at the cause. The alternative stored one reading per variable name, so in T=$(: "'"; echo token); echo $T; T=$(echo doctor); kirocrew $T the later T=$(echo doctor) kept the earlier T=token. Main allows that command, and so does bash, which runs kirocrew doctor. The head refused it.

_alternative_mint_overrides now keeps one reading per assignment, in source order. The resolver pairs each assignment that opens a substitution with its own reading. A reading that names nothing clears the name in the alternative frame.

When the raw scan and the token walk count a name's assignments differently, the occurrences cannot be paired. In that case the assignment keeps the baseline binding, so the alternative adds nothing for it.

Pins:

  • test_main_residuals_stay_allowed: T=$(: "'"; echo token); echo $T; T=$(echo doctor); kirocrew $T, and the same with T=$(echo status). Both are allowed, as on main.

  • test_the_mint_is_refused:

    • T=$(echo status); T=$(: "'"; echo token); kirocrew $T, where the later assignment names the verb;
    • T=$(: "'"; echo token); kirocrew $T; T=$(echo status), where the use comes before the reassignment;
    • the subshell decoy (T=$(echo hi)); T=$(: "'"; echo token); kirocrew $T.

    All three are refused.

Every row in the body table keeps its verdict.

Opus (local lane on 9372c7c0b2): APPROVE, no findings

Opus measured two candidates and dropped both. T=$(grep token notes.txt); kirocrew chat -m "$T" is refused on main too. A quoted substitution before the subcommand never fires on benign arguments.

The split-patch-reach test flagged a direct patch of _matching_close_paren in the first version of the linearity test. That test now counts interpreter work with _interpreter_work and _linear_work, from the denied-command helpers, and patches nothing. It passes on the fix and fails on the old quadratic read.

GPT (local lane on 87412068aa): an unpaired decoy assignment tainted a later benign one

Fixed at the cause. In (T=$(echo token)); T=$(echo doctor); kirocrew $T, the raw scan counted the subshell's T=$( as an assignment, but the token walk did not. The counts disagreed, and the fallback then gave every T the last named reading. Main allows the command, and bash runs kirocrew doctor.

Two changes:

  • The raw scan now counts only the assignments the shell reads as syntax. ( is no longer an assignment boundary, since a subshell's T= doesn't set the parent's variable. A name inside quotes (echo " T=$(...)") is skipped, using the shared _iter_shell_chars state machine.
  • The fallback is gone. An assignment the two scans cannot pair now keeps the baseline binding, so the alternative adds nothing for it.

Pins:

  • test_main_residuals_stay_allowed: (T=$(echo token)); T=$(echo doctor); kirocrew $T, and the quoted decoy, echo " T=$(...echo token)"; T=$(echo doctor); kirocrew $T. Both are allowed, as on main.
  • test_the_mint_is_refused: the subshell decoy before a real verb assignment, which is still refused.

Measured on main and the head: every row in the body table keeps its verdict, and none goes from refused to allowed.

Opus (local lane on 87412068aa): APPROVE, no findings

It probed 18 ordinary commands, and none is newly refused.

First Principles subtraction notes

The scan loop that duplicated _protected_name_in_substitution is gone. It was the deleted _unbalanced_substitution_binding. The other two notes stay declined:

  • _REDIRECT_OP_PREFIX matches &>, >|, <<<, <> and >&N-, which _REDIRECT_START_RE does not. Swapping one for the other changes which operands are skipped.
  • The inline operand walk in argv_floor_self tracks redirect-skip and interpreter-flag state that _self_cli_operands does not expose.

Design Review note 3 (security.md)

Updated in the same commit. The note now says the credential-mint floor reads two resolutions and refuses if either is a mint, explains how the alternative reads the body, and says how the refusal clears.

Design Review note 1 (refuse where the mint runs)

Named under Not a goal as a follow-up. Not filed.

Checks on 246764816c:

  • pre_push_gate --base main: 24 ran, 0 failed.
  • prove.py: PROVEN.
  • 87 security, deny, argv and floor test files: 9,889 passed, 2 skipped.
  • Changed test modules under KIROCREW_NET_STRICT=1 and KIROCREW_PKG_ATTR_STRICT=1: 304 passed.
  • test: check the ssh floor's nesting bound below the gate's depth cap #37964 nesting test file: 382 passed, unchanged.
  • Per-line repo sweep against the parent commit: 3,210 real lines, 0 stable flips. One spec-comment line (sandbox_aliases.py:109) flipped once. In ten isolated trials per tree its verdict changed from run to run in both main and the head, because it comes from the exfil host probe, which this diff does not touch.
  • Local lanes on 246764816c:
    • Opus (claude-opus-5.5): APPROVE, no findings.
    • GPT (gpt-5.6-sol, because gpt-6.1-sol is not on this backend): APPROVE, no findings.

@github-actions github-actions Bot removed the readiness: checking Automated validation is still running label Nov 16, 2026
@jeeshofone

Copy link
Copy Markdown
Contributor Author

Round 19 on head f9e9a73f07: one commit on ff01b2edfb. Both fork-lane findings on fc044685e1 are fixed in the alternative resolution this PR adds. Main's baseline resolution is unchanged.

Opus (comment 6877448080): a bare glob word in the body bound as the product

Fixed at the cause. _raw_substitution_mint_name now treats a glob word as the protected program only when its basename contains a letter. This uses a new shared helper, shell_normalizer._spells_self_name. The literal_program gate in argv_floor_self._is_credential_mint now calls the same helper instead of keeping its own regex, so there is one rule rather than two copies.

Pins, measured on main and on this head:

  • F=$(ls test/*); pytest $F -k token: ALLOWED on both (test_main_residuals_stay_allowed).
  • F=$(ls kiro*); $F token: REFUSED on the head (test_the_mint_is_refused). Main allows it.

GPT F1 (comment 6877469524): alternative bindings escaped their shell scope

Fixed at the cause, with no new regex guard. The alternative no longer rescans the raw source for assignments. _RAW_MINT_ASSIGN_RE and the quote-activity offset set are deleted.

Main's _resolve_local_assignments has no notion of scope. It binds every token that matches _LOCAL_ASSIGN_RE, including the t=$( inside (true; ...), an echo t=$(...) argument, and an assignment inside an uncalled function body. Taking main's set unchanged would therefore not have fixed F1.

The alternative now walks the tokens main produced. It reads a raw substitution body only for an assignment that sets a variable in this shell:

  • The assignment's _argv_programs slot is still empty, so it sits at the command's prefix position and is not an argument.
  • No ( or { group is open before it in the raw text, so it is not inside a subshell, a substitution body, a brace group or a function body. Every unquoted { counts, including one glued to f() as in f(){ ...; }. The exception is a { that opens a ${...} parameter, which is tracked on its own so its } closes nothing. An unquoted } closes a group, so a brace expansion closed within its own word ({a,b}) adds nothing.

The raw word offsets come from one walk of the shared quote state machine (_raw_word_scopes), paired one for one with main's tokens.

The scope check fails closed, toward leaving an assignment unpaired:

  • if the raw word count does not match the token count,
  • if a raw word does not start with the assignment's name,
  • if the value has no substitution opener,
  • if the depth ever goes negative.

An unpaired assignment keeps main's binding, so the alternative frame differs from main's only in how it reads a body, never in which assignments it binds.

Pins, measured on main and on this head:

command main head
T=doctor; (true; T=$(: "'"; echo token)); kirocrew $T (the F1 trigger) allow allow
echo T=$(: "'"; echo token); kirocrew $T (an argument that looks like an assignment, unbalanced) allow allow
X=$(T=$(: "'"; echo token)); kirocrew $T (nested substitution) allow allow
f() { T=$(: "'"; echo token); }; kirocrew $T (uncalled function body) allow allow
T=doctor; f(){ T=$(: "'"; echo token); }; kirocrew $T (uncalled function, { glued to f()) allow allow
{ T=$(: "'"; echo token); } >/dev/null; T=doctor; kirocrew $T (brace group) allow allow
echo {a,b}; T=$(: "'"; echo token); kirocrew $T (a brace expansion before a frame-scope mint) allow REFUSE
T=$(: "'"; echo token); kirocrew $T (the original #36961 mint, at frame scope) allow REFUSE
echo T=$(echo token); kirocrew $T (an argument that looks like an assignment, balanced) REFUSE REFUSE

The last row stays refused because main refuses it. Main's resolver binds the argument token t=$(echo token) although bash never assigns it, so kirocrew $T resolves to kirocrew token. The union keeps main's frame, and the alternative pairs nothing for this command, so the refusal is main's over-binding, not this PR's. test_an_argument_shaped_assignment_keeps_mains_refusal pins both halves: the alternative builds no frame, and the verdict matches main. The PR body lists the row as a residual of main.

Monotonicity

The alternative frame is only ever appended. test_every_command_main_refuses_this_head_refuses covers the full oracle corpus. test_the_union_adds_frames_on_every_repo_substitution_line runs every git grep substitution line in the repo with the alternative off and on. When I changed the code so the alternative replaces the baseline frame instead of being appended, 6 of the 13 property tests failed.

The per-line repo sweep checked 3,214 real lines against ff01b2edfb and found zero stable flips. Two lines, memory-skills-hooks.md:4351 and sandbox_aliases.py:109, read differently in one run, and I ran each in ten isolated trials per tree. memory-skills-hooks.md:4351 was refused in all ten trials on both main and the head, so its single different read was a one-off. sandbox_aliases.py:109 changed from run to run on main as well as on the head. Its verdict comes from the exfil host probe, which this diff does not touch.

Layout

shell_normalizer.py reached the 2,000-line cap. The lexical half of the alternative, _raw_word_scopes and _RAW_VALUE_PREFIX_RE, moved whole into shell_normalizer_walk.py, and shell_normalizer re-exports both. Those two pieces use only walk-layer helpers, so the rule that a lexical owner never imports shell_normalizer still holds. The two functions that read programs stay in shell_normalizer.

Checks on f9e9a73f07:

  • pre_push_gate --base main: 24 ran, 0 failed.
  • prove.py: PROVEN.
  • Changed modules under KIROCREW_NET_STRICT=1 and KIROCREW_PKG_ATTR_STRICT=1, together with split-patch-reach, gate-liveness and the flake ratchet: 386 passed.
  • 87 security, deny, argv, shell and floor test files: 9,905 passed, 2 skipped.
  • Budget row: +272.
  • Local lanes on f9e9a73f07:
    • Opus (claude-opus-5.5): APPROVE, no findings.
    • GPT (gpt-5.6-sol, because gpt-6.1-sol is not on this backend): APPROVE, no findings.

Local GPT lane on d72fa5a726: an uncalled function body whose { is glued to f() counted as top level

Fixed at the cause. The first version of _raw_word_scopes counted a { only when it started a word, so it missed the { in f(){ ...; }. The function body's assignment was then read as this shell's assignment, and T=doctor; f(){ T=$(: "'"; echo token); }; kirocrew $T was refused although main allows it. The walk now counts every unquoted { that does not open a ${...} parameter, as described above. Pins: the glued and spaced function bodies and the brace group are allowed, and the brace-expansion and function-before-mint rows are refused.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Round 20: one commit on 5ff3feda55 (an ancestor of origin/main, cleanly mergeable). F1 and F2 (fork Opus lane on f9e9a73f07) and the F3 Security Scope Review are all fixed at the cause.

F1 (argv_floor_self.py): a ) in a redirect target ended the argv scan before the verb

Fixed at the cause. In _is_credential_mint's argv scan, the redirect skip's continue sat above the depth = max(depth, 0) clamp, so a redirect word skipped the clamp. In kirocrew 2>"x)" $(true; echo token), the ) in the redirect target took the depth to -1. The $(true; only brought it back to 0, _ends_argv broke on its ;, and token) was never read.

The scan now adds the word's depth and clamps in one statement, depth = max(depth + _substitution_depth_delta(later), 0), before any continue, so no path can skip the clamp.

Pin: test_the_mint_is_refused[kirocrew 2>"x)" $(true; echo token)]. Measured with is_denied: main REFUSE, head REFUSE. Its unquoted twin kirocrew 2>x) $(true; echo token) is also refused on both.

Mutation check: with the clamp moved back after the redirect continue (the r19 order), the pin fails (1 failed). Restored, it passes (1 passed).

F2 (test_security_capture_oracle.py): about 6,900 sequential bash -c children in one test

Fixed at the cause, and the proof is kept. test_the_trade_against_bash_is_counted now runs the bodies in batches of 1,000 per bash child (7 children for 6,912 bodies) instead of one child per body.

  • Each body is still its own command substitution, $(eval BODY), so one body's redirects, exit status and output cannot reach the next.
  • Each result is printed behind its index and NUL-delimited, so it is attributed to the body that produced it. A body with no result fails the test and names that body.

Equivalence: on this host, the batched capture matched the per-body capture byte for byte on all 6,912 bodies (0 missing, 0 raw-output differences, 0 verb-captured differences).

Wall time of the test on this host: 39.14 s before, 7.05 s after. It still asserts (FAIL_CLOSED_DELTA, COMPUTED_VERB_RESIDUAL) == (4577, 86). With the expected delta changed to 4576 it fails with assert (4577, 86) == (4576, 86), so it measures the real count. Batching kept per-body attribution, so the test is not made Linux-only.

F3 (Security Scope Review): the second reading over-refused the product name as an operand

Fixed at the cause. The ruling: the second reading applies only when the command's program token (argv[0]) is the product CLI; a product name in an option value, a repo slug, a secret/service name or a glob-bound variable used as another program's argument does not arm it. The r19 head refused six such commands that main allows.

Root cause, in two reading paths:

  • The quoted-substitution branch of _is_credential_mint fired on any token _is_self_program accepted, including kirodotdev/KiroCrew (basename kirocrew) and a --name kirocrew / -s kirocrew operand. It is now gated on a new _program_positions(tokens) signal, so it fires only where the self name is the program word (argv[0]) of its command. gh ... --repo kirodotdev/KiroCrew --body "$(gh auth token)", kubectl ... --from-literal=GH_TOKEN="$(...)", security ... -s kirocrew -w "$(...)" and docker run --name kirocrew -e GH_TOKEN="$(...)" all have gh/kubectl/security/docker as argv[0], so main's allow now stands.
  • The alternative resolution bound TESTS to the program name kirocrew (read from the glob test/*crew*) and then substituted it into pytest $TESTS -k token, where it is pytest's argument. _resolve_local_assignments now honours an alternative PROGRAM-name binding only where $VAR is used at program position, through a shared _resolve_name gate applied to all three variable-reference spellings (plain $VAR/${VAR}, a transform ${VAR:0}/${VAR^^}/${VAR/x/y}, and an indirect ${!V}), so the product name cannot re-enter an operand through any spelling of the glob-bound variable. A use as another program's operand keeps the reference literal, so the variable stays unresolved and main's allow stands. The verb binding (token) is unchanged — it is the mint's own operand. The baseline resolution (no overrides; program_positions is None, so the gate's flag is True) reduces exactly to main's values.get(name, default).

_program_positions reports which token is argv[0] using the same command-boundary walk (_ends_argv, leading VAR= skipped) as _argv_programs. A leading REDIRECTION is not argv[0] (the shell strips it from argv), so it is skipped too: >/dev/null kirocrew "$(printf token)" and 2>/dev/null kirocrew "$(printf token)" are refused, the same as the redirect written after the CLI. The program-only gate and its provenance cover all three variable-reference forms (plain $VAR, transform ${V:0}, indirect ${!V}) at BOTH the use site and the alias site, and the provenance propagates across aliases (assignment, append, chains), so an aliased glob-bound program variable used as an operand also keeps main's allow while the same alias at argv[0] still mints; the transform/indirect resolution at the alias site runs only in the alternative pass, so the baseline stays byte-identical to main. _program_positions, and the pre-existing _program_basename and _argv_programs, live in shell_normalizer_walk.py (re-exported) to keep shell_normalizer.py under its 2,000-line cap; the two relocations are behaviour-neutral.

A product name behind an exec wrapper -- env kirocrew "$(printf token)", sudo kirocrew "$(printf token)", nohup kirocrew "$(printf token)", ! kirocrew "$(printf token)" -- stays ALLOWED, the same as origin/main (verified on origin/main, the PR parent, and head). There argv[0] is the wrapper, not the product CLI, so by the F3 rule main's verdict stands; the plain-verb form env kirocrew token is still refused, by the position-agnostic bare-verb reading. This is left as a declared residual rather than fixed here: the floor has no shared wrapper-unwrapping resolver to reuse (env/sudo/nohup are deliberately absent from the _DATA_CONSUMER_PROGRAMS denylist so an executor defaults to "could run the name"), so refusing the quoted-substitution-behind-a-wrapper form would be a NEW floor rule, not a fix for #36961, and it would diverge from main. It is a pre-existing main gap in the same class and is named in the PR body's "Not covered" list with its main-parity verdict. Both review lanes recorded it as an out-of-scope note, not a finding.

Scope is preserved: a self name AT program position still mints through a quoted substitution (kirocrew "$(printf token)"), a glob program still mints (F=$(ls kiro*); $F token, k* "$(printf token)"), and the core #36961 cases (T=$(: "'"; echo token); kirocrew $T, P=$(echo kirocrew); $P token, python -m kiro_crew "$(printf token)") stay refused.

Verification of the six rows with scripts/deny_diff.py --base origin/main --head HEAD: 0 newly-refused (6 allowed at both refs). Pins: the six rows (plus pytest kirocrew -k "$(printf token)") added to test_main_residuals_stay_allowed; the program-position mints kept in test_the_mint_is_refused.

Sweeps against origin/main

  • Per-line repo sweep: 3,230 real (non-test) lines. 0 stable allowed-to-refused and 0 stable refused-to-allowed. Four lines (all in memory-skills-hooks.md, plus sandbox_aliases.py:109) read a flip in one run; in ten isolated trials per tree they change from run to run on main too, and _is_credential_mint returns False for all four on both trees, so their verdict comes from the exfil host probe this diff does not touch.
  • Whole oracle corpus: 20,736 synthetic MINT-SHAPED probes, not real commands: each of 6,912 oracle substitution bodies is wrapped in the three credential-mint templates python -m kiro_crew "$(BODY)", T=$(: "'"; BODY); kirocrew $T and T=$(BODY); kirocrew $T. Every probe is an attempt to reach the mint. 8,524 go allowed-to-refused and 0 refused-to-allowed. The 8,524 are exactly the smuggled mints this PR now catches that main missed -- they are NOT benign operations that stopped working. They split into the true mints bash would run plus the floor's deliberate fail-closed residual (the (4577, 86) the capture-oracle test pins); the 0 in the other direction is the monotonicity guarantee -- head refuses a superset of what main refuses. Benign real commands are covered by the per-line repo sweep above (0 stable flips), not here.

Checks on the round-20 head dd58135d63

The pushed head dd58135d63 is e6a03a8dc6 plus two test-only changes (sharding the two tests that previously hit CI's per-test timeout); its src/ is byte-identical to e6a03a8dc6, so the lane approvals and the checks below carry over, and pre_push_gate/prove were re-run green on it.

  • pre_push_gate --base main: 24 ran, 0 failed.
  • prove.py --base origin/main: PROVEN.
  • Semgrep, CI's configs (p/python, p/typescript, p/security-audit, p/secrets, p/owasp-top-ten, semgrep/), --baseline-commit the parent: 0 findings. Local semgrep 1.183.0; CI pins 1.78.0.
  • mypy on the changed modules: clean.
  • scripts/deny_diff.py --base origin/main over the six Security Scope rows: 0 newly refused (all six allowed at both refs).
  • test_security_shell_grammar.py: 308 passed.
  • 87 security/deny/argv/shell/floor test files: 9,913 passed, 2 skipped. (Two KIROCREW_NET_STRICT=1 loopback teardown artifacts on unrelated SSH/S3 tests; the test bodies pass and flip run-to-run on this host.)
  • Budget row: +380.
  • Local lanes on e6a03a8dc6 (src byte-identical to the pushed dd58135d63):
    • Opus (claude-opus-5.5): APPROVE. Ran the 34 probe rows plus 22 adversarial alias/reference chains (multi-hop assignment, transform-of-alias, indirect-to-alias, append-with-transform RHS, export, reassignment, glob var as operand): every operand use stays allowed on base and head, every program or verb alias reaching argv[0] or the CLI's operand is refused on head; _program_basename (with @once_per_call()) and _argv_programs confirmed byte-identical relocations; exec-wrapper main-parity recorded as an out-of-scope note.
    • GPT (gpt-5.6-sol): APPROVE. Program-only provenance survives plain, transform, indirect, append and chained aliases while staying gated to argv[0]; 311 targeted tests passed and 49 supplied/adversarial probe cases behaved correctly, with baseline resolution byte-identical to main and no operand over-refusal or mint bypass.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Sensitive change review

Head: dd58135

  • What rule changed: The credential-mint floor (_is_credential_mint) now also refuses a token subcommand carried in a quoted command substitution (kirocrew "$(... token ...)") or an assigned one (T=$(...); kirocrew $T). Main's resolution is kept unchanged; a second "alternative" resolution is added, and the command is refused if either reading is a mint.
  • Before -> after: Before, a quote inside the substitution body stopped main's quote-aware close walk, so the hidden verb was missed and the mint allowed. After, the alternative reads the body quote-aware from the raw source and refuses on the named verb or protected program; this second reading fires only when the product CLI is the command's program token (argv[0]), so a product name used as another program's operand keeps main's verdict.
  • Worst case if wrong: Over-refusal blocks a benign command (visible, recoverable). Under-refusal could let a prompt-injected agent mint a dashboard token; the un-disableable backstop remains the sensitive-path floor over token_signing.key, which this PR does not touch.
  • Evidence checked: deny_diff shows 0 newly-refused over the six Security Scope rows; the whole-oracle monotonicity sweep is 0 refused->allowed (the 8,524 allowed->refused are smuggled mints now caught, not benign commands); the repo substitution-line sweep has 0 stable flips; both local review lanes (Opus 5.5, GPT) APPROVE.
  • Tools still work: The six main-allowed operand shapes (repo slug kirodotdev/KiroCrew, kubectl/security/docker secret/service/container names, glob-bound pytest $TESTS) stay allowed, matching origin/main; exec-wrapper forms stay main-parity.
  • Backward compatible: Yes. Main's resolver and its verdict are byte-identical; the alternative only ADDS a frame, so the floor refuses a strict superset of what main refuses, proven monotonic over the oracle corpus.
  • Existing tests: pre_push_gate 24/0, prove.py PROVEN, semgrep 0 findings, mypy clean; the security grammar, capture-oracle and floor suites pass. The two tests that previously hit CI's per-test timeout are sharded (union max 3.97s, monotonic max 6.22s per instance).
  • How to undo: Revert the single commit dd58135. It is self-contained in src/kiro_crew/security/{argv_floor_self,shell_normalizer,shell_normalizer_walk,denied_rules_tables,argv_floor}.py, the security.md doc, a budget row and two test files, with no schema or data migration.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Round 22 on head 844e1ca504: one commit, rebased onto current origin/main. Against the round-21 head 549a75a11d, the range-diff is the facade re-export, the empty-assignment reset (with the opaque += tail), the _program_basename ownership reroute, and their pins.

F1 (fork GPT lane): a rebinding kept an earlier program-only flag -- fixed

Unchanged from round 21. _set_program_only re-derives a variable's program-only flag from its current binding at every binding site. The Opus lane confirmed it.

Fork GPT lane: P=$(echo kirocrew); P=; P+=token; kirocrew $P -- fixed

Bash sets P="" on the empty assignment and P+=token then builds token, so kirocrew token runs. Main's reader skips an empty right-hand side, keeps the stale kirocrew, and reads P as kirocrewtoken, so main allowed it. Now, in the alternative resolution only, an empty assignment sets the variable to "" and clears its program-only flag, so the append builds token and the command is refused. The baseline pass never enters this branch.

The same lane then found a second case on the first round-22 head: X=$(echo kirocrew); P=; P+=$($Q); kirocrew $P. _protected_name_in_substitution scanned past the self-closing $($Q) to the later kirocrew word and returned it, which skipped the fail-close. For a substitution tail the alternative pass now binds the verb in every case. The baseline frame still binds the program name, so a program-position use ($F token) is still caught there.

Pins (each verified on origin/main and on 5fe2148d33):

  • GPT's trigger: allowed on main and on 5fe2148d33, refused here.
  • P=; P+=token; kirocrew $P with no prior binding: refused here, and already refused on main and 5fe2148d33. It is kept as a regression guard.
  • P=$(echo kirocrew); P=; P+=$(: "'"; echo token); kirocrew $P (the side with unknown output): allowed on main and on 5fe2148d33, refused here.
  • Q=; Q+=x; echo $Q (a variable never passed to the CLI): allowed on main, on 5fe2148d33 and here.
  • The opaque-tail pins P+=$($Q) and P+=$(true).

Mutation proof: with the empty reset disabled in a copy of the tree, GPT's trigger is allowed. Restored, it is refused.

The append-VERB residual U=; U+=$(: "'"; echo token); kirocrew $U stays allowed, as on main. The command has no substitution-valued assignment, so the alternative resolution never runs. It is a row in the body's residual table.

Backend Tests shards 1 and 5 on dd58135d63

  • Shard 1 (test_campaign_facades_base_public_names): round 20 moved the only in-module use of ENV_ASSIGNMENT_RE into shell_normalizer_walk and dropped the import from shell_normalizer, but the name is part of that facade's frozen public surface. shell_normalizer re-imports it.
  • Shard 5 (test_security_split_patch_reach::test_every_reader_of_a_patched_name_reads_it_where_the_patch_writes): round 20 also moved _program_basename into shell_normalizer_walk, but _EXPORTS still named shell_normalizer as its home. So a patch of security._program_basename wrote the reader's re-export, while shell_normalizer_walk._argv_programs read its own binding.

The ownership change (shard 5's fix)

_EXPORTS["_program_basename"] is now shell_normalizer_walk. Every read goes through a shell_normalizer_walk as _walk alias, in shell_normalizer, argv_floor_self, argv_rules, rm_floor and s3_direction, plus acp/batch_guard.py, the one reader outside the package. The facade's TYPE_CHECKING blocks name walk as the home, as test_security_facade and test_argv_floor_facade require. Each call site changes only the module it reads the name from. The function is the same, and no verdict changes: deny_diff 0, and 0 flips in either direction over the 20,736-command oracle corpus against the previous head d4fdb76333. inline_payload and argv_floor_ssh still bind the name at import through from .shell_normalizer import, which is the same function object. This diff does not touch either file, and the split-patch rules cover neither.

Checks on 844e1ca504

  • pre_push_gate --base main: 26 checks ran, 0 failed.
  • prove.py --base origin/main: PROVEN (reverting the production diff fails the quoted-substitution mint pins).
  • Semgrep (CI configs, --baseline-commit the parent): 0 findings.
  • scripts/deny_diff.py over the six scope rows: 0 newly refused.
  • Oracle corpus against the previous head d4fdb76333 (replayed on the same base): 20,736 commands, 0 allowed-to-refused and 0 refused-to-allowed.
  • Oracle corpus against origin/main: 20,736 commands, 8,524 allowed-to-refused (the refusals this PR adds) and 0 refused-to-allowed.
  • test_security_shell_grammar.py: 325 on the previous head and 327 here. The two added cases are the new refused pins.
  • Facade, bindings, split-patch, batch-guard and security suites: 18,693 passed.
  • Budget row: +430.
  • Local lanes on the final commit's content:
    • Opus (claude-opus-5.5): APPROVE, no findings (f13f68fe).
    • GPT (gpt-5.6-sol; CI pins gpt-6.1-sol): APPROVE, no findings (a60a357d).
    • Both lanes reviewed 354fe459d7. Since then the commit was rebased once more, onto bf8094b69b, to resolve a doc-only conflict with feat: add connected-computer gateway side behind a preview flag #34858 in docs/system-specs/modules/security.md. Each side changed a different bullet, and each side's bullet is kept. The commit's changed lines are identical before and after (same patch-id for every file except the doc, and the same changed doc lines). The gate, prove, semgrep, deny_diff and the oracle above were rerun on 844e1ca504.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Sensitive change review

Head: 844e1ca

  • What rule changed: The credential-mint floor (_is_credential_mint) refuses a token subcommand carried in a quoted command substitution (kirocrew "$(... token ...)") or an assigned one (T=$(...); kirocrew $T). The second reading fires only when the product CLI is the command's program token (argv[0]). Round 22 adds a case: in the alternative resolution, an empty assignment resets the variable to "" before a += append, and an append whose base or tail is unknown binds the verb. It also moves the owner of _program_basename to shell_normalizer_walk, which changes only where each reader looks the name up.
  • Before -> after: Before, P=$(echo kirocrew); P=; P+=token; kirocrew $P and X=$(echo kirocrew); P=; P+=$($Q); kirocrew $P were allowed, while bash runs kirocrew token. After, both are refused. Before, a patch of security._program_basename did not reach shell_normalizer_walk's own read. After, every reader reads the owner's binding.
  • Worst case if wrong: An over-refusal blocks a benign command, which is visible and recoverable. An under-refusal could let a prompt-injected agent mint a dashboard token. The backstop that cannot be turned off is still the sensitive-path floor over token_signing.key, which this PR does not touch.
  • Evidence checked:
    • deny_diff: 0 newly refused over the six scope rows.
    • Oracle corpus, 20,736 commands: 0 refused-to-allowed against origin/main, and 0 flips in either direction against the previous head d4fdb76333. So the ownership change alters no verdict.
    • Both local review lanes APPROVE.
    • Each new pin is checked on origin/main and on 5fe2148d33.
    • Mutation proof: disabling the empty reset reopens GPT's trigger.
  • Tools still work: These operand shapes stay allowed, matching origin/main: repo slugs, secret, service and container names, glob-bound pytest $TESTS, and a += that builds a non-verb operand or a variable never passed to the CLI. Exec-wrapper forms keep main's verdict.
  • Backward compatible: Yes. The baseline pass is byte-identical to main. The alternative only adds a frame, so the floor refuses a strict superset of what main refuses.
  • Existing tests:
    • pre_push_gate 26/0, prove.py PROVEN, semgrep 0 findings.
    • Facade, bindings, split-patch, batch-guard and security suites: 18,693 passed.
    • Grammar suite: 327 passed.
    • Backend Tests shards 1 and 5 failed on dd58135d63. Their tests (test_campaign_facades_base_public_names and test_security_split_patch_reach) pass on this head.
  • How to undo: Revert the single commit. It touches src/kiro_crew/security/{__init__,argv_floor,argv_floor_self,argv_rules,denied_rules_tables,rm_floor,s3_direction,shell_normalizer,shell_normalizer_walk}.py, src/kiro_crew/acp/batch_guard.py, the security.md doc, a budget row and three test files. There is no schema or data migration.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Round 23 on head ccc9a0715f: one commit. The diff against the round-22 head 844e1ca504 changes four files: shell_normalizer.py, shell_normalizer_walk.py, the budget row, and one grammar test.

Security Scope Review on 844e1ca504: three benign commands newly refused, fixed

The Scope Review confirmed these three rows on posix:

  • SUMMARY=; SUMMARY+=$(git log --oneline -3); kirocrew chat -m "$SUMMARY"
  • MSG="Upgraded to "; MSG+=$(git describe --tags); kirocrew chat -m "$MSG"
  • OPTS+=--json; kirocrew status $OPTS

Each followed an earlier X=$(...) assignment. Round 22 bound any += with an unknown base or tail to the verb, and the floor refuses token anywhere in a kirocrew argv, so a chat message or a status flag read as a mint.

That verb binding now holds everywhere except where the variable is plainly an argument of a CLI subcommand that has no mint. The three rows are allowed again, as on main.

_after_a_non_mint_subcommand (in shell_normalizer_walk) takes the CLI to be the last product word in the current command. Wrappers (env, command, exec, sudo, nohup, timeout 5), redirects and the top-level flags, which take no value, therefore leave the subcommand slot open. _cli_path_closed then reads only plain words. pod keeps the slot open, because pod token mints. pod exec <name> reopens it, because its REMAINDER argv is a fresh CLI argv. Any word that is not plain also keeps the slot open, since it may expand to nothing, to exec or to several words. That covers $(echo exec), $W, a quoted word, and a flag holding an expansion. With no CLI word in the command (bash -c "kirocrew $P"), the binding stays.

The local review lanes found the wrapper, redirect, pod exec and expanding-word spellings on intermediate heads. Each is pinned as refused.

Checks on ccc9a0715f

  • pre_push_gate --base main: 26 checks ran, 0 failed.
  • prove.py: PROVEN.
  • Semgrep: 0 findings.
  • deny_diff over the six scope rows plus the three Scope Review rows: 0 newly refused.
  • Oracle corpus, 20,736 commands:
    • against 844e1ca504: 0 flips in either direction;
    • against main: 8,524 allowed-to-refused and 0 refused-to-allowed.
  • Facade, bindings, split-patch, batch-guard and security suites: 18,697 passed.
  • Mutation proofs:
    • With the no-CLI case exempted, bash -c "kirocrew $P" is allowed.
    • With the argument-position gate disabled, the status $OPTS row is refused again.
  • Budget row: +492.
  • Local lanes on the final head:
    • Opus (claude-opus-5.5): APPROVE, no findings.
    • GPT (gpt-5.6-sol; CI pins gpt-6.1-sol): APPROVE, no findings.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Round 24 — structural change on head 00ec572a94b6, taking the First Principles and Opus blocks together, plus the redirect and wrapper/keyword program-position bypasses the GPT and Opus lanes surfaced.

What changed (one change, not two patches)

Dropped, as both lanes asked:

  • the fail-closed unknown-+= rule in the alternative pass (alt_unknown, the empty-assignment reset, and the computed-/unknown-tail → "token" bindings), and
  • the hand-copied CLI subcommand tree (_after_a_non_mint_subcommand, _cli_path_closed, _MINT_PARENT_SUBCOMMANDS, _FORWARDING_CHILD, the module/plain-word regexes).

A += append and an empty assignment are now read identically in the baseline and the alternative pass — concatenation from the current binding, exactly as main does. The mint-name read at the use site is now linear: at_program is one lookup into the precomputed program_positions, and the resolver closes over alt_program_only alone — no per-token rescan of out. The F1 rebinding/provenance logic and the substitution-body reading (the actual #36961 fix) are untouched.

Redirect / program-position bypasses (found by the GPT and Opus lanes, fixed here). Two pre-existing spellings let a redirect defeat the program-position gate the mint check relies on, both present on the round-23 head and allowed on main — neither introduced by the r24 subtraction:

  • Leading redirect before a substitution-assignment: 2>/dev/null T=$(: "'"; echo token); kirocrew $T was allowed because the alternative's prefix test used _argv_programs, which reads a leading fd-prefixed redirect (2>, 1>&2, &>, {fd}>) as a program named 2/1/&/{fd}. The prefix test now walks the same redirect-aware walk _program_positions uses.
  • Redirect glued onto the program word: kirocrew>/dev/null "$(printf token)" (also >&2, a file target) was allowed because _program_positions classified any token containing </> as a redirect, so kirocrew never took program position. It now uses _push_token_redirection, which matches a redirection only after an fd number / & / {name} prefix — so a program with a glued redirect keeps program position and its basename resolves to the CLI. (kirocrew2>/dev/null stays allowed: the digit glues to the word, so bash runs kirocrew2, not the CLI.)

Both fixes live only in the alternative pass / the PR's own program-position gate (_program_positions is never used by the baseline), so the baseline is byte-identical to main, and each only adds refusals (oracle still 0 refused-on-main → allowed). Pinned by new rows in test_the_mint_is_refused and test_main_residuals_stay_allowed.

Wrapper / keyword class (found by the GPT and Opus lanes, fixed here). env kirocrew "$(printf token)" (also sudo, command, exec, nohup, time, timeout 5, !, if …; then) was allowed because _program_positions did not step over wrappers or command keywords, so kirocrew behind one never took program position. An earlier round declared this out of scope on the rationale that "the floor has no shared wrapper-unwrapping resolver to reuse" — that was wrong: argv_rules._program_index already steps over the same _WRAPPERS table and keywords. That table and the skip now live in shell_normalizer_walk (_WRAPPERS, _program_index), and _program_positions reuses them — one definition, no second copy — so it agrees with _is_self_program. The widening is only the mint shape: a wrapper running a harmless subcommand (env kirocrew status, sudo kirocrew doctor) keeps main's allow, and the plain-verb form (env kirocrew token) was already refused on both main and head. Pinned by new rows in test_the_mint_is_refused and test_a_wrapper_with_a_harmless_operand_stays_allowed. On the interdiff, a scoped lane pass caught one real issue in a first attempt at the sudo --edit edge: making _program_positions pass an options view so the sudoedit rule fired under-refused sudo -E kirocrew "$(…)" — the floor lowercases before the walk, so -E (--preserve-env, which runs the command) collides with -e (sudoedit). That attempt was reverted. sudo --edit / -e / -E / --preserve-env=PATH with a mint-shaped operand are all refused (the fail-closed side; refusing a sudoedit form nobody mints with is safe), pinned in test_the_mint_is_refused. coproc worker kirocrew "$(…)" stays allowed because bash runs worker (a coproc name needs a compound command, verified on bash 5.2), while the no-name coproc kirocrew "$(…)" is refused.

@FIRST Principles — adopted your subtraction. The += machinery rode along out of the reported defect (a mint whose subcommand comes from a substitution); it is gone. The rows it refused return to main's verdict (table below).

@Opus — the quadratic path is deleted, not merely bounded: there is no _after_a_non_mint_subcommand(out, ...) call left, so the per-$-token rescan is gone. Added a budget pin, test_the_operand_resolution_is_linear_per_use, that drives the exact shape (an alternative-tracked program-only variable used as an operand many times over) at sizes whose command text runs past the 8 KiB classified cap, asserting linear interpreter work. The existing test_the_alternative_reads_each_body_once pins the per-assignment-body axis; the new one pins the per-use axis it missed.

Row table (verified by a three-tree probe: main / round-21-no-+= / this head)

Row main this head
T=$(: q; echo token); kirocrew $T (substitution defect) allow refuse the #36961 fix — kept
P=$(echo kirocrew); P=$(: q; echo token); kirocrew $P (F1 rebind) allow refuse kept
2>/dev/null T=$(: q; echo token); kirocrew $T (leading fd-redirect; also 1>&2, &>, {fd}>) allow refuse GPT-lane bypass, fixed here
kirocrew>/dev/null "$(printf token)" (redirect glued on program; also >&2, file target) allow refuse GPT/Opus-lane bypass, fixed here
env kirocrew "$(printf token)" (wrapper/keyword; also sudo/command/exec/nohup/time/timeout/!/if) allow refuse GPT/Opus-lane bypass, fixed here
env kirocrew status, sudo kirocrew doctor (wrapper + harmless operand) allow allow widening is only the mint shape
P=tok; P+=en; kirocrew $P refuse refuse baseline concat — kept
P+=token; kirocrew $P (no prior) refuse refuse baseline — kept
P=; P+=token; kirocrew $P (no stale) refuse refuse baseline — kept
P=kiro; P+=crew; $P token (program assembly) refuse refuse baseline — kept
P=$(echo kirocrew); P=; P+=token; kirocrew $P (empty-reset) allow allow returns to main
P=; P+=$(: q; echo token) (unresolvable tail) allow allow returns to main
P=; P+=$($Q) / P+=$(true) (opaque tails) allow allow returns to main
OPTS+=x; kirocrew $OPTS (unset base) allow allow returns to main
env/pod exec w/pod $(echo exec) status … $P (subcommand family) allow allow returns to main

Every row that returns to allowed is allowed on main, so the vs-main oracle stays at 0 refused-on-main → allowed (8524 allowed → refused unchanged). The grammar suite's pins for those rows moved from test_the_mint_is_refused / the old subcommand-refusal test into test_an_opaque_append_rides_along_as_main_allows, each with its reason; the two #36961 rows and the four main-refused rows stay pinned refused.

Gates on this head: pre-push gate green, prove.py PROVEN, semgrep 0, deny_diff 0 regressions, oracle 0 refused-on-main→allowed, full grammar + denied-command suites green, budget row +425.

GPT. The local lane ran on gpt-5.6-sol (CI pins gpt-6.1-sol, unavailable locally) and produced the redirect and wrapper findings above, now fixed and pinned. CI's gpt-6.1-sol reviewer separately declined the diff (a provider refusal, not a finding); clearing that needs a repository writer's /ai-review override gpt <sha> <class>: <reason> — not available to this fork PR from our side.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Round 25 on head 0d257a99d6da — both blocking review findings fixed in code, with pins. Parent unchanged; one amended commit.

@Opus (security regression, ssh self-protection) — fixed. A cased product name in a substitution body (F=$(which KiroCrew); …; bash -c 'sudo -H s?h local* id') bound on the lowercased walk but not the case-sensitive cased walk, so the two frame lists differed in length. The ssh floor's len(cased_all) == len(frames) gate then dropped every cased frame to None, read sudo -H (no value) as its lowercase twin -h (takes a value), shifted the program word off s?h, and allowed the self-connection. Fix: the ssh floor now pairs each lowercased frame with a cased frame that lowercases to it (consuming matches), instead of a length-gated positional zip — so the payload frame (sudo -H s?h …) stays matched to its cased twin regardless of an extra alternative frame, and the command is refused, as on origin/main. Pinned: test_denied_commands_security_ssh_self.py::test_a_cased_substitution_does_not_desync_the_ssh_payload_frame (your exact trigger; refused on main and here; a mutant restoring the positional zip re-allows it and fails). The two notes are fixed too: security.md now credits the substitution descent to the payload walk (_shell_payload_walk_uncached), not the mint reader; and the _raw_substitution_mint_name docstring no longer claims _substitution_bodies(..., raw_source=True) — it names the closers (_matching_close_paren / _matching_close_backtick) it calls directly.

@GPT (fenced, over-refusal) — fixed in code. An append that RETAINS or extends its binding cleared the variable's program-only provenance (_set_program_only(name, False) for a computed tail, (name, tail_aliases_program_only) for a literal), so TESTS=$(git ls-files 'test/*crew*'); TESTS+=$(true); pytest $TESTS -k token (and TESTS+='') was wrongly refused as a mint though $TESTS stays pytest's operand. Fix: on a keep/extend, combine the existing program-only flag with the tail's (base_program_only or tail_aliases_program_only); clear/re-derive only on a REPLACEMENT (the substitution names a protected program). Pinned allowed in test_main_residuals_stay_allowed: your TESTS+=$(true) trigger and the TESTS+='' form (allowed on main and here). The r22 refusals are kept — P=; P+=token, P=tok; P+=en, P+=token, P=kiro; P+=crew; $P token all still refused (the combine only preserves program-only where the base already had it, so it cannot loosen an append that builds the verb from an empty base). The fence clears on this push with no override requested.

Differential vs origin/main. Over the oracle corpus: 8,524 allowed→refused, 0 refused→allowed. Over the r25-affected rows (ssh / append / program-only): the only head-vs-main flips are the two intended #36961 tightenings (the quoted-substitution defect and the F1 rebind); your Opus trigger is refused on both, your GPT trigger allowed on both, and every genuine baseline refusal is refused on both.

Checks on this head: pre-push gate green, prove.py PROVEN, semgrep 0, deny_diff 0 regressions, full grammar + denied-command + ssh-self suites green.

Observation, not part of this PR: T=$(echo token); T+=$(true); kirocrew $T is allowed on origin/main and here, while the same command without the +=$(true) append is refused — a pre-existing main-side gap, out of scope for #36961.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Round 27 on head a460e18d63bc. Three of the four flagged defects are fixed; the r26 scope tracking is reverted to the r25 depth-based reading rather than patched further (why below), so a mint inside a { …; } group or a case arm returns to the same verdict as origin/main. Parent unchanged.

@Opus shell_normalizer.py:1195 / @GPT shell_normalizer.py:1193 (substitution through a declaration builtin) — fixed. export T=$(: "'"; echo token); kirocrew $T (and declare/typeset/local/readonly) mints, because _program_positions marked the builtin as argv[0]. The prefix test in _alternative_mint_overrides steps over the declaration builtins and their option words, so the assignment is read and the command is refused. Pinned in test_the_mint_is_refused.

@Opus argv_floor_ssh.py:1183 (ssh self-connection pairing) — fixed, strengthened in review. Two self-ssh payloads differing only in sudo option case de-dup to one lowered frame; the floor now judges the frame under each distinct cased twin and refuses if any refuses (the earlier None-for-ambiguous reading was fail-open for -h, whose lowercase twin takes a value). Opus probed 2,494 cases clean. Both -H/-h and -E/-e orderings pinned with mutants in test_a_deduped_lowered_frame_is_judged_under_each_cased_twin.

@GPT test_security_capture_oracle.py:227 (Bash dialect) — fixed. The oracle test now skips below Bash 4.1 (its corpus uses |& and {v}>&1), via BASH_VERSINFO.

@GPT shell_normalizer_walk.py:616 (leading clobber redirect) — documented residual. 2>| /dev/null kirocrew "$(printf token)" is allowed, as on origin/main. _program_positions reads dequoted tokens, where the clobber >| is byte-identical to a quoted > beside a real pipe, so a correct fix is quote-aware and belongs in its own PR (follow-up filed).

@Opus r26i (the }/esac glued-closer findings) — the r26 scope tracking is reverted to the r25 depth-based reading, not patched again. Across r26 the review lanes found a long tail of exotic edges in the r26 _raw_word_scopes scope tracking that was added to refuse a mint inside a { …; } group or a case arm (keyword-as-argument, esac-as-pattern, esac-as-selector, case-after-then, nested cases, a } before a glued redirect, a bare inner group's }, a } glued to a backtick, )} after a subshell). Each was individually fixable, but the tail is clearly long and each fix risked the next. So _raw_word_scopes is reverted to the r25 depth-based reading — the exact form that shipped on 0d257a99 and that both lanes passed there. A mint assigned inside a { …; } group, a case arm, or an uncalled function body is therefore left exactly as origin/main leaves it (allowed — documented residuals, follow-up filed). The depth walk still refuses the cases that are NOT inside an open group — e.g. X=$(case x in (x) :;; esac); T=$(: "'"; echo token); kirocrew $T and the backtick/)} forms r26i named are refused, because the substitution/subshell closes and the later top-level mint is read.

Residuals (same verdict as origin/main), each with the follow-up: a substitution-valued assignment inside a { …; } group or a case arm; an uncalled function body; and the leading clobber redirect 2>|.

Differential. vs origin/main: head refuses a superset — 0 commands that main refuses are allowed here. vs the prior pushed head 0d257a99: the r26 scope tracking is reverted to the r25 depth-based reading, i.e. back to exactly the walk that shipped on 0d257a99, so no refusal 0d257a99 had is lost, while the declaration-builtin and ssh union-refuse fixes add refusals on top. Oracle corpus (20,736 commands) vs 0d257a99: 0 refused→allowed.

Checks on this head: pre-push gate 26/26, prove.py PROVEN, semgrep 0, deny_diff 0 regressions, full grammar + denied-command + ssh-self + case-frames + capture-oracle suites green.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Round 28 on head 571d9e9e07f462d1edf63141e20ba5cbb3263ee9. The stall is real; one correction to the attribution, and the cost fix is deliberately NOT shipped because it loses refusals.

@Opus shell_normalizer.py:1128 (the alternative pass makes the gate cubic on never-closing substitutions).

By measurement, the cubic is not the alternative pass — main's baseline reader is equally cubic on the same input. is_denied, one call:

size main a460e18 (reviewed head) r28
~2 KiB (reps=200) 10.36s 10.77s 10.72s
~8 KiB (reps=800) >180s >180s >180s

main has no alternative pass yet is as slow. A cProfile puts the cost in main's _resolve_local_assignments → _mint_verb_in_substitution → _substitution_bodies (14.5M _iter_shell_chars calls). The alternative pass this PR adds is about 5% of the gate on the 2 KiB trigger (pinned by test_the_alternative_pass_is_a_small_share_of_the_gate).

We tried the cost fix you suggested — return None for an unproven body in _raw_substitution_mint_name — and reverted it, because both local lanes showed it drops refusals the prior head makes. It is a fail-open: "unproven" is not "unclosed". Three live-verified reproducers (allowed on main, REFUSED on a460e18, allowed under the shortcut):

F=$(: # )
printf kirocrew); $F token                    # folded-newline literal self-name
T=$(: ${x:-(}; printf kirocre[w]); $T token    # param-default paren, glob self-program
T=$(cat <<E
(
E
printf kirocre[w]); $T token                   # heredoc paren, glob self-program

Root cause: the reader folds a real newline to ; before the closer walk, and ; does not end a # comment the way a newline does, so the close is unprovable; bash closes it at the later ). And the lettered-glob self-program path (kirocre[w], k*) is read ONLY by the alternative, so the baseline does not backstop it. These are now pinned REFUSED by test_a_bash_closable_unprovable_body_mint_stays_refused.

So r28 ships: the alternative keeps a460e18's whole-remainder read (no verdict change — the corpus differential is verdict-identical to a460e18), plus the _judge_ssh_to_self cased-twin pairing-loop rename (tokens→lowered) that restores the "exactly one frame loop" cost invariant (the Backend shard-3 red). The baseline cubic is pre-existing on main and left to a main-side DoS issue, #39617, with the table, the profile, the fold/closer root cause and these three reproducers as the reason a cost-bound in the reader loses refusals.

No overclaim: the gate is still slow on this input — it is main's baseline cost; this PR neither adds nor removes that, and regresses no verdict.

Checks on 571d9e9e07f462d1edf63141e20ba5cbb3263ee9: pre-push gate 26/26, prove.py PROVEN, semgrep 0, deny_diff 0, corpus differential 0 flips vs main and vs a460e18 (verdict-identical to a460e18), full grammar + denied-command + ssh-self + case-frames + cost + capture-oracle suites green. Local confirmation lane APPROVE (both lanes already approved fix (B) at the prior head).

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Round 29 on head 4f04687c01df. Fixes the fork GPT F1; two review-note acks.

@GPT shell_normalizer.py:1418 — Program-only bindings disappear inside executable shell payloads — fixed. The alternative frame's nested bash -c "$F token" payload was descended as the bare script $F token without the program-only $F binding (_resolve_name returns the default for a program-only name off a program position, and the payload walk carried no binding into the inner walk), so is_denied returned None while bash ran ./kirocrew token.

Fix in src/kiro_crew/security/shell_normalizer.py: _shell_payload_walk_uncached now carries the alternative frame's bindings (the values and the program-only set) into the descent of that frame's nested payloads and resolves each program-only name at the INNER payload's program positions (_resolve_local_assignments gains seed_values / seed_program_only / bindings_out; _self_tokens threads them). Ordinary data operands stay unexpanded — _resolve_name is not widened to resolve program-only names at every operand.

Pinned by test_a_program_only_binding_is_carried_into_nested_payloads: the trigger, the plain ./kirocrew spelling, sh -c, and the same behind env / sudo / timeout 5 each refused, with a three-tree table (main / pushed 571d9e9e07f4 / this head) in the docstring; a mutant that drops the carried bindings makes each glob mint go allowed, and the data-operand control bash -c "echo $F" stays allowed. Differential: 0 refused→allowed vs the pushed head and vs main over the 20,736-command corpus.

One fail-closed over-refusal, recorded as a documented residual. The binding-carry refuses a non-executable command where the nested bash -c sits inside the binding's OWN initializer — F=$(: "'"; printf %s ./kirocre[w]; bash -c "$F token"); true — which is allowed on main and the pushed head but REFUSED here. It needs the pre-existing lone-quote tokenizer defect to mis-surface the initializer's own payload as a top-level carrier; $F is unset while that RHS runs, so bash never mints, and refusing a command bash does not run is the safe direction. It is recorded in "Not covered → Residual (over-refusal, fail-closed)" with the three-tree table and pinned REFUSED, rather than closed with another alternative-pass edge.

@Design — ssh-floor cased-twin behaviour named. Added a body subsection ("ssh self-connection floor: cased-twin judging") naming the _judge_ssh_to_self change (judge the lowered frame under each distinct cased twin, refuse if any refuses) for the ssh floor's owner.

@FIRST Principles — item 1 (the dequoted body scan as a second reader). Noted; it stays as-is in this PR for the union oracle and is deferred, no code change.

Checks on 4f04687c01df: pre-push gate 26/26, prove.py PROVEN, semgrep 0, deny_diff 0, corpus differential 0 refused→allowed vs main and vs the pushed head (8524 new refusals vs main; one fail-closed over-refusal class, documented above), full grammar + denied-command + ssh-self + case-frames + cost + capture-oracle suites green. Both local review lanes ran on this head: Opus APPROVE; the local GPT lane flagged the one fail-closed over-refusal recorded above.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Round 30 on head 3d406deee885. Fixes the fork Opus block.

@Opus shell_normalizer_walk.py:1655 (_raw_word_scopes) — the quoted-substitution mint can be undone by an unbalanced { or ) earlier on the line — fixed. _raw_word_scopes kept one running grouping depth for the whole line, so a bare { word (echo {; T=$(: "'"; echo token); kirocrew $T) left the depth open and a finished case (case x in x) :;; esac; T=$(...); kirocrew $T) drove it negative; every later word then read top_level=False, _alternative_mint_overrides skipped the real top-level T=, no alternative frame was built, and the baseline over-read the lone quote so kirocrew token was allowed.

Fix in src/kiro_crew/security/shell_normalizer_walk.py, in _raw_word_scopes: when the line's grouping does not balance — depth left open at the end, or it goes negative at any point (a bare {/} word, or a case pattern's )) — the running depth cannot place an assignment, so every word is treated as top level (fail-closed). The alternative frame is then built, and it can only ADD a refusal. This is your second suggested fix (build the frame when the scope can't be classified), not the per-command depth-counter rework.

Pinned by test_an_unbalanced_prefix_cannot_drop_the_alternative_frame: your two triggers plus a )-after-esac variant, each refused, with a three-tree table (main / pushed 4f04687c01df / this head) in the docstring; a mutant that restores the pre-fix per-word scopes makes each go allowed. Balanced groups stay allowed and pinned — an uncalled f(){ … } body, a function-keyword body, and a real { …; } group.

Differential over the 20,736-command corpus: 0 refused→allowed vs origin/main and vs the pushed head 4f04687c01df; the only new refusals are the unbalanced-line mints (not in the single-line corpus). The fail-closed reading pairs every never-closing assignment, so on the documented never-closing residual the alternative pass's share of the gate rises to ~31% at 2 KiB; it stays flat across sizes (29% / 31% / 31% at 1 / 2 / 4 KiB), the same order as main's baseline reader, which still dominates (the residual table in the body is updated).

Checks on 3d406deee885: pre-push gate 26/26, prove.py PROVEN, semgrep 0, deny_diff 0, full suites green. Local Opus review lane: APPROVE (verified independently — 0 refused→allowed vs origin/main and vs the pushed head across four corpora plus a 90-command prefix×tail combinatorial; all three balanced-grouping controls stay allowed; the pre-fix mutant flips the triggers to allowed; 723 tests pass).

The assignment resolver reads dequoted tokens, so a ' that bash reads inside
double quotes (T=$(: "'"; echo token)) opened a quote there, the program-name
scan ran into the use site, and $T was bound to the program instead of the
verb. When the substitution may produce both, keep the program reading and add
the verb after a bare use. The mint floor also reads a quoted substitution
argument (kirocrew "$(printf token)") through the shared substitution reader.
Only refusals are added.

test-only (r31): test_the_floor_binds_on_the_verbs_presence is sharded into 8
(sharing _MONOTONIC_SHARDS with the monotonicity sweep) after it hit the 120s
CI per-test timeout on shard 4 of head 3d406de -- the fail-closed rule
raised the gate's cost on the never-closing corpus. Each shard pins its own
slice's here-string residual via a structural _is_herestring_body predicate,
and the 120-body total is pinned by test_the_here_string_residual_total_is_pinned.
Per-shard call seconds on this host: s0 1.46, s1 1.38, s2 1.62, s3 1.44,
s4 1.51, s5 1.40, s6 1.59, s7 1.43 (max 1.62s, from ~12s unsharded). The proof
the test runs is unchanged.

Fixes kirodotdev#36961

Signed-off-by: Will Laws <will.laws@me.com>

This branch has not been deployed

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

Labels

fork Pull request from a fork (external contributor) readiness: action required A blocking check or review needs attention

Projects

None yet

Development

Successfully merging this pull request may close these issues.

credential-mint guard misses a mint whose subcommand comes from a substitution holding a quote

2 participants