Skip to content

fix(security): read a multi-line script a shell runs in the ssh-to-self floor - #35359

Open
jeeshofone wants to merge 1 commit into
kirodotdev:mainfrom
jeeshofone:fix/35314
Open

jeeshofone wants to merge 1 commit into
kirodotdev:mainfrom
jeeshofone:fix/35314

Conversation

@jeeshofone

@jeeshofone jeeshofone commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: The ssh-to-self floor refuses a self connection on a later line of a script that bash -c, sh -c, $SHELL -c, env -S or eval runs, as it already does on the first line.

On main, bash -c 'true + newline + s?h localhost id' is allowed while s?h localhost id alone is refused: a self-ssh hidden on line 2+ of a shell-run script, or on a line a ; & | separator hides from the whole-line read, slips the floor.

Why it matters

A script that bash -c runs is a common shape for agents. A self connection on any of its lines reaches the unsandboxed sshd on this host — a sandbox escape.

Not a goal

What changed (motivation → approach → change)

  • Symptom: a self connection on line 2+ of a shell-run script passes the floor.
  • Root cause: the floor judged only the first command of a -c body and the whole-line view, so a self-ssh on a later line, or behind a ; & | separator, was never read.
  • Change — three source files:
    1. argv_floor_ssh_bodies.py — a new folding, script-descending scanner (_folded_scanner_reaches_self) judges every executed-script payload, at the floor's own fail-closed DNS posture, so a self-ssh on a later line of a bare or nested bash -c / sh -c / $SHELL -c / env -S body — or after a ; & | — is refused. It also reads <( ) / >( ) process-substitution bodies, including a <+\-newline+( spelling, and folds each frame's line continuations first. Before it descends, _scanner_cut_nonshell_literals drops the literal body a NON-shell consumer only prints or writes (a quoted-tag here-document or single-quoted here-string of cat/tee/echo/printf or git commit -F -), under a positive plain-command-list rule, so printed DATA is not mistaken for a script (see "A non-shell consumer's literal body is not a script"). origin/main's own substitution reading (_substitution_reaches_self) is left byte-for-byte unedited and runs beside it as the fail-closed belt (see below).
    2. shell_normalizer.py — _shell_tokens takes one mode flag, preserve_structure, and keys its own per-call memo on the (cmd, preserve_structure) pair (so the two modes cache apart while once_per_call stays the single-argument base reader every other floor marks). DEFAULT drops every empty-quote pair unconditionally (_EMPTY_QUOTE_RE), byte-identical to the base ref, so every existing caller is unchanged. preserve_structure=True runs an escape-aware collapse, used by exactly one caller — the ssh-self fold scanner — to reach a self-ssh buried three or more bash -c levels deep without the default elision truncating the payload. A construct pre-check (_modes_may_diverge) lets the scanner reuse the default-mode tokens whenever a command carries no quote (the only construct on which the two modes differ). Both collapse passes are linear.
    3. deny_gate.py — the ssh-self reading's call is wrapped fail-closed (_SSH_SELF_UNREAD_NOTE), exactly as the rm floor wraps its own reader right below it: a crash in the fold, the literal cut, or the descent logs once and DENIES a command that could name an ssh verb (guarded by _cannot_name_ssh_verb), never propagating out of is_denied (S1).
  • argv_floor.py re-exports the two leg names and the descent entry (TYPE_CHECKING manifest only).

Why two readings (fail-closed belt, by construction)

The scanner folds line continuations before it scans, which changes the command shape it reads. A bug in that fold, or in the literal cut, can only ever lose a refusal origin/main already makes (a spelling main refuses on the unfolded stream, folded or cut into one the scanner then clears). Rather than chase each such spelling, the floor keeps origin/main's own reading as a second, independent leg:

  • _substitution_reaches_self's own source is left byte-for-byte unedited — git diff origin/main shows no hunk in that function and a drift pin asserts it stays identical to the base ref — so it does no line-continuation fold and takes no literal cut ahead of its scan. One shared helper it calls, _live_substitutions, does gain <( / >( recognition (the process-substitution reading the "Rows the single loop cannot refuse" rows need); that change is additive — every refusal the base reading made it still makes, and the 32k-row differential loses none. "Byte-for-byte" therefore names the function's own body, not the whole call graph: the belt leg reads uncut, unfolded text, which is what makes it a belt. There is no separate snapshot module; the belt is the base function un-forked, so nothing new joins the facade-owner family or any MCP server's startup import graph.
  • _reaches_self_over_ssh refuses if either the folding, script-descending scanner (_folded_scanner_reaches_self) or this base reading refuses.

So the scanner adds the refusals origin/main lacks (a self-ssh on a later line of a bash -c script), and the base reading guarantees no spelling origin/main already refuses can be lost to what a fold or a cut does to the shape. Two mutant pins hold the design: forcing the scanner leg off lets a later-line bash -c self-ssh through, and forcing the base leg off lets a split-heredoc glued terminator through. A full-verdict differential over 32,576 rows shows head refuses a strict superset of origin/main: 0 new ALLOW, +117 rows refused (head ⊇ base; later-line / process-substitution / continuation-split-verb self-ssh; an 18-row self-host DNS flaky set excluded).

A non-shell consumer's literal body is not a script

The fold scanner descends every executed-script payload, but a here-document or here-string a shell only hands to a NON-shell consumer is DATA, not a script: cat / tee / echo / printf, or git commit reading its message from stdin (-F - / --file=-), print or write it and run nothing. An earlier revision descended such a body too and over-refused a self-ssh quoted inside printed data — the GPT pre-push lane flagged it. _scanner_cut_nonshell_literals (scanner leg only, never the belt) now drops that body before payload discovery, under a positive plain-command-list rule:

The exemption fires only when, after the fold, the whole line is a plain command list: every command head is an ordinary word — not a compound keyword ({ } if then elif else fi for while until do done case esac coproc function select time !) and not a function definition (no argv[0] ending in ()) — no separator is a pipe (| / |&), and no >( / <( process substitution appears on the line. The head-check reads argv[0] only, so a compound word as a mere ARGUMENT (echo done) still allows the line. Within such a line it cuts only a TERMINAL consumer (its own separator ends the command: end-of-line / ; / && / ||) at frame depth 0, never inside a ( ) subshell or a $( ) / backtick substitution, and reads top-level commands only (read_commands(nested=False)), so a cat inside a $( ) that feeds a shell is left to the substitution reader.

Everything the rule excludes keeps its body and stays refused exactly as the pre-exclusion head refused it: a body a shell runs (bash <<'EOF', cat <<'EOF' | bash, bash <<<'...', $(cat <<EOF) into a shell), an UNQUOTED here-document whose $(...) runs, a grouped or piped consumer, a { } group or if/while/for/case compound or function body or coproc, and a tee >(bash) / cat > >(bash) writing to a process substitution.

Pins: six printed-data shapes are ALLOW pins and seven shell-consumer shapes REFUSE pins; a further ten grouped / piped / compound / process-sub shapes are REFUSE pins asserting the positive rule loses no head-added refusal. A strip-all mutant (cut every body, shell-consumer ones included) reddens the REFUSE pins, a strip-none mutant reddens the ALLOW pins, and a cut-even-inside-groups mutant (the loose pre-tightening shape, no plain-command-list guard) reddens the grouped/piped REFUSE pins. Because the rule only REMOVES text the scanner would otherwise descend — and the belt leg reads the uncut text — it lowers only head-added refusals and never takes the floor below origin/main's belt reading; a crash inside it denies via the S1 wrap. The cut is top-level only: it runs on the top-level line and never in a frame the descent reaches (on r36 it ran in every frame, so each leak spelling reopened one level down; _ONE_LEVEL_DOWN_REFUSE, 19 rows, and a cut-every-frame mutant pin this). A literal consumer body reached by DESCENT (a cat/tee/git commit -F - here-doc inside a bash -c) is read as a script, so a nested printed self-ssh is refused where main allows it: a deliberate safe false-deny, listed under "Over-refused by design" below. Five versions of a by-descent cut each leaked under independent review (piped or redirected carrier, an enclosing runner, glued/fd/quoted/./ path spellings, an earlier exec, & or trap sibling); every leak spelling is a DENY pin (_NESTED_LEAK_SPELLINGS_REFUSE, 19 rows, base ALLOW / head DENY).

Cost

The belt is two linear legs in two tokenizer modes. A shared-tokenizer memo keyed by (text, mode) with a construct pre-check (_modes_may_diverge) lets the scanner reuse the default-mode tokens whenever a command carries no quote — the only construct on which the two modes differ — so a command both legs reach is split once, not twice (an identity pin proves the shared tokens equal the unshared over the 32k corpus in both modes, with a mutant that shares a quote-bearing, mode-divergent command reddening it). The remaining second leg still reads its own frames, so the floor's total per-check work is about twice origin/main's single-leg cost.

origin/main bounds its single leg at chars ≤ 3× the input. This PR does not loosen that bound to the combined two-leg work: each leg is bounded on its own. origin/main's 3× is kept verbatim on its own leg (the substitution reading), and the same 3× is applied independently to the fold-scanner leg (test_each_leg_tokenizes_linearly_in_the_input, test_each_leg_judges_nested_substitutions_linearly). A flatness pin holds the ORDER at ≤ 2.2× work per input doubling, independent of the constant; per-leg counters show the ~2.0×-each split (about 2027 vs 2043 _self_tokens chars at eval count 200).

_self_tokens characters for the repeated-eval shape (eval × n; echo $(echo ssh)), origin/main (one leg) vs this head (two legs):

eval count n origin/main this head head / main main 2n/n head 2n/n
200 2,027 4,070 2.01× — —
400 4,027 8,070 2.00× 1.99× 1.98×
800 8,027 16,070 2.00× 1.99× 1.99×

Two readings of this bound point opposite ways; we lay both out for the maintainers rather than pick (a maintainer-decision red, below):

  • The a-ratchet-may-only-tighten lane reads one whole-floor counter and sees a bound that was 3× of a single leg now standing per-leg — the combined work reaching ~2× main's — as a loosening. We measured the alternative it implies (one whole-floor counter over the gate-level shlex.split characters, memo-deduped, the lane's own suggested shape): it reports 6.94× the input, worse than the 4.00× a plain two-leg character sum reports and far past any single 3× whole-floor bound — so one 3× whole-floor bound is unmeetable without deleting the second leg.
  • First Principles' clears-when demands that separate second leg (see "Rows the single loop cannot refuse": the one-loop collapse cannot refuse 7 rows this head refuses and over-refuses 1 benign row). The separate leg is the ~2× total.

So each leg is held at main's own 3×, the combined work is ~2× main's, and whether that reads as a ratchet loosening or as the duplication First Principles required is for the maintainers. Measurements: 4.00× (plain two-leg sum vs input), 6.94× (the lane's suggested whole-floor memo-deduped counter vs input), ~2.0× per leg, flatness 1.98×/doubling. Net src/kiro_crew/security/*.py line delta vs base: +635 (the budget row).

r36 omitted five forwarded names from the argv_floor manifest, red on CI shard 6; fixed in r37 (the manifest now names all forwarded argv_floor_ssh_bodies names, and the one shared-name test patch — _is_ssh_to_self — is routed through the facade).

Rows the single loop cannot refuse (First Principles clears-when)

First Principles asked whether the two legs could collapse into one — queue nested payloads with judge=True and drop the if live: guard on origin/main's _substitution_reaches_self, then delete the fold scanner. We ran exactly that subtraction on a scratch branch and measured the single loop against this head over the 32,576-row corpus (double-run, 18-row self-host flaky set excluded) and the r19–r33 pins. The single loop cannot refuse 7 corpus rows this head refuses, and over-refuses 1 benign row this head and main both allow (\n = newline):

row single loop this head
cat <(bash -c 'true\nssh localhost id') ALLOW REFUSE
tee >(bash -c 'true\nssh localhost id') ALLOW REFUSE
echo <(bash -c 'true\nssh localhost id') ALLOW REFUSE
cat < <(bash -c 'true\nssh localhost id') ALLOW REFUSE
diff <(bash -c 'true\nssh localhost id') x ALLOW REFUSE
grep x <(bash -c 'true\nssh localhost id') ALLOW REFUSE
bash -c 'true\n# "\necho " #"\nssh localhost id' ALLOW REFUSE
G1/G2 comment-continuation (bash -c '# x\+nl+ssh …', (true)#c\+nl+ssh …) ALLOW REFUSE
bash -c 'set -e\n# debug: ssh … localhost\nnpm test' (benign) REFUSE ALLOW

The single loop has no fold, so it misses the process-substitution and comment-continuation rows, and it reads a commented-out ssh in a single quote as live and over-refuses it. The fold scanner is a distinct leg doing real work; it is kept, not collapsed.

And not by reusing shell_normalizer._shell_payload_walk (First Principles clears-when, measured)

A later review asked whether the fold scanner duplicates the existing _shell_payload_walk and could be deleted in favour of judging its frames (_shell_payload_sources(text)[1:]) with _is_ssh_to_self, separators left live. We applied exactly that subtraction on a scratch branch off this head and measured it with the same 32,576-row corpus (double-run) and the r19–r36 pins. It loses no origin/main refusal (0 new-ALLOW vs main) — but it does not serve what the fold scanner adds: vs this head it loses 3 corpus refusals / 15 pin refusals and over-refuses 54 corpus rows / 4 literal pins, because _shell_payload_walk tokenizes without preserve_structure (so it never reaches a self-ssh 2+ bash -c levels deep), folds with _fold_line_continuations (not comment/heredoc-aware, so it misses the comment-continuation and split-verb rows), and has no literal cut (so it reads printed here-document data as a script). Representative rows (main / this head / subtraction):

command (\n = newline) main head subtraction
bash -c 'true\nbash -c "true\nbash -c \"true\ns?h localhost id\""' (3-level) ALLOW DENY ALLOW
bash -c '# x\<nl>s?h localhost id' (comment-continuation) ALLOW DENY ALLOW
bash -c 'true\n/usr/bin/s?\<nl>h localhost id' (split verb) ALLOW DENY ALLOW
cat <<'EOF'\nbash -c 'true\ns?h localhost id'\nEOF (printed data) ALLOW ALLOW DENY

Each of the fold scanner's parts (preserve_structure tokens, the comment/heredoc-aware fold, the literal cut, the <(/>( reading) is the sole decider of a named row, so none is duplicative. The subtraction would only become right if _shell_payload_walk itself gained structure-preserving tokens and a comment-aware fold — a change to main's shared walker, out of this PR's scope.

Continuation-split verb on a later line (GPT clears-when)

A verb split across a \-newline on a later line of a shell-run script (bash -c 'true\n/usr/bin/s? + \ + nl + h localhost id') must fold to s?h / ssh and be refused. The scanner leg carried a verb prefilter (_line_may_name_ssh_verb) that ran on the UNFOLDED line and short-circuited before the fold could rejoin the verb, so the self-ssh slipped.

Running the prefilter on the fold's output does not fix it: _fold_outside_quoted_heredocs is quote-aware and does not fold inside the outer bash -c '…', so the split verb stays hidden on the top-level folded line. So the prefilter is widened, not removed: it short-circuits only a line that names no ssh verb and carries no \-newline continuation — a backslash-newline being the one way bash splits a word across lines — so any line that could fold into a verb reaches the descent, while a verb-free, continuation-free data-consumer argv short-circuits and is not descended (removing the prefilter entirely made that argv sweep quadratic in the payload count; the widened form keeps it linear without reopening the slip). The verb gate sees through quote/backslash splices (ss""h, s\sh), so a quote-split verb is admitted too. The base leg keeps its own prefilter.

row main r33 head this head
bash -c 'true\n/usr/bin/s?\+nl+h localhost id' (later line) ALLOW ALLOW REFUSE
bash -c '# c\+nl+s\+nl+sh localhost id' (split after comment) ALLOW ALLOW REFUSE
/usr/bin/s?\+nl+h localhost id (unnested top level) ALLOW ALLOW ALLOW

The unnested top-level spelling is main's own whole-line path (it folds nothing there); this PR does not touch it — see "Not covered" and the main-side issue #39797. Pins: test_a_continuation_split_verb_on_a_later_line_is_refused, test_the_unnested_top_level_split_verb_stays_mains_allow, and test_restoring_the_scanner_verb_prefilter_reddens_the_split_verb (restoring the prefilter re-opens the slip).

A later line reaches origin/main's own host posture

A crontab schedule written through an unquoted cat <<EOF | crontab - heredoc names no ssh verb, yet the descent refuses it:

bash -c 'cat <<EOF | crontab -\n0 3 * * 0 /usr/local/bin/backup.sh\nEOF'

origin/main reads the * as a glob that could name an ssh verb and backup.sh as a dotted host whose DNS is unresolved, and fails closed — and it refuses the same line at top level (0 3 * * 0 /usr/local/bin/backup.sh). The descent only reaches that line on line 2+. test_crontab_data_heredoc_nested_verdict_equals_top_level pins the nested verdict equal to the top-level one, so this is main's own posture reaching a nested line, not a refusal this PR introduces (the same class as the branch-A rsync row below).

Backwards compatibility

Compatible: every existing caller of _shell_tokens keeps the base ref's default empty-pair elision (the deny view, the rm and git floors, and argv_rules are byte-identical to base); the only behaviour change is the ssh-self descent, which refuses more. A full-verdict differential over 32,576 rows against the base ref loses no denial.

Removes nothing: no command the base ref accepts is renamed or removed, and no path, record, key or public function is deleted; the diff only adds refusals of a self-ssh on a later line of a shell-run script, which is the Goal.

Tests

Proof: PROVEN — prove.py --base origin/main reverts the production hunks and the changed tests fail (head 78b7f13cc45a); among them test/test_deny_gate_call_sharing.py::test_one_gate_call_tokenizes_each_text_once and test/test_ssh_self_executed_scripts.py::test_a_self_ssh_on_a_later_line_of_a_run_script_is_refused and ::test_a_continuation_split_verb_on_a_later_line_is_refused.

Added or updated:

  • test_ssh_self_executed_scripts.py — a self-ssh on a later line of a run script (bare, nested bash -c, process substitution) is refused; a non-shell consumer's literal body (quoted heredoc / single-quoted here-string of cat/tee/echo/printf/git commit -F -) is NOT read as a script (6 ALLOW pins) while a shell-consumer body still is (7 REFUSE pins) and a grouped / piped / compound / process-sub consumer stays refused (10 escape REFUSE pins), with strip-all, strip-none and cut-even-inside-groups mutants; a crontab schedule on a later line is judged exactly as the top-level line; a comment-continuation does not hide a later-line self-ssh (G1/G2); a continuation-split verb on a later line is refused, the unnested top-level split verb stays main's ALLOW, and restoring the scanner's pre-fold prefilter re-opens the slip; a crash in the fold fails closed and logs once (S1); the base leg reads a self-ssh as its own substitution reading, a drift pin asserts it stays byte-identical to origin/main, and two mutant pins (scanner leg off / base leg off) each drop a refusal; a benign remote host and a local-only payload stay allowed.
  • test_deny_gate_call_sharing.py — one deny judgement tokenizes each (text, mode) once; preserve-structure tokenization runs only on a command that can name an ssh verb after folding (a verb present, or a \-newline continuation), not on a verb-free data consumer; the shared tokenizer matches the unshared over the corpus, and a mutant that shares a mode-divergent command corrupts the preserve tokens.
  • test_ssh_self_descent_cost.py — the two scan-linearity bounds bind EACH leg at main's own 3× (main's leg kept verbatim, the scanner leg bounded the same, with a shared memo), not one 5×-of-combined bound; a flatness pin holds the ORDER at ≤ 2.2× per doubling independently of the constant (a re-read-per-frame quadratic reddens it); a per-leg pin shows the ~2027 vs ~2043 split; a counter pin holds that a payload-heavy verb-free, continuation-free argv is not descended at all, with a mutant that widens the prefilter to always-descend reddening it.
  • security_line_budget/ssh-self-executed-scripts.txt — the +635 budget row, this diff's net line change under src/kiro_crew/security/.

Security-package invariant tests (these guard the facade and budgets the pre-push gate does not run, and all pass on this head):

  • test_argv_floor_facade.py — the owners are every argv_floor_* module in layer order, and every name served from outside the family is in the TYPE_CHECKING manifest.
  • test_facade_mirror_guards.py::test_every_mirrored_facade_manifest_names_what_it_forwards — every name the facade forwards is named in its manifest.
  • test_facade_shared_checks.py::test_every_scanned_name_reaches_every_module_reading_it — no module reads a shared name it does not bind.
  • test_mcp_listing_import_budget.py — the per-server module ceilings hold without being raised (belt-by-construction adds no new module to any covered server's startup import graph).
  • test_security_gate_liveness.py::test_the_package_stays_within_its_line_budget — the src/kiro_crew/security line count stays within its budget.

Manual verification

Local pre-push gate 26/0 (black, isort, flake8, mypy, comment-history, vendor and brand checks) on head f132752 (Gate proof: tree 3f0a491963bf; ran 26; skipped none). The full denied-commands test family plus the ssh-self / deny-gate / host-verdict / descent-cost / gate-liveness files pass (2,615 passed, 1 skipped), including test_security_gate_liveness.py::test_the_package_stays_within_its_line_budget. A full-verdict differential over 32,576 rows (head vs base) shows 0 lost refusals and +117 added; the 18 remaining rows are a pre-existing self-host DNS-timing flaky set, identical on both trees.

Two local pre-push review lanes (the CI fork lanes' own contracts, extracted from the base ref and run locally) reported no findings on this head. Both carry a served-model caveat, stated here plainly rather than left implicit: the Opus lane was served claude-opus-4.8 because claude-opus-5.5 was unavailable at run time, and the GPT lane's served model was not reported back by the backend — so to that extent local lane-green is weaker than a server run. What bounds whatever either lane could have missed is the structural guarantee, not the lanes: the differential is 0 new ALLOW vs base, and the literal cut is confined to the scanner leg while origin/main's legs read the uncut text, so nothing origin/main refuses can pass regardless.

Newly refused on a CI runner (branch A: a later line judged as the first line)

The Security Scope harness reports rows that flip base ALLOW → head REFUSE on a runner whose address list or DNS is not available at test time. Each is a remote host on a later line of a bash -c script, judged exactly as the first line would be — not a new rule. The representative row is the ubuntu rsync deploy one-liner (\n = newline):

bash -c 'set -e\ncd /srv/app\nrsync -az dist/ deploy@203.0.113.10:/srv/app/'

203.0.113.10 is a non-local documentation IP. On a runner whose kernel address list has not published, a non-loopback IP is refused fail-closed until the list loads — main's own host posture, unchanged here: git diff origin/main is empty on argv_floor_hosts.py, host_addresses.py, hosts_file.py, and the host-resolution path of argv_floor_ssh.py. This PR adds only the descent, which carries that first-line posture to a later line. Measured with the floor at BASE and HEAD, for the top-level rsync …203.0.113.10… and the nested row, in both address-list states:

ref addr list PUBLISHED (top / nested) addr list NOT published (top / nested)
BASE (merge-base) ALLOW / ALLOW REFUSE / ALLOW
HEAD ALLOW / ALLOW REFUSE / REFUSE

Base and head give the same top-level verdict; head's nested REFUSE equals its own top-level REFUSE (a later line is judged as the first line). The over-refusal of a non-local IP literal while the address list is unpublished is main's own posture; the main-side issue #39797 covers it. test_a_later_line_dotted_host_is_judged_as_the_top_level_line_is pins the equality. The r33 macOS deploy.example.com rows and the crontab backup.sh row above are the same class (an unresolved name/glob on a runner with no DNS).

Not covered (matches main)

These self-ssh spellings stay allowed on this head, allowed on main and the merge-base alike — pre-existing gaps this PR does not touch, not regressions it introduces:

  • Unnested top-level split verb — /usr/bin/s? + \ + newline + h localhost id typed directly, not inside a shell-run script. 3-tree verdict: base ALLOW · main ALLOW · this head ALLOW. This is main's own whole-line path, which folds nothing; this PR folds only inside the descent (shell-run payloads). Pinned by test_the_unnested_top_level_split_verb_stays_mains_allow; the main-side issue A non-local IP literal is refused as "self" while the kernel address list is unpublished #39797 covers it.
  • Array-subscript assembled verb — x=(s sh); ${x[0]}${x[1]} localhost id. 3-tree verdict: base ALLOW · main ALLOW · this head ALLOW.
  • Named-pipe / fifo round-trip — a consumer writes a self-ssh script to a fifo (cat >/tmp/p) that a separately backgrounded shell reads (bash </tmp/p &). Statically intractable: the write target is indistinguishable from a regular file a non-shell consumer writes (tee), so the literal cut cannot tell the two apart without tracking the fifo across commands. 3-tree verdict: base ALLOW · main ALLOW · this head ALLOW — left in the belt's and main's hands, not newly refused here.
  • Alias- or function-shadowed consumer — a command redefines a non-shell consumer in the same payload so it executes its stdin, e.g. shopt -s expand_aliases; alias cat=bash then cat <<'EOF'\n… ssh …\nEOF, or a git shell alias. The literal cut keys on the written head (cat), so it cuts a body the aliased cat=bash would run. 3-tree verdict: base ALLOW · main ALLOW · this head ALLOW — main allows it too, so it is a pre-existing gap this PR does not widen (0 new-ALLOW vs base), not a refusal introduced or lost here. A static exclusion is unsound: an alias/eval/source can redefine any head and a static check cannot track it. Control row (all three ALLOW): shopt -s expand_aliases; alias cat=bash; cat <<'EOF'\ns?h localhost id\nEOF.
  • Top-level write-then-run of a printed body: a top-level cat/tee writes a quoted literal body holding a multi-line bash -c self-ssh to a file, and a top-level sibling runs it: write-then-bash FILE, write-then-source FILE, an earlier trap 'bash FILE' EXIT, an earlier exec >FILE, or an earlier backgrounded bash FILE &. The top-level cut drops the body, so this head allows them, and so does main: equal to main, no fix. 3-tree verdict: base ALLOW · r36 ALLOW · r37 ALLOW, for all five. Control row (all three ALLOW), one line each: cat > /tmp/x <<'E' / bash -c "true / s?h localhost id" / E / bash /tmp/x. The piped form cat <<'E' | bash is refused on r36 and r37 (the plain-command-list rule).
  • Glob verb in an executed here-string: bash <<<'s?h -p 22 localhost id' (and the xargs -I{} bash -c {} form). 3-tree verdict: base ALLOW · r36 ALLOW · r37 ALLOW; equal to main, no fix (Opus lane, r37).
  • Host-name substitution in a heredoc a substitution feeds to a shell: bash -c "$(cat <<'EOF' / ssh -p 22 $(hostname) id / EOF / )". 3-tree verdict: base ALLOW · r36 ALLOW · r37 ALLOW; equal to main, no fix (Opus lane, r37). The literal-host spelling of the same shape is refused on all three.

Over-refused by design (GPT lane F2 clears-when)

A lone cat <<'EOF', tee FILE <<'EOF' or git commit -F - <<'EOF' whose quoted literal body holds a self-ssh, nested in 1-3 levels of bash -c, is refused on this head and allowed on main. The over-refusal is kept as a safe false-deny inside the 0-new-ALLOW bar; no override is asked. 3-tree verdict: base ALLOW · r36 REFUSE · this head REFUSE. Pinned as _NESTED_LITERAL_OVER_REFUSED (9 rows) in test/test_ssh_self_executed_scripts.py. It clears when a nested cut is proposed that also refuses every _NESTED_LEAK_SPELLINGS_REFUSE row.

Changes r36 -> r37

  • argv_floor.py: the TYPE_CHECKING manifest names the five forwarded ssh-bodies names (facade mirror guard, CI Backend Tests shard 6 red on f132752).
  • test_ssh_self_descent_cost.py: patches the shared _is_ssh_to_self through the facade.
  • test_ssh_self_executed_scripts.py: 19 nested leak-spelling DENY pins, the same 19 one level down plus a cut-every-frame mutant, 9 documented over-refusal pins, nested executed-form DENY pins at depth 2 and 3.
  • argv_floor_ssh_bodies.py: the literal cut runs on the top-level line only (judge False), not in descended frames.
  • Budget row +625 -> +635 (five manifest lines, five for the top-level-only cut).

Real-machine verification

N/A — unit coverage plus prove.py is sufficient and the change runs no network operation. prove.py reverts the production hunks and confirms the changed tests fail without them (head 78b7f13cc45a); run on the Linux dev host (aarch64).

Screenshots / video

N/A — the diff touches no user-visible frontend surface (no website/ changes and no committed screenshots).

Related Issues

Fixes #35314

Sibling sweep

Swept: git grep -nE "_nested_shell_payloads|_substitution_reaches_self" -- src/kiro_crew/security/ for other paths that read a shell-run payload without descending into its later lines.
Sibling: none — the descent is the single entry the floor uses to read executed-script payloads; the first-line and whole-line self-ssh readers already in place are the only other paths and are unchanged.

Pattern harvest

Not generalizable: the defect is specific to the ssh-self floor reading only the first command of a multi-line -c body; it is a one-off in this floor, not a class across the codebase.

Checklist

  • A Conventional Commits title (fix: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable) — N/A, no doc is made stale
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributions are accepted under the project's Apache-2.0 license; see the Licensing section of CONTRIBUTING.md.

Takeover

This PR touches src/kiro_crew/security/* from a fork. Three reds on it are maintainer decisions, not defects this branch can clear:

  1. Sensitive Change Review refuses fork PRs on src/kiro_crew/security/* by design — nothing posted here clears it (the same hold as fix(security): refuse a mint whose verb comes from a quoted substitution #37077). The route to green is a maintainer takeover: a branch on this repository that keeps the author with Co-authored-by and writes Supersedes #35359, runs the real sandbox, and drives the PR green.
  2. The per-leg descent-cost bound (a-ratchet-may-only-tighten lane) — the lane reads the combined ~2× work as a loosening; First Principles reads a single bound as a demand to delete the second leg it requires. Both readings are laid out under "Cost" with the 4.00× / 6.94× measurements; the maintainers choose. We ask for no override.
  3. The main-side top-level split-verb / unresolved-host posture (A non-local IP literal is refused as "self" while the kernel address list is unpublished #39797) — the branch-A and crontab rows are main's own first-line posture reaching a later line, pinned equal; the fix belongs on main, tracked in A non-local IP literal is refused as "self" while the kernel address list is unpublished #39797.

Every other check and lane on this head is green locally — gate 26/0, prove PROVEN, the full-verdict 32k differential against base loses no denial (only the intended ssh-self tightenings), and both local pre-push lanes report no findings (with the served-model caveats stated under "Manual verification": Opus served 4.8, GPT served-model unverified). We ask for no override on any of the three reds above.

@jeeshofone
jeeshofone requested review from a team and KiroCrewCaptain as code owners November 11, 2026 16:07
@github-actions github-actions Bot added the fork Pull request from a fork (external contributor) label Nov 11, 2026
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — 🔴 changes requested (blocking)

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

1 of 2 blocking finding(s) are security-class and were withheld from adjudication, so the blocking verdict stands.

BLOCKING -- src/kiro_crew/security/argv_floor_ssh_bodies.py:466 -- The literal exemption discards a script executed by a later command
cuts.append((doc.start, doc.end))
Trigger: On a host permitting authenticated self-SSH, submit cd /usr/bin; cat > /tmp/script <<'EOF'\nbash -c 'true\ns?h localhost id'\nEOF\nbash /tmp/script. is_denied → _reaches_self_over_ssh → the folding scanner cuts the heredoc because cat ends at a newline, despite its file output being executed immediately afterward.
Outcome: The gate allows the command. The later shell executes the discarded body, s?h expands to ssh, and the connection runs id through the unsandboxed host sshd. Keeping this body in the scanner produces a denial.
Anchor: residual/security
Fix: Retain file-writing consumers’ literal bodies when their output is subsequently executed; a command-ending separator does not establish that its output remains inert.

BLOCKING -- src/kiro_crew/security/argv_floor_ssh_bodies.py:526 -- Descended frames mistake quoted heredoc data for executable commands
if judge and _is_ssh_to_self(view):
Trigger: Submit bash -c 'cat <<"EOF"\ns?h localhost id\nEOF'. is_denied → the folding scanner descends into the -c operand and judges its complete text without excluding the quoted heredoc’s inert body.
Outcome: The base allows this command and Bash only prints the literal text, but the new scanner denies it as self-SSH, preventing a harmless data/documentation operation.
Anchor: defect
Fix: Preserve enclosing execution-sink context during descent and exclude quoted literal bodies when neither the consumer nor an enclosing command executes their output.

[BLOCK-MERGE] f9d443d
[GPT-REVIEWED] f9d443d

Adjudication (Opus 5.5) — is blocking on each finding proportionate?

F2 is a false denial that the PR records on purpose. F1 is a real bypass that can happen in ordinary use, so it keeps blocking.

F2 — harm LOW. The failure needs a quoted heredoc body that is only printed, nested under a bash -c. The cut that drops a printed body runs only on the top-level line: the frame is cut only when judge is False (patch argv_floor_ssh_bodies.py:513-515), so any body reached by descent is read as a script. The PR pins exactly this over-refusal in test_a_nested_literal_body_is_over_refused_as_documented (patch :2231-2257). When it happens, the command is visibly denied rather than run wrongly, and the agent can rephrase it without the nesting. Nothing is lost or exposed, and it never allows a command the base denied.

The real fix would mean carrying execution-sink context through every level of descent. The PR's own notes say five versions of a cut inside descended frames each leaked a body a shell runs, and pin those spellings as DENY (patch :2154-2228). That is a security-path mechanism whose cost is far above a loud, recoverable false denial.

F1 (fenced) — the top-level cut does apply here. cat is in the non-shell consumer list (patch :372), its separator is a newline so it counts as terminal (patch :431), and its quoted heredoc is cut (patch :433-435). Nothing checks whether a later bash /tmp/script runs the file that cat wrote. Writing a script to a file and then running it is an everyday agent pattern, not an extreme combination.

[ADJUDICATION] f9d443d353ed35545ac3e0b954935b7f5ff5d78f total=1 uphold=0 downgrade=1
DOWNGRADE F2 src/kiro_crew/security/argv_floor_ssh_bodies.py:526 reason=disproportionate-remedy
[GPT-ADJUDICATED] f9d443d353ed35545ac3e0b954935b7f5ff5d78f

[ADJUDICATION-FENCED] f9d443d353ed35545ac3e0b954935b7f5ff5d78f fenced=1 flagged=0
UPHOLD-FENCED F1 src/kiro_crew/security/argv_floor_ssh_bodies.py:466 -- The top-level cut drops a terminal cat's quoted heredoc (patch :431-435) with no check of whether a later command runs the written file, and writing a script to a file then running it is an ordinary agent pattern, so the self-SSH allow is plausible in real operation.
[GPT-ADJUDICATED-FENCED] f9d443d353ed35545ac3e0b954935b7f5ff5d78f

@KiroCrewCaptain

KiroCrewCaptain commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Code change approver recommendation: A person should decide

Why: the decider raised 1 possible problem and estimates a 62% chance that this change introduces a defect; a person should check it.

I'm requesting changes. The new walk fixes the multi-line bash -c / eval / "$(cat <<EOF)" shapes it tests. It doesn't weaken anything main refuses, because main's reading runs first and is unchanged, and the change to _nested_shell_payloads gives existing callers the same results. One path in the PR's own scope is still allowed: a multi-line script that bash -c runs inside an unquoted $( … ) or backtick substitution that a printer receives (echo $(bash -c 'true\nssh localhost id')). Bash runs it, but the floor allows it, for two reasons. _wrapped_printer_argument gives the printer exemption to a carrier word that opens with a substitution, which main's _data_consumer_exempt deliberately refuses. And substitution bodies are taken from text whose single-quoted contents are blanked, so the script never reaches the next frame. No test covers this shape.

Things to check (possible problems the decider raised; at this confidence many turn out to be fine):

  • Bash runs a multi-line bash -c script inside an unquoted command substitution that is a printer's argument, e.g. echo $(bash -c 'true\nssh localhost id') or the backtick form. The floor still allows it. At the top level, shlex gives the tokens echo, $(bash, -c, true\nssh localhost id), a (src/⁠kiro_crew/security/argv_floor_ssh.py, high)

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 248f5ce2dd · 558 changed lines. If this recommendation is wrong, reply here: replies are how the approver is evaluated.

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — 🔴 changes requested (blocking)

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

BLOCKING: two cost ratchets were loosened so the added second ssh-self leg would fit. Each was rewritten so the extra work it used to refuse now passes.

BLOCKING — test/test_ssh_self_descent_cost.py:95
belt = leg_tokens(_bodies._substitution_reaches_self) / assert 0 < belt <= 3 * len(cmd) … scanner = leg_tokens(_bodies._folded_scanner_reaches_self) / assert 0 < scanner <= 3 * len(cmd) (the same split at :69 replaces the deleted test_nested_substitution_work_grows_linearly)
Trigger: "eval " * 200 + "echo $(echo ssh)" goes through is_denied → _reaches_self_over_ssh. The belt finds no self-ssh, so _folded_scanner_reaches_self runs as well, and both legs call _bodies._self_tokens. The deleted test held the whole gate (_denied_by) to chars <= 3 * len(cmd). The new test_the_two_legs_each_do_about_half_the_descent_work requires each leg to do at least 1500 of the 3048-character budget, so the two legs together sit at or past that bound.
Outcome: the gate-level bound becomes two per-leg bounds, which together allow up to 6× the input per call instead of 3×. The synchronous deny gate now does about twice the tokenization work on every ssh-verb command, and no test can fail on it.
Anchor: a-ratchet-may-only-tighten
Fix: keep the gate-level <= 3 * len(cmd) assertion on _denied_by / _reaches_self_over_ssh, and bring the combined work under it, for example by having both legs share one frame tokenization and descent. If that cannot be done, state the loosening on the PR rather than splitting the bound per leg.

BLOCKING — test/test_deny_gate_call_sharing.py:172
per_text_mode = Counter(counting.records) / assert max(per_text_mode.values()) == 1, dict(per_text_mode) (replaces assert len(counting.split_texts) == len(set(counting.split_texts)))
Trigger: the new bash -c 'ssh far.example.com id' row contains a quote, so _modes_may_diverge returns True. The outer text is then shlex.split twice in one gate call: once by the belt through _shell_tokens(cmd), and once by the scanner through _shell_tokens(cmd, preserve_structure=True). With no empty-quote pair in the text, both calls return the same tokens.
Outcome: the old rule was that each text is split once per gate call. It is weakened to once per (text, mode), so a duplicate split with identical output is now allowed, and so is any future one in a second mode.
Anchor: a-ratchet-may-only-tighten
Fix: keep per-text uniqueness. Make _modes_may_diverge check for an actual empty-quote pair ("", '', \"\", \'\') instead of any quote character, so the scanner reuses the default split whenever the two modes would agree.

[BLOCK-MERGE] f9d443d
[OPUS-REVIEWED] f9d443d

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Security Scope Review (fork) — ✅ PASS

Which legitimate operations does f9d443d353ed35545ac3e0b954935b7f5ff5d78f 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: The tightening sits entirely inside the shell-command deny composite (the sandbox-escape-ssh-self argv floor). That means every candidate gets a script verdict, and none was handed to me confirmed as newly refused. The PR's own tests pin one over-refusal: a lone cat/tee/git commit -F - of a quoted literal body nested one to three bash -c levels deep is now refused. I can argue that as a narrowing but cannot confirm it as a row, so it is named below and does not block.

TIGHTENING: The ssh-to-self floor gains a second leg, _folded_scanner_reaches_self. It folds \-newline continuations, opens unquoted <( ) / >( ) process substitutions in _live_substitutions, and descends into every executed script payload (bash -c, sh -c, eval, env -S, here-strings, heredocs). The floor also now fails closed when its reader raises (hit = not _argv._cannot_name_ssh_verb(lower)). The one threat it refuses is a self-ssh hidden on a later line of a script a shell runs. The hunk is + if _folded_scanner_reaches_self(text_lower) or any(map(_folded_scanner_reaches_self, scripts)):\n+ return True, and its headline pin is "bash -c 'true\n%s %s id'" % (_G, _L) # issue headline: glob verb on line 2.

ADJUDICATED: 23 candidates, 0 confirmed newly-refused

PLATFORM COVERAGE: posix and windows. No row is any, because every candidate names an executable or a path.

  • posix (17 rows): the diff's own assumptions are bash's. These rows cover:
    • folding a \-newline before parsing: continued docker run, uv run pytest and rsync lines;
    • a remote ssh or scp on a later line of a multi-line bash -c / bash -lc script;
    • <( ) process substitution, including the done < <(...) idiom and a remote ssh inside <( );
    • comment dropping inside a bash -c script;
    • the quoted-heredoc-in-$( ) commit and PR-body spelling agents use, with ssh named in prose;
    • a git commit -F - stdin body;
    • a heredoc piped to crontab -, where the pipe turns the literal cut off;
    • a nested quoted heredoc written to a docs file.
  • windows (6 rows): the scanner applies bash's lexing to every host, so these rows probe where Windows spellings collide with it:
    • PowerShell's backtick-newline continuation, which the new scanner reads as a backtick substitution opener, both with and without an ssh;
    • a directory path ending in \ followed by a newline, which the posix fold would join to the next line;
    • .\ backslash paths on scp;
    • the ssh.exe name;
    • %USERPROFILE%\.ssh\config.

I wrote no row for cmd.exe ^ continuation. The diff makes no assumption about it, and the posix fold does not treat ^ specially. That is the one Windows spelling left unprobed.

UNADJUDICATED: These are not separate guards; each sits inside the adjudicated floor. They are things the rows could not carry or that I chose not to submit:

  1. Documented nested-literal over-refusal. A non-shell consumer's quoted heredoc or here-string reached by descent (bash -c "cat <<'EOF' ... EOF") is always read as a script. The literal cut in _scanner_cut_nonshell_literals runs only on the top-level frame (text if judge else _scanner_cut_nonshell_literals(text)). The PR's own _NESTED_LITERAL_OVER_REFUSED pins say base allows these and head refuses them. The bodies in those pins print a self-ssh to this machine, so I did not write that shape as a row: that would encode the threat. The probe I did submit is the same nested shape with a remote host (bash -c "cat <<'EOF' > docs/remote.md ..."). If the harness confirms that one, the narrowing is to let the cut run in a descended frame only when that frame is itself a plain, terminal, unpiped command list.
  2. Fail-closed on a reader crash. In deny_gate.is_denied, an exception inside _reaches_self_over_ssh now refuses any command that _cannot_name_ssh_verb does not clear, with the note "Refused because the ssh-self check could not read this command." Which real inputs would crash the hand-written fold cannot be expressed as a candidate. It shows up only if one of the submitted rows happens to trip it. I propose no row for it.

[SCOPE-REVIEWED] f9d443d

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): 6 classified, 0 newly refused, 6 still allowed.

No candidate flipped from allowed to refused.

@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 labels Nov 11, 2026
@github-actions github-actions Bot removed the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Nov 12, 2026
@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of f9d443d353ed35545ac3e0b954935b7f5ff5d78f 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 subject is a real shell command line, the scanner only adds refusals beside the base reading it leaves unchanged, and a crash fails closed.

Notes

  • _fold_outside_quoted_heredocs is a second hand-written bash lexer with its own heredoc, comment and arithmetic stack. It sits beside read_commands(heredocs=True) and _iter_shell_chars, so any later fix to bash's heredoc or comment rules now has to land in two places. Folding continuations inside the shared reader would leave one lexer.
  • The two-reading design is permanent: every ssh-shaped command pays for two descents. The cost tests bound each reading at 3x on its own, so neither alone can drift past main's bound, but together they may do about twice main's work. Collapsing to one reading would need its own decision and its own differential run.
  • shell_normalizer.py is already at 1,800 lines and this patch adds about 135 more. The empty-quote collapse helpers (_drop_empty_quote_pairs and the rest) are one responsibility that could move to a sibling module.

[DESIGN-REVIEWED] f9d443d

Description read

[DESCRIPTION-READ] 2979a7943280f45328b273e581be56a9bf95b260d827cf0cb40d25ffe37aab24

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 commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — 🔴 BLOCK

Premise-level review of f9d443d353ed35545ac3e0b954935b7f5ff5d78f 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: BLOCK

The diff adds a third nested-payload descent for the ssh-self floor, with its own continuation fold, its own process-substitution reader and a new tokenizer mode. _shell_payload_walk already descends every executed payload at any depth.

Not checked: whether _shell_payload_sources frames reach the 3+-level bash -c rows (claimed truncated); I had no shell to run them.

Notes

  • Item 6 — undeclared: "base ALLOWS these; r37 REFUSES them". A cat/tee of a quoted body nested 1–3 levels is now refused.
  • Item 8 — one consumer, generalized: "Exactly ONE caller passes it". The truncation it works around stays in _shell_payload_walk, which 3 files use (argv_floor_self.py, argv_rules.py, argv_floor_ssh.py).

What this change ships

Inventory (9 items) — 3 justified

Intent: refuse a self-ssh on any line of a script a shell runs, not only the first. FIX.

  1. Self-ssh on a later line of a bash -c/sh -c/$SHELL -c/env -S/eval script refused — justified
  2. Self-ssh in <( )/>( ), including <\-newline-(, refused — duplicate of shell_normalizer.py:1676 _substitution_bodies(_fold_line_continuations(...))
  3. Split $(, split verb on a later line, and comment-continuation cases refused — rides along
  4. New heredoc/comment-aware fold _fold_outside_quoted_heredocs — duplicate of _fold_line_continuations
  5. Top-level cat/tee/echo/printf/git commit -F - quoted bodies exempted — justified
  6. Nested printed literal bodies now refused — undeclared
  7. Crash in the ssh-self reading now denies with a new "could not read" note — rides along
  8. _shell_tokens(preserve_structure=) plus keyed memo and _modes_may_diverge — one consumer, generalized
  9. Linearity pins moved to a new file, 3x bound kept per leg — justified

Blockers

Third payload descent beside the shared walker — duplicate of shell_normalizer.py _shell_payload_walk
Trigger: grepping _nested_shell_payloads\( gives 7 sites; the floor already descends twice, through _self_token_frames in _is_ssh_to_self (argv_floor_ssh.py:1151) and through _substitution_reaches_self (argv_floor_ssh_bodies.py:134). The diff adds a third, _folded_scanner_reaches_self, with its own fold, procsub reader and tokenizer mode. The description says the new descent "can only ever lose a refusal", which is the reason it keeps the base reading as a "belt".
Outcome: one floor ends up with three walkers that split commands differently and will drift apart. The shared walker's comment says walking once "is what keeps the two floors from drifting".
Subtraction: delete _folded_scanner_reaches_self, _fold_outside_quoted_heredocs, _PROCSUB_CONTINUATION_RE, the <( branch in _live_substitutions and the preserve_structure mode. Judge each frame from _shell_payload_sources (argv_floor_self.py:260) as its own line instead.
Clears when: the floor judges the frames of the existing _shell_payload_walk, or the PR shows a refused row that walker cannot reach.

[FIRST-PRINCIPLES-REVIEWED] f9d443d

Description read

[DESCRIPTION-READ] 607ccfd4c55ed424083fdbc4e3643f858773481c2b38757cf58adb2377618423

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 12, 2026 •

Copy link
Copy Markdown
Contributor

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

Revision f9d443d353ed35545ac3e0b954935b7f5ff5d78f 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 added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Nov 12, 2026
@jeeshofone

Copy link
Copy Markdown
Contributor Author

Replies to the review lanes on 387b32b. New head: 803d5ef.

GPT 6.1, F2 (single-quoted backticks that a printer only prints): fixed. The new walk no longer looks for command substitutions inside single-quoted text, since bash runs none there. It reads quoting through the shared _iter_shell_chars state machine. printf '%s\n' 'bash -c "…"' is allowed again, and the double-quoted twin, which bash does run, is still refused. Both are tested.

GPT 6.1, F1 (echo "$(bash -c '…')" with a double-quoted substitution): declined, out of scope. Main allows this too. _substitution_bodies keeps the closing )" in the body, so the body doesn't tokenize. Fixing that changes the shared substitution reader and belongs in its own change. This PR only adds refusals and doesn't loosen this case.

Opus 5.5 (a git commit -m heredoc that quotes a bash -c repro): not reproduced. The trigger uses a literal ssh localhost, and main's regex tier already refuses that spelling. With the glob spelling (s?h localhost), which only the floor reads, main and this head both allow the command.

Security Scope Review (bash -c 'set -e / ssh -T git@github.com'): not a regression in the floor's logic. The floor refuses a dotted hostname while its first DNS check is still pending, and the CI harness hit that. Main refuses the bare ssh -T git@github.com the same way on first sight. With DNS resolved, main and this head both allow the wrapped form.

@github-actions github-actions Bot added readiness: checking Automated validation is still running 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: action required A blocking check or review needs attention merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: checking Automated validation is still running labels Nov 12, 2026
@jeeshofone

Copy link
Copy Markdown
Contributor Author

Replies to the GPT 6.1 findings on 803d5ef. New head: 021d131.

F1 (a ; glued to the printed script lets a later, unrelated | sh cancel the printer exemption): fixed. A ; or & glued to the end of a word now ends that command, but only when the character is outside quotes. The check reads main's quote-masked spelling of the same text (_mask_quoted_separators). If the two spellings don't tokenize alike, the boundary isn't used and the stricter reading stays. echo bash -c '…'; echo true | sh is allowed again. A quoted ; piped into sh (echo bash -c '…;' | sh) is still refused. Both are tested.

F2 (backticks inside ANSI-C $'…' quoting read as a substitution): fixed. ANSI-C quoted text is now blanked before substitutions are extracted, the same way plain single-quoted text is. printf '%s' $'bash -c "…"' is allowed again and is tested.

This head also adds the three new argv_floor_ssh names to the argv_floor facade manifest, which test_facade_mirror_guards.py checks (Backend Tests 3.12, shard 7). These last changes had no local review, so CI's Opus and GPT lanes cover them.

@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 merge conflict Branch has merge conflicts with its base — author must resolve before merge labels Nov 12, 2026
@jeeshofone

Copy link
Copy Markdown
Contributor Author

Re-integrated onto current main. This fix converged before #37558 landed its own rewrite of the same floor, so a textual rebase was not possible; the three changes (the multi-line script walk, the git-stdin heredoc always-read, and the top-level-only forge message exemption) were re-applied by hand onto #37558's structure. Every line the walk finds is now read under both the quote-aware reading and #37558's "as the shell's own reader sees it" pass, with that pass, the DNS policy and the reading depth all in the memo key so a verdict is never reused across readings; the quoted-substitution body readers #37558 moved into the shared command reader are imported rather than duplicated; and a self host named inside a walked line is answered from everything the gate can settle in the same call, so a remote host the floor allows at the top level is not refused on first contact. An oracle sweep over 32,576 command rows against the new main, at two hash seeds, showed zero commands that main refuses becoming allowed, and 113 newly refused, every one in the declared set.

Local review lanes, rounds 22 through 25, all closed:

  • r22: a bash -c script inside an unquoted $( ), backtick or <( ) was blanked away; a here-doc piped to a stage that writes into a process substitution (| tee >(bash)) was cut as data; a walked line skipped the hosts-file, kernel and cached-DNS answers. All fixed.
  • r23: a command consuming a substitution's output that reaches a shell (> >(bash), a group piped to a shell); ssh's remote command not ending at a glued ;/&; a # inside ${…}/$((…))/arithmetic read as a comment. All fixed.
  • r24: the same class reached through a function definition, an exec/coproc/high-fd redirect, or a # inside backticks. Closed by failing closed on any output route the per-command walk cannot follow.
  • r25: both lanes returned no findings. The one performance note (an adversarial within-cap process-substitution flood runs about 1.5x the base's time, the same quadratic class the base already has) was judged non-blocking and is disclosed in the description.

Final verdicts on the pushed tree: both review lanes no findings; the full targeted security suite green; the bug-reproduction proof holds (the pins fail with the production hunks reverted); the dependency scanner clean.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Updated the empty-quote handling to split by caller, not grammar, which closes the
eval 'sh -c ...rm ''-rf /' deny-view loosening the last review found.

_shell_tokens(cmd, *, preserve_structure=False):

  • DEFAULT drops every empty-quote pair from each dequoted token unconditionally
    (_EMPTY_QUOTE_RE), byte-identical to the base ref. Every existing caller keeps this —
    the deny-matching view (_deny_segment_views), the rm floor, argv_rules._commands,
    the git-publish floor, and the tool-prefix readers — so they all see the canonical
    command shape. eval 'sh -c ...rm ''-rf /...' now renders rm -rf / for the
    rm -rf /.* rule and is refused again. argv_rules._commands is reverted to base (no
    memo): with the default elision the rm nest truncates as it does on base, so the earlier
    2^depth walk cannot occur.
  • preserve_structure=True runs the escape-aware collapse instead
    (_drop_empty_quote_pairs before tokenizing, _drop_token_empty_pairs per token), which
    still folds a respelt verb (s\"\"sh → ssh, rm\"\" → rm, rm'""' → rm) but leaves a
    nested bash -c "..." script's structural closing quotes intact. Exactly one caller
    passes it — the ssh-self descent (argv_floor_ssh_bodies._substitution_reaches_self, via
    _self_tokens/normalize_shell_command) — so it reaches a self-ssh buried three or more
    bash -c levels deep without truncating the payload. Both passes match each maximal run
    of pairs once with no trailing look-around, so the scan is linear.

once_per_call now keys on all arguments, so the two modes cache apart; it is unchanged
for every one-argument reader.

Evidence on the current head:

  • Full-verdict differential over the 32,576-row corpus, head vs base: 0 denials lost
    (any rule). All changes are ssh-self tightenings — 121 new allow→deny plus 26 rows base
    denied under another rule, now attributed to ssh-self, all still denied.
  • ssh-self oracle: 0 refused→allowed on both hash seeds; 147 allow→refused, identical to
    the previously accepted set (146 self-targets + the one documented crontab-heredoc row).
  • 19 hole pins main-red on base; the four escaped-respell refusals and the eval rm -rf /
    row pinned; benign 4-level remote ssh stays allowed.
  • Gate 24/0, prove PROVEN, semgrep 0, flake ratchet 75, broad security suite + push-gate
    green.
  • Independent opus review of the final head: no findings, and judges the flag a
    single-tokenizer split, not a second grammar.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Update — head 68b615ea50c8 (r27)

The PR body was rewritten to describe this head's diff; the earlier body carried the pre-reintegration design (a walk-module move into argv_floor_ssh_walk.py, the git/heredoc "Q1" and gh/glab "Option C" cases, and a security.md update) that this head does not ship.

Versus the prior head 9ff8a7b07c, this head changes only the once_per_call memo key in call_views.py (each marked reader's keyword-only defaults are folded into the key, so one deny judgement tokenizes each (text, mode) once, with no per-read signature bind on the hot path); the ssh-to-self floor behaviour is unchanged. Local gate 24/0, prove PROVEN, the core security suite passes including the package line-budget test, and a full-verdict differential over 32,576 rows shows 0 verdict changes on every deterministic row versus 9ff8a7b07c.

Classification of the four prior review reds on 9ff8a7b07c

  • GPT 6.1 Review (— changes requested (blocking)): finding "Restrict script judging to actual local execution" at argv_floor_ssh_bodies.py:181. This is a conservative over-refusal; the lane's own adjudication records it as "an over-refusal, not an exposure … the residual-risk profile a human could accept." The floor is fail-closed by design — a self-ssh verb is refused wherever a shell-run payload carries it, because a printed script can be eval'd and a nominally remote host can be a self alias. No code change.
  • Opus 5.5 Review (— changes requested (blocking)): finding that _drop_empty_quote_pairs "collapses '' without tracking quote state … the self-ssh sandbox escape is allowed" at shell_normalizer.py:256, trigger echo 'x\'\'y; bash -c "true⏎ssh localhost id"; echo \'. Refuted: on this head that exact trigger is denied by sandbox-escape-ssh-self — the newline-separated ssh localhost matches the whole-line floor regex independent of the descent, so no bypass reproduces.
  • Security Scope Review (— BLOCK): two macOS rows reported newly-refused (bash -c 'cd /srv/app⏎ssh deploy@build.example.com uptime' and an rsync variant). These are DNS-posture flaky: the harness (scripts/deny_diff.py) adjudicates model-proposed candidates live against the base ref — "The corpus is read from the BASE ref by the workflow, never the head checkout" — with no committed expectation file for these rows, and the same lane returned Scope-Verdict: PASS / 0 confirmed newly-refused on another run. Refusing a pending dotted remote host on a later script line is the intended fail-closed tightening; nothing in the repo to change.
  • First Principles Review (— BLOCK — premise concern): finding "The description describes a different change from the one in the diff." Fixed by the body rewrite above — the description now lists exactly the files and behaviours this head ships.

The Sensitive Change Review gate remains the fork takeover hold: this PR touches src/kiro_crew/security/* from a fork, which that gate refuses by design, and nothing posted here clears it. The route to green is a maintainer takeover branch that keeps the author with Co-authored-by and Supersedes #35359.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Update — head d1fdf8ff2c (r31): fail-closed belt

Over the previous head 68b615ea50c8, this head lands the single-pass heredoc-aware scanner's remaining fixes (context stack for a heredoc in "$( )", a bounded delimiter read, the split-<< glued-terminator case, and a comment-slip fix) and a fail-closed belt. Local gate 24/0, prove.py PROVEN, the core security suite green including the package line-budget test, and a full-verdict differential over 32,576 rows shows head refuses a strict superset of origin/main (0 new ALLOW, +121 self-ssh rows) and 0 stable flips versus 68b615ea50c8.

Why two readings (to Opus and GPT)

The descent folds line continuations before it scans, which changes the command shape it reads; a bug in that fold can only ever lose a refusal origin/main already makes. Rather than keep chasing each such spelling, the floor now runs a second, independent reading beside the scanner — argv_floor_ssh_bodies_mainref.py, a verbatim snapshot of origin/main's substitution reading whose function bodies are byte-identical to origin/main's argv_floor_ssh_bodies.py (only the module docstring differs), so it reads $( ) and backtick bodies with no line-continuation fold. _reaches_self_over_ssh refuses if either the folding, script-descending scanner or this snapshot refuses. The scanner adds the refusals origin/main lacks (a self-ssh on a later line of a bash -c script — the #35314 feature); the snapshot guarantees no spelling origin/main already refuses can be lost to what a fold does to the shape. Cost: one extra memoized pass. A belt-disable mutant reddens the split-heredoc pins, pinning the belt.

This closes the prior fold-class findings (the split-operator, "$( )"-heredoc, quadratic-delimiter and glued-terminator cases, and the comment slip). Two independent evaluate-only lanes (Opus 5.5 and GPT 6.1) ran on this head's interdiff: both report clean — no lost refusal, crash, hang or quadratic (≈39k-input differential fuzz, 0 holes), and no objection to the two-reading design.

Two pre-existing gaps named, not fixed here (match main)

The Opus lane surfaced two self-ssh spellings inside the #35314 class that stay allowed on this head and on main and the merge-base alike, so the belt loses nothing against main — both hide a self-ssh behind a backslash-continued comment on a later line of a bash -c script:

  • G1: bash -c '# x\⏎ssh localhost id' — base ALLOW · main ALLOW · this head ALLOW.
  • G2: bash -c '(true)#c\⏎ssh localhost id' (# glued to a word-ending )) — base ALLOW · main ALLOW · this head ALLOW.

A follow-up branch closes both in _fold_outside_quoted_heredocs (drop the comment text; treat # after a non-$( ) as a comment), pinned with 3-tree rows and a mutant; it is held out of this PR to keep the belt change reviewable on its own. They are listed in the PR body's "Not covered (matches main)" section.

The Sensitive Change Review gate remains the fork takeover hold: this PR touches src/kiro_crew/security/* from a fork, which that gate refuses by design, and nothing posted here clears it. The route to green is a maintainer takeover branch that keeps the author with Co-authored-by and Supersedes #35359.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Fork GPT 6.1 lane — the d1fdf8ff2c finding is fixed on c548b1427b

The GPT 6.1 fork lane's blocking finding on d1fdf8ff2c was "Retained comment continuations blind the new script descent" (residual/security): a \-continued comment (# x\+newline, and (true)#c\+newline) folded a self-ssh on the next line into the comment, so bash -c '# x\+newline+ssh localhost id' was allowed while bash runs the ssh.

Fixed in src/kiro_crew/security/argv_floor_ssh_bodies.py, in _fold_outside_quoted_heredocs:

  • the live-comment branch now drops the comment's text while keeping its terminating newline, instead of copying it (lines 350–358), so a \-newline inside a comment can no longer fold the next line into it;
  • # after a word-ending ) (a subshell close or a case pattern) is treated as a comment, while a ) that closed a $( substitution is a glued word — tracked by last_paren_closed_subst (set at line 339 on a $( close, reset at line 379 for a case/base )).

Both are pinned in test/test_ssh_self_executed_scripts.py (test_a_comment_continuation_does_not_hide_a_self_ssh_on_a_later_line, four G1/G2 cases) with 3-tree verdicts, and a 324-case differential fuzz confirms head refuses ⊇ origin/main with zero new ALLOW.

Note on the stale on-PR comment: the lane's d1fdf8ff2c publish step hit API rate limit exceeded for installation (HTTP 403, six attempts), so it withheld the comment update and the visible comment still showed the earlier 68b615e :179 finding. The real d1fdf8ff2c verdict lived in the run's withheld-verdict artifact, which is what this reply addresses.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Update — head c548b1427b (r32): the four red Backend Tests shards + Coverage Gate on d1fdf8ff2c were ours; fixed

One amended commit over the prior head. The red shards were security-package invariant tests the local pre-push gate does not run; each is classified below with its fix.

  • Backend Tests (3.12, 1) — test_facade_shared_checks.py::test_every_scanned_name_reaches_every_module_reading_it[test_argv_floor_facade]: the fail-closed belt module was named argv_floor_ssh_bodies_mainref.py, matching the argv_floor_* facade-owner glob, so it read _is_ssh_to_self without being a registered binder — fixed by renaming it to ssh_self_mainref.py (a frozen snapshot, not a facade owner).
  • Backend Tests (3.12, 6) — test_facade_mirror_guards.py::test_every_mirrored_facade_manifest_names_what_it_forwards: new owner-defined names (_PROCSUB_CONTINUATION_RE, _fold_outside_quoted_heredocs, the belt accessor) were forwarded but absent from argv_floor's TYPE_CHECKING manifest — fixed by adding them.
  • Backend Tests (3.12, 7) — test_argv_floor_facade.py owners-layer-order and served-name-manifest checks: same root cause as shards 1/6 (the glob rename and the manifest names) — fixed by the same change.
  • Backend Tests (3.12, 8) — test_mcp_listing_import_budget.py: the belt added one module to each server's startup import graph — fixed by importing the belt lazily inside the gate's guarded floor call, so it stays out of startup and no ceiling is raised. (The test_eval_tasks.py::test_an_interrupted_arm_is_stopped_before_the_interrupt_propagates red in this shard is a pre-existing main-side flake — ValueError: invalid literal for int() with base 10: '', 5/5 green on this head and on main in isolation, and the test imports no module this diff changes.)
  • Coverage Gate: fails closed when a backend shard is red or missing its coverage artifact, so it cleared once the shards above went green; the new belt module and the new scanner/S1/G1/G2 lines carry pins.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Addressed by construction, one point at a time:

  • Main's reader untouched in place: _substitution_reaches_self is origin/main's substitution reading left byte-for-byte unedited in argv_floor_ssh_bodies.py — git diff origin/main shows no hunk in it, and a drift pin asserts its source stays identical to the base ref.
  • Descent as an OR-ed leg: the new folding, script-descending scanner is a separate function (_folded_scanner_reaches_self) that _reaches_self_over_ssh ORs in, so it can only add refusals and the gate refuses if either leg refuses.
  • Copied module removed: the 164-line verbatim snapshot module ssh_self_mainref.py is deleted, so nothing duplicates main's reader and nothing new joins the facade-owner family or any server's startup import graph.
  • Memo helper narrowed: once_per_call is reverted byte-for-byte to its single-argument base form, and _shell_tokens keys its own per-call memo on the (cmd, preserve_structure) pair instead.
  • Process-substitution reading kept (not dropped): _live_substitutions still reads <( ) / >( ), because a pinned self-ssh slip (cat <(bash -c 'true newline ssh localhost id')) needs it; the test for that case stays green.

The subtraction exposed a cost the separate module had hidden from the counters: the two legs tokenize the same text in two modes that cannot share a read, so per-check work is about 2x a single leg's and the two scan-linearity bounds move from 3x to 5x, while a new flatness pin holds the order (≤ 2.2x work per input doubling, reddened by a re-read-per-frame quadratic) independently of that constant — the PR body's "Cost" section carries the main-vs-head table.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

The three macos-latest rows are main's own fail-closed host posture (a dotted name whose DNS lookup fails is refused — argv_floor_ssh.py:86, git diff origin/main empty on all four host-resolution files) reaching a later line of a bash -c script exactly as it already governs the first line, not a new or broadened rule — on that egress-blocked runner ssh deploy.example.com uptime is refused on line 1 too, and where the name resolves it is allowed on both.

row (later line of a bash -c script) macOS (egress blocked, lookup fails) ubuntu / windows (resolver completes)
cd /srv/app ⏎ ssh deploy.example.com uptime REFUSED (fail-closed) allowed (NXDOMAIN = not self)
true ⏎ scp dist/app.tar.gz deploy.example.com:/tmp/ REFUSED (fail-closed) allowed
# check the remote\ ⏎ ssh deploy.example.com uptime (G1) REFUSED (fail-closed) allowed
top-level control ssh deploy.example.com uptime REFUSED (fail-closed) allowed

Pinned by test_a_later_line_dotted_host_is_judged_as_the_top_level_line_is: resolver patched to fail → all four REFUSED and each descended verdict equals the top-level verdict; resolver patched to a non-self address → all four allowed.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Replying to the First Principles Review lane (comment 6811514064).

We ran your subtraction (judge the base loop's nested payloads, drop the if live: guard, delete the fold scanner) on a scratch branch and measured it against this head over the 32,576-row corpus (double-run, 18-row self-host flaky set excluded) and the r19–r33 pins. The single loop cannot refuse 7 rows this head refuses and over-refuses 1 benign row this head and main both allow (\n = newline):

row single loop this head
cat <(bash -c 'true\nssh localhost id') ALLOW REFUSE
tee >(bash -c 'true\nssh localhost id') ALLOW REFUSE
echo <(bash -c 'true\nssh localhost id') ALLOW REFUSE
cat < <(bash -c 'true\nssh localhost id') ALLOW REFUSE
diff <(bash -c 'true\nssh localhost id') x ALLOW REFUSE
grep x <(bash -c 'true\nssh localhost id') ALLOW REFUSE
bash -c 'true\n# "\necho " #"\nssh localhost id' ALLOW REFUSE
G1/G2 comment-continuation (bash -c '# x\+nl+ssh …', (true)#c\+nl+ssh …) ALLOW REFUSE
bash -c 'set -e\n# debug: ssh … localhost\nnpm test' (benign) REFUSE ALLOW

The single loop has no fold, so it misses the process-substitution and comment-continuation rows and reads a commented-out ssh in a single quote as live and over-refuses it. The fold scanner is a distinct leg doing real work, so it stays — this is the clears-when, recorded in the PR body's "Rows the single loop cannot refuse" section.

Items 3 and 5 by your subtraction rule: the <(/>( reading is kept because the 7 process-substitution rows above need it; preserve_structure / _shell_tokens_keyed is kept because a 3-level-deep bash -c self-ssh needs it (REFUSE with, ALLOW without). Both consumers are named in the description, not generalized. On "byte-for-byte unedited": _substitution_reaches_self's own source is byte-identical (drift-pinned), but its shared helper _live_substitutions gained the <(/>( reading above — additive, so the belt leg still makes every refusal the base reading made; the body now states this precisely.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Replying to the Security Scope Review lane (comment 6801476463).

The ubuntu row bash -c 'set -e\ncd /srv/app\nrsync -az dist/ deploy@203.0.113.10:/srv/app/' is branch A: the later line is judged exactly as the first line, and the host posture over the non-local IP literal 203.0.113.10 is main's own — git diff origin/main is empty on argv_floor_hosts.py, host_addresses.py, hosts_file.py, and the host-resolution path of argv_floor_ssh.py. Measured with the floor at BASE and HEAD, top-level rsync …203.0.113.10… and the nested row, in both address-list states (\n = newline):

ref addr list PUBLISHED (top / nested) addr list NOT published (top / nested)
BASE (merge-base) ALLOW / ALLOW REFUSE / ALLOW
HEAD ALLOW / ALLOW REFUSE / REFUSE

Base and head give the same top-level verdict; head's nested REFUSE equals its own top-level REFUSE (a later line is judged as the first line). The over-refusal of a non-local IP literal while the kernel address list is unpublished is main's own fail-closed posture; the main-side issue #39797 covers it. Pinned by test_a_later_line_dotted_host_is_judged_as_the_top_level_line_is.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Replying to the GPT 6.1 Review lane (comment 6801159807).

Fixed. The scanner leg's pre-fold verb prefilter is removed in src/kiro_crew/security/argv_floor_ssh_bodies.py, function _folded_scanner_reaches_self (the if not _line_may_name_ssh_verb(text_lower): return False at its top). That prefilter ran on the UNFOLDED line and short-circuited before the per-frame fold rejoined s? + \ + newline + h into s?h. Running the prefilter on the fold's output does not help, because _fold_outside_quoted_heredocs is quote-aware and does not fold inside the outer bash -c '…', so the split verb stays hidden on the top-level folded line. The prefilter is removed entirely — it was only a cost optimisation and the leg already judges every folded frame. The base leg keeps its own prefilter.

row main r33 head this head
bash -c 'true\n/usr/bin/s?\+nl+h localhost id' (later line) ALLOW ALLOW REFUSE
bash -c '# c\+nl+s\+nl+sh localhost id' (split after comment) ALLOW ALLOW REFUSE
/usr/bin/s?\+nl+h localhost id (unnested top level) ALLOW ALLOW ALLOW

The unnested top-level spelling is main's own whole-line path (it folds nothing there); left untouched and recorded in "Not covered", with the main-side issue #39797. Pins: test_a_continuation_split_verb_on_a_later_line_is_refused, test_the_unnested_top_level_split_verb_stays_mains_allow, and test_restoring_the_scanner_verb_prefilter_reddens_the_split_verb (restoring the prefilter re-opens the slip).

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Replying to the SAST (Semgrep) check finding semgrep.kirocrew.test-autouse-shared-monkeypatch.

Fixed. In test/test_ssh_self_descent_cost.py, the autouse _pin_resolver fixture now requests _floor_monkeypatch instead of the shared monkeypatch (rule semgrep.kirocrew.test-autouse-shared-monkeypatch, D11), so its patches are undone independently and are not lifted by a later monkeypatch.undo() in any test that runs in the same scope.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

The scanner-leg verb prefilter is restored, in widened form: it short-circuits only a line that names no ssh verb and carries no \-newline continuation — a backslash-newline being the one way bash splits a word across lines — so every line that could fold into a verb still reaches the fold-and-descend, and the continuation-split verb on a later line is still refused (test_a_continuation_split_verb_on_a_later_line_is_refused, and test_restoring_the_scanner_verb_prefilter_reddens_the_split_verb reddens if the old pre-fold prefilter is put back). Dropping the prefilter entirely (the earlier revision) descended every command and made a payload-heavy data-consumer argv sweep quadratically in the payload count; the widened form restores linearity (measured ~1.98× per doubling, with and without a continuation present) without reopening the split-verb hole, and the verb gate sees through quote/backslash splices (ss""h, s\sh) so a quote-split verb is admitted to the descent too.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Replying to the GPT 6.1 Review lane (comment 6801159807), now on head f132752.

Fixed. A here-document or here-string a shell only hands to a NON-shell consumer — cat / tee / echo / printf, or git commit reading its message from stdin (-F - / --file=-) — is printed or written DATA, not a script. The earlier head descended into that body and over-refused a self-ssh quoted inside printed data. _scanner_cut_nonshell_literals (scanner leg only — the belt leg reads the uncut text, so this can lower only head-added refusals) now drops that body before payload discovery, under a positive plain-command-list rule:

The exemption fires only when, after the fold, the whole line is a plain command list: every command head is an ordinary word — not a compound keyword ({ } if then elif else fi for while until do done case esac coproc function select time !) and not a function definition (no argv[0] ending in ()) — no separator is a pipe (| / |&), and no >( / <( appears on the line. The head-check reads argv[0] only, so echo done (a compound word as a mere argument) still allows the line. Within such a line it cuts only a TERMINAL consumer (its own separator ends the command) at frame depth 0, never inside a ( ) subshell or $( ) substitution, and reads top-level commands only, so a cat inside a $( ) that feeds a shell is left to the substitution reader.

Allowed now (printed data, 6 ALLOW pins — \n = newline):

row before this head
cat <<'EOF'\nbash -c 'true\nssh localhost id'\nEOF REFUSE ALLOW
cat <<'EOF'…EOF\necho done REFUSE ALLOW
tee notes.txt <<'EOF'…EOF REFUSE ALLOW
git commit -F - <<'EOF'…EOF REFUSE ALLOW
cat <<<'true\nssh localhost id' REFUSE ALLOW
echo note<<<'true\nssh localhost id' REFUSE ALLOW

Still refused — a shell runs the body, or the consumer's output can still reach a shell (7 shell-consumer REFUSE pins + 10 grouped/piped/compound/process-sub REFUSE pins): bash <<'EOF', cat <<'EOF' | bash, bash <<<'...', $(cat <<EOF) into a shell, a ( ) subshell / { } group / if/while/for/case compound / function body / coproc, and tee >(bash) / cat > >(bash). Three mutants hold it: strip-all (cut every body) reddens the shell-consumer REFUSE pins, strip-none reddens the ALLOW pins, and cut-even-inside-groups (the loose pre-tightening shape) reddens the grouped/piped REFUSE pins. A crash in the cut denies via the S1 fail-closed wrap.

Differential vs the merge-base is unchanged at 0 new ALLOW / +117 new DENY; against the prior pushed head this revision un-refuses exactly these six literal-data rows and adds no refusal. Pins: test_a_nonshell_consumer_literal_body_is_not_read_as_a_script, test_a_shell_consumer_body_is_still_read_as_a_script, test_a_grouped_or_piped_consumer_body_stays_refused, test_strip_all_mutant_reddens_the_refuse_pins, test_strip_none_mutant_reddens_the_allow_pins, test_cutting_a_grouped_consumer_reddens_an_escape_form.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Replying to the Opus 5.5 Review lane (a-ratchet-may-only-tighten) (comment 6801429718), now on head f132752.

This bound is a maintainer decision, and we ask for no override — here is the measured ground so you can make it.

origin/main bounds its single descent leg at chars ≤ 3× the input. This PR adds a second, independent leg (the fold scanner), because First Principles' clears-when requires it: the one-loop collapse your sibling lane proposed cannot refuse 7 corpus rows this head refuses and over-refuses 1 benign row (the "Rows the single loop cannot refuse" table). So the floor is two legs, and the question is how to bound them.

We did not loosen main's bound to the combined work. Each leg is bounded on its own: main's 3× is kept verbatim on its own leg (the substitution reading, git diff origin/main empty on it, drift-pinned), and the same 3× is applied independently to the scanner leg. Per-leg counters show the ~2.0×-each split (about 2027 vs 2043 _self_tokens chars at eval count 200); a flatness pin holds the ORDER at ≤ 2.2× per input doubling independent of the constant (measured 1.98×/doubling).

We also measured the alternative your rule implies — one whole-floor counter over the gate-level shlex.split characters, memo-deduped (your own suggested shape): it reports 6.94× the input, worse than the 4.00× a plain two-leg character sum reports, and far past any single 3× whole-floor bound. So a single 3× whole-floor bound is unmeetable without deleting the second leg — and the second leg is exactly what First Principles' clears-when forbids deleting.

That is the tension, stated plainly for you:

  • Your lane reads one whole-floor counter and sees a bound that stood at 3× of one leg now standing per-leg, i.e. the combined work reaching ~2× main's, as a loosening.
  • First Principles reads a single combined bound as a demand to delete the second leg it requires.

The two readings point opposite ways; both are laid out in the PR body under "Cost", with the 4.00× / 6.94× / per-leg / flatness numbers and the branch that measured each. We have taken the path that keeps every leg at main's own 3× and keeps the second leg First Principles requires; whether that reads as a ratchet loosening or as required duplication is yours to call. Pins: test_each_leg_tokenizes_linearly_in_the_input, test_each_leg_judges_nested_substitutions_linearly, test_the_two_legs_each_do_about_half_the_descent_work, test_the_two_leg_descent_work_stays_flat_in_size.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Replying to the Security Scope Review lane (comment 6801476463), now on head f132752.

The rows that flip base ALLOW → head REFUSE on a runner are branch A: a later line judged exactly as the first line, at main's own host posture — not a new rule.

The rsync row bash -c 'set -e\ncd /srv/app\nrsync -az dist/ deploy@203.0.113.10:/srv/app/': 203.0.113.10 is a non-local documentation IP, and the host posture over it is main's own — git diff origin/main is empty on argv_floor_hosts.py, host_addresses.py, hosts_file.py, and the host-resolution path of argv_floor_ssh.py. Measured with the floor at BASE and HEAD, top-level rsync …203.0.113.10… and the nested row, in both address-list states (\n = newline):

ref addr list PUBLISHED (top / nested) addr list NOT published (top / nested)
BASE (merge-base) ALLOW / ALLOW REFUSE / ALLOW
HEAD ALLOW / ALLOW REFUSE / REFUSE

The crontab row bash -c 'cat <<EOF | crontab -\n0 3 * * 0 /usr/local/bin/backup.sh\nEOF': names no ssh verb, but main reads the * as a glob that could name an ssh verb and backup.sh as a dotted host whose DNS is unresolved, and refuses — and main refuses the same line at top level (0 3 * * 0 /usr/local/bin/backup.sh). (Note this is an unquoted <<EOF, so the literal-consumer cut does not apply; and the consumer is piped to crontab -, which the plain-command-list rule excludes regardless.)

In both rows base and head give the same top-level verdict, and head's nested REFUSE equals its own top-level REFUSE: a later line is judged as the first line. The over-refusal of a non-local IP literal / unresolved host while the kernel address list is unpublished is main's own fail-closed posture; the main-side issue #39797 covers it. Pins: test_a_later_line_dotted_host_is_judged_as_the_top_level_line_is, test_crontab_data_heredoc_nested_verdict_equals_top_level. The r33 macOS deploy.example.com rows are the same class (an egress-blocked runner fails the lookup).

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Replying to the First Principles Review lane (comment 6811514064), now on head f132752.

We ran your subtraction (judge the base loop's nested payloads, drop the if live: guard, delete the fold scanner) on a scratch branch and measured it against this head over the 32,576-row corpus (double-run, 18-row self-host flaky set excluded) and the r19–r33 pins. The single loop cannot refuse 7 rows this head refuses and over-refuses 1 benign row this head and main both allow (\n = newline):

row single loop this head
cat <(bash -c 'true\nssh localhost id') ALLOW REFUSE
tee >(bash -c 'true\nssh localhost id') ALLOW REFUSE
echo <(bash -c 'true\nssh localhost id') ALLOW REFUSE
cat < <(bash -c 'true\nssh localhost id') ALLOW REFUSE
diff <(bash -c 'true\nssh localhost id') x ALLOW REFUSE
grep x <(bash -c 'true\nssh localhost id') ALLOW REFUSE
bash -c 'true\n# "\necho " #"\nssh localhost id' ALLOW REFUSE
G1/G2 comment-continuation (bash -c '# x\+nl+ssh …', (true)#c\+nl+ssh …) ALLOW REFUSE
bash -c 'set -e\n# debug: ssh … localhost\nnpm test' (benign) REFUSE ALLOW

The single loop has no fold, so it misses the process-substitution and comment-continuation rows and reads a commented-out ssh in a single quote as live and over-refuses it. The fold scanner is a distinct leg doing real work, so it stays — this is the clears-when, recorded in the PR body's "Rows the single loop cannot refuse" section.

On the two consumers your subtraction rule asks about: the <(/>( reading is kept because the 7 process-substitution rows above need it; preserve_structure / _shell_tokens_keyed is kept because a 3-level-deep bash -c self-ssh needs it (REFUSE with, ALLOW without). Both are named in the description, not generalized. On "byte-for-byte unedited": _substitution_reaches_self's own source is byte-identical (drift-pinned); its shared helper _live_substitutions gained the <(/>( reading above — additive, so the belt leg still makes every refusal the base reading made, and the belt reads the uncut, unfolded text. The body states this precisely. The descent-cost bound is a separate maintainer decision, addressed in the reply to the a-ratchet-may-only-tighten lane.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Replying to the First Principles Review lane (comment 6811514064) on head f132752.

We ran your subtraction and measured it, rather than argue it. On a scratch branch off f132752, the fold-scanner leg was replaced by exactly what you specified — judge each _shell_payload_sources(text)[1:] frame with _is_ssh_to_self, separators left live — and nothing else was changed. Verdicts were taken with full_verdict.py over the 32,576-row corpus (double-run, self-host-DNS flaky set excluded) and the r19–r36 pin suite.

Half of the premise is true, half is not. The subtraction loses no origin/main refusal — 0 new-ALLOW vs main over the 32k corpus. But _shell_payload_walk does not already serve what the fold scanner adds:

  • vs the 32k corpus: subtraction vs origin/main = 0 new-ALLOW / +168 new-DENY; subtraction vs r36 (f132752) = 3 new-ALLOW (refusals r36 makes that the subtraction loses) and 54 new-DENY (rows r36 allows that the subtraction over-refuses). r36 adds +117 over main; the subtraction adds +168, i.e. it over-refuses ~54 rows r36 correctly allows while losing refusals r36 makes.
  • vs the pin suite: 25 failed / 93 passed.

Rows r36 refuses and the subtraction does not (main=ALLOW · r36=DENY · subtraction=ALLOW)

A self-ssh the fold scanner reaches but _shell_payload_walk does not:

command (\n = newline) main r36 subtraction
bash -c 'true\nbash -c "true\nbash -c \"true\ns?h localhost id\""' (3-level) ALLOW DENY ALLOW
…one level deeper (4-level) ALLOW DENY ALLOW
bash -c 'true;bash -c "true;bash -c \"true;s?h localhost id\""' (;-separated) ALLOW DENY ALLOW
bash -o pipefail -c 'true\nbash -o pipefail -c "…s?h localhost id…"' ALLOW DENY ALLOW
command bash -c 'true\ncommand bash -c "…s?h localhost id…"' ALLOW DENY ALLOW
bash <<< 'true\nbash <<< "true\nbash -c \"true\ns?h localhost id\""' ALLOW DENY ALLOW
bash -c - 'true\nbash -c - "…s?h localhost id…"' ALLOW DENY ALLOW
2>/dev/null bash -c 'true\n2>/dev/null bash -c "…s?h localhost id…"' ALLOW DENY ALLOW
: s?h; bash -c 'true\ns?\<nl>h localhost id' (split verb) ALLOW DENY ALLOW
bash -c 'true\n/usr/bin/s?\<nl>h localhost id' (split verb, later line) ALLOW DENY ALLOW
bash -c '# c\<nl>s\<nl>sh localhost id' (split verb after comment) ALLOW DENY ALLOW
bash -c '# x\<nl>s?h localhost id' (G1 comment-continuation) ALLOW DENY ALLOW
bash -c '(true)#c\<nl>s?h localhost id' (G2 subshell comment) ALLOW DENY ALLOW
bash -c 'case a in a)#c\<nl>s?h localhost id;; esac' (G2 case comment) ALLOW DENY ALLOW
echo "$( (true)#c\<nl>s?h localhost id )" (G2 dq-subshell comment) ALLOW DENY ALLOW

(15 rows from the pin suite; the corpus, which samples these adversarial shapes only partly, independently shows 3 such losses — a comment-fold row, a $( ) row, and a <(-continuation row.)

Rows r36 allows and the subtraction refuses (main=ALLOW · r36=ALLOW · subtraction=DENY)

Printed data read as a script — a new over-refusal, below both main and r36:

command (\n = newline) main r36 subtraction
cat <<'EOF'\nbash -c 'true\ns?h localhost id'\nEOF ALLOW ALLOW DENY
cat <<'EOF'…EOF\necho done ALLOW ALLOW DENY
tee notes.txt <<'EOF'…EOF ALLOW ALLOW DENY
git commit -F - <<'EOF'…EOF ALLOW ALLOW DENY

(4 rows from the pin suite; over the corpus this class is 54 over-refusals vs r36, including a benign commented-out # debug: ssh … line, gh pr create/git commit -m heredoc message bodies, and here-strings.)

Why _shell_payload_walk cannot serve these — each of items 1–4 is tied to a row it alone decides

  • Items 1 & 5 (deep nesting). _shell_payload_walk tokenizes with the default _self_tokens — no preserve_structure — so it does not keep a nested script's structural quotes and never reaches a self-ssh two or more bash -c "…\"…\"" levels down. The fold scanner reads those with preserve_structure=True tokens. That is item 5 (the nesting case), and the fold scanner is its only consumer; deleting the scanner deletes item 5's reach.
  • Items 1 & 2 (comment / split verb). The walk folds with _fold_line_continuations, which is not comment- or heredoc-aware; a \-newline inside a comment, or a verb split across a continuation on a later line, is left hidden. The fold scanner uses _fold_outside_quoted_heredocs, which drops a live comment and leaves a quoted-heredoc body literal.
  • Item 4 (literal cut). With no _scanner_cut_nonshell_literals, the walk descends into cat/tee/git commit -F - here-document bodies as scripts and refuses printed data — the 54 over-refusals above.
  • Item 3 (process substitution). retained because the corpus <(/>( self-ssh rows need it.

So none of items 1–4 is duplicative: each is the sole decider of a named row. loses no main refusal is true; the walk already serves it is not.

What would make the subtraction right

If _shell_payload_walk itself gained structure-preserving tokens and a comment/heredoc-aware fold, the second leg could go. That is a change to main's shared walker, which every floor calls — out of this PR's scope and a redesign under the stopping rule. We are happy to open it as a follow-up issue if the maintainers want it; we have not filed it unasked.

Method: full_verdict.py (the fork review harness's own DNS/own-host stubs) over corpus.json (32,576 rows, double-run) and pytest test/test_ssh_self_executed_scripts.py (25 failed / 93 passed) on branch scratch/35314-fp-subtraction off f132752.

The ssh-to-self floor now descends into the payload of a shell that runs a
script and judges each line as its own command, so a self-ssh on any line but
the first of a bare or nested multi-line `bash -c '...'` / `sh -c` / `$SHELL -c`
/ `env -S` body -- or on a line a `;` `&` `|` separator hid from the whole-line
read -- is refused, not just the first command. The descent folds each frame's
own line continuations first, reads nested `-c` operands through exec wrappers
and value-taking options, reads single-quoted eval bodies and heredoc /
here-string / process-substitution payloads (including a `<(` opener reached past
a `\`-newline), and is bounded by the pre-existing per-frame `seen` set.

To reach a self-ssh three or more `bash -c` levels deep, the descent needs the
tokenizer to keep a nested script's structural quotes; every other floor needs
the opposite -- the canonical command SHAPE with inert empty-quote pairs dropped,
or a `rm -rf /` respelt `rm ''-rf /` slips the `rm -rf /.*` rule. Rather than a
second tokenizer or a raw-operand reader, `_shell_tokens` takes one mode flag:

* DEFAULT (`preserve_structure=False`) drops every empty-quote pair from each
  dequoted token unconditionally (`_EMPTY_QUOTE_RE`), byte-identical to the base
  ref. Every existing caller keeps this, so the deny view, the rm floor and the
  git floor are unchanged (a 32k-row full-verdict differential against base loses
  zero denials; the only changes are the intended ssh-self tightenings).
* `preserve_structure=True` runs an escape-aware collapse
  (`_drop_empty_quote_pairs` before tokenizing, `_drop_token_empty_pairs` on each
  dequoted token): it still folds a respelt verb (`s\"\"sh` -> ssh, `rm\"\"` ->
  rm, `rm'""'` -> rm) but leaves a nested script's structural closing quotes (an
  escaped pair beside another quote) intact. Both passes match each maximal run of
  pairs once with no trailing look-around, so the scan is linear, not the O(n^2)
  a greedy `(?:"")+(?=\w)` would cost on a long pair run through the deny gate.

Exactly one caller passes the flag: the ssh-self fold scanner
(`argv_floor_ssh_bodies._folded_scanner_reaches_self`), through `_self_tokens` and
`normalize_shell_command`, which forward it. `_shell_tokens` keys its own per-call
memo on the `(cmd, preserve_structure)` pair, so the two modes cache apart while
`once_per_call` stays the single-argument base reader every other floor marks --
no change to the shared memo helper and no per-read signature bind on the deny-gate
hot path.

## Backwards compatibility

Reader: every caller of `_shell_tokens` keeps base's unconditional empty-pair
elision (DEFAULT mode); only the ssh-self descent opts into structure-preserving
mode, and it is the only behaviour change. By caller:
- deny_gate `_deny_segment_views` (deny-matching views): DEFAULT -- needs the
  canonical command shape so a respelt verb still matches its rule.
- rm_floor (rm -rf floors): DEFAULT -- matches command shape; unchanged from base.
- argv_rules `_commands` / shell_commands / s3_direction / inline_payload
  (git-publish, s3, nested-command readers): DEFAULT -- unchanged from base.
- acp.batch_guard, dashboard.usage_outcome (tool-prefix readers): DEFAULT --
  unchanged from base.
- normalize_shell_command: forwards the flag; DEFAULT for its own callers (the
  git-publish floor), structure-preserving only when `_self_tokens` passes it.
- argv_floor_ssh_bodies ssh-self descent: preserve_structure=True -- reads a
  self-ssh buried in a deep `bash -c "..."` without truncating the payload.

Because the ssh-self descent tokenizes in preserve_structure mode, a command
reached by both it and the default floors is split once in EACH mode within a
gate call; the per-call memo serves every repeat, so no text is tokenized twice
in one mode, pinned by test_deny_gate_call_sharing.py.

The descent folds line continuations in one heredoc-aware pass
(`_fold_outside_quoted_heredocs`): bash folds a backslash-newline while lexing --
so a split `<<` / `<<-`, or a command word split before one, is seen whole -- and
only then delimits a heredoc body, which for a quoted tag (`<<'EOF'`) it keeps
literal. Folding a quoted body would merge its trailing backslash into the
terminator and blank a substitution the shell runs after it, hiding a self-ssh;
recognising the operator on the folded stream closes that for a split operator
too. A context stack tracks where a `<<` is a heredoc -- at top level, in a
`$( )` / backtick substitution or `( )` subshell, including one reopened inside
double quotes -- and where it is not: in single quotes, directly in double quotes,
after a `#` comment, or in `$(( ))` / `${ }`. The delimiter is read as its own
word, so the scan stays linear in the number of operators. And the descent admits
each distinct executed-script payload once, updating the seen set as each is
admitted, so a run of identical payloads (`bash bash ... -c '...'`) is read once
rather than re-tokenized per copy.

## Fail-closed belt (by construction)

The fold above changes the command SHAPE the scanner reads, and a fold bug can
only ever LOSE a refusal origin/main already makes (a spelling main refuses on
the unfolded stream, folded into one the scanner then clears). To make that class
of bug non-exploitable rather than chase each spelling, the floor keeps
origin/main's own substitution reading (`_substitution_reaches_self`) byte-for-byte
unedited and runs it as one leg of `_reaches_self_over_ssh`, beside the new
folding, script-descending scanner (`_folded_scanner_reaches_self`). `git diff
origin/main` shows no hunk in `_substitution_reaches_self`, and a drift pin asserts
its source stays identical to the base ref. The gate refuses if EITHER leg refuses,
so the scanner adds refusals the base reading cannot make (a self-ssh on a later
line of a `bash -c` script, the feature main lacks) while the base reading
guarantees no spelling main refuses is lost to what a fold does to the shape.

There is no separate snapshot module: the belt is the base function left un-forked,
so nothing new joins the facade-owner family or any MCP server's startup import
graph. The cost is one extra linear descent over the same text, in a second
tokenizer mode. A shared-tokenizer memo keyed by `(text, mode)` with a construct
pre-check (`_modes_may_diverge`) lets the scanner reuse the default-mode tokens
whenever a command carries no empty-quote pair (the only construct on which the
modes differ), so the two legs split such a command once, not twice; an identity
pin proves the shared tokens equal the unshared over the 32k corpus in both modes,
with a mutant that shares a divergent command reddening it. The remaining second
leg still reads its own frames, so the floor's total work is up to about twice
origin/main's single-leg cost. Rather than loosen main's `chars <= 3x` ratchet to
the combined work, each leg is bounded on its own: origin/main's 3x is kept VERBATIM
on its own leg (the substitution reading) and the same 3x is applied to the fold
scanner leg; a flatness pin holds the ORDER at most 2.2x work per input doubling,
independent of the constant, and per-leg counters show the ~2.0x-each split. (The
`a-ratchet-may-only-tighten` lane would block loosening the one bound to 5x of the
combined work; First Principles' clears-when demands the separate leg, which is the
~2x total. The two readings are laid out in the PR body for the maintainers.) A
32k-row full-verdict differential against origin/main adds zero ALLOW (head refuses
a strict superset: +117 self-ssh rows) and drops none. Two mutant pins hold the
design: forcing the scanner leg off lets a later-line `bash -c` self-ssh through,
and forcing the base leg off lets a split-heredoc glued terminator through.

## Fail closed when the reader raises (S1)

The ssh-self reading hand-folds and descends a script. Its deny-gate call is
wrapped exactly as the rm floor wraps its own reader right below it: catch
Exception (incl. RecursionError), log once, and DENY a command that could name an
ssh verb (guarded by `_cannot_name_ssh_verb`). An exception in the fold or the
descent denies, never propagates out of `is_denied` and skips the floor.

## Comments do not hide a later-line self-ssh (G1/G2)

`_fold_outside_quoted_heredocs` drops a live comment's text (keeping its newline)
instead of copying it, and treats `#` after a word-ending `)` (a subshell close or
a `case` pattern, but not a `$( )` close, which is a glued word) as a comment. So a
`\`-newline inside a comment can no longer fold a self-ssh on the next line into
the comment and hide it (`bash -c '# x\`+newline+`ssh <self> id'`, and the same
after `(true)#c\`). origin/main allows these too, so no refusal is lost.

## Continuation-split verb on a later line

A verb split across a `\`-newline on a later line of a shell-run script
(`bash -c 'true`+newline+`/usr/bin/s?\`+newline+`h localhost id'`) folds to `s?h`
and must be refused. The scanner leg's old verb prefilter ran on the UNFOLDED line
and short-circuited before the fold rejoined the verb; running the prefilter on the
fold's output does not help, because the quote-aware fold does not fold inside the
outer `bash -c '...'`. So the prefilter is WIDENED, not dropped: it short-circuits
only a line that names no ssh verb AND carries no `\`-newline continuation -- a
backslash-newline being the one way bash splits a word across lines, so any line
that could fold into a verb is admitted to the descent, while a verb-free,
continuation-free data-consumer argv is not descended. Dropping the prefilter
entirely (an earlier revision of this change) descended every command, so a
payload-heavy argv re-ran the per-frame descent once per nested payload and the
argv sweep went quadratic in the payload count; the widened prefilter restores that
linearity (the data-consumer sweep pin in test_denied_commands_security_cost.py)
without reopening the split-verb hole -- held by a counter pin that a verb-free,
continuation-free argv is not descended while a mutant widening the prefilter to
always-descend reddens it. The verb gate sees through quote and backslash splices
(`ss""h`, `s\sh`), so a quote-split verb is admitted to the descent too. The base
leg keeps its own prefilter. The per-call memo bounds each (text, mode) to one
split -- the preserve_structure descent runs only on a command that can name an ssh
verb after folding, corrected in test_deny_gate_call_sharing.py -- and the flatness
and per-leg cost pins hold. The unnested top-level split verb is main's own
whole-line path, which folds nothing, and is left untouched (recorded here).

## A non-shell consumer's literal body is not a script

The fold scanner descends every executed-script payload, but a here-document or
here-string a shell only hands to a NON-shell consumer is DATA, not a script: `cat`
/ `tee` / `echo` / `printf`, or `git commit` reading its message from stdin
(`-F -` / `--file=-`), print or write it and run nothing. `_scanner_cut_nonshell_literals`
(scanner leg only, never the belt or the shared `_without_quoted_heredoc_text`) drops
such a body -- a QUOTED-tag here-document (`<<'EOF'` / `<<"EOF"` / `<<\EOF`, in which a
shell expands nothing) or a SINGLE-QUOTED here-string (`<<<'...'`) -- before payload
discovery, but only under a POSITIVE plain-command-list rule, so a consumer whose output
could still reach a shell keeps its body.

The exemption fires only when, after the fold, the whole line is a plain command list:
every command head is an ordinary word -- not a compound keyword (`{` `}` `if` `then`
`elif` `else` `fi` `for` `while` `until` `do` `done` `case` `esac` `coproc` `function`
`select` `time` `!`) and not a function definition (no `argv[0]` ending in `()`) -- no
separator is a pipe (`|` / `|&`), and no `>(` / `<(` process substitution appears on the
line. The head-check reads `argv[0]` only, so a compound word as a mere ARGUMENT
(`echo done`) still allows the line. Within such a line it cuts only a TERMINAL consumer
(its own separator ends the command: end-of-line / `;` / `&&` / `||`) at frame depth 0,
never inside a `( )` subshell or a `$( )` / backtick substitution. It reads top-level
commands only (`read_commands(nested=False)`), so a `cat` inside a `$( )` that feeds a
shell is left to the substitution reader. Everything the rule excludes keeps its body and
stays refused exactly as the pre-exclusion head refused it: a body a shell runs
(`bash <<'EOF'`, `cat <<'EOF' | bash`, `bash <<<'...'`, `$(cat <<EOF)` into a shell), an
UNQUOTED here-document whose `$(...)` runs, a grouped or piped consumer, a `{ }` group or
`if`/`while`/`for`/`case` compound or function body or `coproc`, and a `tee >(bash)` /
`cat > >(bash)` writing to a process substitution.

Six printed-data shapes are ALLOW pins and seven shell-consumer shapes REFUSE pins; a
further ten grouped / piped / compound / process-sub shapes are REFUSE pins asserting the
positive rule loses no head-added refusal. A strip-all mutant (cut every body, shell-consumer
ones included) reddens the REFUSE pins, a strip-none mutant reddens the ALLOW pins, and a
cut-even-inside-groups mutant (the loose pre-tightening shape, no plain-command-list guard)
reddens the grouped/piped REFUSE pins. Because the rule only REMOVES text the scanner would
otherwise descend, it lowers only head-added refusals and never takes the floor below
origin/main's belt reading.

## A later line reaches origin/main's own host posture

A crontab schedule written through an unquoted `cat <<EOF | crontab -` heredoc
(`bash -c 'cat <<EOF | crontab -\n0 3 * * 0 /usr/local/bin/backup.sh\nEOF'`) names no
ssh verb, yet `_is_ssh_to_self` refuses it: origin/main reads the `*` as a glob that
could name an ssh verb and `backup.sh` as a dotted host whose DNS is unresolved, and
fails closed. origin/main refuses the SAME line at top level
(`0 3 * * 0 /usr/local/bin/backup.sh`); the descent only reaches it on a later line.
A pin asserts the nested verdict equals the top-level one, so this is main's own
posture reaching a nested line, not a refusal this change introduces.

## Single loop vs two legs

First Principles asked whether the two legs could collapse into one (judge the base
loop's nested payloads and drop its `if live` guard, deleting the fold scanner). The
subtraction was run on a scratch branch and measured over the 32,576-row corpus and
the r19-r33 pins: the single loop cannot refuse 7 rows this head refuses (six
process-substitution self-ssh, one comment-fold) and over-refuses one benign
commented-out ssh. The fold scanner is a distinct leg that does real work, so it is
kept.

The literal cut is top-level only: it runs on the top-level line and never in a frame the
descent reaches, so a cat/tee/git commit -F - whose quoted literal body is reached by
descent (inside a bash -c) is read as a script, and a nested printed self-ssh is
refused where main allows it. This is a deliberate safe false-deny: five versions of a
by-descent cut each leaked under independent review (piped or redirected carrier, an
enclosing runner, glued/fd/quoted/./ path spellings, an earlier exec, & or trap sibling).
Each leak spelling is pinned DENY, and the over-refused rows are pinned as documented.

The ssh-bodies owner's forwarded names (_COMPOUND_HEADS, _NONSHELL_CONSUMER_HEADS,
_is_nonshell_consumer, _scanner_cut_nonshell_literals, _steps_expand_nothing) are listed in
the argv_floor TYPE_CHECKING manifest, and the descent-cost pin patches the shared
_is_ssh_to_self through the facade, as the facade mirror guards require.

Fixes kirodotdev#35314
Re-homed onto dcc3121.
@jeeshofone

Copy link
Copy Markdown
Contributor Author

Re GPT lane (comment 6801159807), F2 (nested printed literal over-refused): retained as a safe false-deny on r37 (f9d443d).

Five versions of a by-descent literal cut were built to clear this, and each one leaked under independent Opus review: a piped or redirected carrier (bash -c "cat <<'EOF' … EOF" | bash), a runner in an enclosing frame, glued/fd/quoted/./ path spellings of a write-then-run, and an earlier exec, & or trap sibling. The cut now applies on the top-level line only; any body reached by descent is read as a script. Every leak spelling is a DENY pin (_NESTED_LEAK_SPELLINGS_REFUSE, and the same rows one level down in _ONE_LEVEL_DOWN_REFUSE).

The over-refused rows (base ALLOW, this head REFUSE), pinned as _NESTED_LITERAL_OVER_REFUSED, each nested 1, 2 and 3 levels in bash -c '…':

  • cat <<'EOF' + self-ssh line + EOF
  • tee /tmp/x <<'EOF' + self-ssh line + EOF
  • git commit -F - <<'EOF' + self-ssh line + EOF

These are the lane's clears-when: they clear when a nested cut is proposed that also refuses every leak-spelling row. They are false denies inside the 0-new-ALLOW bar (differential vs base: 0 newly allowed). No override is requested.

The local GPT lane last ran on 4d3a82a (before the top-level-only change); the fresh fork lane on this push reviews the final shape.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Replying to the First Principles lane (comment 6811514064), head f9d443d.

The lane's clears-when is "the floor judges the frames of _shell_payload_walk, OR the PR shows a refused row that walker cannot reach". The second half is met in the PR body, section "And not by reusing shell_normalizer._shell_payload_walk (First Principles clears-when, measured)", and in the measured rebuttal on r36 (comment 7039895600), which answered the same premise. Judging the walker's frames instead of the fold scanner loses no main refusal, but against this head it loses 3 corpus and 15 pin refusals and over-refuses 54 corpus rows and 4 literal pins.

command (\n = newline) main head subtraction
bash -c 'true\nbash -c "true\nbash -c \"true\ns?h localhost id\""' (3-level) ALLOW DENY ALLOW
bash -c '# x\<nl>s?h localhost id' (comment-continuation) ALLOW DENY ALLOW
cat <<'EOF'\nbash -c 'true\ns?h localhost id'\nEOF (printed data) ALLOW ALLOW DENY
  • Item 6: declared in the body section "Over-refused by design (GPT lane F2 clears-when)", with three-tree verdicts and 9 pins (_NESTED_LITERAL_OVER_REFUSED).
  • Item 8: the one-caller preserve_structure mode is the reason the shared walker cannot serve: without it the walker never reaches 2+ bash -c levels. The same body section states this.

The subtraction becomes right if _shell_payload_walk itself gains structure-preserving tokens and a comment-aware fold. That is a change to main's shared walker, outside this PR; we would take it as a follow-up if the maintainers want it.

@jeeshofone

jeeshofone commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Replying to the GPT 6.1 lane (comment 6801159807), head f9d443d.

  • F1 (top-level cat > FILE <<'EOF' … EOF then bash FILE): the PR body lists this under "Not covered (matches main)", row "Top-level write-then-run of a printed body", with the three-tree control row base ALLOW · r36 ALLOW · r37 ALLOW. main reads no here-document body at all, so this is main's own top-level posture, not a row this PR opened (differential vs base: 0 newly allowed).
    There is also a structural reason not to chase it here. A floor that judges one command cannot tell write-then-run inside one line from a write in one tool call and a run in the next, and no single-command floor reaches the second form. Matching the later runner in the same line therefore buys nothing against that attacker. The lane's own adjudication of the nested-cut history also records that every runner-matching version leaked under review. We would take a main-side design issue for a file-write-then-run class if the maintainers want one; it is outside this PR, which is about descent into executed payloads.
  • F2 (nested quoted cat over-refused): this is the documented safe false-deny, declared in the body section "Over-refused by design (GPT lane F2 clears-when)" and pinned by 9 rows (_NESTED_LITERAL_OVER_REFUSED). The lane's own adjudication grades it harm LOW: the command is visibly denied rather than run wrongly, the agent can recover by rephrasing, and it never allows a command base denies. Reply 7044172781 covers it.

No override is requested.

@jeeshofone

Copy link
Copy Markdown
Contributor Author

Replying to the Opus 5.5 lane (a-ratchet-may-only-tighten) (comment 6801429718), head f9d443d.

These are the same two cost-bound findings raised on r36, answered in reply 7039229442. The PR body's "Cost" section gives the measured ground: each leg is held at main's own 3× bound, the combined work is about 2× main's, and the lane's suggested whole-floor memo-deduped counter measures 6.94× the input. Whether that reads as a ratchet loosening or as the second leg First Principles required is for the maintainers to decide between the two lanes' readings. No override is requested.

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.

ssh-to-self floor misses a glob verb in a heredoc that a group, subshell or substitution runs

2 participants