Skip to content

fix(hooks): close three permission-gate holes in command segmentation - #4129

Merged
KuSh merged 1 commit into
developfrom
lex/01-gate-security
Oct 2, 2026
Merged

KuSh merged 1 commit into
developfrom
lex/01-gate-security

Conversation

@KuSh

@KuSh KuSh commented Sep 19, 2026 •

Copy link
Copy Markdown
Collaborator

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.

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 stack
  • echo $(rm -rf /) with allow: ["Bash(echo:*)"] yields Ask, not Allow; 2>&1 git push --force yields a segment the deny rules can see; a generated matrix of 1000+ prefix constructs, none of which hides a denied command

Part 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

@KuSh
KuSh added this pull request to stack #4139 September 19, 2026 13:31
@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working hooks security labels Sep 19, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🔴 Risk high

Summary

This PR fixes three permission-gate security holes in the command segmentation logic used by hooks. It adds detection for ANSI-C quoting ($'...') that the lexer parses differently from bash, causing segments to be silently merged and bypass permission checks, and fixes redirect handling for leading fd-numbered redirects and <& fd-duplication operators that were previously mis-tokenized as command text or background operators.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #3188


Analyzed automatically by wshm · This is an automated analysis, not a human review.

@KuSh
KuSh marked this pull request as ready for review September 19, 2026 13:36
@KuSh
KuSh force-pushed the lex/01-gate-security branch from 405213a to a065bb7 Compare September 20, 2026 23:23
@KuSh
KuSh force-pushed the lex/01-gate-security branch 2 times, most recently from 283c520 to c7f76b7 Compare September 25, 2026 23:29
@KuSh
KuSh force-pushed the lex/01-gate-security branch from c7f76b7 to 7677155 Compare September 26, 2026 17:55

@aeppling aeppling 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.

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.

@KuSh
KuSh force-pushed the lex/01-gate-security branch 2 times, most recently from 7092083 to 5711823 Compare October 1, 2026 16:20
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]>
@KuSh
KuSh force-pushed the lex/01-gate-security branch from 5711823 to 3992e0a Compare October 2, 2026 15:04
@KuSh
KuSh merged commit 4932d16 into develop Oct 2, 2026
5 checks passed
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Oct 2, 2026
@KuSh
KuSh deleted the lex/01-gate-security branch October 2, 2026 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working hooks security

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants