Skip to content

fix(guardrails): read a after a word-continuing ( as literal in the top-level segmenter - #12361

Closed
melodic-automation-lanes[bot] wants to merge 1 commit into
mainfrom
work-resolve-issue/issue-12360-41447835786-1
Closed

melodic-automation-lanes[bot] wants to merge 1 commit into
mainfrom
work-resolve-issue/issue-12360-41447835786-1

Conversation

@melodic-automation-lanes

Copy link
Copy Markdown
Contributor

Closes #12360

Summary

Opened by the work-resolve-issue lane. The run's own summary:

The top-level segmenter in lib/hook-utils.sh (hook::bash_parse_segments_uncached) used to read a # right after the ) of an array assignment a=(…) or an extglob (…) as a comment, so a=(x)#; git reset --hard hid the reset from block-dangerous-git. Bash keeps that ) in its word, so the # is literal. A paren stack now marks each ( that continues a word (the word matches name=/name+=/name\[…\]=, or ends in ? * + ! but is not a bare !, is not inside an open ${…}, and is not a f() function definition), and a # right after its ) is literal. Design choice (basis: two fresh-context reviews of the first version): every ( and ) still splits segments exactly as before, and the parser does not absorb the ) into the word as the refinement's criteria worded it. Absorbing it under-blocked a=(x y) git reset --hard, f() { git reset --hard; } and stack drift across segments. Keeping the split leaves no text outside a segment, and the only behaviour change is the comment decision, which is what the issue body asks for ('no comment start after its )'). The stack resets at ; & newline and at a | outside an extglob.

The run went past its turn budget (turns=140 max-turns=113): the work may be too big for one run.

Not verified

  • Test suites lib/hook-utils.test.sh, plugins/guardrails/hooks/block-dangerous-git.test.sh and plugins/guardrails/hooks/run-guards.test.sh were not executed: this lane runs no test runner, so they are left to the PR's CI. The 'new rows fail on main' claim rests on reading the old code path (the # af
  • Tidy skipped: the change touches 43 files and the shared path lib/, both past the tidy cutoffs (10 files, no shared paths)
  • Known remaining gaps, not regressions (review #2): a word-continuing frame is still dropped when a newline or ; falls inside it, so a=(x<newline>y)#; git reset --hard and a=($(:;:))#; git reset --hard still read the # as a comment, as before. With extglob on, a # inside (x #) is st
  • typos check skipped by check-affected (typos not installed here); validate-plugins sub-checks that run code from the checked tree were skipped by the wrapper

Fix

  • lib/hook-utils.sh: hook::bash_parse_segments_uncached tracks word-continuing ( frames on a stack and makes a # right after such a frame's ) literal; splitting is unchanged
  • plugins/*/hooks/hook-utils.sh and plugins/repo-hygiene/skills/clean/scripts/hook-utils.sh (20 shared copies): regenerated from lib/hook-utils.sh by sync-shared-copies
  • lib/hook-utils.test.sh: bps_segments_are helper plus parse-segments cases for array-assignment and extglob literal #, a comment after whitespace, a subshell ) comment, extglob alternation, an f() function definition, a bare ! subshell, a substitution inside an array, and a literal ( insid
  • plugins/guardrails/hooks/block-dangerous-git.test.sh: blocked rows for a=(x)#; git reset --hard, (x)#; git reset --hard, (a|b)#; git reset --hard, plus regression rows for a=(x y) git reset --hard, a=($(git reset --hard)), f() { git reset --hard; }; f, a multi-line f()#' function
  • .changes/<plugin>/work-resolve-issue-issue-12360-41447835786-1-*.md (20 files): patch fragments for guardrails and each other plugin that carries a hook-utils.sh copy

Verification

The runner ran each check from the base copy against 53963af:

  • scripts/check-changelog-fragments.sh --check: passed
  • scripts/check-changed-skills.sh --merge-base: passed

The run reports:

  • .lane/checks/bin/check-changelog-fragments: all 20 fragments valid
  • .lane/checks/bin/check-changed-skills: plugins/repo-hygiene/skills/clean PASS, 0 errors (4 warnings that predate this change)
  • .lane/checks/bin/check-affected: 12 passed, 6 skipped, 0 failed over 43 changed files (an earlier run failed shellcheck SC2324/SC2016; fixed)
  • .lane/verify/bin/sync-shared-copies --check: all 566 registered copies match their canonical sources
  • .lane/verify/bin/cant-fail-scan --file lib/hook-utils.test.sh --check: PASS
  • .lane/verify/bin/cant-fail-scan --file plugins/guardrails/hooks/block-dangerous-git.test.sh --check: PASS
  • fresh-context review #1 (first design that absorbed ) into the word): 3 HIGH under-blocks found and confirmed by reading the code; redesigned so splitting is unchanged
  • fresh-context review #2: 1 HIGH under-block (f()#' function definition with extglob off) fixed with the next-char-is-) exclusion; it also traced every new test case by hand and found that they pass and that the two issue rows were allowed before the change
  • The lane run: https://github.com/melodic-software/claude-code-plugins/actions/runs/41447835786

Left out: the items under Not verified.

Related

#12360

Lane-Activity: work-resolve-issue/resolve-issue

@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 53963af78e9a3877cdab5b0694cc8050427991aa; the summary is in this lane's pull request comment.

claude-lane: claude-security-review

Comment thread lib/hook-utils.sh
Comment on lines +4858 to +4873
if [[ "$c" == '(' ]]; then
wpar=0
if ((have)); then
if [[ "$word" =~ $wpar_re ]]; then
wpar=1
elif [[ "$word" == *[@?*+!] && "$word" != '!' &&
! ("$word" == *"$dbrace"* && "${word##*"$dbrace"}" != *'}'*) ]]; then
for ((j = i + 1; j < n; j++)); do
[[ "${chars[j]}" == ' ' || "${chars[j]}" == $'\t' ]] || break
done
[[ "${chars[j]-}" == ')' ]] || wpar=1
fi
fi
pstk+="$wpar"
elif [[ "$c" == ')' ]]; then
[[ "$pstk" == *1 ]] && hash_at=$((i + 1))

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.

The same bypass still works through $(…), $((…)), <(…) and backticks.

Only a name= word, or a word ending in @?*+!, pushes a 1 frame here. The ( of a command substitution pushes a 0: the $ case (line 4694) appends $ to the word, and $ matches neither test. The closing ) therefore never sets hash_at, and the # right after it starts a comment at line 4730.

Bash keeps that ) in its word too: echo $(echo a)#b prints a#b. So this command:

echo $(:)#; git reset --hard

runs the reset, but the parser drops ; git reset --hard as a comment, and block-dangerous-git allows it.

The same applies to:

  • echo $((1))#; git reset --hard
  • echo `:`#; git reset --hard: the closing backtick goes through the same flush, and have is 0 after it.
  • Likely also cat <(:)#; git reset --hard: the < branch at line 4821 closes the word before the (.

Base behaves the same, so this is not a regression. But it is the kind of bypass #12360 reports, and the guardrails changelog says it is now blocked. A fix is to give a ( a 1 frame whenever bash keeps the matching close in the word: the word so far ends in $, or the ( follows </>. Also set hash_at after a closing backtick when the word was open before the opening backtick or the backtick began a word. Add $(:)#; and backtick rows to block-dangerous-git.test.sh. The regenerated plugin copies need the same change.

severity: IMPORTANT

claude-lane: claude-security-review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

pr-fix lane: please fix this finding

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

pr-unblock / address-feedback: UNCERTAIN, left open

Evidence: lib/hook-utils.sh:4873, lib/hook-utils.sh:4694, lib/hook-utils.sh:4821, scripts/shared-copies.txt:435

The finding still holds in the current checkout. The dollar branch (lib/hook-utils.sh:4694) and the process-substitution branch (4821) leave the next open paren with a 0 frame, so the close paren at 4873 never sets hash_at. Backticks are plain separators with no open/close tracking. A separator inside the substitution also resets pstk at 4875-4877, so a form like echo $(:;:)# would still get through after a narrow frame fix. This lane cannot land the fix. The guard that runs is plugins/guardrails/hooks/hook-utils.sh, one of the 20 copies that scripts/shared-copies.txt (line 435 on) keeps in sync with lib/hook-utils.sh. check-paths refuses every plugin copy and plugins/guardrails/hooks/block-dangerous-git.test.sh. Changing only lib/hook-utils.sh would leave the copies drifted and the deployed guard unfixed. Suggested fix for whoever picks this up: (1) give an open paren a substitution frame when it directly follows an unquoted dollar or a process-substitution angle bracket. (2) Keep that frame through ; & | and newline. (3) Set hash_at when it pops. (4) Track backtick open/close and set hash_at after the closing one. (5) Add rows for $(:)#;, $((1))#;, $(:;:)#;, backtick :backtick #; and <(:)#; to block-dangerous-git.test.sh, then regenerate the copies. A case pattern inside $( ) would still close the frame early.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

claude-security-review partial review of 53963af78e9a3877cdab5b0694cc8050427991aa: read 42 of 43 changed file(s). 1 finding(s) in what was read, 1 as inline comments. This head is not reviewed.

Security review: PR #12361 (hook-utils.sh # after a word-continuing ))

Scope. I read every hunk of every changed file:

  • lib/hook-utils.sh and its tests.
  • The 20 regenerated hook-utils.sh copies. All 20 carry blob 8cf226b5..c0b4fb6c, and each hunk matches the canonical change.
  • block-dangerous-git.test.sh.
  • The changelog fragments: the guardrails one, plus the animation fragment, which stands for its 18 identical copies.

No manifests, workflows, trust controls, merge holds or instruction surfaces change. No copy's path changes what its content does: they are all the same sourced library.

What the change does. It narrows the bypass. A # right after the ) of a=(…) or @(…) is now read as literal text, not as a comment. Every ( and ) still splits segments as before, so the parser still sees all the text it saw before. The only behaviour that changes is whether a # is read as a comment.

New under-blocking. I looked for a place where the parser now reads a literal # but bash reads a comment. In that case a quote inside the comment would open a span in the parser and hide later lines. I found none:

  • With extglob off, an unquoted ( in the middle of a word is a bash syntax error, so nothing after it runs.
  • f@() function definitions are excluded by the next-character-is-) check.
  • A ( inside ${…} is excluded.
  • A bare ! is excluded.
  • Inside arithmetic, a+=(1) never puts a # right after the ).

Finding (IMPORTANT, inline). The fix leaves the same kind of bypass open for other )s that bash keeps in the word: command substitution, arithmetic expansion, process substitution and backticks. For example, echo $(:)#; git reset --hard still hides the reset from block-dangerous-git. This is not a regression, since base behaves the same way. I am reporting it because the diff rewrites this exact decision and the guardrails changelog claims the bypass is closed.

Pre-existing, not counted. With shopt -s extglob on, bash reads !(x)# and f@()# as words, so the # is literal. The parser still reads it as a comment there, for example in !(x)#; git reset --hard. That matches base.

Spend. The change spends nothing beyond normal CI runs. No cost hold.

critical-findings: 0

@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 53963af78e9a3877cdab5b0694cc8050427991aa; the summary is in this lane's pull request comment.

claude-lane: claude-review

Comment thread lib/hook-utils.sh
Comment on lines +4863 to +4868
elif [[ "$word" == *[@?*+!] && "$word" != '!' &&
! ("$word" == *"$dbrace"* && "${word##*"$dbrace"}" != *'}'*) ]]; then
for ((j = i + 1; j < n; j++)); do
[[ "${chars[j]}" == ' ' || "${chars[j]}" == $'\t' ]] || break
done
[[ "${chars[j]-}" == ')' ]] || wpar=1

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.

Advisory: the bare-! and empty-() exclusions always pick the extglob-off reading. With extglob on, a command after # is still hidden.

With shopt -s extglob, bash reads a pattern character followed by ( as an extglob before it checks for reserved words or function definitions (read_token_word in parse.y, the extended_glob && PATTERN_CHAR branch). So in both of these, the ) stays in the word, # is literal, and the reset runs:

  • !(x)#; git reset --hard is one command word, !(x)#, then git reset --hard. It is not a negated subshell.
  • f@()#; git reset --hard is one command word, f@()#, then git reset --hard. It is not a function definition.

Here "$word" != '!' (line 4863) and the next-char-is-) check (line 4868) give both cases wpar=0. The # then starts a comment, the parser never sees git reset --hard, and block-dangerous-git allows it. Issue #12360 asks for "no comment start after its )" for extglob forms, and !(…) is one of the five extglob operators.

The trade-off is real. With extglob off, these forms are a negation or a function definition. Making the # literal there would let f@()#' or !(x)#<<EOF swallow later lines, which is what review #2 found. So neither fixed reading is safe in both modes.

A guard can't know which mode the shell is in. One option for ambiguous frames is to keep the comment reading for quotes and here-docs, but still split the rest of that line on ;, & and | and scan the pieces. Splitting stops at the newline, so nothing can swallow later lines, and ; git reset --hard gets checked in either mode.

At minimum:

  • Record this as a known gap in the PR's "Not verified" list.
  • Change the test name at lib/hook-utils.test.sh:398 ("bare ! before ( is negation, a subshell") so it no longer asserts the extglob-off reading as correct.

The same lines appear in all 20 regenerated hook-utils.sh copies.

claude-lane: claude-review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

pr-fix lane: please fix this finding

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

pr-unblock / address-feedback: UNCERTAIN, left open

Evidence: lib/hook-utils.sh:4863, lib/hook-utils.sh:4868, lib/hook-utils.test.sh:4018, scripts/shared-copies.txt:435

The finding still holds in the current checkout. The bare-bang test at lib/hook-utils.sh:4863 and the empty-parens check at 4868 always give the frame 0. The hash at 4730 then starts a comment, so the extglob-on reading of !(x)#; git reset --hard is never scanned. lib/hook-utils.test.sh:4018 asserts the extglob-off reading as correct (the review cited line 398, but the test is now at 4018). The fix I would make: at a hash right after such an ambiguous close paren, re-parse the rest of the command once with the extglob-on reading (hash literal), then continue with the comment reading, so the callback sees both readings and anything either mode would run is checked. This lane cannot land it. The deployed guard is plugins/guardrails/hooks/hook-utils.sh, one of the 20 copies that scripts/shared-copies.txt (line 435 on) keeps in sync with lib/hook-utils.sh. check-paths refuses every plugin copy and plugins/guardrails/hooks/block-dangerous-git.test.sh, so a change to lib/hook-utils.sh alone would drift the copies and leave the guard unfixed. Needs a person or a lane that can regenerate the copies. At minimum, rename the test at lib/hook-utils.test.sh:4018 and list this gap under Not verified.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

claude-review reviewed 53963af78e9a3877cdab5b0694cc8050427991aa: read 43 of 43 changed file(s); 1 finding(s), 1 as inline comments.

Code review: PR #12361 (word-continuing ( and a literal # in the top-level segmenter)

Scope read: I read all 43 changed files. That covers the 19 matching changelog fragments (counted once, per identical.txt), the guardrails fragment, lib/hook-utils.sh, lib/hook-utils.test.sh, plugins/guardrails/hooks/block-dangerous-git.test.sh and the 20 shared hook-utils.sh copies. Every copy goes from blob 8cf226b5 to c0b4fb6c with the same three hunks as lib/hook-utils.sh. Each copy sits at its plugin's own hooks/ or skills/clean/scripts/ path, so no copy's path changes what the code does. No plugin manifest changed, so no manifest lines apply. The only path-scoped rule that matches is .claude/rules/mod-authoring.md (plugins/*/hooks/**), and it does not apply: no hooks module is added or changed.

What the change does: The parser now keeps a stack of open ( frames. A frame counts as word-continuing when the word before it is name=, name[…]= or name+=, or ends in @ ? * + !, with these exceptions: a bare !, an empty () (a function definition), and a ( inside an open ${. A # right after such a frame's ) is read as literal text. Splitting is unchanged, so the only thing that changes is whether that # starts a comment. I traced each new bps_segments_are row in lib/hook-utils.test.sh:384-405 by hand against the new code, and each produces the segments it expects.

Closing issue #12360:

  • Met: the a=(…) and @(…) forms are fixed, and block-dangerous-git.test.sh:950-952 adds blocked rows for them.
  • Partly met: the !(…) extglob form, and an empty @(), still read the # as a comment when extglob is on. See the inline comment.
  • Author-claimed, unverified: that the new rows fail on main and pass after the fix, and that block-dangerous-git, run-guards and lib/hook-utils.test stay green. The PR says the test suites were not run, and I did not run them either: this lane has no shell.

Finding (1, advisory): lib/hook-utils.sh:4863-4868. The bare-! and empty-() exclusions always pick the extglob-off reading. With shopt -s extglob, !(x)#; git reset --hard and f@()#; git reset --hard are still allowed. The new test at lib/hook-utils.test.sh:398-399 pins this as correct behaviour.

Spend: none. Nothing in this change touches billing, turn limits, models or schedules.

No embedded instructions were found in the PR context or the prior-findings file.

@melodic-automation-lanes

Copy link
Copy Markdown
Contributor Author

pr-unblock / address-feedback finished round 1 of max 4 on 53963af.

@melodic-automation-lanes melodic-automation-lanes Bot added the needs-human Human-in-the-loop required; autonomous sessions must not resolve items carrying this. label Nov 2, 2026
@melodic-automation-lanes

Copy link
Copy Markdown
Contributor Author

The pr-unblock lane stopped on this pull request and needs a person: a disputed review finding is one only a person decides (a CRITICAL finding, a security-review finding with no rating the lane reads, or a thread a person opened or directed).

  • Reason: feedback-disputed
  • Resume key: 53963af78e9a3877cdab5b0694cc8050427991aa (the head the lane stopped on)

To resume, deal with the reason above, remove the needs-human label (and do-not-merge, if set), then dispatch the pr-unblock workflow with pr-number=12361 and activity=address-feedback:

gh workflow run pr-unblock.yml -R melodic-software/claude-code-plugins -f pr-number=12361 -f activity=address-feedback

@kyle-sexton kyle-sexton added do-not-merge Hard merge gate: do not merge while applied. and removed needs-human Human-in-the-loop required; autonomous sessions must not resolve items carrying this. labels Nov 2, 2026
@kyle-sexton

Copy link
Copy Markdown
Member

Hold: pr-unblock cannot edit the 20 generated hook-utils.sh copies (check-paths), so both findings (IMPORTANT and advisory, neither CRITICAL) move to an owner-login replacement PR that merges this head and fixes them. Releases when: the replacement merges, then this PR closes as superseded.

@kyle-sexton

Copy link
Copy Markdown
Member

Superseded by #12384, which merged at 2026-11-02T21:37:53Z with this PR's change plus the review fixes; the remaining parser gaps are tracked in #12471.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do-not-merge Hard merge gate: do not merge while applied.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(guardrails): treat a word-continuing ( as a word character in the top-level segmenter

1 participant