Repository navigation
fix(guardrails): read a after a word-continuing ( as literal in the top-level segmenter - #12361
melodic-automation-lanes[bot] wants to merge 1 commit into
Conversation
Lane-Activity: work-resolve-issue/resolve-issue
| 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)) |
There was a problem hiding this comment.
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 --hardecho `:`#; git reset --hard: the closing backtick goes through the same flush, andhaveis 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
There was a problem hiding this comment.
pr-fix lane: please fix this finding
There was a problem hiding this comment.
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.
|
claude-security-review partial review of Security review: PR #12361 (hook-utils.sh
|
| 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 |
There was a problem hiding this comment.
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 --hardis one command word,!(x)#, thengit reset --hard. It is not a negated subshell.f@()#; git reset --hardis one command word,f@()#, thengit 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
There was a problem hiding this comment.
pr-fix lane: please fix this finding
There was a problem hiding this comment.
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.
|
claude-review reviewed Code review: PR #12361 (word-continuing
|
|
pr-unblock / address-feedback finished round 1 of max 4 on 53963af. |
|
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).
To resume, deal with the reason above, remove the gh workflow run pr-unblock.yml -R melodic-software/claude-code-plugins -f pr-number=12361 -f activity=address-feedback |
|
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. |
Closes #12360
Summary
Opened by the work-resolve-issue lane. The run's own summary:
The run went past its turn budget (turns=140 max-turns=113): the work may be too big for one run.
Not verified
#af;falls inside it, soa=(x<newline>y)#; git reset --hardanda=($(:;:))#; git reset --hardstill read the#as a comment, as before. With extglob on, a#inside(x #)is stFix
(frames on a stack and makes a#right after such a frame's)literal; splitting is unchanged#, a comment after whitespace, a subshell)comment, extglob alternation, anf()function definition, a bare!subshell, a substitution inside an array, and a literal(insida=(x)#; git reset --hard,(x)#; git reset --hard,(a|b)#; git reset --hard, plus regression rows fora=(x y) git reset --hard,a=($(git reset --hard)),f() { git reset --hard; }; f, a multi-linef()#'functionVerification
The runner ran each check from the base copy against 53963af:
The run reports:
)into the word): 3 HIGH under-blocks found and confirmed by reading the code; redesigned so splitting is unchangedf()#'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 changeLeft out: the items under Not verified.
Related
#12360