Skip to content

fix: make the deny gate's shell walk linear in command length - #36437

Merged
KiroCrewCaptain merged 1 commit into
mainfrom
fix/13980-linear-shell-walk
Nov 17, 2026
Merged

KiroCrewCaptain merged 1 commit into
mainfrom
fix/13980-linear-shell-walk

Conversation

@bolichen97

@bolichen97 bolichen97 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

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_denied still 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

  • Changing what any rule or floor allows or denies.
  • Where the gate runs or how often a text is walked: the gate already runs in the tool-gate pool, and each text is walked once per decision (fix: walk a shell command once per deny decision, not per floor #32115).
  • The rest of a decision's cost. What remains is spread over the floors, and each of them is already linear in the command.

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_states reads it once for the whole text, and both readers that look for spans across a whole command, _substitution_bodies and shell_commands.without_substitutions, hand each span's open its state, so _matching_close_paren starts 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_chars takes 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.

Command (Linux host) main this PR
issue reproducer, 1.9 KB 118 ms 74 ms
issue reproducer, 7.7 KB 912 ms 174 ms
issue reproducer, 30.9 KB 12.3 s 571 ms
echo 2>$(e) f on every line, 16.8 KB 7.8 s 379 ms
per-call tool gate, 17 KB heredoc 8.2 s 698 ms
per-call tool gate, 16 KB redirect shape 14.2 s 670 ms

The 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 of echo 2>$(e) f: counts the quote scanner's steps over one is_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, a case pattern, 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.
  • The existing deny suites pass 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.py and test_rm_root_row_scratch_view.py, plus every test file that imports shell_commands (2,171 passed, 1 skipped).
  • test_the_package_stays_within_its_line_budget (test_security_gate_liveness.py) carries the re-measured budget.
  • Determinism: both new tests count work, never time. 20 runs at -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 commit a395b012c44b), each in its own python3 -I process with a fresh KIROCREW_HOME. The per-call tool gate (HookManager.on_tool_call) was driven on replayed kiro-cli execute frames; 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:

heredoc-benign-18KB    len= 17432 base=   8177.8 ms fix=   698.2 ms action=allow same_action=True same_reason=True
redirect-benign-16KB   len= 16100 base=  14185.1 ms fix=   670.1 ms action=allow same_action=True same_reason=True
heredoc+nested-push    len= 17451 base=   1932.2 ms fix=    91.4 ms action=deny  same_action=True same_reason=True
redirect+nested-push   len= 16133 base=   6880.0 ms fix=   157.0 ms action=deny  same_action=True same_reason=True
heredoc+rm-root        len= 17440 base=   2008.7 ms fix=   144.3 ms action=deny  same_action=True same_reason=True
redirect+rm-root       len= 16121 base=   7025.7 ms fix=   232.5 ms action=deny  same_action=True same_reason=True
  • The commands carrying a nested git push origin main or rm -rf / are refused on both builds with byte-identical reasons; the benign ones are allowed on both.
  • On a recent base (a558571065; the host's commit 4b32cb5c4f6c has this PR's tree, and main has since moved only outside the security package), is_denied takes 180 ms on a 7.9 KB heredoc, 590 ms on a 30.5 KB one and 520 ms on 16.1 KB of echo 2>$(e) f (main: 1.0 s, 19.8 s and 13.5 s); git status is allowed, rm -rf / is refused and rm -rf /tmp/build gets the same answer as on main.
  • A differential fuzz against main's modules, 200,000 texts (seed 13980): without_substitutions, redirect_free_views, _substitution_bodies and _matching_close_paren at 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_denied over 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_end 0), so it reads no text before the span; the span walks at shell_commands.py:226 and :338 have the same shape

Pattern 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

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Sensitive Change Review

Sensitive files changed:

  • src/kiro_crew/security/shell_commands.py

  • src/kiro_crew/security/shell_normalizer_walk.py

  • No reasoning template for 990bb26 yet.

How to clear this check:

  1. The PR author posts the template below as a PR comment, with
    every field filled (N/A needs a reason). Keep the
    Head commit line: it must name the current head SHA.
  2. The check turns green within a minute, with no push.
  3. A new push resets this check: post it again for the new head.

Only the author's comment counts this way. A reviewer can instead
paste the filled template into an "Approve" review on this head.
Merging still needs a reviewer's approval.

## Sensitive change review
- Head commit: 990bb26730706ec6bfda57277a31401acd4f01e3
- What rule changed:
- Before -> after (what was allowed/blocked, now):
- Worst case if wrong (breaks commands / leaks secret / locks user out):
- Evidence checked (which sandbox result proves it):
- No regression:
  - Tools still work (which tools/commands were run, result):
  - Backward compatible (old config / old data / old callers still work, how checked):
  - Existing tests (which suites passed on this head):
- How to undo:

@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Nov 13, 2026
@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

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

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of 990bb26730706ec6bfda57277a31401acd4f01e3 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

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 .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 13, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

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

First-Principles-Verdict: PASS

Nothing blocks: the fix 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 ships

Inventory (5 items) — 5 justified

Intent: 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.

  1. A long command with a $( span on many lines gets its allow or deny answer in linear time instead of seconds, with the same answer — justified
  2. The quote state is read once per text (_quote_states), with 2 consumers: _substitution_bodies and without_substitutions — justified
  3. _matching_close_paren / _close_paren_walk take an optional at resume state; 2 callers pass it, and the other 5 callers already start at the open on a slice (, 0)) — justified
  4. _iter_shell_chars takes a start offset in place of a copied slice — justified
  5. The security spec's bounded-cost bullet, the line-budget entry and the work-count pins are updated — justified

[FIRST-PRINCIPLES-REVIEWED] 990bb26

Description read

[DESCRIPTION-READ] 850737f01bed57a3e8a1b795232775607b8569274b974b071718bb04c16572bf

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

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed 990bb26730706ec6bfda57277a31401acd4f01e3 — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 990bb26

Verdict parsed from the review's SHA-scoped output markers for commit 990bb26730706ec6bfda57277a31401acd4f01e3.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 990bb26730706ec6bfda57277a31401acd4f01e3 false-positive: <reason, 20 to 500 characters>

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 990bb26730706ec6bfda57277a31401acd4f01e3 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 990bb26

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 990bb26730706ec6bfda57277a31401acd4f01e3 false-positive: <reason, 20 to 500 characters>

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Security Scope Review -- ✅ PASS

Which legitimate operations does 990bb26730706ec6bfda57277a31401acd4f01e3 newly refuse? Updated in place on each push. A script-confirmed regression blocks; the model proposes candidates and never decides a refusal.

Verdict basis: adjudicated, zero confirmed regressions.

Security scope review -- adjudicated candidates

  • macos-latest (platform posix): 15 classified, 0 newly refused, 13 still allowed.
  • ubuntu-latest (platform posix): 15 classified, 0 newly refused, 13 still allowed.
  • windows-latest (platform windows): 3 classified, 0 newly refused, 2 still allowed.

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. _matching_close_paren now picks up each $( / <( / >( span at its open, using the quote state recorded by _quote_states, instead of re-reading the text from offset 0. No pattern, floor or tier gained a match. I wrote 18 boundary candidates around the walk's resume point so the classifier can confirm the verdicts are unchanged; none of them has a verdict back yet.

TIGHTENING: none. The payload-walk resume is the only mechanism touched (+ pos, (state, ansi) = (0, (0, False)) if at is None else (open_end, at) in _close_paren_walk; + end, proven = _matching_close_paren(text, i + 2, quote_at[i + 2]) in _substitution_bodies; + pos = _shell._matching_close_paren(command, hit + 2, quote_at[hit + 2])[0] in shell_commands.without_substitutions). It names no new threat to refuse; the only refusal it touches is the existing substitution-body extraction. The risk in the other direction would be a resumed state that differs from the walk from offset 0. That would change which span a body closes at, and so which text the sensitive and deny tiers read. Two things argue against it. First, a $( open lies on a step boundary: only a backslash pair is a two-character step, and $ / ( are never its first character, so quote_at[open_end] exists. Second, dollar_run is 0 at the ( either way.

ADJUDICATED: 18 candidates, 0 confirmed newly-refused

PLATFORM COVERAGE: posix (15 rows) and windows (3 rows), no any rows. The diff makes no path, separator, interpreter or environment assumption. The one assumption it does make is the bash quote/escape grammar behind _iter_shell_chars and _quote_states.

  • posix rows: these probe that grammar at the resume point:
    • a quoted $(
    • <( and >( process substitutions
    • ${VAR:-$(...)}, which runs the expansions-aware second walk
    • an ANSI-C $'...' before a span
    • an escaped \$( next to $$
    • a case with ) arms after a substitution
    • a heredoc whose body holds a quoted $(
    • a redirect cut by a substitution (2>$(...))
  • windows rows: these feed PowerShell $(...) subexpressions and ( ... ) paren expressions, including one inside a double-quoted string with \ separators, to the same walk. That covers the Windows shell text that reaches it.

[SCOPE-REVIEWED] 990bb26

@bolichen97
bolichen97 enabled auto-merge (squash) November 13, 2026 05:50
@github-actions github-actions Bot added merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Nov 13, 2026
@bolichen97
bolichen97 force-pushed the fix/13980-linear-shell-walk branch from 1b00f90 to efe952d Compare November 13, 2026 10:17
@bolichen97

Copy link
Copy Markdown
Contributor Author

Rebased: main moved in test/test_security_gate_liveness.py and docs/system-specs/modules/security.md since this head went green (b7a4e1e -> c22557c); CI re-running.

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]

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

Copy link
Copy Markdown
Collaborator

Code change approver recommendation: Approve

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

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: checking Automated validation is still running and removed merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision labels Nov 13, 2026
@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision readiness: checking Automated validation is still running and removed readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision labels Nov 14, 2026
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.
@bolichen97
bolichen97 force-pushed the fix/13980-linear-shell-walk branch from efe952d to 990bb26 Compare November 17, 2026 11:25
@bolichen97

Copy link
Copy Markdown
Contributor Author

[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.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: passed Eligible automated validation passed for the current revision merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: checking Automated validation is still running labels Nov 17, 2026

@KiroCrewCaptain KiroCrewCaptain left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@KiroCrewCaptain
KiroCrewCaptain merged commit af270ec into main Nov 17, 2026
102 of 103 checks passed
@KiroCrewCaptain
KiroCrewCaptain deleted the fix/13980-linear-shell-walk branch November 17, 2026 12:51
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Nov 17, 2026
buluoray pushed a commit that referenced this pull request Nov 19, 2026
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)
buluoray pushed a commit that referenced this pull request Nov 19, 2026
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)
buluoray pushed a commit that referenced this pull request Nov 19, 2026
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)
buluoray pushed a commit that referenced this pull request Nov 19, 2026
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)
bolichen97 added a commit that referenced this pull request Nov 19, 2026
… 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

security: is_denied is quadratic in command length and runs on the event loop — 8 KB heredoc = 4 s stall, 32 KB kills the gateway

2 participants