Repository navigation
fix(security): bound how deep the ssh floor nests its readings - #37820
Conversation
First Principles Review (Opus 5.5, fork) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS The fix has a linked issue (#37792) and a test that fails on base. It bounds the depth inside the function that every recursion path calls, so it fixes the mechanism rather than a call site. Notes
What this change shipsInventory (3 items) — 2 justifiedIntent: a shell request with deeply nested
[FIRST-PRINCIPLES-REVIEWED] 984c4fb Description read[DESCRIPTION-READ] 8fc12020d9085d6b6e75a802fb07a15038c535d91268d978603b77df3c8a0bdb That sha256 names the PR description this verdict read. Recompute it with |
UX Review (Opus 5.5, fork) — ⏭️ skippedRevision |
Design Review (Opus 5.5, fork) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks: the depth bound sits on the one function every recursion path enters, and it fails closed. Notes
[DESIGN-REVIEWED] 984c4fb Description read[DESCRIPTION-READ] 946de305591139a4b7dca9a6ead483bff093412393116a792df4af44c115d7f1 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 |
GPT 6.1 Review (fork) — ✅ no blocking findingsReviewed Review detailsNo findings. |
Opus 5.5 Review (fork) — ✅ no blocking findingsReviewed |
Security Scope Review (fork) — ✅ PASSWhich legitimate operations does adjudicated, zero confirmed regressions Scope-Verdict: PASS Reason: The change caps how deeply the ssh-to-self floor can nest its readings of TIGHTENING: ADJUDICATED: 16 candidates, 0 confirmed newly-refused PLATFORM COVERAGE: posix (12 rows): plain ssh/scp/sftp/rsync to remote hosts, [SCOPE-REVIEWED] 984c4fbSecurity scope review -- adjudicated candidates
No candidate flipped from allowed to refused. |
f377d54 to
dde343d
Compare
A double-quoted $( ) nested about 1,000 levels deep made _is_ssh_to_self and _double_quoted_substitution_bodies recurse past Python's limit, so the RecursionError aborted the turn before the permission request was answered. _is_ssh_to_self now counts the readings open at once and refuses past 32. Only text that names an ssh-family verb recurses, so a deep nest without one is still allowed. Fixes kirodotdev#37792
dde343d to
984c4fb
Compare
Code change approver recommendation: ApproveWhy: the decider estimates an 8% chance that this change introduces a defect, within the 12% limit for approving on its own, and every check passes. The PR puts a depth counter (a ContextVar) on 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 |
KiroCrewCaptain
left a comment
There was a problem hiding this comment.
Approved. On head 984c4fb the review checked these things:
- The ssh-self test file passes (382 tests), and the argv_floor, facade mirror, facade bindings and gate liveness suites pass (441 tests).
- Probes show nesting depths 0 to 31 are read as before and depth 32 is refused.
- With src reverted to the merge-base, 3 of the 5 new tests fail.
- Mutating the bound check, the
return Trueor thereset(token)call turns tests red.
Security: this change only tightens the ssh-to-self floor. Every recursion path calls the module-level _is_ssh_to_self, so the bound covers all of them. Past 32 open readings the floor refuses, and the ContextVar resets in finally. Nothing that main refuses is allowed now. The only new refusals are commands nested 32 or more levels deep that name an ssh verb, and the body lists those under Backwards compatibility.
Done well: the bound is a fixed depth rather than a caught RecursionError, so the result does not depend on the interpreter stack. The tests count calls instead of measuring time.
Optional:
- Add an allowed row at the boundary, for example
_nested_double_quoted(_MAX_SSH_READING_DEPTH - 1, "ssh far-host id")is False. Today a cap lowered to 2 still passes every test, yet it would refuseecho "$(echo "$(ssh far-host id)")". - The comment near argv_floor_ssh.py:1025 says only text that names an ssh verb recurses. In fact the refusal at the bound runs before the verb probe, so the refused reading itself may name no verb. It fails closed, so only the wording needs a fix.
- After #37875 lands, share one constant for the two 32-level caps, or add a comment saying they are separate on purpose.
A double-quoted $( ) nested about 1,000 levels deep made _is_ssh_to_self and _double_quoted_substitution_bodies recurse past Python's limit, so the RecursionError aborted the turn before the permission request was answered. _is_ssh_to_self now counts the readings open at once and refuses past 32. Only text that names an ssh-family verb recurses, so a deep nest without one is still allowed. Fixes #37792 (cherry picked from commit 7505181)
…37964) #37875 made the gate refuse any command nested past 32 substitutions before a floor reads it, so the 128-level nest in #37820's test is now refused by that cap and the test, which expected the ssh floor's rule, is red on main. The test now checks the floor's bound at exactly 32 levels, which the cap lets through, and that 128 levels get the cap's own refusal. Closes #37963
…37964) #37875 made the gate refuse any command nested past 32 substitutions before a floor reads it, so the 128-level nest in #37820's test is now refused by that cap and the test, which expected the ssh floor's rule, is red on main. The test now checks the floor's bound at exactly 32 levels, which the cap lets through, and that 128 levels get the cap's own refusal. Closes #37963 (cherry picked from commit 529da1e)
…37964) #37875 made the gate refuse any command nested past 32 substitutions before a floor reads it, so the 128-level nest in #37820's test is now refused by that cap and the test, which expected the ssh floor's rule, is red on main. The test now checks the floor's bound at exactly 32 levels, which the cap lets through, and that 128 levels get the cap's own refusal. Closes #37963 (cherry picked from commit 529da1e)
…37964) #37875 made the gate refuse any command nested past 32 substitutions before a floor reads it, so the 128-level nest in #37820's test is now refused by that cap and the test, which expected the ssh floor's rule, is red on main. The test now checks the floor's bound at exactly 32 levels, which the cap lets through, and that 128 levels get the cap's own refusal. Closes #37963 (cherry picked from commit 529da1e)
…37964) #37875 made the gate refuse any command nested past 32 substitutions before a floor reads it, so the 128-level nest in #37820's test is now refused by that cap and the test, which expected the ssh floor's rule, is red on main. The test now checks the floor's bound at exactly 32 levels, which the cap lets through, and that 128 levels get the cap's own refusal. Closes #37963 (cherry picked from commit 529da1e)
Problem / Motivation
Goal: A shell request with deeply nested double-quoted
$( )gets a permission answer instead of aborting the turn.A request that nests double-quoted
$(about 1,000 levels deep ('echo "$(' * 1000 + 'true; echo ssh' + ')"' * 1000, 10,014 characters, valid Bash) makes the ssh-to-self floor raiseRecursionError. Nothing on the_resolve_permissionpath catches it, so the turn aborts before the permission request is answered.Why it matters
One crafted command can abort any turn that runs the denied-command gate. The command never runs, but the turn is lost.
Not a goal
RecursionError.What changed (motivation → approach → change)
_is_ssh_to_selfreads each double-quoted$( )body, bound function body and carried value with another_is_ssh_to_selfcall, and nothing bounds how many are open at once. A decorator,_bounded_ssh_reading, now counts the open readings in a context variable and refuses (returns True) once_MAX_SSH_READING_DEPTH(32) are open. The bound sits on the function every recursion path goes through, so no path can reset it, and an over-deep request is refused, never allowed and never a crash. Only text that names an ssh-family verb recurses at all (the verb probe returns False first), so a deep nest with no ssh verb is still allowed. A deterministic bound was chosen over atry/except RecursionErroraround the floor because the answer then does not depend on the interpreter's stack limit or how deep the caller already is. The facade manifest inargv_floor.pynames the three new names, and a line-budget row records the +33 lines.Backwards compatibility
Compatible: only commands that nest 32 or more ssh-naming readings deep get a different answer (a refusal instead of a crash or a read).
Removes nothing: the change adds a refusal for an input that crashed the gate.
Tests
TestSshFloorNestingDepthintest/test_denied_commands_security_ssh_self.py:_reaches_self_over_sshwith no exception, after exactly_MAX_SSH_READING_DEPTH(32) body readings (fails withRecursionErroron origin/main);is_deniedrefuses a 128-level nest assandbox-escape-ssh-selfwith no exception;_MAX_SSH_READING_DEPTHon a 200-level nest, and the counter is back to 0 afterwards (a count, no wall clock);ssh localhost idinside is still refused, and one without ssh is still allowed;The first version also sent the 1,000-level command through
is_denied. It passed locally but timed out at 120 s on the 4-core CI runner. The cost is not in the ssh floor:_reaches_self_over_sshanswers in about 0.5 s locally. On main,_shell_payload_walk,_self_kill_span/_bare_kill_raw_bodiesand_is_git_publisheach make about 1,001 calls on that command and take about 20 s before the ssh floor is reached (20.7 s locally with the ssh floor stubbed out, 22.9 s on main before its RecursionError). That quadratic work is a separate defect, tracked by #37874 and fixed by #37875, so the gate row uses 128 levels (0.35 s locally) and the 1,000-level row asserts on the floor the issue names.Proof: not provable -- other: prove.py reported INCONCLUSIVE (the mutated run errored at collection); reverting src/ to origin/main by hand makes the two new depth tests fail with RecursionError and AttributeError.
Related suites (denied_commands_security*, argv_floor*, facade_mirror*, facade_bindings, security_*, liveness_facade): 8,047 passed, 2 skipped. pre_push_gate passed. Flake-pattern ratchet passed. Semgrep (semgrep/ rules), black, flake8, isort and mypy are clean on the changed files.
Local reviews on this head: the Opus 5.5 lane found no findings after validation. Its two discovery candidates were dropped: (1) the bound refuses a 32nd reading without running that reading's own verb probe. That only happens when the 31 enclosing readings each name an ssh verb, and refusing there is the fail-closed direction. (2) Sibling fan-out cost is the same on main (see Not a goal). The GPT lane found nothing, but it did not run on the pinned gpt-6.1-sol model and could not run pytest, so CI's GPT review is the real pass.
Manual verification
Known limitation: a command refused at the depth bound gets the usual ssh-to-self refusal note, which tells the agent to wait and retry the same command.
N/A — unit coverage sufficient: the floor is a pure function of the command text.
Related Issues
Fixes #37792
Related: #35359, #37785 (both edit
argv_floor_ssh.py; this diff keeps to the_is_ssh_to_selfdefinition and the facade manifest)Related: #37874 (quadratic work in the other floors on deep nesting), fixed by #37875
Sibling sweep
Swept: git grep -nE "_double_quoted_substitution_bodies(|RecursionError" -- src/kiro_crew/security/
Sibling: src/kiro_crew/security/deny_gate.py:892 -- not affected -- the rm floor already catches RecursionError and fails closed
Sibling: src/kiro_crew/security/inline_payload.py:216 -- not affected -- the payload parser already catches RecursionError
Pattern harvest
Rule candidate: review-prompt
Pattern: a security reader that recurses once per shell nesting level needs a deterministic depth bound that refuses past it
Checklist