Skip to content

fix: model parameter expansion and comments in shell char walk - #9241

Closed
aniruddhaadak80 wants to merge 1 commit into
kirodotdev:mainfrom
aniruddhaadak80:fix/shell-chars-parameter-expansion
Closed

aniruddhaadak80 wants to merge 1 commit into
kirodotdev:mainfrom
aniruddhaadak80:fix/shell-chars-parameter-expansion

Conversation

@aniruddhaadak80

@aniruddhaadak80 aniruddhaadak80 commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Make the shell-character walk expand parameters and skip comments the way a shell does, so a deny check sees the command that would actually run.

_iter_shell_chars (src/kiro_crew/security/shell_normalizer.py) models shell quote and
escape handling, but did not handle two additional bash syntax categories:

  • ${...} parameter expansion
  • # comments

A literal ) inside $(echo ${v:-}) or after a # comment arrived with
active=True, so _matching_close_paren counted it as the substitution
closer and truncated the extracted body. The nested-payload extractor then
handed every downstream scan a fragment while bash runs the whole body, so a
nested publish hides below the truncation point -- is_denied allows a command
bash executes (same shape as the documented quoted-paren truncation behind
_matching_close_paren).

Why it matters

The _iter_shell_chars generator is the single source of truth for shell
quote/escape state walked by every consumer: the boundary walk, the git-publish
border walk, the nested-payload extractor, and is_denied/is_sensitive_bash_command.
When the machine did not know about ${...} or #, those constructs were
invisible to it, and a ) inside them was treated as a bare ) -- a
false-allow of the class security reviews have caught before.

Not a goal

  • Not a new deny rule and not a change to the denied-command catalog.
  • Not a general rewrite of the shell normalizer; only the expansion/comment behaviour is corrected.
  • No change to how a verdict is decided once the command text is normalized.

What changed (motivation → approach → change)

Rebuilt on current main, where the shell reader now lives in shell_normalizer_walk.py and the # comment half is already fixed (_opens_comment in _matching_close_paren). Only the ${...} half was still open: on main, $(: ${v:-)}; printf X) still ends the body at : ${v:-.

Main handles comments inside _matching_close_paren, not in _iter_shell_chars, so this fix does the same for ${...}. When the walk meets an unpaired ${, it jumps to the matching } (_matching_close_brace). Quotes count inside the span, a nested { adds a level, and a } inside a nested $(...) or backtick body belongs to that command. That last rule answers both review findings: the old head closed ${v:-$(echo })} at the inner }. An unclosed expansion fails closed and returns the whole remainder. _iter_shell_chars itself is unchanged.

The package line budget in test_security_gate_liveness.py goes up by 90, with a note.

Backwards compatibility

Compatible: substitution bodies only get longer, never shorter, so nothing allowed today becomes refused unless the hidden part of its body holds a refused command.

Removes nothing: no rule, key or function is removed or renamed.

Tests

test/test_security_shell_grammar.py::TestParameterExpansionHoldsItsOwnParens:

  • the body runs to the real closer for ${v:-)}, double- and single-quoted }, nested ${, bare nested {, } inside $(...) and backticks, $${ (PID, no expansion) and \$${ (escaped first dollar);
  • a verb hidden behind ${v:-)} is now refused;
  • a } inside a nested substitution cannot close the expansion early, and the command stays refused;
  • benign expansions stay allowed; unclosed expansions return the whole remainder.

Every body shape was checked in bash. The 94 security, shell, argv-floor, push-gate and rm-floor test files pass locally (9,620 tests).

Proof: PROVEN test/test_security_shell_grammar.py::TestParameterExpansionHoldsItsOwnParens (head 732edb4)

Manual verification

N/A — unit coverage sufficient: the change is a pure text walk, and each case was checked against bash.

Related Issues

Fixes #9181. Original work by @aniruddhaadak80, kept as co-author.

Sibling sweep

Swept: git grep -n "_matching_close_paren(" -- src/
Sibling: src/kiro_crew/security/shell_normalizer_walk.py:279 -- changed here (via _matching_close_paren)
Sibling: src/kiro_crew/security/argv_floor_self.py:589 -- changed here (calls _matching_close_paren, gets the fix)
Sibling: src/kiro_crew/security/rm_floor.py:96 -- changed here (calls _matching_close_paren, gets the fix)

Pattern harvest

Not generalizable: one missing shell construct in a single span walk; the comment and case rules beside it already cover the other cases.

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title
  • 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

Contribution License Agreement

Contributions are accepted under the project's Apache-2.0 license; see the Licensing section of CONTRIBUTING.md.

@aniruddhaadak80
aniruddhaadak80 requested review from a team and smeyffret September 7, 2026 13:13
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: checking Automated validation is still running merge conflict Branch has merge conflicts with its base — author must resolve before merge labels Sep 7, 2026
@aniruddhaadak80
aniruddhaadak80 force-pushed the fix/shell-chars-parameter-expansion branch from 68fd3a5 to accef9a Compare September 8, 2026 06:28
@aniruddhaadak80 aniruddhaadak80 changed the title fix: add parameter expansion and # comment handling to _iter_shell_chars fix: model parameter expansion and comments in shell char walk Sep 8, 2026
@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 merge conflict Branch has merge conflicts with its base — author must resolve before merge labels Sep 8, 2026
@github-actions

github-actions Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5, fork) — 🟡 CONCERNS

Premise-level review of 7173f920e16e743b53d3b3cec72e1abe00185811 via the fork AI-review pipeline — 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.

I've now verified the base state machine (_iter_shell_chars at shell_normalizer.py:1482 has no ${ or # branch on base, so the defect is real), the accounting-inventory test the description cites (test_push_branch_gate.py:1265, with existing ${ and # rows at lines 1287 and 1302), checked for existing mechanisms (${VAR:-literal} handling at line 429 does a different job — literal classification, not span protection), and counted siblings of the root cause (walker constructs whose interior is data: quotes and ANSI-C handled on base, ${...} and # fixed here, heredoc bodies not modeled anywhere in the char walk — only token-level _heredoc_marker at line 1211).

First-Principles-Verdict: CONCERNS

Harvest claims "not generalizable," but heredoc bodies are a third data-interior construct the walker still truncates on — same root cause, left uncounted.

What this change ships

Intent: stop is_denied allowing commands bash runs because a literal ) inside ${...} or a # comment truncated the extracted substitution body — a FIX (linked issue #9181, repros pinned by tests added here).

Inventory (4 items)
  1. A ) inside ${...} no longer truncates the substitution body handed to deny scans — justified
  2. A ) inside a # comment no longer truncates; comments now run to the newline for every consumer — justified
  3. A quoted } inside ${...} reads as literal, so the expansion cannot close early and desync the walk — justified
  4. Two regression tests pin the reviewer repros — justified

Watch

  • The root cause is "_iter_shell_chars doesn't model constructs whose interior is data"; this fixes two instances, but heredoc bodies are an unfixed sibling (count: 1 — no << branch exists in the walker, only token-level _heredoc_marker at shell_normalizer.py:1211). A ) on its own line inside $(cat <<E … E) closes the span "proven" and hides everything below it — the same allow-direction shape as security: _iter_shell_chars does not model parameter expansion or comments, so a literal ) inside them truncates every substitution span #9181, while the description's harvest section says nothing is left.
    Clears when: the heredoc-body case is shown covered by an existing layer (a test on base) or filed as a linked issue.

[FIRST-PRINCIPLES-REVIEWED] 7173f92

@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 Sep 8, 2026
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

GPT 5.6 Review (fork) — ⚠️ review incomplete

Reviewed accef9afaf9706b156602a38e3e1050bc92df940 via the fork AI-review pipeline; updated in place on each push.

No completed GPT verdict for this commit; see the Fork GPT 5.6 Review job logs.

@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 8, 2026
@github-actions

github-actions Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5, fork) — 🟡 CONCERNS

Design-level review of 7173f920e16e743b53d3b3cec72e1abe00185811 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

I have what I need. I traced the new ${...} interior loop against the POSIX rule the PR itself cites, and found a concrete gap worth flagging.

Design-Verdict: CONCERNS

The ${...} interior tracker skips quoted strings but not command substitutions, so ${v:-$(echo })} re-opens the truncation class — and regresses an input base handled.

Watch

  • POSIX 2.6.2, quoted in the diff, says the matching } is found "skipping over enclosed quoted strings, and command substitutions"; the interior loop implements only the first half. In kill $(echo ${v:-$(echo })}; git push origin main), the loop closes the expansion at the } inside $(echo }), the following ) comes back active, _matching_close_paren truncates the outer body before the ;, and is_denied never scans the nested publish bash executes. Pre-PR this input extracted correctly (the embedded $() self-balanced under plain paren counting), so the hunk's claim "errs toward scanning MORE, never less" is false for exactly this shape — it's a new fail-open, not fail-closed. Backticks inside ${} miss the same way.
    Clears when: the interior loop skips embedded command substitutions ($( … ) and backticks) when matching }, with a regression pin for ${v:-$(echo })} showing the full body extracted.

Suggestions

  • The interior loop is a second, simplified quote tracker embedded inside "THE shell quote/escape state machine" whose own docstring names two-scanners drift as the defect it was cured of twice — the $() miss is that drift; prefer routing the }-match through the shared machinery (recursive descent, as _matching_close_paren does for parens) over growing the local tracker.

[DESIGN-REVIEWED] 7173f92

@github-actions

github-actions Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Opus 4.8 Review (fork) — ✅ no blocking findings

Reviewed 7173f920e16e743b53d3b3cec72e1abe00185811 via the fork AI-review pipeline; updated in place on each push.

Review details

I've completed my falsification analysis. Let me summarize the key verification I did before finalizing.

The candidate's core premise — that bash closes ${v:-a{b} at the first } because a bare { doesn't nest (only ${ does, per extract_dollar_brace_string) — is correct, and the diff's elif qstate == 0 and c == "{": depth += 1 does over-extend the span past bash's real close point. That divergence is real.

But the candidate's claimed observable wrong outcome (an allow-direction miss) does not hold up. I traced every consumer of _iter_shell_chars:

  • git-publish (_git_publish_floor_tags → _split_push_command_segments): the over-extended segment still contains ${, so _AMBIGUOUS_EXPANSION_RE.search(command) matches → _GIT_PUBLISH_UNGATED deny; and if no clean push segment survives, if not saw_push also denies. Over-blocks — the safe direction. (Candidate conceded this.)
  • self-kill, both families: detection runs through _self_tokens/_shell_payload_walk, which use shlex.split(posix=True) — independent of _iter_shell_chars. echo ${v:-a{b}; kill $(pgrep -f kirocrew) tokenizes to a clean kill token whose own argv _substitution_bodies scans and matches kirocrew; pkill is caught via _argv_programs/_ends_argv on the trailing ;. The candidate's assertion that "the self-kill path has no backstop" is false.
  • is_denied: Pass 1 is a whole-string substring/regex match; Pass 2 splitting is purely textual (_CMD_SEPARATOR_RE), not _iter_shell_chars. Unaffected.
  • _substitution_bodies/_matching_close_paren: an over-extended span never balances → returns unproven → yields the whole remainder → scans more. Fail-closed.
  • The "cd-tracking" consumer the candidate cites (_split_shell_segments) does not exist as a live function — only referenced in a docstring.

So the divergence at most causes over-blocking (the diff's stated, safe "scan MORE" direction) and is backstopped everywhere against an actual allow. I could not re-derive a concrete allow-direction failure at 80+ without a contrived, unverifiable combination — which the falsification bar forbids. No new grounded defect surfaced in the diff either.

No findings.

[OPUS-REVIEWED] 7173f92

@bolichen97

Copy link
Copy Markdown
Contributor

@aniruddhaadak80 Thanks for staying on this. Notes from a repository-wide audit of open PRs, audited at 68fd3a5. Your branch has since moved to accef9a, so please treat anything already fixed as resolved.

Already on main: merged #9183 deleted src/kiro_crew/security.py and split it into the src/kiro_crew/security/ package, moving _iter_shell_chars to src/kiro_crew/security/shell_normalizer.py. Your current head already targets that file and adds test/test_security.py, which clears the conflict and the missing-test gap we recorded.

Still missing on main: _iter_shell_chars there has only the backslash, single-quote, ANSI-C and double-quote branches, with no ${...} and no comment handling. The gap you describe is real and open, and that remaining scope is the value of this PR.

Two defects we found in 68fd3a5, please confirm they are addressed in the new revision:

  1. The ${...} branch passed True as the seventh _ShellChar field, trailing_escape, and left active true. A ) inside ${v:-)} was therefore still counted by _matching_close_paren, so the stated fix did not take effect.
  2. The comment branch tested only state == 0 and ch == '#', not word start. An unquoted mid-word #, which bash prints literally in echo a#b, ended the generator and hid the rest of the command. That is an allow-direction truncation.

One description fix: #8830 is the case-pattern terminator, which #9164 addresses and this diff does not. #9164 edits the same walk, so please coordinate with it before rebasing.

Posted from the 2026-09-08 open-PR relationship audit (read-only, one auditor per PR); reply here if any of this is wrong.

@aniruddhaadak80

Copy link
Copy Markdown
Contributor Author

All blocking findings addressed in the latest push (rebased onto current main, 2 commits total):

@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 9, 2026
@dwu96 dwu96 added needs-pr-triage PR scanner: awaiting automated triage drive-to-green PR claimed by drive-to-green pipeline and removed needs-pr-triage PR scanner: awaiting automated triage labels Sep 16, 2026
@dwu96

dwu96 commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

🤖 Kiro Crew [operator: dwu96#8c08bee0]: This PR has been inactive for 7+ days with failing CI. I've assessed the blockers and they appear resolvable — I'll send fixes directly to this branch as a co-author.

Assessment: Exactly two red checks on 7173f920e, both mechanical. (1) Backend Lint & Type Check fails the comment-history gate only — narration counts grew 21→24 in shell_normalizer.py and 68→72 in test_security.py; the gate names its own remedy ("rewording the matched lines is what clears it"), so this is a text edit, no logic change. (2) GPT 5.6 Review is not a verdict: the check-run title reads "review incomplete" and its in-place comment is still pinned to the superseded head accef9afa, so the lane never produced a verdict for the current head — a rerun/refresh clears it. Every other lane is favourable at this head: Opus 4.8 clean, Design and First Principles advisory CONCERNS. Both CONCERNS have concrete, stated clearance conditions and I plan to adopt them: Design's is a real fail-open the interior loop introduced — POSIX 2.6.2 (which the diff itself quotes) says the matching } is found skipping quoted strings and command substitutions, and the loop implements only the first half, so ${v:-$(echo })} closes early and truncates the outer body; the fix is to skip embedded $( … ) and backticks when matching }, plus a regression pin. First Principles' clears by filing the heredoc-body sibling as a linked issue, which I'll do. No merge conflict (mergeable), 132 added lines over 2 files.

Scope note: I will not change the design direction. Design's further suggestion — replacing the local interior tracker with recursive descent through the shared machinery — is an architecture change I'll disposition rather than perform, and I will not weaken any refusal this walk feeds.

If you'd prefer I don't touch this PR, add the pr-no-autofix label.

iamwhatever pushed a commit that referenced this pull request Oct 4, 2026
Add regression tests for `${...}` parameter expansion and `#` comment
handling already present in the shell-char walk via `_iter_shell_chars`
and `_matching_close_paren` / `_opens_comment`.

## What these tests pin

**test_security.py (7 new methods in TestBuiltinDenyPatterns /
TestSubstitutionCloserReadsCommandGrammar):**

- `test_expansion_close_ignores_quoted_braces` — a quoted `}` inside
  `${...}` is literal per POSIX 2.6.2 and must not close the expansion.
- `test_expansion_interior_reports_its_own_quote_state` — interior chars
  report their own quote state so a single-quoted `$(` reads as data.
- `test_expansion_nests_only_on_dollar_brace` — a bare `{` does not open
  an expansion; only `${` does.
- `test_ansi_c_hidden_self_kill_is_denied` — a self-kill hidden behind an
  ANSI-C `$'\''` inside `${...}` is still DENIED.
- `test_comment_inside_nested_substitution_is_skipped_by_gate` — a `#`
  comment inside a nested `$(...)` must not make `)` or `}` reach the
  outer walk as structural closers.  Three shapes bash 5.2 runs where the
  gate must still deny: `${v:-$(echo # )\necho y)}`-style and the
  bare-paren-default `: ${v:-(}` shapes.
- `test_a_hash_right_after_an_empty_substitution_is_a_word_not_a_comment`
  — a `#` right after `$()` opens a comment only if a word break precedes.
- `test_an_interior_quote_opener_is_syntax_not_a_quoted_run` — an interior
  quote delimiter is syntax for the frame it opens from, not quoted data.

**test_security_gate_liveness.py (4 new functions):**

- `test_expansion_opener_flood_is_fast` / `test_self_kill_expansion_flood_is_fast`
  — a flood of unclosed `${` openers must not make the gate quadratic via
  the `is_sensitive_bash_command` entry point.
- `test_is_denied_single_trailing_brace_flood_is_fast` /
  `test_is_denied_self_kill_brace_flood_is_fast` — same flood through
  `is_denied` (no size ceiling; Opus finding #1 & #3 from #9241).

## What was NOT kept

Three tests from the earlier PR (#9241 / wip/shell-char-walk-expansion)
reference private functions (`_expansion_span`, `_skip_nested_substitution`)
that exist only on the wip branch; those are follow-up work.

## Related

Closes #9181
Supersedes #9241

Co-authored-by: cixuuz <26782336+cixuuz@users.noreply.github.com>
iamwhatever pushed a commit that referenced this pull request Oct 4, 2026
Add regression tests for `${...}` parameter expansion and `#` comment
handling already present in the shell-char walk via `_iter_shell_chars`
and `_matching_close_paren` / `_opens_comment`.

## What these tests pin

**test_security.py (7 new methods in TestBuiltinDenyPatterns /
TestSubstitutionCloserReadsCommandGrammar):**

- `test_expansion_close_ignores_quoted_braces` — a quoted `}` inside
  `${...}` is literal per POSIX 2.6.2 and must not close the expansion.
- `test_expansion_interior_reports_its_own_quote_state` — interior chars
  report their own quote state so a single-quoted `$(` reads as data.
- `test_expansion_nests_only_on_dollar_brace` — a bare `{` does not open
  an expansion; only `${` does.
- `test_ansi_c_hidden_self_kill_is_denied` — a self-kill hidden behind an
  ANSI-C `$'\''` inside `${...}` is still DENIED.
- `test_comment_inside_nested_substitution_is_skipped_by_gate` — a `#`
  comment inside a nested `$(...)` must not make `)` or `}` reach the
  outer walk as structural closers.  Three shapes bash 5.2 runs where the
  gate must still deny: `${v:-$(echo # )\necho y)}`-style and the
  bare-paren-default `: ${v:-(}` shapes.
- `test_a_hash_right_after_an_empty_substitution_is_a_word_not_a_comment`
  — a `#` right after `$()` opens a comment only if a word break precedes.
- `test_an_interior_quote_opener_is_syntax_not_a_quoted_run` — an interior
  quote delimiter is syntax for the frame it opens from, not quoted data.

**test_security_gate_liveness.py (4 new functions):**

- `test_expansion_opener_flood_is_fast` / `test_self_kill_expansion_flood_is_fast`
  — a flood of unclosed `${` openers must not make the gate quadratic via
  the `is_sensitive_bash_command` entry point.
- `test_is_denied_single_trailing_brace_flood_is_fast` /
  `test_is_denied_self_kill_brace_flood_is_fast` — same flood through
  `is_denied` (no size ceiling; Opus finding #1 & #3 from #9241).

## What was NOT kept

Three tests from the earlier PR (#9241 / wip/shell-char-walk-expansion)
reference private functions (`_expansion_span`, `_skip_nested_substitution`)
that exist only on the wip branch; those are follow-up work.

## Related

Closes #9181
Supersedes #9241

Co-authored-by: cixuuz <26782336+cixuuz@users.noreply.github.com>

Copy link
Copy Markdown
Contributor

Open-PR triage (2026-10-05), audited at head 7173f92 against origin/main af1eb61. This is a read-only check, with one auditor per PR.

Status: DISTINCT and still needed. Change requested: before merge, fix one fail-open that this head adds.

Already on main: merged PR #8579 (7169c95) taught _matching_close_paren about # comments via _opens_comment. Main already extracts kill $(echo x # )\npgrep -f kirocrew) whole, so the comment branch here duplicates that part. The ${...} gap from Issue #9181 is still open on main: kill $(echo ${v:-)}; pgrep -f kirocrew) extracts echo ${v:- and is_denied returns None. This PR fixes that input.

What breaks: I ran is_denied on main and on the merge result of this head into main. Both inputs below are fail-closed on main and fail-open on this PR:

command main this PR
P=$(: ${v:-$(echo })}; pgrep -f kirocrew); kill $P denied allowed, body : ${v:-$(echo }
T=$(: ${v:-$(echo })}; printf token); kirocrew $T denied allowed

In bash, both bodies run the second command ([PGREP_RAN] / [token]). The cause is the interior loop in _iter_shell_chars. It closes ${ at a } that sits inside a nested $(...), so the outer ) comes back active. This is the same issue Design Review flagged and dwu96 said they would fix (POSIX 2.6.2 also skips command substitutions). No commit has landed since then, so it is still present at this head. The docstring's claim "errs toward scanning MORE, never less" does not hold for these inputs.

Fix: when matching }, skip nested $(...) (for example with _matching_close_paren) and backtick bodies, then pin the two rows above. You could also put the ${ rule in _matching_close_paren, next to _opens_comment, and drop the duplicate # branch.

Other notes:

@bolichen97 bolichen97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P0] ${...} interior loop closes at a } inside a nested $(...), re-opening a self-protection bypass main blocks

Class: Shell-normalizer span truncation: a security floor fails open.

Mechanism: The new ${...} interior loop in _iter_shell_chars closes the expansion at a } that sits inside a nested command substitution. The following ) is then treated as active and the outer substitution body is cut short, so later commands are hidden from the argv-floor rules. At least two command shapes that main denies (self-protection and token-exfiltration floors) return no denial on this head and on the merge result with main.

User impact: Agent commands that main blocks at the self-protection floor would be allowed after merge.

Location: src/kiro_crew/security/shell_normalizer.py:1594 @ 7173f920e16e743b53d3b3cec72e1abe00185811

Minimum fix: When scanning ${...}, skip nested $(...) and backtick bodies before counting { / } (e.g. reuse _matching_close_paren), and pin main-denied shapes as regression rows.

How to verify: Regression rows in test_security asserting is_denied is non-None for nested-substitution-inside-${...} shapes. They pass on main, fail on this head, and must pass after the fix along with this PR's own new case.

Exploit details are left out on purpose. They are available to maintainers through the repo's private security reporting.

Independently re-verified on head 7173f920e1 with a focused local reproduction.

qstate = 0
elif qstate == 0 and c == "{":
depth += 1
elif qstate == 0 and c == "}":

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P0] ${...} interior loop closes at a } inside a nested $(...), re-opening a self-protection bypass main blocks — see the review summary for mechanism, fix and test.

@cruisercohen cruisercohen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed locally at head 7173f92. Current main already handles the #-comment half of the issue (_opens_comment in _matching_close_paren). The ${v:-)} half is still real, and this PR fixes it. But the new ${...} interior loop counts a } inside a nested $(...) or backtick as the closer, so a parameter expansion whose default holds a command substitution that contains } is now cut short at that inner brace. Main extracts the full body for that shape, both on this head and when the branch is merged with main. Requested changes:

  1. When scanning for the matching }, skip enclosed command substitutions, for example by reusing the existing paren matcher for nested $( and backticks.
  2. Add regression cases that assert the deny result is unchanged for nested-substitution-inside-${...} shapes.
  3. Rebase onto main's comment handling and drop the second comment model, so there is one comment rule. Then re-check TestSubstitutionCloserReadsCommandGrammar.
  4. Backend Lint & Type Check (3.12) is red on this head. flake8 and black are clean locally, so this is likely mypy.

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Oct 18, 2026
…on closer

A ) inside a parameter expansion such as ${v:-)} is the default word,
not a closer, but the span walk closed the substitution body there and
the commands after it were never scanned. _matching_close_paren now
jumps past the matching }; quoting runs through the span, nested {
opens a level, and a } inside a nested $(...) or backtick body belongs
to that command. An unclosed expansion fails closed.

Fixes kirodotdev#9181

Co-authored-by: ANIRUDDHA ADAK <aniruddhaadak80@users.noreply.github.com>
@iamwhatever
iamwhatever force-pushed the fix/shell-chars-parameter-expansion branch from 7173f92 to 732edb4 Compare October 24, 2026 01:23
@iamwhatever
iamwhatever requested review from a team and KiroCrewCaptain as code owners October 24, 2026 01:23
Copilot AI balanced review requested due to automatic review settings October 24, 2026 01:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@github-actions github-actions Bot removed the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Oct 24, 2026
@iamwhatever

Copy link
Copy Markdown
Contributor

Thanks @aniruddhaadak80 for this fix. Main has moved a lot since this opened: the shell reader now lives in shell_normalizer_walk.py, and the # comment half was already fixed there. The ${...} half was still open, so I rebuilt it on current main inside _matching_close_paren and addressed the review finding: a } inside a nested $(...) or backtick no longer closes the expansion.

A fork PR cannot pass the Sensitive Change Review gate for files under src/kiro_crew/security/, so the work continues in #23620, with you kept as co-author. This PR stays open until that one merges.

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Oct 24, 2026
@bolichen97

Copy link
Copy Markdown
Contributor

[operator: bolichen97] Closing as superseded by #23620, which carries this change forward from a same-repo branch (a fork PR cannot pass the Sensitive Change Review). Please follow #23620; thank you for the contribution.

@bolichen97 bolichen97 closed this Oct 25, 2026
@github-actions github-actions Bot removed the readiness: action required A blocking check or review needs attention label Oct 25, 2026
@mbriones98 mbriones98 removed the drive-to-green PR claimed by drive-to-green pipeline label Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor) merge conflict Branch has merge conflicts with its base — author must resolve before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

security: _iter_shell_chars does not model parameter expansion or comments, so a literal ) inside them truncates every substitution span

7 participants