Repository navigation
fix(security): refuse a command nested past 32 substitutions before the floors walk it - #37875
Conversation
UX Review (Opus 5.5, fork) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5, fork) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing blocks. The fix answers a reproducible cost from #37792 and sits at the gate on purpose, because the floor files belong to three in-flight PRs. The description says this, and it says what is left. Notes
What this change shipsInventory (5 items) — 3 justifiedIntent: a deeply nested shell command gets an immediate refusal, not a gate stalled for seconds. FIX.
[FIRST-PRINCIPLES-REVIEWED] e75d3b2 Description read[DESCRIPTION-READ] 7ae47e7db1590331c2099803fac0593a2f2cf743de2ac6b035d3d8efeb918229 That sha256 names the PR description this verdict read. Recompute it with |
Design Review (Opus 5.5, fork) — ✅ PASSDesign-level review of Design-Verdict: PASS The cap refuses instead of stopping its read, so nothing gets past it unread. It runs before every floor, and a real command never nests 32 substitutions deep. Notes
[DESIGN-REVIEWED] e75d3b2 Description read[DESCRIPTION-READ] 28826b4903fb7f430371da48a218a00202d18336e850ea4eac662391ea59e99e 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 |
Opus 5.5 Review (fork) — ✅ no blocking findingsReviewed Review detailsNo findings. Notes:
[OPUS-REVIEWED] e75d3b2 |
GPT 6.1 Review (fork) — ✅ no blocking findings (all downgraded on adjudication)Reviewed Opus 5.5 adjudication downgraded every blocking finding. Downgraded: the remedy each one needs is disproportionate to the harm it prevents. Adjudication downgraded all 1 blocking finding(s): the remedy each one requires is disproportionate to the harm it prevents. Review detailsBLOCKING -- src/kiro_crew/security/deny_gate.py:706 -- Quoted shell payloads bypass the substitution-depth bound
Adjudication (Opus 5.5) — is blocking on each finding proportionate?LOW rung. Nothing gets past the deny gate here: the walk still reads every payload ( |
Security Scope Review (fork) — ✅ PASSWhich legitimate operations does adjudicated, zero confirmed regressions Scope-Verdict: PASS Reason: The change adds one guard: a depth cap on TIGHTENING: A new pre-floor refusal in ADJUDICATED: 17 candidates, 0 confirmed newly-refused PLATFORM COVERAGE: posix (13 rows):
These cover the scanner's posix assumptions: fresh quote contexts, case patterns, comments, escapes and ANSI-C quotes. windows (4 rows):
These cover the diff's stated assumption that PowerShell backtick escapes and UNADJUDICATED: The cap's scanner ( [SCOPE-REVIEWED] e75d3b2Security scope review -- adjudicated candidates
No candidate flipped from allowed to refused. |
c9f47c9 to
4da4908
Compare
…he floors walk it
4da4908 to
e75d3b2
Compare
|
Opus review on 4da4908: fixed in e75d3b2. The depth refusal's key, Pinned by |
Code change approver recommendation: A person should decideWhy: the decider thinks the change is probably fine but estimates a 27% chance that this change introduces a defect, above the 12% limit for approving on its own. The PR adds a single linear pre-scan at the top of 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 this head the review checked these points. Deny refusals for nesting of 32 levels or less are unchanged. Depth 33 gets the exact two-line refusal before any floor runs. The refusal key is a self-guard, so no override card is offered and a waiver naming the key still refuses. The new tests pass and are deterministic. Mutation runs showed that removing the early return, removing the self-guard key, or moving the cap by one level each turns tests red. A scan of about 86,800 whole files found no false refusals.
Done well: the tests count _iter_shell_chars steps instead of timing anything. They measure 0 steps at depth 100 and 200, so the cost claim is pinned without a clock.
Security: this PR changes a safety gate, but it only adds a refusal. The cap refuses and does not stop reading, so no command gets past the gate unread. A command the cap does not catch still goes through every floor exactly as it does on main. The new key cannot be waived, which matches how the unverifiable push target is held.
Optional:
docs/system-specs/modules/security.md(the Bounded cost bullet and the "no numeric depth cap" wording) and the never-overridable list indocs/request-for-change/rfc-deny-session-override.mddo not mention the new refusal yet. AGENTS.md asks for that in the same commit. A short follow-up, or a linked issue, would close the gap.- A nest inside a single-quoted payload such as
bash -c '...'oreval '...'skips the cap. It is still walked at main's cost, so this is not a regression or a bypass. The body's claim that the cap covers "all three floors" is broader than the code. Naming this residual in_nested_past_cap's docstring and in a follow-up issue would keep the record accurate. - #37820 is on main now, so the comment about unifying
_MAX_SUBSTITUTION_DEPTHwith_MAX_SSH_READING_DEPTHcan point to a tracking issue, or the two values can come from one constant.
…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)
… main (#39387) Backports to release/0.13.0 the fixes from #39342, squashed into one commit and brought up to date with release/0.13.0 after the Kiro Memory preview backport (#39360). Picks, in order; each [pick sha] is the commit on the #39342 branch, whose body has main's cherry-picked-from line: - fix(monitor): wake the session once when its watched PR merges or closes (#37776) [pick 84da419] - fix(security): match deny rules against Windows program suffixes and cmd/PowerShell in-word escapes (#33569) [pick e4941b7] - fix(mimocode): refuse a chat that would answer from Xiaomi's hosted model unsigned (#37629) [pick 1bfdce3] - fix(acp): end a quiet agent-started cycle on the shared runtime's stale gate (#37932) [pick fa870a2] - fix(conductor): re-read goal-conductor after a compaction (#37917) [pick ccdd67c] - fix(security): refuse a command nested past 32 substitutions before the floors walk it (#37875) [pick 2f9bd67] - fix(mates): carry a moved Bot's shell toolsSettings entry as execute_bash instead of holding it (#37973) [pick 16022f4] - fix: hold an app to its own sessions, jobs and runs (#37804) [pick 3dff0c0] - fix(acp): keep a KAS chat's stored record when its resume fails after the load (#37955) [pick 6ceb7b5] - fix(gateway): ignore a repeat SIGTERM during graceful shutdown (#38031) [pick b48287d] - fix(security): keep a quote in a $( ) from hiding a later ssh (#37558) [pick b9fed61] - fix(agent): rebuild takes a server's disabledTools from the kiro global (#38147) [pick c50bbcc] - fix(mcp_gateway): keep the backend when a stub leaves an open listen (#38121) [pick 3e9c7f9] - fix: refresh plan and model list after a kiro-cli sign-in to another account (#38053) [pick 5ad3d36] - fix(slack): keep a picture sent in a linked thread for the dashboard turn (#38076) [pick 7f8903e] - fix: keep a resolved tool policy when the gateway is too slow to answer (#36669) [pick 9be5691] - fix: open owner-only files and prune overlay folders without following links (#36994) [pick 6a1acc9] - fix: refuse the local token mint to app backends (#38113) [pick d166dab] - fix(subagent): hold a spawn's first prompt for its MCP servers (#38127) [pick e3e0298] - fix(security): refuse a headless git --output write with no approver (#36326) [pick 1abee40] - fix(restore): require an explicit mode, never default to replace (#38094) [pick ca86c06] - fix: make the deny gate's shell walk linear in command length (#36437) [pick 22c100b] - fix(chat): let a cancelled background start finish its session teardown (#36664) [pick b1dd34f] - fix(acp): refuse a start when two differing specs declare the agent's name (#38411) [pick 80ccfff] - fix: probe a silent turn while its co-tenant keeps the process busy (#38662) [pick 6354490] - fix(kas): let a reopened chat switch to models its load select omits (#38376) [pick 1640822] - fix(cloud): install a patched node 22 on run on aws instances (#38732) [pick 059cb90] - fix: write member record files without following links (#37303) [pick aac9642] - fix: fail a kas start fast once the host has turned its mcp off (#38677) [pick e147238] - fix(decisions): fall back to HTTP/1.1 when h2 is missing (#38759) [pick fc16482] - fix(mcp_gateway): free a spawn permit when its backend exits before initialize (#38793) [pick a084ba2] - fix(kas): start an app chat whose tool policy hides every kirocrew-cron tool (#38761) [pick ccea5a9] - fix(acp): give the tool-stall watchdog a margin over native waits (#34117) [pick b5d9e89] - fix(mcp): bind the working-folder answer to its launch; observe launch changes behind name grants (#38983) [pick a1069be] - fix(mcp): hide the gateway secret files from the npm install and third-party probe spawns (#39000) [pick 14d8571] - fix(sandbox): fold env name case on Windows in the agent env scrub (#33997) [pick 9bdce76] - fix(apps): let app turns follow the Auto approval mode (#38830) [pick e8336df] - fix: end a dashboard turn stopped while it is still being prepared (#36478) [pick 37fff93] - fix(acp): cleanup_session deletes the record on the session's own host (#39046) [pick 31b5ec4] - fix(acp): scan only the new part of a streamed tool result (#38852) [pick e2870db] - fix(sandbox): make an unscoped session host the OOM victim, not the gateway (#39189) [pick 2be6dde] - fix(security): read aws s3 downloads piped into tail, with 2>&1, or with an AWS_PROFILE prefix (#38271) [pick bf65d97] - fix: redact four display and log paths and use a fixed 502 message for session switch refusals (#38982) [pick 55d9ec7] - fix(sandbox): pin a credential-masking app's folder after the masks on the namespace backend (#34339) [pick afd2b14] - fix(acp): wait for systemd scope admission as its own startup stage (#37008) [pick 3e29c86] - fix(acp): wait for systemd scope admission on the AcpClient spawn path (#38303) [pick 85f0a25] - fix(acp): stamp the spawn instance on the AcpClient child (#38810) [pick f36eb5a] - fix: end the turn when the agent never confirms a stop (#38512) [pick 2665c9d] - fix(kas): renew a refused crew sign-in instead of failing every start (#38978) [pick 2f48cac] - fix(security): keep self-host detection when the ssh verb is a glob (#34056) [pick ab53f10] - fix(security): judge each substitution body for ssh-to-self (#37616) [pick d5b7ac3] - test: check the ssh floor's nesting bound below the gate's depth cap (#37964) [pick 6c9ae1d] - fix(mate): apply held Bot limits on shared claude sessions and refuse unnarrowed KAS children (#39212) [pick 721bbbe] - test(integration): wait out and reset the hidden-chat refresh between boots (#37933) [pick e77586d] - fix(browser): fall back to Kiro's Chromium under Automatic when Chrome's remote debugging is off (#39265) [pick 31912eb] Left out of #39342's set (not needed for 0.13.0): the provider_labels refactor, #34045, #36668, #39120 and #37627. Merge with release/0.13.0: kept both imports in runtime_session_load, runtime_spawn_launch and session_provider; session_allocation lists the memory_policy owner; the keep-record resume path passes kas_memory_mode as main does; dropped the ACP_BACKENDS_SESSION_EVICTION import that moved to session_provider_new_conversation.
Problem / Motivation
Goal: A command nested deeper than 32 substitutions gets a prompt refusal from the deny gate, instead of a gate that works for seconds or tens of seconds before answering.
is_denieddoes work that grows with nesting depth times command length on a deeply nested command substitution. On the 1,000-deep double-quoted$( )command from #37792, it takes about 20 s even with the ssh floor stubbed to return False. #37820 bounds only the ssh floor. Counts on main (cProfile, ssh floor stubbed): the shared quote machine_iter_shell_charstakes 120,930 steps at depth 100 and 471,830 at depth 200. Doubling the depth multiplies the work by 3.9.Why it matters
The deny check runs on every shell tool call. While it works, the turn is stalled and a gate worker is held. A nest of a few hundred levels is a few KB of text, so one generated or hostile command is enough to hold the gate for seconds.
Not a goal
shell_normalizer.py,shell_normalizer_walk.pyandargv_floor_self.py, which fix(security): read a multi-line script a shell runs in the ssh-to-self floor #35359, fix(security): refuse a mint whose verb comes from a quoted substitution #37077 and fix: make the deny gate's shell walk linear in command length #36437 are rewriting._MAX_SSH_READING_DEPTH. That is a follow-up once both land.What changed (motivation → approach → change)
Three floors each re-walk the nested text once per level:
_shell_payload_walk_uncachedre-tokenises each substitution body as its own string and calls_substitution_bodieson it (201 calls at depth 200). Each call's_matching_close_parenwalks that body again, so total work is the sum of every body's length._bare_kill_raw_bodiesis called 201 times from_self_kill_spanand re-walks each body the same way.is_deniedcalls_is_git_publishonce per payload frame (201 calls), and each call scans that frame's whole source text.The bound sits at the gate, not inside each floor, because three in-flight PRs own the files those floors live in. One check at the top of
is_deniedcaps all three floors at about 32 walks of the command today without touching those files. #36437 removes the re-read from offset 0 inside_close_paren_walk, which is the per-call cost. It does not remove the per-level re-walk, so the two changes are complementary.deny_gate._nested_past_capmakes one pass over the command and refuses once nesting passes_MAX_SUBSTITUTION_DEPTH = 32, before any floor runs. Bash opens a fresh quoting context inside each$(, so"$(echo "$(…nests even though the quotes alternate. A flat quote state misreads that, so the pass keeps a stack of contexts: command text, double quotes and a backtick body. Only$(,<(and>(count. A bare(and a backtick span are tracked, so a)or quote inside them reads right, but neither counts. An unescaped backtick cannot open inside a backtick span: it closes it, even from inside a double-quoted string there, as bash reads it. So at most one backtick level sits between two counted levels, and the floors' work stays within about twice the cap. Not counting them also keeps strings that are not posix shell from tripping the cap.is_deniedreads every tool string on every host: PowerShell escapes a quote with a backtick,cmd /cpaths end in\", and code or a regex may nest parentheses freely. Single-quoted and ANSI-C text and escaped characters open nothing. A#comment runs to its newline, and between acasein command position and itsesaca)ends a pattern rather than a frame, as the floors' own walk reads them, so neither a quote in a comment nor a case pattern can hide a deep nest from the cap.The refusal reads:
It is never waived, because past the cap no floor can read the command in bounded time. Its key is a self-guard key in
deny_waiver.deny_key_is_self_guard, like the unverifiable push target, so the dashboard offers no override card for it. A deny event is written for it like every other refusal.The payload walk's docstring says it takes no numeric depth cap, because a cap that stops reading is a bypass. This cap refuses rather than stops reading, so no command gets past it unread.
Backwards compatibility
Breaking: a command nested more than 32 substitutions deep, which main allowed, is now refused. Writer sweep on origin/main @ 467de02:
_nested_past_caprun over every line ofsrc/andtest/(outside this PR's new test file) returns False for all of them, and commands the suites build at run time are covered by those suites passing unchanged.Reader: src/kiro_crew/security/deny_gate.py:is_denied -- chat|cron|subagent|app -- every caller already handles a refusal string; this adds one more reason, test_one_past_the_cap_is_refused_with_the_way_out
Tests
test/test_denied_commands_security_nesting.py:test_a_deep_nest_is_refused_without_walking_it[100|200]: at depth 100 and 200 the gate refuses with the depth reason and_iter_shell_charstakes 0 steps (main: 120,930 and 471,830). Counts only, no clock.test_the_cap_itself_reaches_the_floors: depth 32 is allowed and the floors run.test_a_floor_still_refuses_at_the_cap: a protected-branch push 32 deep is still refused by its floor, not by the cap.test_no_card_is_offered_for_the_substitution_depth_cap(test_deny_session_override.py):override_offer_keyreturns None for the depth refusal, and a call that waives its key still refuses.test_one_past_the_cap_is_refused_with_the_way_out: depth 33 gets the exact two-line refusal.TestStringsThatAreNotPosixShell: the Security Scope Review's two PowerShell JSON writes (one repo value renamed for the brand gate), a generated 40-escape JSON write, a PowerShell status line with$( ),cmd /cpaths ending in\"and a drive-letter path are not refused by the cap. Code, prose and a regex nesting parentheses are not refused either, and the issue's 1,000-deep command still is.test_the_depth_scan_reads_the_shell_contexts: a quote in a comment, case patterns inside 40 levels,caseas a plain argument in 40 sibling substitutions (allowed), an escaped\$(run,$#, an unclosed backtick run, a backtick closing its span from inside a quote, alternating quotes, a 33-deep subshell (allowed), groups inside substitutions, process substitution, sibling backticks, single-quoted, escaped and ANSI-C text, and two 32-deep siblings.Proof: PROVEN test/test_denied_commands_security_nesting.py::TestSubstitutionDepthCap::test_a_deep_nest_is_refused_without_walking_it[100], test/test_denied_commands_security_nesting.py::TestSubstitutionDepthCap::test_a_deep_nest_is_refused_without_walking_it[200], test/test_denied_commands_security_nesting.py::TestSubstitutionDepthCap::test_one_past_the_cap_is_refused_with_the_way_out, test/test_denied_commands_security_nesting.py::TestSubstitutionDepthCap::test_the_depth_scan_reads_the_shell_contexts[echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo "$(echo x)")")")")")")")")")")")")")")")")")")")")")")")")")")")")")")")")"-True], test/test_denied_commands_security_nesting.py::TestSubstitutionDepthCap::test_the_depth_scan_reads_the_shell_contexts[$( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( ($( (true) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )) )-True] (head e75d3b2)
Local pre-push reviews: a GPT lane on the previous head found a comment quote and a case pattern hiding a deep nest from the scan; both are fixed and pinned here. The local GPT lane runs on the session model rather than the pinned
gpt-6.1-sol, so CI's GPT lane is the real pass.Every existing allow and refuse row in the denied-commands, security, argv, shell_commands and shell_normalizer suites passes unchanged.
Manual verification
N/A — unit coverage sufficient: the change is one pure function and one early return in
is_denied, both exercised directly.Related Issues
Fixes #37874
Related #36437, #37792, #37820
Sibling sweep
Swept:
git grep -nE "_matching_close_paren\(|_substitution_bodies\(|_is_git_publish\(" -- src/Sibling: src/kiro_crew/security/shell_normalizer.py -- not affected -- its per-level walk is capped by the gate check, which runs first
Sibling: src/kiro_crew/security/argv_floor_self.py -- not affected -- its per-level walk is capped by the gate check, which runs first
Sibling: src/kiro_crew/security/argv_floor_ssh.py -- tracked in #37820
Pattern harvest
Rule candidate: review-prompt
Pattern: "a recursive reader that re-extracts each nested body as a new string and walks it again costs depth times length; bound the depth at the entry point or walk by offset"
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)docs/system-specs/modules/security.mdis being edited by fix(security): read a multi-line script a shell runs in the ssh-to-self floor #35359 and fix(security): refuse a mint whose verb comes from a quoted substitution #37077; the cap goes into it in the follow-up that unifies it with fix(security): bound how deep the ssh floor nests its readings #37820's capContribution License Agreement
Contributions are accepted under the project's Apache-2.0 license.