Repository navigation
fix(hooks): close three permission-gate holes in command segmentation - #4129
Conversation
📊 Automated PR Analysis
SummaryThis PR fixes three permission-gate security holes in the command segmentation logic used by hooks. It adds detection for ANSI-C quoting ( Review Checklist
Linked issues: #3188 Analyzed automatically by wshm · This is an automated analysis, not a human review. |
405213a to
a065bb7
Compare
283c520 to
c7f76b7
Compare
c7f76b7 to
7677155
Compare
aeppling
left a comment
There was a problem hiding this comment.
Differential against the merge-base over 25,673 inputs × 7 rule sets: no loosening, 442 cases newly Defer → Deny, ANSI-C bypass fixed. Suite, clippy, fmt green.
Non-blocking, separate issue: do / then / else / elif aren't in strip_grammar_residue, so if …; then git push --force; fi still escapes an exact deny rule. Pre-existing, not introduced here.
7092083 to
5711823
Compare
The gate decides deny/ask/allow from `split_for_permissions`, so anything
that segmentation cannot see is a rule that silently does not apply. Three
constructs do that today.
**ANSI-C quoting.** `\'` inside `$'…'` is the one case where the lexer's
quote state provably diverges from bash: bash reads a literal `'` and keeps
the word open, the lexer closes the span and reopens it. Every operator
after that point then produces no token, so
ls -la $'\'' ; rm -rf /
reaches the gate as a single segment beginning `ls `, which an
`allow: ["Bash(ls:*)"]` rule matches while the `rm -rf /` rides along. The
verdict was Allow, and the hook returned it as an auto-approve.
`ansi_c_quote_defeats_lexer` already detected this but only
`rewrite_multiline_block` consulted it; `contains_unattestable_construct`
now does too, so the gate prompts instead. It moves to `lexer.rs` with
`QuoteScan`, next to the `advance_quote_state` both are built on.
**Leading redirects.** A redirect may precede the command it applies to.
`split_for_permissions` truncated each segment at its first redirect, so
`2>&1 git push --force` truncated at offset 0 and returned no segments at
all: the deny loop iterated an empty list and the rule did not fire. A
redirect seen before any command text is now stepped over, along with its
file operand, so the command behind it is still checked. Trailing redirects
keep truncating as before.
**Grammar residue.** `{` and `}` are not segment boundaries, so
`... && { rm -rf / ; }` reaches the gate as `{ rm -rf /` and an exact deny
pattern cannot match it. Deny and ask now also test the segment with leading
`{`, `}`, `!`, `(`, `)` words stripped. Allow deliberately does not:
stripping can only make a rule fire on more segments, so applying it to deny
and ask can only make a verdict stricter, while applying it to allow would
let `{ git status` inherit `Bash(git:*)` and turn a prompt into an
auto-approve.
Stepping over a leading redirect also widens the allow side: `2>&1 <cmd>`
used to produce no segment at all and fall to Default, and now matches the
rule that covers the command behind it. That is rule-faithful and applies
only to fd-dup and /dev/null targets — a real file target still reaches
`contains_unattestable_construct` and prompts — but it is a genuine
widening, so it is pinned by its own test.
Rewriting is unchanged except where the ANSI-C guard now defers, verified by
differential against develop over 13 compound forms (pipelines, subshells,
background, brace groups, heredocs, multiline, quoted separators, env
prefixes): one diff, the intended one.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
5711823 to
3992e0a
Compare
The gate decides deny/ask/allow from
split_for_permissions, so anything that segmentation cannot see is a rule that silently does not apply. Three constructs do that today.ANSI-C quoting.
\'inside$'…'is the one case where the lexer's quote state provably diverges from bash: bash reads a literal'and keeps the word open, the lexer closes the span and reopens it. Every operator after that point then produces no token, soreaches the gate as a single segment beginning
ls, which anallow: ["Bash(ls:*)"]rule matches while therm -rf /rides along. The verdict was Allow, and the hook returned it as an auto-approve.ansi_c_quote_defeats_lexeralready detected this but onlyrewrite_multiline_blockconsulted it;contains_unattestable_constructnow does too, so the gate prompts instead. It moves to
lexer.rswithQuoteScan, next to theadvance_quote_stateboth are built on.Leading redirects. A redirect may precede the command it applies to.
split_for_permissionstruncated each segment at its first redirect, so2>&1 git push --forcetruncated at offset 0 and returned no segments at all: the deny loop iterated an empty list and the rule did not fire. A redirect seen before any command text is now stepped over, along with its file operand, so the command behind it is still checked. Trailing redirects keep truncating as before.Grammar residue.
{and}are not segment boundaries, so... && { rm -rf / ; }reaches the gate as{ rm -rf /and an exact deny pattern cannot match it. Deny and ask now also test the segment with leading{,},!,(,)words stripped. Allow deliberately does not: stripping can only make a rule fire on more segments, so applying it to deny and ask can only make a verdict stricter, while applying it to allow would let{ git statusinheritBash(git:*)and turn a prompt into anauto-approve.
Stepping over a leading redirect also widens the allow side:
2>&1 <cmd>used to produce no segment at all and fall to Default, and now matches the rule that covers the command behind it. That is rule-faithful and appliesonly to fd-dup and /dev/null targets — a real file target still reaches
contains_unattestable_constructand prompts — but it is a genuine widening, so it is pinned by its own test.Rewriting is unchanged except where the ANSI-C guard now defers, verified by differential against develop over 13 compound forms (pipelines, subshells, background, brace groups, heredocs, multiline, quoted separators, env prefixes): one diff, the intended one.
Test plan
cargo fmt --all && cargo clippy --all-targets && cargo test --all— 3850 passing, clippy clean, at this commit rather than only at the tip of the stackecho $(rm -rf /)withallow: ["Bash(echo:*)"]yields Ask, not Allow;2>&1 git push --forceyields a segment the deny rules can see; a generated matrix of 1000+ prefix constructs, none of which hides a denied commandPart of stack #4139 (#4129 → #4138). Bottom of the stack; based on
develop. Each commit gates green on its own, so the stack is bisectable.🤖 Generated with Claude Code