fix(hook): respect Claude Code deny/ask permission rules on rewrite#576
fix(hook): respect Claude Code deny/ask permission rules on rewrite#576FlorianBruniaux wants to merge 4 commits intodevelopfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds Claude Code permission-awareness to the rtk rewrite flow so hooks can avoid auto-allowing rewritten commands when deny/ask rules match, preserving user intent while still enabling rewrites.
Changes:
- Introduces
src/permissions.rsto load Claude Code settings and evaluatedeny/askBash rules against commands. - Updates
rtk rewriteto return a richer exit-code protocol (0/1/2/3) to drive hook behavior (auto-allow vs prompt vs passthrough). - Updates hook scripts and hook versioning to handle the new exit codes, and documents the new module in
ARCHITECTURE.md.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/rewrite_cmd.rs | Adds permission checking and new exit-code protocol for rtk rewrite. |
| src/permissions.rs | Implements settings discovery + deny/ask rule matching (with unit tests). |
| src/main.rs | Registers the new permissions module. |
| src/hook_check.rs | Bumps expected hook version to 3. |
| hooks/rtk-rewrite.sh | Updates hook logic to respect deny/ask via rtk rewrite exit codes. |
| .claude/hooks/rtk-rewrite.sh | Same as above, with audit logging retained. |
| ARCHITECTURE.md | Documents the new module and updates module counts. |
Comments suppressed due to low confidence (1)
ARCHITECTURE.md:301
- The module counts in this section are internally inconsistent after the update: it now claims “Total: 65 modules (38 command + 27 infrastructure)”, but the breakdown immediately below still lists different numbers (e.g. “Command Modules: 34”). Please reconcile these counts (and the “Complete Module Map (30 Modules)” heading earlier in this section if it’s still accurate).
**Total: 65 modules** (38 command modules + 27 infrastructure modules)
### Module Count Breakdown
- **Command Modules**: 34 (directly exposed to users)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/rewrite_cmd.rs
Outdated
| PermissionVerdict::Ask => { | ||
| print!("{}", rewritten); | ||
| std::process::exit(3); | ||
| } |
The PreToolUse hook was emitting permissionDecision: "allow" on every rewritten command, silently bypassing deny and ask rules configured in Claude Code settings.json. For example, a user with Bash(git push --force) in their deny list would have the command rewritten and auto-allowed. Add a permissions module that loads Bash(...) deny/ask rules from all 4 Claude Code settings files and checks the original command before deciding the hook response: - Deny match → pass through (return false), native deny handles it - Ask match → rewrite but omit permissionDecision, tool prompts user - No match → rewrite with permissionDecision: "allow" (existing behavior) Same fix applied to Claude Code, Gemini CLI, and Cursor hook handlers. Ref: rtk-ai/rtk#576 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The PreToolUse hook was emitting permissionDecision: "allow" on every rewritten command, silently bypassing deny and ask rules configured in Claude Code settings.json. For example, a user with Bash(git push --force) in their deny list would have the command rewritten and auto-allowed. Add a permissions module that loads Bash(...) deny/ask rules from all 4 Claude Code settings files and checks the original command before deciding the hook response: - Deny match → pass through (return false), native deny handles it - Ask match → rewrite but omit permissionDecision, tool prompts user - No match → rewrite with permissionDecision: "allow" (existing behavior) Same fix applied to Claude Code, Gemini CLI, and Cursor hook handlers. Ref: rtk-ai/rtk#576 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
follow-up wildcards issueThe PR's pattern matching only handles * at the end of a pattern. Claude Code's permission system supports * Works:
Doesn't work:
|
…289) ## Summary - The `PreToolUse` hook was emitting `permissionDecision: "allow"` on every rewritten command, silently bypassing deny and ask rules configured in Claude Code `settings.json` - Adds a `permissions` module that loads `Bash(...)` deny/ask rules from all 4 Claude Code settings files and checks the **original** command before deciding the hook response - **Deny** → pass through silently, letting Claude Code's native deny handle it - **Ask** → rewrite but omit `permissionDecision`, so the tool prompts the user - **Allow** → rewrite with `permissionDecision: "allow"` (existing behavior) - Same fix applied to Claude Code, Gemini CLI, and Cursor hook handlers Ref: rtk-ai/rtk#576 ## Test plan - [x] 17 unit tests in `permissions.rs` (exact match, prefix match, wildcard, deny precedence, compound commands, word boundary, settings file integration) - [x] Full test suite: 1324 passed, 0 failed - [x] Clippy clean, fmt clean - [x] Existing hook handle and install tests still pass Credit: rtk-ai/rtk#576 - thank you @FlorianBruniaux 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
|
Ran a second deeper review focused on edge cases. Found 2 additional gaps beyond @aeppling's wildcard finding. Bug 1 (Critical): deny rules skipped for non-RTK commands
Commands that return Concrete failure: user adds Fix: run Bug 2 (Critical):
|
| Bug | Impact | Status |
|---|---|---|
| Deny skipped for non-RTK commands | Security bypass for rm, mv, kill, etc. |
New finding |
*:* matches nothing |
Catch-all deny rule is a no-op | New finding |
| Leading/middle wildcards | @aeppling's finding, confirmed | Known |
Core architecture is solid — these are fixable without redesign. Happy to re-review after fixes.
|
@pszymkowiak @aeppling All four findings addressed. Two commits on this branch:
8 new tests cover all the fixed cases, including integration variants with |
6bf2100 to
0841ec3
Compare
|
@FlorianBruniaux This PR has a merge conflict with develop (likely git fetch upstream develop
git rebase upstream/develop
# resolve conflicts
git push --force-with-leaseThis is P1 security — we'd like to get it in the next release. Thanks! |
The PreToolUse hook was emitting `permissionDecision: "allow"` on every rewritten command, bypassing deny and ask rules in .claude/settings.json. - Add `src/permissions.rs`: loads Bash deny/ask rules from all 4 Claude Code settings files (project + global, settings.json + settings.local.json), checks commands (including compound && / || / | / ;) and returns Allow / Deny / Ask verdict. 16 unit tests. - Modify `src/rewrite_cmd.rs`: after finding a rewrite, check the original command against permissions. Exit 0 = allow (auto-approve rewrite), exit 2 = deny (passthrough, let CC native deny handle it), exit 3 = ask (print rewrite but no permissionDecision, CC prompts user). - Update both hook files to handle exit codes 2 and 3. Version bumped 2→3. - Bump `CURRENT_HOOK_VERSION` 2→3 in `hook_check.rs` so users with the old hook get the upgrade prompt. - Fix set -euo pipefail bug in .claude/hooks/rtk-rewrite.sh: capture exit code with `|| EXIT_CODE=$?` instead of bare assignment. Fixes #260 Signed-off-by: Florian BRUNIAUX <florian@bruniaux.com>
Bug 1 (Critical): check_command() was called inside Some(rewritten), so non-RTK commands (rm, kill, python3 -c) bypassed deny rules entirely. Move verdict check before registry::rewrite_command() so all commands are evaluated regardless of whether RTK has an equivalent. Bug 4 (Medium): print!() before process::exit() could leave stdout unflushed. Add explicit std::io::stdout().flush() after each print!(). Add Eq derive to PermissionVerdict (required for == comparison). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: Florian BRUNIAUX <florian@bruniaux.com>
Bug 2 (Critical): *:* catch-all matched nothing. strip_suffix('*') left
"*:" which after trim became "*" (non-empty), so the branch returned
false instead of true. Fix: detect empty-or-star prefix after stripping.
Bug 3 (Medium): leading wildcards ("* --force"), middle wildcards
("git * main"), and multi-wildcard patterns ("git * --force *") fell
through to exact match, silently failing. Add glob_matches() with
character-level segment anchoring: first segment must be prefix, last
must be suffix, middle segments found via str::find in order.
Colon normalization in glob_matches(): "sudo:*" -> "sudo *" so both
fast path and glob path interpret colon syntax consistently.
New tests: test_star_colon_star_matches_everything,
test_leading_wildcard, test_leading_wildcard_no_partial,
test_middle_wildcard, test_middle_wildcard_no_match,
test_multiple_wildcards, test_deny_with_leading_wildcard,
test_deny_star_colon_star.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Florian BRUNIAUX <florian@bruniaux.com>
validate-docs.sh counts mod declarations in main.rs (66). Update ARCHITECTURE.md total to match. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: Florian BRUNIAUX <florian@bruniaux.com>
0841ec3 to
8a70932
Compare
Problem
The
PreToolUsehook (rtk-rewrite.sh) was emittingpermissionDecision: "allow"on every rewritten command, silently bypassing deny and ask rules defined in.claude/settings.json.Concrete example: user has
Bash(git push --force)in deny rules → hook rewrites tortk git push --forcewith auto-allow → command executes without confirmation. P1-critical bug reported by the community and confirmed by @pszymkowiak.Closes #260
Solution
Permission logic lives in the Rust binary (
rtk rewrite), not the shell. The hook interprets exit codes.Exit code protocol:
permissionDecision(CC prompts user)Changes
src/permissions.rs(new) — loadsBash(...)deny/ask rules from all 4 Claude Code settings files ($PROJECT_ROOT/.claude/settings.json,.local.json,~/.claude/settings.json,.local.json). Checks commands including compound (&&,||,|,;) and returnsAllow/Deny/Askverdict. Deny takes priority over Ask. 16 unit tests.src/rewrite_cmd.rs— after finding a rewrite, callscheck_command()on the original command. Exits 0/2/3 accordingly.hooks/rtk-rewrite.sh+.claude/hooks/rtk-rewrite.sh— handle exit codes 2 (passthrough silently) and 3 (rewrite withoutpermissionDecision). Hook version bumped 2→3.src/hook_check.rs—CURRENT_HOOK_VERSION2→3 so existing users get the upgrade prompt.ARCHITECTURE.md— updated module count.Test plan
src/permissions.rs(exact match, prefix match, wildcard, deny precedence, compound commands, word boundary)echo '{"permissions":{"deny":["Bash(git push --force)"]}}' > .claude/settings.json→rtk rewrite "git push --force"exits 2rtk rewrite "git status"→ exits 0 as before (no regression)set -euo pipefailbug fixed in.claude/hooks/rtk-rewrite.sh(use|| EXIT_CODE=$?pattern)🤖 Generated with Claude Code