Skip to content

fix(plugins): bash-guard — block compound commands where any segment matches a deny rule (#36637) - #36645

Closed
Pro777 wants to merge 2 commits into
anthropics:mainfrom
Pro777:Pro777/fix-bash-compound-cmd-perms
Closed

Pro777 wants to merge 2 commits into
anthropics:mainfrom
Pro777:Pro777/fix-bash-compound-cmd-perms

Conversation

@Pro777

@Pro777 Pro777 commented Mar 20, 2026

Copy link
Copy Markdown

Summary

Fixes #36637 — chained bash commands silently bypass the tool permission allowlist.

The bug

Claude Code's built-in permission checker evaluates a Bash tool call as a single string against the permissions.allow/permissions.deny rules. For a compound command like:

git status && rm -rf /important/dir

the checker finds that the string starts with git status (matching an allow-rule prefix) and grants the whole command — without ever evaluating rm -rf /important/dir against the deny rules. The same bypass works with ;, ||, and |.

This was confirmed by inspecting the minified CLI bundle (cli.js). The VL1 matcher function explicitly skips compound commands when evaluating allow-rules (skipCompoundCheck: false), meaning the per-segment deny walk in va9 is only reached via the sandbox/auto-allow code path and not during interactive permission prompts.

The fix

This PR adds a new plugin, plugins/bash-guard/, that registers a PreToolUse hook for Bash tool calls. The hook:

  1. Splits the command on &&, ||, ;, and | — honouring single quotes, double quotes, and backtick quoting so operators inside strings are not treated as separators.
  2. Reads Bash(...) deny patterns from ~/.claude/settings.json and the project-local .claude/settings.json / .claude/settings.local.json.
  3. Checks each segment independently against every deny pattern using prefix + glob matching that mirrors the built-in rule semantics.
  4. Blocks the entire command (exit 2) if any segment is denied, printing a clear message naming the offending segment and matching rule.

The hook also strips leading VAR=value env-var assignments before matching to close the X=1 rm -rf / env-prefix bypass.

Before / after

With "Bash(rm -rf /*)" in permissions.deny:

Command Before After
rm -rf /tmp ✅ blocked ✅ blocked
git status && rm -rf /tmp ❌ bypassed ✅ blocked
echo x; rm -rf ~/Documents ❌ bypassed ✅ blocked
false || rm -rf /home/user ❌ bypassed ✅ blocked
cat /etc/passwd | dd if=/dev/zero of=/dev/sda ❌ bypassed ✅ blocked
git fetch && git status ✅ allowed ✅ allowed

Test plan

  • 34 unit tests covering: compound splitter, glob matching, env-prefix normalisation, and the exact bypass scenario from the issue
  • All tests pass: python3 -m pytest plugins/bash-guard/tests/ -v
  • Fail-open: hook exits 0 on any parse error so legitimate commands are never silently blocked by a hook crash
  • No-op on single-segment commands (those are already handled by the built-in checker)

Pro777 added 2 commits March 20, 2026 08:31
…ss (#36637)

Chained bash commands like `git status && rm -rf /` bypass the
permissions.deny allowlist because Claude Code's built-in checker
evaluates the full string against prefix rules and matches only the
first segment.

bash-guard is a PreToolUse hook plugin that:
- Splits the command on &&, ||, ;, | (honouring quoting)
- Checks each segment independently against Bash(...) deny patterns
  from ~/.claude/settings.json and project-local settings
- Blocks the entire command if any segment is denied
- Also strips VAR=value env-prefix bypasses before matching

All 34 unit tests pass. Fixes #36637.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chained bash commands silently bypass the tool permission allowlist

1 participant