Repository navigation
fix(guardrails): read a # after a word-continuing ( or substitution as literal - #12384
Conversation
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>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 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".
|
claude-security-review reviewed claude-security-review: incremental review of
|
|
claude-review reviewed claude-review: code-review lane (incremental,
|
…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>
…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>
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>
|
The pr-verify verifier failed head What it wrote (92 lines)This pull request leaves the merge queue, and ci-status holds it out while it carries |
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
… 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
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.shcopies.echo $(:)#; git reset --hardstill 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.shopt -s extglob,!(x)#; git reset --hardandf@()#; …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):1word-continuing (array assignment, extglob),ssubstitution ((right after an unquoted$, or a process-substitution</>),bbacktick,aextglob-dependent (!(…),f@()),ccase statement,0anything else. A)that pops1ors, or a closing backtick, makes the#right after it literal.;,&,|and newlines inside them. Bash allows these inside$(…),@(x;y)and multi-linea=(…), which the old reset-on-separator dropped.caseoresacin command position opens or closes acframe. A case pattern's)pops nothing, so$(case x in x) :;; esac)#keeps its substitution frame, and a#after a pattern)stays a comment.echo `#`; git reset --hardandecho `'`; git reset --hard #'are now blocked).aframe 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.bare ! before (andf@()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.mdto…cb95f8c0.md) becausetyposflags thebasuffix and blocked the push.Verification
Bash was the oracle for every new case. Each ran with harmless commands (
echo HIT), underbash -candbash -O extglob -c:echo $(:)#; echo HIT,echo $((1))#; …,echo $(:;:)#; …,$(:⏎:)#,echo `:`#; …,echo `:;:`#; …,cat <(:)#; …,: >(:)#; …: HIT runs in both modes (the#is literal).echo $(:) #; echo HITandecho `:` #; …: 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) anda=(x⏎y)#; echo HIT: HIT runs.echo `#`; echo HITandecho `'`; 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.shalso 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 includedbash plugins/guardrails/hooks/block-dangerous-git.test.sh: PASS=969 FAIL=0bash plugins/guardrails/hooks/run-guards.test.sh: PASS=714 FAIL=0bash plugins/guardrails/hooks/block-root-delete-target.test.sh: PASS=5727 FAIL=0bash plugins/repo-hygiene/skills/clean/scripts/destructive-guard.test.sh: 338/338plugins/guardrails/hooks/*.test.shandplugins/source-control/hooks/*.test.sh: all exit 0, 0 failuresbash scripts/sync-shared-copies.sh --check: all 567 registered copies matchcheck-affected: passedNot verified:
shopt -s extglobset earlier in the same command. Both readings are checked, so the guard does not depend on the mode.caseused 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