Repository navigation
fix: make the deny gate's shell walk linear in command length - #36437
Conversation
Sensitive Change ReviewSensitive files changed:
How to clear this check:
Only the author's comment counts this way. A reviewer can instead |
UX Review (Opus 5.5) — ⏭️ skippedRevision |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks: the quote state is computed once and each span's walk resumes from it, which removes the quadratic cost where it comes from. No rule changes and nothing is taken away. Notes
[DESIGN-REVIEWED] 990bb26 Description read[DESCRIPTION-READ] 8be8b6ee3f8046b8617e07888d53eb3f73195e746f2058a5898abfc7076bdd3d 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 |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing blocks: the fix removes the per-span re-read from offset 0 that made the walk quadratic (#13980), and the added parameters have real callers. What this change shipsInventory (5 items) — 5 justifiedIntent: a long heredoc-fed or substitution-heavy shell command should get its deny verdict without stalling the gateway, and the verdict should not change. This is a FIX.
[FIRST-PRINCIPLES-REVIEWED] 990bb26 Description read[DESCRIPTION-READ] 850737f01bed57a3e8a1b795232775607b8569274b974b071718bb04c16572bf That sha256 names the PR description this verdict read. Recompute it with |
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
Security Scope Review -- ✅ PASSWhich legitimate operations does Verdict basis: adjudicated, zero confirmed regressions. Security scope review -- adjudicated candidates
No candidate flipped from allowed to refused. Scope-Verdict: PASS Reason: Nothing in this diff makes a rule stricter. It is a cost refactor of the payload walk. TIGHTENING: none. The payload-walk resume is the only mechanism touched ( ADJUDICATED: 18 candidates, 0 confirmed newly-refused PLATFORM COVERAGE: posix (15 rows) and windows (3 rows), no
[SCOPE-REVIEWED] 990bb26 |
1b00f90 to
efe952d
Compare
|
Rebased: main moved in The branch conflicted on the security package's line budget, which main raised for an unrelated change; the budget keeps main's note and states this change's +27 on top (35,928, re-measured). The fix itself is unchanged. [operator: bolichen97] |
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. This is a cost-only fix and the code preserves the old behaviour. The old _close_paren_walk walked from offset 0 and skipped every step before open_end before any jump could happen and before dollars/prev were updated, so at the open it held exactly the (state, ansi) that _quote_states records. _iter_shell_chars never looks back past its own position, and dollar_run is 0 at the open in both versions because the character before it is '('. Every lookup at i+2 or hit+2 finds an entry, because the open's first character is never a backslash, so the '(' after it is always a step of its own. Callers that pass no state keep the old walk. The new tests count work, not time: one would fail on main, and one checks that the resumed walk closes where the walk from the start does across tricky spellings. The spec and the line budget are updated. Most CI was still pending and the PR has a merge-conflict label; both are process matters, not defects in the code. 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 |
The payload walk found each $( / <( / >( span's closing paren by re-reading the command from offset 0 just to learn the quote state at the span's open. A heredoc with one span per line therefore cost O(spans x length): about 1 s of deny-gate time at 8 KB and 13 s at 31 KB, and the per-call tool gate held a ~18 KB command for over 5 s. The redirect reader that blanks unquoted substitutions did the same for a command with a redirect cut by a substitution on every line. The walk now records the quote state at every step of the text once and starts each span's close search at its open with that state, in both readers. The steps from the open on are the ones a walk from offset 0 takes, so every span closes where it did and no verdict changes. The scanner also resumes at an offset instead of copying a slice. One walk now takes under three scanner steps per character. Pinned on work counts: scanner steps stay under ten per character of both command shapes, and a span resumed at its open closes where a walk from the start does across the quoting, comment, case, ANSI-C, escaped, backtick and process-substitution spellings.
efe952d to
990bb26
Compare
|
[operator: bolichen97] Rebased onto current main to resolve the merge conflict in src/kiro_crew/security/shell_commands.py, test/test_denied_commands_security_cost.py and test/test_security_gate_liveness.py. |
KiroCrewCaptain
left a comment
There was a problem hiding this comment.
Reviewed head 990bb26. The resumed paren walk starts each span from the quote state that a walk from the beginning of the text holds at that span's opening, and that walk never jumps or updates its counters before the opening, so every span closes at the same place with the same proven flag. The deny-gate and shell-reader suites pass on this head, including the new step-count and resume-equality tests. A differential fuzz of 60,000 generated commands gave identical output on the base and on this head for the substitution readers and every paren close. The deny verdict, run on 15,000 of those commands, was identical too. No command that was refused before is allowed now, so nothing blocks.
The payload walk found each $( / <( / >( span's closing paren by re-reading the command from offset 0 just to learn the quote state at the span's open. A heredoc with one span per line therefore cost O(spans x length): about 1 s of deny-gate time at 8 KB and 13 s at 31 KB, and the per-call tool gate held a ~18 KB command for over 5 s. The redirect reader that blanks unquoted substitutions did the same for a command with a redirect cut by a substitution on every line. The walk now records the quote state at every step of the text once and starts each span's close search at its open with that state, in both readers. The steps from the open on are the ones a walk from offset 0 takes, so every span closes where it did and no verdict changes. The scanner also resumes at an offset instead of copying a slice. One walk now takes under three scanner steps per character. Pinned on work counts: scanner steps stay under ten per character of both command shapes, and a span resumed at its open closes where a walk from the start does across the quoting, comment, case, ANSI-C, escaped, backtick and process-substitution spellings. (cherry picked from commit af270ec)
The payload walk found each $( / <( / >( span's closing paren by re-reading the command from offset 0 just to learn the quote state at the span's open. A heredoc with one span per line therefore cost O(spans x length): about 1 s of deny-gate time at 8 KB and 13 s at 31 KB, and the per-call tool gate held a ~18 KB command for over 5 s. The redirect reader that blanks unquoted substitutions did the same for a command with a redirect cut by a substitution on every line. The walk now records the quote state at every step of the text once and starts each span's close search at its open with that state, in both readers. The steps from the open on are the ones a walk from offset 0 takes, so every span closes where it did and no verdict changes. The scanner also resumes at an offset instead of copying a slice. One walk now takes under three scanner steps per character. Pinned on work counts: scanner steps stay under ten per character of both command shapes, and a span resumed at its open closes where a walk from the start does across the quoting, comment, case, ANSI-C, escaped, backtick and process-substitution spellings. (cherry picked from commit af270ec)
The payload walk found each $( / <( / >( span's closing paren by re-reading the command from offset 0 just to learn the quote state at the span's open. A heredoc with one span per line therefore cost O(spans x length): about 1 s of deny-gate time at 8 KB and 13 s at 31 KB, and the per-call tool gate held a ~18 KB command for over 5 s. The redirect reader that blanks unquoted substitutions did the same for a command with a redirect cut by a substitution on every line. The walk now records the quote state at every step of the text once and starts each span's close search at its open with that state, in both readers. The steps from the open on are the ones a walk from offset 0 takes, so every span closes where it did and no verdict changes. The scanner also resumes at an offset instead of copying a slice. One walk now takes under three scanner steps per character. Pinned on work counts: scanner steps stay under ten per character of both command shapes, and a span resumed at its open closes where a walk from the start does across the quoting, comment, case, ANSI-C, escaped, backtick and process-substitution spellings. (cherry picked from commit af270ec)
The payload walk found each $( / <( / >( span's closing paren by re-reading the command from offset 0 just to learn the quote state at the span's open. A heredoc with one span per line therefore cost O(spans x length): about 1 s of deny-gate time at 8 KB and 13 s at 31 KB, and the per-call tool gate held a ~18 KB command for over 5 s. The redirect reader that blanks unquoted substitutions did the same for a command with a redirect cut by a substitution on every line. The walk now records the quote state at every step of the text once and starts each span's close search at its open with that state, in both readers. The steps from the open on are the ones a walk from offset 0 takes, so every span closes where it did and no verdict changes. The scanner also resumes at an offset instead of copying a slice. One walk now takes under three scanner steps per character. Pinned on work counts: scanner steps stay under ten per character of both command shapes, and a span resumed at its open closes where a walk from the start does across the quoting, comment, case, ANSI-C, escaped, backtick and process-substitution spellings. (cherry picked from commit af270ec)
… 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 long shell command with a
$(on many of its lines, such as a heredoc-fed script, gets its deny-rule verdict in time that grows with its length rather than its length squared, with the same allow or deny answer as before.is_deniedstill spent most of a decision finding where each$(span ends. On a Linux host, the issue's reproducer (git add f && python3 - <<'PY' ... PY, one line of$b, quotes, backticks and$(e)repeated) took 118 ms at 1.9 KB, 912 ms at 7.7 KB and 12.3 s at 30.9 KB on main, and the per-call tool gate held a 17 KB copy for 8.2 s. A command with a redirect cut by a substitution on every line (echo 2>$(e) f) grew the same way: 7.8 s at 16.8 KB.Why it matters
The deny check runs on every shell tool call, and a heredoc-fed script of a few KB is an ordinary agent call, not an attack. Seconds to tens of seconds spent deciding one call is a stalled turn for the person waiting on it and a gate worker held the whole time; the issue's crash dump is the gateway's loop-stall watchdog ending the process while this check ran.
Not a goal
What changed (motivation → approach → change)
A long command with a span on every line gets its deny-rule answer up to twenty times faster, and the answer is the same.
Every
$(,<(and>(span needs its closing), and the walk that finds it has to know whether the span's start sits inside quotes. It learned that by reading the whole command again from its first character, once per span, so a command with a span on every line was read once per line.The quote state at each point of the text does not depend on which span asks.
_quote_statesreads it once for the whole text, and both readers that look for spans across a whole command,_substitution_bodiesandshell_commands.without_substitutions, hand each span's open its state, so_matching_close_parenstarts at the open instead of re-reading everything before it. From the open on it takes exactly the steps the walk from the first character took, so every span closes where it did._iter_shell_charstakes a start offset, so a walk that resumes reads the same text instead of a copied slice. A caller that passes no state keeps the walk from the first character.echo 2>$(e) fon every line, 16.8 KBThe security spec's bounded-cost bullet describes the resumed walk and its pins, and names the
${...}-aware second walk as the one reading that is not linear yet; the security package's line budget is re-measured.Backwards compatibility
Compatible: every span closes at the same index with the same proven flag as before, so every floor reads the same bodies and no verdict changes; the new parameters default to the existing behaviour.
Removes nothing: no rule, floor, view or caller is dropped; only repeated reading is.
Tests
TestOneDecisionWalksEachTextOnce::test_one_walk_reads_each_character_a_bounded_number_of_times(test_denied_commands_security_cost.py), for a 135-line heredoc and for 300 lines ofecho 2>$(e) f: counts the quote scanner's steps over oneis_denied. They stay under ten per character; on main they are 137 and 301 per character, and grow with the command's length.TestOneDecisionWalksEachTextOnce::test_a_span_resumed_at_its_open_closes_where_a_walk_from_the_start_does: for every span in eight tricky spellings (a quoted),${v:-)}, a#comment, acasepattern, ANSI-C quoting, escaped and doubled$, backticks, process substitution, unclosed spans, the heredoc), the resumed walk returns the same close as the walk from the first character.test_a_span_without_a_brace_is_walked_once: its spy forwards the new state argument; its assertions are unchanged.test_denied_commands_security*.py,test_security_publish_floor.py,test_security_shell_grammar.py,test_push_branch_gate_parser.py,test_shell_commands_reader.py,test_security_gate_liveness.pyandtest_rm_root_row_scratch_view.py, plus every test file that importsshell_commands(2,171 passed, 1 skipped).test_the_package_stays_within_its_line_budget(test_security_gate_liveness.py) carries the re-measured budget.-n0, 10 runs of the whole cost file at-n 4 --dist loadgroup --max-worker-restart=2, and the whole cost file in a shuffled order (seed 13980), all green.Proof: PROVEN test/test_denied_commands_security_cost.py::TestOneDecisionWalksEachTextOnce::test_one_walk_reads_each_character_a_bounded_number_of_times[heredoc], test/test_denied_commands_security_cost.py::TestOneDecisionWalksEachTextOnce::test_one_walk_reads_each_character_a_bounded_number_of_times[redirect-cut-by-substitution] (head 4b32cb5c4f6c)
Manual verification
Run on a disposable Ubuntu x86_64 host, main (
b7a4e1e70c) and this change on that base back to back (the host's commita395b012c44b), each in its ownpython3 -Iprocess with a freshKIROCREW_HOME. The per-call tool gate (HookManager.on_tool_call) was driven on replayed kiro-cliexecuteframes; no isolated gateway was started, because a gateway only reaches this gate when a signed-in agent backend emits a shell tool call. From the smoke log:git push origin mainorrm -rf /are refused on both builds with byte-identical reasons; the benign ones are allowed on both.a558571065; the host's commit4b32cb5c4f6chas this PR's tree, and main has since moved only outside the security package),is_deniedtakes 180 ms on a 7.9 KB heredoc, 590 ms on a 30.5 KB one and 520 ms on 16.1 KB ofecho 2>$(e) f(main: 1.0 s, 19.8 s and 13.5 s);git statusis allowed,rm -rf /is refused andrm -rf /tmp/buildgets the same answer as on main.without_substitutions,redirect_free_views,_substitution_bodiesand_matching_close_parenat every open give byte-identical output, and on this branch every resumed walk (303,750 opens) closes where the walk from the start does.is_deniedover 30,000 fuzz texts (seed 4242) gives byte-identical answers on both builds (6,130 refusals).Screenshots / video
N/A: no user-visible surface changes.
Related Issues
Closes #13980
Sibling sweep
Swept:
git grep -nE "_matching_close_paren\(|_iter_shell_chars\(.*\[.*:\]" -- src/kiro_crew/security/Sibling: src/kiro_crew/security/shell_normalizer_walk.py:283 -- changed here
Sibling: src/kiro_crew/security/shell_commands.py:411 -- changed here
Sibling: src/kiro_crew/security/argv_floor_self.py:637 -- not affected -- it walks each span from a slice that starts at the span (
open_end0), so it reads no text before the span; the span walks at shell_commands.py:226 and :338 have the same shapePattern harvest
Rule candidate: review-prompt
Pattern: a loop over items restarts a scanner at offset 0 for each item only to recover the scanner's state where that item begins
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)