Skip to content

fix(security): a bare opener in a redirect target is not grammar (#8634) - #8672

Merged
bolichen97 merged 1 commit into
mainfrom
fix/output-redirect-scan-quoting-8634
Sep 5, 2026
Merged

bolichen97 merged 1 commit into
mainfrom
fix/output-redirect-scan-quoting-8634

Conversation

@bolichen97

@bolichen97 bolichen97 commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

_output_redirect_scan finds where an output redirect's target ends so the shell-command security gate can tell a redirect from a script path. Its span walk counted every (/{ as a depth opener. The tokenizer that feeds it resolves quoting, so a quoted ( — one filename character to bash — arrived bare, opened a span that never closed, and the target ran past the <<</<< operator that should have ended it. The program arriving on stdin was then never scanned.

Measured in bash — each of these runs the here-string program while the gate returned is_denied = None:

python3 2>'a)(b'<<< '<program>'    # quoted parens
python3 > "a{b" <<< '<program>'    # quoted brace
python3 2>a{b<<<'<program>'        # a bare { needs no quoting at all

Third site of the #8150 defect class (a delimiter counted in text where quoting decides whether it is a delimiter at all).

Why it matters

A program arriving on stdin via a here-string bypasses the deny scan entirely — the exact failure mode the scanner's own docstring describes for a glued heredoc marker, reached through a quoting spelling. The end-to-end consequence is real: python3 2>'a)(b'<<< 'from kiro_crew.cli import main; main()' was allowed on main and is denied with this fix.

What changed (motivation → approach → change)

The issue proposed routing the walk through #8150's shared _quoting_step rule. Two review rounds subtracted that to a single, smaller rule:

  • Pre-push GPT 5.6 proved quote-tracking alone is inert end-to-end: _self_token_frames shlex-resolves quoting, so the production tokens arrive de-quoted (2>'a)(b'<<< → 2>a)(b<<<) and there are no quotes left to track.
  • The First Principles lane then proved the quote-tracking rider was not merely inert but harmful on the tokens production actually carries: a quote character that survives tokenization is literal filename data (2>"a'b" → 2>a'b), and reading it as grammar consumed the <<< to end-of-text — reopening the defect in the opposite direction. Its subtraction demand was verified and taken.

What ships is one rule: at depth zero, only a $-prefixed opener ($(, ${, $(() starts a substitution span. A bare ( mid-word is never grammar in a command that runs — unquoted it is a bash syntax error, so the only executable spelling that reaches the scan bare is a quoted filename character. A bare { is an ordinary filename character (measured). Inside a substitution every opener still counts (a nested subshell closes with its own )). Quote characters are stepped over as data.

Not changed, per the issue's scope: where a redirect target begins, the unterminated-target fallback, and the sibling scanners (_substitution_depth_delta, _is_self_kill, _operand_span_end — #8633's territory).

Residuals, documented in the code: a quoted '$(' or a quoted backtick in a filename de-quotes to the same characters as real grammar and still holds/toggles a span — indistinguishable without the quoting the tokenizer already destroyed. Closing that class needs quote-preserving tokenization at the frame level (follow-up issue, see comments).

Tests

TestOutputRedirectScanQuoting (6 tests), every assertion measured against real bash; reverting the one-line rule is caught by 4 distinct tests:

  • End-to-end positive controls through is_denied() with the exact production spellings (all None on main).
  • De-quoted-domain boundaries (2>a)(b<<<, 2>a{b<<<x, $((arith)) still spans).
  • Quote-bearing boundaries (quote chars as data): quoted/escaped (, {; ANSI-C $'a(b'.
  • The review-found inverse defect pinned: a data quote is not grammar (2>a'b<<<x keeps its boundary; $$', backtick-interior quote unchanged).
  • Inverse controls byte-identical before/after: 2>$( (x)>y ), 2>`echo>x` , ${x:->}, unterminated quote, trailing backslash.
  • Detector-level _python_reads_stdin on both token forms.

By construction the rule only ever opens fewer spans than main, so the target can only end earlier-or-equal — an operator main exposed can never be hidden by this change.

Manual verification

All bash ground-truth probes in the docstrings/tests were executed against real bash (file creation + program execution observed): > 'a(b' <<<, glued 2>'a)(b'<<<, 2>a{b<<<, 2>"a'b"<<<, 2>$'a\')', and the syntax-error control 2>a(b.

Related Issues

Closes #8634
Refs #8150

Pattern harvest

Rule candidate: review-prompt
Pattern: a fix must be proven at the public verdict level on the tokens production actually carries — the tokenizer between the entry point and the helper can both destroy the information a rule keys on (quotes stripped) and deliver that information as data the rule must not re-interpret as grammar (quotes surviving as filename characters).

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|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) — N/A: module spec documents the sibling de-quoted path, not this scanner's internals
  • No secrets, credentials, or internal references in the diff

@github-actions

github-actions Bot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5) — ✅ PASS

Design-level review of fb2662f18ebb508a74803f65c2459cb621ec6629 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

A real deny-scan bypass at the agent boundary, closed by a strictly narrowing one-line rule whose fail-closed monotonicity is provable from the diff, with residuals honestly documented.

[DESIGN-REVIEWED] fb2662f

@github-actions

github-actions Bot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review — ✅ no blocking findings

GPT 5.6 completed its review of fb2662f18ebb508a74803f65c2459cb621ec6629 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] fb2662f

False positive or not applicable? A repository writer can comment:
/ai-review override gpt fb2662f18ebb508a74803f65c2459cb621ec6629: <one-sentence reason>

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

github-actions Bot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5) — ✅ PASS

Premise-level review of fb2662f18ebb508a74803f65c2459cb621ec6629 — 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/CONCERNS are advisory.

The change is a one-line rule tightening in _output_redirect_scan plus its docstring and six tests. I verified the code at src/kiro_crew/security.py:4543, confirmed the helper is private with 2 production call sites (both in security.py, the existing detector path — no new surface), and confirmed the declared out-of-scope siblings exist: _substitution_depth_delta (security.py:4354, counts ) unconditionally, used at 4 sites), _operand_span_end, _is_self_kill — all named in the description as #8633's territory. The deeper cause (the tokenizer destroys quoting before the scan) is named in the diff's own residuals paragraph with a follow-up. Nothing rides along; no config key, flag, or public symbol is added.

First-Principles-Verdict: PASS

A measured deny-scan bypass is closed with one subtractive rule, no new surface, and the deeper cause and unfixed siblings are declared, counted, and tracked.

What this change ships

Intent: stop a quoted ( or bare { in a redirect target from hiding the here-string program from the deny scan — a FIX.

  1. Three measured bash spellings (2>'a)(b'<<<, 2>a{b<<<, > "a{b" <<<) now denied — justified, reproduced defect.
  2. At depth zero only $(/${/$(( opens a substitution span; real substitutions still span — justified, mechanism-level.
  3. Docstring states the rule, the inverse defect, and the residual quoted-$( class — justified, same-commit doc invariant.
  4. Six tests pin the fix, the inverse direction, and byte-identical unmoved boundaries — justified.

The fix sits at mechanism level; the nameable cause (quote-preserving tokenization at the frame level) and the 3 sibling scanners sharing the #8150 root cause (_substitution_depth_delta at 4 call sites, _is_self_kill, _operand_span_end) are declared and deferred to #8633 in both the description and the docstring — accepted-and-deferred, not a demand.

[FIRST-PRINCIPLES-REVIEWED] fb2662f

@github-actions

github-actions Bot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Opus 4.8 Review — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] fb2662f

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

False positive or not applicable? A repository writer can comment:
/ai-review override fable fb2662f18ebb508a74803f65c2459cb621ec6629: <one-sentence reason>

@bolichen97
bolichen97 force-pushed the fix/output-redirect-scan-quoting-8634 branch from 5746aa4 to fb2662f Compare September 5, 2026 06:45
@bolichen97 bolichen97 changed the title fix(security): quote-aware span walk in the redirect target scan (#8634) fix(security): a bare opener in a redirect target is not grammar (#8634) Sep 5, 2026
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 5, 2026
@bolichen97

Copy link
Copy Markdown
Contributor Author

Disposition — First Principles BLOCK ("delete the _quoting_step wiring; keep only the depth-zero rule") on 5746aa4: ACCEPTED, subtraction taken in full on head fb2662f.

Verified on-source before acting, then shipped exactly the demanded subtraction. The _quoting_step wiring, the $$-run parity loop, the backtick-scoped guard, the end clamp, and the _quoting_step/_ANSI_C_QUOTE helpers are all deleted; the depth-zero $-prefix rule alone remains, and all three measured bypasses stay denied end-to-end. The inverse defect is pinned by test_a_data_quote_is_not_read_as_grammar so quote-as-grammar cannot return silently. One correction for the record: the review's end-to-end claim did not reproduce (is_denied denied the exact spelling on the blocked head too, via the self-import gate on the glued frame token) — the unit-level widening was real and sufficient grounds on its own.

Measured on the blocked head: _output_redirect_scan("2>a'b<<<x") returned ("a'b<<<x", 9) (operator swallowed) vs ("a'b", 5) on main, and bash executes the spelling (creates a'b, runs the program).
Both call sites (security.py:5025,5039) are fed by _self_token_frames — the quote-bearing domain has zero production members, exactly as the review stated.
By construction the remaining rule only opens FEWER spans than main, so an operator main exposed can never be hidden by this change.

@bolichen97

Copy link
Copy Markdown
Contributor Author

Disposition — Design watch item "the documented residual needs a filed issue, not a docstring note" on 5746aa4: ACCEPTED, resolved on head fb2662f.

The residual class (quoted '$(', and the sibling quoted-backtick spelling the review's example generalizes to) is filed as issue #8683, scoping the quote-preserving tokenization work at the frame level with all four known sites of the #8150 class enumerated. The scanner's docstring now points at tracking rather than declaring the residual "not chased".

@bolichen97

Copy link
Copy Markdown
Contributor Author

Disposition — Design watch item "_quoting_step copied verbatim from unlanded #8579 with a comment-enforced pact" on 5746aa4: RESOLVED BY DELETION on head fb2662f.

The First Principles lane proved the wiring had zero quote-bearing production callers and actively misread data quotes as grammar, so the helper and all its guards were subtracted rather than the pact strengthened. This PR no longer carries any code from #8579; the helper lands there with its real consumer.

@bolichen97
bolichen97 enabled auto-merge (squash) September 5, 2026 18:49
@bolichen97
bolichen97 merged commit d3d8ff9 into main Sep 5, 2026
95 of 102 checks passed
@bolichen97
bolichen97 deleted the fix/output-redirect-scan-quoting-8634 branch September 5, 2026 19:30
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Sep 5, 2026
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: _output_redirect_scan's span walk is quote-blind, so a quoted '(' swallows the here-string after it

2 participants