Repository navigation
fix(approvals): make redirect detection in trusted patterns quote-aware - #19968
Conversation
A trusted program grant such as `git *` stops covering a command that
writes through a redirect. The check read every `>` as a redirect, so a
`>` inside a quoted argument (`git log --format='%h -> %s'`, a jq filter,
a grep pattern) also raised an approval card under a grant that covers
it.
The trusted-pattern tier now drops the `>` characters bash reads as text
(inside single or double quotes, or after a backslash outside single
quotes) before applying the read-only classifier's write rule. The same
reading applies to the segment check, to whether a pattern spells a
write redirect, and to the timed-grant deadline, so the three agree.
Every case it cannot follow keeps the old rule, with each `>` counted:
a quote left open, a command carrying a heredoc, a comment, ANSI-C
`$'...'` or `${...}`, and Windows, where the shell is PowerShell and a
backslash escapes nothing. The read-only tier is unchanged.
Fixes #19319
|
Closing and reopening right after opening so CI runs on hosted runners: same-repo runs from this account queue on the fleet label without a runner (details on #19683). |
UX Review (Opus 5.5) — ✅ PASSUX-level review of UX-Verdict: PASS Check that the attached screenshots belong to this PR: they show the Dispatch tree page and agent-name errors, and none of it is touched by this diff. [UX-REVIEWED] 2e039a8 Description read[DESCRIPTION-READ] 47281f8ecad1598c65a3ea7c81739950f39265691dcaf79396870a451fc27103 That sha256 names everything this verdict read: the PR description, plus the 17 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 |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Check one thing: masking escaped [DESIGN-REVIEWED] 2e039a8 Description read[DESCRIPTION-READ] ccf4ec3a9f1faf2bbbe18d97fa9759c9e58db3cb5609f1a571614ac3d05662d2 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 Check one thing: the splitter now masks every escaped What this change shipsInventory (6 items) — 6 justifiedIntent: a user who trusted
All three trust-tier callers of [FIRST-PRINCIPLES-REVIEWED] 2e039a8 Description read[DESCRIPTION-READ] cde7550e96ccb75953427d16b8b437739a267f1b6168f32f27ecaa28aada8979 That sha256 names the PR description this verdict read. Recompute it with |
Opus 5.5 Review — ✅ no blocking findingsReviewed Review detailsNot blocking: one advisory finding. FINDING — src/kiro_crew/trust_patterns.py:302 — the PR adds [OPUS-REVIEWED] 2e039a8 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: |
The trusted-pattern redirect check walked each segment's quotes from an unquoted start. The splitter also cuts at a separator escaped inside double quotes, so a segment can start inside a quote an earlier one opened, and the walk then took the `>` bash runs as a redirect for quoted text. Under `echo *` and `ls *`, `echo "x\|ls '" > out '\'` was covered while bash writes `out`; the same held for `\;`, `\&` and a backslash-newline. Quotes are read once over the whole command, and each segment leaves out only the `>` characters that walk marks as text. An escaped `>` is text for the splitter too, so the `&` after it separates commands. The timed-grant deadline and the pattern side read the same way. The tests that assert the POSIX reading pin `platform_compat.IS_WINDOWS` to False, so they give the same verdict on the Windows runner.
|
|
span=de44d143bf45
|
|
span=063869a5f101
|
|
Closing and reopening so CI runs on hosted runners: same-repo runs from this account queue on the fleet label without a runner (details on #19683). |
bolichen97
left a comment
There was a problem hiding this comment.
Readiness sweep: full diff read, PR Readiness success on this head, no live change requests.
Sensitive change review
Rule changed: trusted-pattern tier (redirect_outside_grant, _covers_write_redirect, timed_grant_deadline) now drops > that bash reads as literal text (inside '...'/"..." or backslash-escaped outside single quotes) before applying the unchanged readonly_bash.writes_through_redirect. Before: every non-discard > (quoted or not) required a grant spelling the redirect. After: quoted/escaped > no longer counts, so e.g. git log --format='%h -> %s' is auto-approved under git *. Fail-closed fallbacks: open quote, any <<, #, $', ${ anywhere in the command, Windows, or a segment not found verbatim in the command -> every > counts again. Quote state is computed once over the whole command (fixes the cut-inside-double-quotes bypass the Opus lane found on c6d8b84). Splitter change: escaped > is now masked in _mask_quoted_separators so \>& is no longer read as a >& redirect; this only produces more splits (more segments must match grants) -> stricter, not looser. Pattern side only loses coverage (quoted > in a pattern no longer spells a write). Worst case if wrong: a real file write via redirect auto-approved under a base program grant without a card. Evidence checked: read whole diff and the head's split_command_segments/_mask_quoted_separators; reasoned through word-boundary preservation (quotes/backslashes stay in place after removing literal >, so >/dev/null'>'x and 2>&1'>' still count as writes), segment offset lookup (find from running offset, falls back to count-all), substitution guard unchanged; tests cover the redirect table incl. heredoc/comment/ANSI-C/${} and escaped-separator shapes; all backend test shards (Linux/Windows/macOS) green on head. Read-only tier untouched. Undo: revert the squash commit.
Undo: revert the squash commit.
Problem / Motivation
Goal: A trusted program grant such as
git *,gh *orgrep *covers a command whose only>sits inside a quoted argument without asking again, while every redirect bash really performs still needs a grant that spells it.A program grant does not cover a write redirect:
redirect_outside_grant(src/kiro_crew/trust_patterns.py:255) declines the grant for a segment that writes through one. The write test it uses,writes_through_redirect(src/kiro_crew/security/readonly_bash.py:1488), counts every>that is not a discard idiom, quoted or not. Sogit log --format='%h -> %s',gh api repos/o/r/pulls --jq 'select(.n > 1)'andgrep -E "a>b" file.txteach raise an approval card under a grant that should cover them, as #19319 describes.Why it matters
Format strings, jq filters and regular expressions carry
>routinely, so a user who trustedgitorghkeeps answering cards for commands that write nothing. Each needless card teaches the user to approve without reading, which weakens the card that guards a real redirect.Not a goal
_classify_bashkeeps refusing every>; only the trusted-pattern tier changes.$'...',${...}, Windows) every>keeps counting, as on main.>inside[[ ]]or(( ))as a comparison. An unquoted one there still counts as a write.split_command_segments(_COMMAND_SUBSTITUTION_RE,src/kiro_crew/trust_patterns.py:33). It also ignores quotes, sogrep '>(' forgrep '$(' fmatches no grant at all. That guard decides whether a command is matched in the first place, not whether a matched command writes, and loosening it is a separate decision.What changed (motivation → approach → change)
Symptom: a quoted
>raises the "trusted pattern not applied" card. Root cause: the trusted-pattern tier hands raw segment text to a write rule that does not know about quoting. Change, all insrc/kiro_crew/trust_patterns.py:_literal_gt_offsets(src/kiro_crew/trust_patterns.py:292) walks the whole command with bash's rules for'...',"..."and a backslash outside single quotes, and returns the offset of each>bash reads as text. A quote left open returns an empty set, so every>counts._mask_quoted_separators(src/kiro_crew/trust_patterns.py:154) masks an escaped>as well as the separators inside quotes. An escaped>is text for the splitter too, so the redirect mask (_REDIRECT_RE,src/kiro_crew/trust_patterns.py:27) never reads it as the start of>&, and the&after it separates commands. An unescaped2>&1,>&2or&>filestays one redirect, as before._segment_writes(src/kiro_crew/trust_patterns.py:326) reads quote state once over the whole command, not per segment, because the splitter also cuts at a separator escaped inside double quotes (_mask_quoted_separators,src/kiro_crew/trust_patterns.py:154), so a segment can start inside quotes an earlier one opened. It finds each segment at its offset in the command, and_writes_through_redirect(src/kiro_crew/trust_patterns.py:348) applies the unchangedwrites_through_redirectto the segment without the>at those offsets; quotes and backslashes stay in place, so a discard idiom (>/dev/null,2>&1) ends its word exactly where it did before. A segment that is not cut verbatim from the command is read, with every later one, with every>counted.>counted, also applies when the whole command contains one of_UNMODELLED_QUOTING(src/kiro_crew/trust_patterns.py:289:<<,#,$',${), or on Windows, where kiro-cli runs the command with PowerShell and a backslash is not an escape (src/kiro_crew/name_grant.py:73,src/kiro_crew/name_grant.py:99). The constructs are searched in the whole command rather than the segment, because a heredoc body or a comment can sit in a later segment than the<<or#that changes its quoting.redirect_outside_grant(src/kiro_crew/trust_patterns.py:276), whether a pattern spells a write in_covers_write_redirect(src/kiro_crew/trust_patterns.py:370), and each part intimed_grant_deadline(src/kiro_crew/trust_patterns.py:557). Without the last one, a chat-longgit *grant and a quoted>would be approved by one function and given an already-passed deadline by the other.docs/system-specs/modules/security.md: the redirect paragraph states the quote-aware rule, that quote state is read over the whole command, its fallbacks, and that the read-only tier is unchanged.Rejected: making
writes_through_redirectitself quote-aware. It states the read-only classifier's rule, which this issue does not ask to loosen.Rejected: looking for the unmodelled constructs per segment. A heredoc body lands in a later segment than its
<<, and a quote walk over that segment alone sees balanced quotes around a redirect bash runs (the heredoc case below).Rejected: walking quotes per segment.
echo "x\|ls '" > out '\'is cut at the\|inside the double quotes, and a walk that starts the second segment unquoted reads its>as single-quoted text while bash writesout.Rejected: counting every
>in any command with a backslash before|,&,;or a newline. It closes the same case but keeps prompting for quoted>the whole-command reading gets right, such asecho "x\|ls '>'".Backwards compatibility
Breaking: a trusted pattern whose only
>sits inside quotes (jq '.a > 1' *) no longer counts as spelling a write redirect, so a command it matches that also writes (jq '.a > 1' x > out) shows the approval card instead of auto-approving. Nothing is blocked. Writer sweep on origin/main @ 3791b79: the "trust this command" card storesexact_trust_patternof the command (src/kiro_crew/dashboard/chat_handlers.py:10442), which keeps the command's own quoting, so an exact grant still covers its own redirect (test_an_exact_grant_for_the_line_covers_its_redirect); the "trust this program" card storesbase_trust_patterns(src/kiro_crew/dashboard/chat_handlers.py:10462), which contain no>;acquire_worker_slotinsrc/kiro_crew/apps/worker_slots.pytakes app-supplied patterns, and no app in the tree passes any.Reader: src/kiro_crew/dashboard/chat_runner.py:_redirect_outside_grant -- chat -- the trusted-pattern tier declines and the card is shown, as for any uncovered redirect; test_a_quoted_gt_in_a_pattern_does_not_spell_a_write
Reader: src/kiro_crew/dashboard/chat_runner.py:_timed_grant_deadline -- chat -- reads coverage through the same helper, so the deadline matches the decision; test_a_quoted_gt_is_covered_by_a_chat_long_base_grant
Security
Fail-closed: compared with main, the only commands newly auto-approved are ones whose every non-discard
>is literal under bash's quoting, in a command with no<<,#,$'or${, on a POSIX host. A case the walk cannot decide (an open quote, an unmodelled construct, Windows) keeps the current rule. On the pattern side the change only removes coverage.Redirects that still need a grant spelling them, pinned by
test_a_redirect_bash_runs_still_needs_a_grant_that_spells_it(\nis a newline):echo 'a' > b,echo "a > b" > c>is outside quotesecho hi >> out.txt,&> out.txt,2> err.txt,2>&1 > out.txt,>| out.txtecho \"a > out\",echo \'a > out\'>is not quoted (bash writesout")echo 'a\' > outls && echo '>' > out,ls; echo '>' > outecho 'a > b,echo "a > bcat <<EOF\n'\nEOF\nls > out '\n''is text to bashls # '\nls > out # ''after#is text to bashecho $'\'' > out \'\'is an escaped quote inside$'...'echo "${x:-'"'}" > out \'${...}pair differentlyecho $(echo '>' > out)For each of the last five rows, bash 5.2 writes
outwhile a plain quote walk sees the>inside quotes.Two more shapes, each pinned by its own test, with grants
echo *andls *:echo "x\|ls '" > out '\', and the same with\;,\&or a backslash-newline: the splitter cuts at the escaped separator inside the double quotes, so the second segment starts inside them and its>is outside every quote (test_a_cut_inside_double_quotes_does_not_hide_a_redirect).echo a \>&2,echo 1\>&2,echo a \>& touch out,echo a \>&touch out: an escaped>is text for the splitter too, so the&after it separates commands, as bash reads it, and the command after the&needs a grant of its own (test_an_escaped_gt_is_text_for_the_splitter_too).Tests
In short: the issue's three commands and the other quoted or escaped spellings stay covered by a base grant; every redirect bash runs still needs a grant that spells it; red on main and green on the fix, three runs each, and each fallback has a mutant that its own case catches.
test/test_trust_patterns.py::TestQuotedRedirectIsText:test_a_quoted_or_escaped_gt_stays_covered: the issue's three commands,echo "a > b",echo a \> b,echo "a \" > b",echo 'a'\''b > c',echo 'a\' 'b > c',echo hi 2'>'file,echo '>' 2>/dev/null,echo '>|' x,ls && grep 'a > b' f,echo "a\>&b".test_a_redirect_bash_runs_still_needs_a_grant_that_spells_it: the first table above, plus a heredoc whose quotes balance over the whole command (cat <<EOF\nls '\nEOF\nls > out '\').test_a_cut_inside_double_quotes_does_not_hide_a_redirectandtest_an_escaped_gt_is_text_for_the_splitter_too: the two shapes listed under Security.test_an_unescaped_redirect_operator_stays_one_segment:echo a 2>&1,echo a >&2,echo a &>out,echo a 2>/dev/nullandecho a \\>&2(an escaped backslash, then>&2) each stay one segment, with the same write verdict as before.test_a_segment_is_read_where_it_is_cut: inecho 'x | cat > out' | cat > outand its double-quoted form, the second segment's text also sits inside the first one's quotes; it is read at its own offset, so its>is a write and the quoted one is not.test_a_segment_not_cut_verbatim_reads_every_gt: a segment that is not a verbatim cut of the command counts every>.test_a_redirect_inside_a_substitution_matches_no_grant,test_on_windows_every_gt_counts,test_a_quoted_gt_in_a_pattern_does_not_spell_a_write.test/test_trust_timed_grants.py::TestTimedGrantDeadline:test_a_quoted_gt_is_covered_by_a_chat_long_base_grant(the deadline reads coverage the same way) andtest_a_cut_inside_double_quotes_does_not_hide_a_redirect.The tests that assert the POSIX reading pin
platform_compat.IS_WINDOWStoFalsewithpatch.objectinside the test, so they give the same verdict on the Windows runner;test_on_windows_every_gt_countspins it toTrue. WithIS_WINDOWSforcedTruearound every test, the pinned tests pass in 3 of 3 runs.Red on main (
trust_patterns.pyfrom origin/main, same tests, three runs):The cut-inside-quotes tests pass on main, which counts every
>, and guard the reading this change adds. Green on the fix, three runs:96 passed. Mutants, each restored afterwards, each caught: quote walk removed (the issue cases fail); quotes walked per segment, with or without the constructs searched in the whole command (the cut-inside-quotes tests); an escaped>left to the redirect mask, or counted as a write again before&(the escaped-operator test); an escaped>inside double quotes read as a redirect (echo "a\>&b"); each segment looked up from the start of the command (the repeated-segment test); the verbatim-cut check removed (its test); an open quote read as text (echo 'a > b,echo "a > b); backslash escape removed (echo a \> b,echo \"a > out\"and three more); backslash escaping inside single quotes (echo 'a\' 'b > c'); each of the<<,#,$'and${checks removed (its own row of the table); the Windows check removed (test_on_windows_every_gt_counts); the pattern side or the deadline left on the old rule (their own tests).The new tests, repeated: 20 runs at
-n0and 10 at-n 4, every run green.Neighbours, all green: at
-n0,test/test_trust_patterns.py,test/test_trust_timed_grants.py,test/test_tool_permission.py,test/test_trust_reads.py,test/test_readonly_bash_assigning_expansions.py,test/test_readonly_bash_common_filters.py,test/test_security_gate_liveness.py,test/test_transport_permission_floor.py,test/test_chat_utils.py,test/test_chat_handlers_composition_contract.py,test/test_chat_runner_composition_contract.py,test/test_denied_commands_security.py; at-n 4, every other test file that names the module or its two entry points:test/test_acp_client.py,test/test_acp_runtime.py,test/test_chat_runner_coverage.py.Manual verification
Every command in the table and in
test_a_quoted_or_escaped_gt_stays_coveredthat can run locally was run withbash -cin an empty directory (GNU bash 5.2.15). The covered commands create no file.echo 'a' > out,echo "a > b" > out,echo 'a\' > out,echo "it's" > out,echo hi >| outand the heredoc, comment,$'...',${x#...}and${x:-...}rows createout;echo \"a > out\"createsout". Each command in the second list (echo "x\|ls '" > out '\'and its\;,\&and backslash-newline forms,echo a \>& touch out,echo a \>&touch out) createsout. Underbash -x,echo a \>&2,echo 1\>&2,echo a \>& touch outandecho a \>&touch outeach run as two commands, cut at the&;echo a 2>&1,echo a >&2,echo a &>out,echo a 2>/dev/nullandecho a \\>&2each run oneecho, and of these onlyecho a &>outcreates a file.echo 'x | cat > out' | cat > outand its double-quoted form createout.Screenshots / video
N/A: the diff touches no frontend surface.
Related Issues
Fixes #19319
Refs #19212
Pattern harvest
Rule candidate: review-prompt
Pattern: a shell operator test (
>,|,;,#) run over raw command text without tracking quotes and escapes, or with a quote walk applied where bash quotes differently (heredoc bodies, comments,$'...',${...})Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)Contribution License Agreement
Contributed under the project's Apache-2.0 license.