Skip to content

fix(guardrails): read a # after a word-continuing ( or substitution as literal - #12384

Merged
kyle-sexton merged 12 commits into
mainfrom
fix-12361-replacement
Nov 2, 2026
Merged

kyle-sexton merged 12 commits into
mainfrom
fix-12361-replacement

Conversation

@kyle-sexton

@kyle-sexton kyle-sexton commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

Closes #12398
Refs: #12361

Summary

This PR closes #12398. It also delivers the #12360/#12361 parser work: it replaces lanes PR #12361, merges #12361's head, and fixes the review findings the lanes fixer could not land, because its path guard refuses the 20 generated hook-utils.sh copies.

  1. Security review (IMPORTANT): echo $(:)#; git reset --hard still hid the reset. Bash keeps the closing ) of $(…), $((…)), <(…), >(…) and a closing backtick in its word, so the # after it is literal. The guard read it as a comment.
  2. Code review (advisory): with shopt -s extglob, !(x)#; git reset --hard and f@()#; … are one command word and the reset runs. The guard only checked the extglob-off reading.

Later review rounds on this PR found more forms in the same family. The changelog fragments list them all.

Fix

In lib/hook-utils.sh (hook::bash_parse_segments_uncached):

  • The paren stack now tags each frame: 1 word-continuing (array assignment, extglob), s substitution (( right after an unquoted $, or a process-substitution </>), b backtick, a extglob-dependent (!(…), f@()), c case statement, 0 anything else. A ) that pops 1 or s, or a closing backtick, makes the # right after it literal.
  • Frames stay open across ;, &, | and newlines inside them. Bash allows these inside $(…), @(x;y) and multi-line a=(…), which the old reset-on-separator dropped.
  • An unquoted case or esac in command position opens or closes a c frame. A case pattern's ) pops nothing, so $(case x in x) :;; esac)# keeps its substitution frame, and a # after a pattern ) stays a comment.
  • Inside backticks, a comment or a quote ends at the closing backtick, as in Bash (echo `#`; git reset --hard and echo `'`; git reset --hard #' are now blocked).
  • For an a frame followed by #, the parser passes the rest of the command (open frames and pending here-docs included) once with extglob on, then continues with the comment reading. The callback sees the commands of both readings. This happens at most once per parse, so the cost stays at most 2n.
  • Both review threads asked for the bare ! before ( and f@() unit rows to change. They now expect both readings' segments.

The 20 copies were regenerated with scripts/sync-shared-copies.sh. The guardrails changelog fragment from #12361 is extended. #12361's context-guard fragment was renamed (…cb95f8ba.md to …cb95f8c0.md) because typos flags the ba suffix and blocked the push.

Verification

Bash was the oracle for every new case. Each ran with harmless commands (echo HIT), under bash -c and bash -O extglob -c:

  • echo $(:)#; echo HIT, echo $((1))#; …, echo $(:;:)#; …, $(:⏎:)#, echo `:`#; …, echo `:;:`#; …, cat <(:)#; …, : >(:)#; …: HIT runs in both modes (the # is literal).
  • echo $(:) #; echo HIT and echo `:` #; …: HIT does not run (comment).
  • !(x)#; echo HIT, !()#; …, f@()#; …, echo !(x)#; …: HIT runs with extglob on. With it off, there is a negation, a function definition or a syntax error, and HIT does not run.
  • !(x)#'⏎'; echo HIT: HIT runs with extglob on.
  • echo @(x;y)#; echo HIT (extglob on) and a=(x⏎y)#; echo HIT: HIT runs.
  • echo `#`; echo HIT and echo `'`; echo HIT #': HIT runs.
  • echo $(case x in x) :;; esac)#; echo HIT: HIT runs. a=($(case x in x)#'⏎echo HIT;; esac)): HIT runs (the # is a comment).

lib/hook-utils.test.sh also carries a generated oracle table: 9276 rows (all 6912 at depth 1, and 2364 of 75744 depth-2 nestings, sampled one in 32 by a fixed hash). For each row, bash in both extglob modes decides whether the trailing command runs and whether a redirect writes its file, and the parser must report both. On the previous head, 243 rows were hidden; on main, 1138; now none.

Tests, run in the worktree after regenerating the copies:

  • bash lib/hook-utils.test.sh: PASS=693 FAIL=0, oracle table included
  • bash plugins/guardrails/hooks/block-dangerous-git.test.sh: PASS=969 FAIL=0
  • bash plugins/guardrails/hooks/run-guards.test.sh: PASS=714 FAIL=0
  • bash plugins/guardrails/hooks/block-root-delete-target.test.sh: PASS=5727 FAIL=0
  • bash plugins/repo-hygiene/skills/clean/scripts/destructive-guard.test.sh: 338/338
  • Every other plugins/guardrails/hooks/*.test.sh and plugins/source-control/hooks/*.test.sh: all exit 0, 0 failures
  • bash scripts/sync-shared-copies.sh --check: all 567 registered copies match
  • Pre-push check-affected: passed

Not verified:

  • shopt -s extglob set earlier in the same command. Both readings are checked, so the guard does not depend on the mode.
  • case used in an unusual keyword position (after a word other than ! { then do else elif if while until time) is not tracked as a case frame.

Related

🤖 Generated with Claude Code

melodic-automation-lanes Bot and others added 5 commits November 2, 2026 13:26
Lane-Activity: work-resolve-issue/resolve-issue
Bash keeps the closing ) of $(…), $((…)), <(…), >(…) and a closing
backtick in its word, so a # right after one is literal text, not a
comment: `echo $(:)#; git reset --hard` runs the reset. The parser now
tracks those frames, keeps every frame open across ; & | and newlines
inside it, and pushes a case frame so a case pattern's ) pops none. A
comment or quote inside backticks ends at the closing backtick.

For !(x)# and f@()#, where # is literal only with extglob on, the parser
reads the rest of the command once with extglob on and once with it off,
so the callback sees the commands of both readings.

Regenerated the 20 hook-utils.sh copies with scripts/sync-shared-copies.sh.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KeBVgcHpe4YpFuL5b24Ft2
The hex suffix cb95f8ba ends in `ba`, which typos flags.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-02T14:41:33.680526Z 608aa2e PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 608aa2e936

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread lib/hook-utils.sh Resolved

@github-actions github-actions Bot 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.

claude-security-review: 1 inline finding(s) on 608aa2e9361f0546341e81f274f3e117643c21c4; the summary is in this lane's pull request comment.

claude-lane: claude-security-review

Comment thread lib/hook-utils.sh Resolved
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

claude-security-review reviewed 0072d87129294937b7033da2ac297e0d34ffc15e: read 23 of 23 changed file(s); 4 finding(s), 2 as inline comments.

claude-security-review: incremental review of 52d4cfbb..0072d871

critical-findings: 0

The round cap applied (past the cap: yes), so only CRITICAL and IMPORTANT findings are reported. There are 4 IMPORTANT findings: 2 new inline comments and 2 restated as already posted. None is CRITICAL. Each one is a way past a local guardrail hook, reachable only by text that steers the agent's command. None of them leaks a token or secret, compromises an install path or can cause a charge.

What the increment fixes:

  • Backticks in the (( lookahead. hook::_bps_dparen_is_arith now skips backtick text. That closes this lane's ((`:))`; bash <<< 'rm -rf ~') ) finding (lib L4603-4615) and the dp_end/dtop finding on the $((…))# and @(…)# forms (source-control L4605-4617). The new block-root-delete-target rows cover the first.
  • Array subscripts. The […] reader now stops at a quote, backslash, $ or backtick, and falls back when there is no matching ]. This is strictly narrower than the previous head. The new a=([$(git reset --hard)]=1), backtick and quoted-] rows are blocked.

New findings (inline):

  1. $'…' in the lookahead (lib L4605-4608). The lookahead's ' case still ends a $'…' string at an escaped \'. So (($'\''))'; bash <<< 'rm -rf ~') ) takes the arithmetic reading, and the here-string is lost. This was the "related gap" in this lane's earlier lookahead finding, and it is still open. It is a regression from base.
  2. ( inside ${…} (lib L5268-5272). The new check finds an open ${ only in the current word. The ( itself closes that word, so a second ( in the same expansion, or one after a blank, still pushes a frame. echo $(echo ${x/(/(})#; git reset --hard still hides the reset.

Already posted (still open, lines unchanged):

What I checked:

  • Scope. I read every hunk of all 23 files in the increment.
  • The 20 regenerated hook-utils.sh copies. Each carries the same three hunks as lib/hook-utils.sh, line for line, offset by +2 lines. The findings apply to all of them. Fixing lib/ and re-running scripts/sync-shared-copies.sh covers them.
  • Test files. The two test-file hunks (block-dangerous-git.test.sh, block-root-delete-target.test.sh) only add exit-2 rows. They run nothing beyond the hooks under test.
  • Settled threads. None matches the new candidates.

Not applicable:

  • No trust control, token request, kill switch, merge hold, workflow, plugin manifest or standing instruction is touched in this increment.
  • I found no embedded instructions in the PR context, settled-threads or prior-findings files.

I traced the inputs above by hand; there is no shell in this lane. Please confirm each against bash -c before fixing.

@github-actions github-actions Bot 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.

claude-review: 2 inline finding(s) on 608aa2e9361f0546341e81f274f3e117643c21c4; the summary is in this lane's pull request comment.

claude-lane: claude-review

Comment thread lib/hook-utils.sh Resolved
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

claude-review reviewed 0072d87129294937b7033da2ac297e0d34ffc15e: read 23 of 23 changed file(s); 1 finding(s), 1 as inline comments.

claude-review: code-review lane (incremental, 52d4cfbb..0072d871)

I found 1 blocking finding, posted inline on lib/hook-utils.sh. The new rule that a plain ( inside an open ${…} pushes no frame has no matching rule for ). So a balanced (…) inside ${…} now closes the frame around it one paren early. echo $(echo ${x/(a)/y})#; git reset --hard hid nothing at the previous head. It hides the reset now.

Round cap. The prior-findings file says past the cap: yes (5 rounds, cap 3). I report blocking findings only, and I dropped advisory candidates.

Earlier findings from round 5 (each marked lines changed since posted: yes):

  • Subscript scan (r4446710886, r4446710899, r4446710904): looks fixed.
    • The scan stops at ', ", \, $ or a backtick. With no matching ] it leaves c and i alone, so the main loop reads those characters.
    • The slice chars[@]:i:__hu_bps_k-i and i=__hu_bps_k-1 both end on the matching ].
    • __hu_bps_k is declared local at L4888.
    • New exit-2 rows cover $(…), a backtick, "x]", ']'x, "]" and ['[']. I'm not raising these again.
  • ( inside ${…} in an array (r4446710912): the here-doc half is fixed, and the a=(${x/(/y}) row covers it. The ) half of that finding is still open: echo $(echo ${x/)/y})#; git reset --hard. I folded it into the new inline finding, because one fix covers both.
  • Settled threads: nothing in this increment reopens them.

Backtick skip in hook::_bps_dparen_is_arith: this is correct as I read it. Escaped characters are skipped, and an unterminated backtick runs to n and leaves dp_end=-1. The two new block-root-delete-target.test.sh rows cover it.

Coverage. I read every hunk of all 23 files in scope. The 20 regenerated hook-utils.sh copies carry the same four hunks as lib/hook-utils.sh, offset by +2 lines (+0 for the [ hunk header pair). I also read the two test files' hunks.

Other checks

  • Closing issue fix(guardrails): scan the command inside an escaped backtick nested in backticks #12398: this increment doesn't touch it. It was checked in an earlier round.
  • Manifests: no plugin.json or marketplace.json changes in this increment.
  • Embedded instructions: none in the PR context, settled threads or prior findings.
  • Verification: this lane has no shell, so I ran nothing. The traces in the finding come from reading the code at head. The test counts in the PR and in replies are the author's claims and unverified.

…docs ordered

Review fixes on the parser in lib/hook-utils.sh:

- The second ( of $(( or (( opens an arithmetic frame. Inside it every
  ( but a substitution's is a plain group, and case or esac is a
  variable name, so echo $((case))#; git reset --hard is blocked. An
  esac closes only a case frame that is innermost.
- A backtick inside a backtick substitution's comment or quote closes it
  only after an even run of backslashes, as in bash, so an escaped
  backtick in a comment no longer blocks a harmless command.
- The extglob-on pass's here-doc bodies merge at the end of the parse:
  one entry per start offset, in source order, as substitutions_to in
  the repo-hygiene destructive guard expects.
- The 19 shared changelog fragments now describe the whole parser
  change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@github-actions github-actions Bot 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.

claude-review: 1 inline finding(s) on 76005f26c0647070d9a36ff0f96facdb09394bd7; the summary is in this lane's pull request comment.

claude-lane: claude-review

Comment thread lib/hook-utils.sh Resolved

@github-actions github-actions Bot 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.

claude-security-review: 1 inline finding(s) on 76005f26c0647070d9a36ff0f96facdb09394bd7; the summary is in this lane's pull request comment.

claude-lane: claude-security-review

Comment thread lib/hook-utils.sh Resolved
…a backtick

Review fixes on the parser in lib/hook-utils.sh:

- A case frame now carries its parse state, as bash's PST_CASEPAT: it
  wants the subject, then `in`, then is a pattern position (after `in`,
  `;;`, `;&`, `;;&`, where only esac is reserved) or a clause body. A
  pattern spelled `case` no longer opens a second case frame, so
  `echo $(case x in x) :;; case) :;; esac)#; git reset --hard` is
  blocked, after a newline too.
- Inside backticks the $'...' reader, the quoted and unquoted here-doc
  delimiter readers and the here-doc body skip end at the closing
  backtick. Here-docs opened inside backticks are dropped when it
  closes, since their bodies come from the substitution's own text.
- Inside backticks an escaped backtick opens or closes a nested
  substitution: the parser splits there and keeps a # right after it
  literal, so `echo \`echo \\\`git reset --hard\\\`\`` is checked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@github-actions github-actions Bot 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.

claude-security-review: 3 inline finding(s) on f8dcb574bcb518d9f871e84db53c8c2db767790a; the summary is in this lane's pull request comment.

claude-lane: claude-security-review

Comment thread lib/hook-utils.sh Resolved
Comment thread lib/hook-utils.sh Resolved
Comment thread plugins/speech/hooks/hook-utils.sh Resolved

@github-actions github-actions Bot 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.

claude-review: 2 inline finding(s) on f8dcb574bcb518d9f871e84db53c8c2db767790a; the summary is in this lane's pull request comment.

claude-lane: claude-review

Comment thread lib/hook-utils.sh Resolved
Comment thread lib/hook-utils.sh Resolved
lib/hook-utils.test.sh gains an oracle table: 2448 rows built from lists
of openers ($(, $((, <(, backtick, a=(, @(, every case pattern position,
a clause body), tricky words (case, esac, in, \;;, \), \`, ")", #, <<E),
placements, a pending here-doc lead and two tails. For each row bash
itself, with extglob off and on, decides whether the trailing command
runs; when it does, the parser must report it. On the previous head 120
of the 700 rows bash runs were hidden; on main, 423.

Parser fixes in lib/hook-utils.sh that close them:

- Case: in a pattern position esac is reserved only before the first
  pattern word, so a|esac) is a pattern and `case x in esac` closes on
  one line; ;; ;& ;;& count only when the first ; was an operator, so
  \;; is a word and a ;. Keywords exist only in a command context, so
  case inside a=(...) or @(...) opens nothing.
- Extglob frames (g) are told apart from array assignments (1): inside
  an extglob or arithmetic # < > are literal text; inside an array
  assignment < > redirect nothing, and a syntax error there (< > ; & |)
  cancels the line's here-docs.
- Each pending here-doc records its level (the $( backtick a=( @( ((
  frames open when it was read) and takes its body only at a newline on
  that level, so one read before a construct that spans lines takes its
  body after the line where the construct closes. Here-docs read inside
  a construct are dropped when it closes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@github-actions github-actions Bot 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.

claude-review: 1 inline finding(s) on 4de738a7cfa8c97670123a34d6705966db940df1; the summary is in this lane's pull request comment.

claude-lane: claude-review

Comment thread lib/hook-utils.sh Resolved

@github-actions github-actions Bot 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.

claude-security-review: 2 inline finding(s) on 4de738a7cfa8c97670123a34d6705966db940df1; the summary is in this lane's pull request comment.

claude-lane: claude-security-review

Comment thread lib/hook-utils.sh Resolved
@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

The pr-verify verifier failed head 52d4cfbbe48fb694770fd58f000cfc6cf2702d57. What it wrote, as data:

What it wrote (92 lines)
Summary: Chunk 2 of 3, fail: Issue #12398 (closes). Chunk 2 holds 10 of the synced hook-utils.sh
  copies (desktop-notification, eol-normalizer, explainer-video, go-format, guardrails, harness-ops,
  markdown-format, playwright, plus their shared parser change) and the guardrails test files. Every
  copy in this chunk carries the same blob (48376c095), so they are byte-identical. Whether they
  match lib/hook-utils.sh is for chunk 1 to judge.

Done-when items in this chunk:
- Delivered: plugins/guardrails/hooks/block-dangerous-git.test.sh adds the single-nested row
  (expects exit 2), the doubly nested row (exit 2) and the harmless `echo ok` nested row (exit 0).
- Delivered: the fail-safe for nesting that can't be resolved. An odd run of backslashes before a
  backtick inside backticks splits the segment, and scanning goes on, so the text is still read as
  commands.
- Partly delivered: parsing `\`` as opening a nested substitution and handing its contents to the
  callback. The contents do reach the callback, so the issue's exact forms are blocked. But the
  parser does not open or close a nesting level at `\``. Its comment, single-quote and here-doc
  readers end only at a closing backtick that follows an even run of backslashes, which is the
  outermost level. So a comment or quote inside a nested substitution swallows the commands after
  it, and a dangerous command can still hide.
- Not settled: "existing backtick/#/here-doc tests still pass" and "the synced-copies check passes".
  In upstream-jobs.json, select-tests and every test job were skipped, so no CI evidence exists
  either way.

The fail is for the nested-comment and nested-quote bypass. Much of the rest of the diff is #12361
  parser work the issue did not ask for.
Chunk 1 of 3, pass-notes: Chunk 1 of 3 for issue #12398 (spec_kind closes). I read all 18 parts and
  the manifest whole. Nothing in these parts blocks.

Done-when items, as far as this chunk covers them:
- Item 1, delivered in full here. In `lib/hook-utils.sh`, `hook::bash_parse_segments_uncached` now
  reads an odd run of backslashes before a backtick inside a `b` frame as the start or end of a
  nested substitution. It ends the current segment there, so the nested command is handed to the
  callback.
- Item 2 (fail-safe), delivered here. The new branch only ends segments and never drops text, so a
  nested form it cannot resolve is still scanned as commands.
- Items 3 and 4, not delivered in my parts. The block-dangerous-git.test.sh rows are in chunk 2
  (part-010). My parts carry the matching unit rows in `lib/hook-utils.test.sh`: single nesting,
  double nesting, a `#` after a nested substitution, and an escaped backtick outside backticks.
- Item 5 (harmless nested form still allowed), partly delivered. The parser yields only
  `echo`/`echo`/`echo ok` segments, but the guard-level test for it would be in chunk 2.
- Item 6 (existing tests pass), not settled by this chunk.
- Item 7 (copies regenerated), partly delivered. Of the 20 copies, the 5 in this chunk (animation,
  autonomy, bash-format, biome-format, context-guard) all have the same post-image blob and the same
  hunks as lib. The other copies are in chunks 2 and 3.
- Item 8 (sync check passes), not settled by this chunk.

There is no CI evidence: `select-tests` and every test job were skipped on the judged head.
- plugins/guardrails/hooks/hook-utils.sh: bypass (and in every copy in this chunk). Command: echo
  `echo \` # \`; git reset --hard`. Bash reads the outer backticks' contents as: echo ` # `; git
  reset --hard. The inner substitution holds only a comment, then `git reset --hard` runs. The
  parser handles `\`` only by flushing the segment and setting hash_at. It pushes no frame, so the
  `#` (with a space before it) starts a comment at the outer level. The comment loop stops only
  where hook::_bps_bt_closes is true (an even run of backslashes), so it skips the `\`` and swallows
  `; git reset --hard` up to the outer closing backtick. The command never reaches the callback.
- plugins/guardrails/hooks/hook-utils.sh: the same gap exists in the single-quote reader. In echo
  `echo \`'\`; git reset --hard #'`, the quote runs past the nested `\`` closer. Bash ends that
  quote at the nested closing backtick and runs the reset. The $'…' reader and the here-doc
  delimiter and body readers use the same even-backslash test, so they share the problem.
- Fix: in lib/hook-utils.sh, have `\``-style escapes inside backticks open and close a nesting level
  that records its backslash depth (for example a frame per level). The comment, quote, ANSI-C,
  here-doc delimiter and here-doc body readers should then end at the closing escape for the
  innermost level, not only at the outermost unescaped backtick. Add exit-2 rows for both commands
  above to plugins/guardrails/hooks/block-dangerous-git.test.sh, and regenerate the copies with
  scripts/sync-shared-copies.sh.
- plugins/guardrails/hooks/block-dangerous-git.test.sh:119-129: the rows the issue asks for are
  present and look correct: the single-nested, doubly nested and harmless forms, plus a
  `#`-right-after-nested-close row.
- upstream-jobs.json: select-tests and every test job (test-bash, lint-shell and the rest) concluded
  `skipped`, so this run gives no evidence either way for block-dangerous-git.test.sh,
  hook-utils.test.sh or the synced-copy check. The PR body's local results (969/0 and others) are
  unverified context.
- Out of scope (non-blocking): this chunk also carries the large #12361/#12360 parser rewrite
  (extglob dual reading, case-state frames, arithmetic versus nested-subshell `((…) )`, `=~` regex
  frames, here-doc levels, array syntax errors) and new rows in
  plugins/guardrails/hooks/block-root-delete-target.test.sh:700-702. Issue #12398 does not ask for
  any of this. It deserves a human's attention as separate work.
- lib/hook-utils.sh: besides the #12398 fix, the diff carries the whole #12360/#12361 parser rework:
  extglob/array/substitution `)#` handling, case-statement states, `((…) )` subshells, `[[ =~ ]]`
  regex groups, here-doc levels and a second extglob-on pass. #12398 does not ask for this. The PR
  body names it (Refs #12361). I found no correctness or safety defect in it: each change makes more
  text get scanned, not less. A human should still check the size of this out-of-scope change.
- .changes/*/work-resolve-issue-issue-12360-41447835786-1-*.md: 19 changelog fragments describe the
  #12360 parser work as well as #12398. Out of the issue's scope but harmless.
- lib/hook-utils.test.sh: adds a bash-oracle table that runs about 9,276 `bash -c` rows (in a mktemp
  scratch dir, removed afterwards). It is correct but will add noticeable runtime to the test-bash
  lane.
- upstream-jobs.json: select-tests was skipped (a label or body-edit re-run), so no job shows that
  lib/hook-utils.test.sh, block-dangerous-git.test.sh or the sync check ran on head 52d4cfbb. Items
  6 and 8 rest only on the PR body's claims:
  https://github.com/melodic-software/claude-code-plugins/actions/runs/41507632270/job/125499454377
- plugins/guardrails/hooks/block-dangerous-git.test.sh: issue items 3 and 4 (the single and double
  nested `git reset --hard` rows) are in chunk 2, part-010, not in this chunk. Chunk 2's verifier
  must confirm them.

This pull request leaves the merge queue, and ci-status holds it out while it carries pr-verify-failed. A pr-verify pass at a new commit removes the label.

@github-actions github-actions Bot added the pr-verify-failed pr-verify failed this head; a pass at a new commit, or after a trusted body edit, removes it. label Nov 2, 2026
kyle-sexton and others added 2 commits November 2, 2026 13:40
Review fixes on the parser in lib/hook-utils.sh:

- The second ( of (( or $(( opens arithmetic only when bash would: its
  matching ) (quotes, nested parens and a case inside a $( skipped) is
  followed by another. Otherwise bash re-reads the text as ( (…) ) or
  $( (…) ), so it is a command context with real redirections (frames
  d and e) and ((bash <<< 'rm -rf ~') ) is checked again.
- After =~ inside [[ ]] the operand's parentheses are regex text (frame
  x): no array frame, no here-doc clearing.
- A case pattern word that starts with $(, $((, a backtick or @( clears
  the pattern-start flag, so an esac after | is a pattern.
- Text bash fixes by matching parentheses before reading it (((…) ),
  $((…) ), regex groups, extglobs) bounds a comment or here-doc body at
  its matching ). Here-docs read inside $( or <( or a ((…) ) at command
  level take their bodies after the line; inside re-read text, none.
- A syntax error inside a=(…) drops the array frame and the line's
  here-docs, and the next line is read afresh; a [ word in an array is
  read to its matching ], and an extglob there gets both readings.

The oracle table now also generates ((…)), ((…) ), $((…) ), [[ =~ k=( ]]
and [[ =~ ( ]] contexts, a redirect word whose write bash must match, a
here-doc tail with a quote, and every bare opener nested in every
context (depth 2, sampled one row in 32). It runs 9276 rows; on the
previous head 243 were hidden, on main 1138.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Inside backticks an odd run of backslashes before a backtick (\`, \\\`,
...) opens or closes a nested substitution one level deeper, so the
parser splits there and no stray backslash stays on the nested words.
Adds the #12398 rows: a doubly nested form around git reset --hard
(blocked) and a harmless nested form (allowed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KeBVgcHpe4YpFuL5b24Ft2

@github-actions github-actions Bot 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.

claude-review: 4 inline finding(s) on 52d4cfbbe48fb694770fd58f000cfc6cf2702d57; the summary is in this lane's pull request comment.

claude-lane: claude-review

Comment thread lib/hook-utils.sh Resolved
Comment thread plugins/guardrails/hooks/hook-utils.sh Resolved

@github-actions github-actions Bot 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.

claude-security-review: 4 inline finding(s) on 52d4cfbbe48fb694770fd58f000cfc6cf2702d57; the summary is in this lane's pull request comment.

claude-lane: claude-security-review

Comment thread lib/hook-utils.sh Resolved
Comment thread plugins/guardrails/hooks/hook-utils.sh Resolved
… backticks in ((

Regression-only fixes from the round-5 review, each a form main's guard
blocked and the previous head did not:

- The array subscript scan reads plain text only. At a quote, backslash,
  `$` or backtick, or with no matching `]`, the `[` stays one character
  and the main loop reads on, so a quoted `]` or `[` and a substitution
  in a subscript keep their readers again.
- A `(` inside an open `${…}` is expansion text: it is no array syntax
  error and pushes no frame, so the array's own `)` closes the array and
  a pending here-doc keeps its body.
- The `((`/`$((` lookahead skips backtick text, so a `))` inside
  backticks does not turn a `((…) )` subshell into arithmetic.

Adds an exit-2 row for each repro.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vcupom3BKF6QuC5U3sRJPj

@github-actions github-actions Bot 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.

claude-review: 1 inline finding(s) on 0072d87129294937b7033da2ac297e0d34ffc15e; the summary is in this lane's pull request comment.

claude-lane: claude-review

Comment thread lib/hook-utils.sh Resolved

@github-actions github-actions Bot 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.

claude-security-review: 2 inline finding(s) on 0072d87129294937b7033da2ac297e0d34ffc15e; the summary is in this lane's pull request comment.

claude-lane: claude-security-review

Comment thread lib/hook-utils.sh Resolved
Comment thread lib/hook-utils.sh Resolved
@github-actions github-actions Bot removed the pr-verify-failed pr-verify failed this head; a pass at a new commit, or after a trusted body edit, removes it. label Nov 2, 2026
@kyle-sexton
kyle-sexton added this pull request to the merge queue Nov 2, 2026
Merged via the queue into main with commit 019478d Nov 2, 2026
90 checks passed
@kyle-sexton
kyle-sexton deleted the fix-12361-replacement branch November 2, 2026 21:37
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.

fix(guardrails): scan the command inside an escaped backtick nested in backticks

1 participant