Claude Code deny rules
Add Claude Code deny rules for rm, .env, and secrets, then work from a reviewable branch.
Published here , updated · 3 sources · Resume reading
Claude Code deny rules sit in .claude/settings.json under permissions.deny and block matching tool calls before any allow rule can save them [1][2]. Deny runs first in the deny, ask, allow order [1]. I commit a short list for rm, .env, and secrets/ so the agent can still run tests on a branch.
Start with one project file
Per the settings docs, shared project rules belong in .claude/settings.json at the repo root so every clone gets the same baseline [2]. The file must be strict JSON: no // comments, no trailing commas, or Claude Code reports a settings error on the next start [2].
Drop this shape in place, then adjust paths to match your tree:
{
"$schema": "https://json.schemastore.org/claude-code-settings.json",
"permissions": {
"allow": [
"Bash(npm run *)",
"Bash(git commit *)"
],
"deny": [
"Bash(rm *)",
"Read(./.env)",
"Read(./secrets/**)"
]
}
}The permissions page uses the same wildcard style for npm run, git commit, and git push examples [1]. After you save, open /permissions inside Claude Code to confirm which file each rule came from [1].
Why deny beats allow every time
Rules are not "most specific wins." Claude Code evaluates deny, then ask, then allow, and the first match decides [1]. A broad deny like Bash(aws *) blocks every AWS CLI call even if you also listed Bash(aws s3 ls) under allow [1]. You cannot carve exceptions out of a deny with a narrower allow [1].
Scoped denies such as Bash(rm *) leave the Bash tool available but refuse matching commands when Claude tries them [1]. A bare tool name like "deny": ["Bash"] removes the tool from context entirely, which is a stronger cut [1].
Permissions run deny, then ask, then allow; the first pattern that matches wins, and deny ends the request.
The permissions docs also warn that rules are enforced by Claude Code, not by the model [1]. Instructions in CLAUDE.md can still make Claude try a forbidden action; the deny rule is what actually stops the tool call [1].
Syntax that actually matches
Use Tool(specifier) form [1]. For Bash, put * after the subcommand: Bash(git log *) matches log variants, while Bash(git *) matches every git subcommand [1]. Patterns like Bash(command:rm *) are ignored with a startup warning because compound commands could bypass them [1].
For file reads, Read(./.env) matches that path relative to the working directory, and Read(./secrets/**) covers everything under secrets/ [1]. The docs show the same .env example in the settings walkthrough [2].
Wrong pattern family to watch: Bash(rm*) without a space after rm is a different string than Bash(rm *) when Claude sends rm -rf. The same gap hits Read(.env) versus Read(./.env) when your working directory is not the repo root. Mirror the path style the docs show and confirm with /permissions after you change the file [1][2].
Ask rules belong in the middle bucket. If you want human eyes on every git push but not on git commit, put Bash(git push *) under ask instead of allow. Deny still wins if a push somehow matched both Bash(git push *) on ask and Bash(git *) on deny, so keep deny patterns coarse only when you mean to block the whole family [1].
Replay the deny-first table locally
Claude Code owns the real matcher. To sanity-check your deny list before you commit it, this small script copies the deny-then-ask-then-allow order from the permissions page [1]:
#!/usr/bin/env python3
def parse_rule(rule):
rule = rule.strip()
if "(" not in rule:
return rule, None
tool, rest = rule.split("(", 1)
pattern = rest[:-1] if rest.endswith(")") else rest
return tool, pattern
def bash_matches(pattern, command):
if pattern in (None, "*"):
return True
if pattern.endswith("*"):
return command.startswith(pattern[:-1])
return command == pattern
def read_matches(pattern, path):
if pattern is None:
return True
if pattern.endswith("/**"):
return path.startswith(pattern[:-3])
return path == pattern
def matches(rule, tool, detail):
rtool, pattern = parse_rule(rule)
if rtool != tool:
return False
if tool == "Bash":
return bash_matches(pattern, detail)
if tool == "Read":
return read_matches(pattern, detail)
return pattern is None
def evaluate(deny, ask, allow, tool, detail):
for bucket, rules in (("deny", deny), ("ask", ask), ("allow", allow)):
for rule in rules:
if matches(rule, tool, detail):
return bucket, rule
return "ask", "(default)"
deny = ["Bash(rm *)", "Read(./.env)", "Read(./secrets/**)", "Bash(aws *)"]
allow = ["Bash(npm run *)", "Bash(git commit *)"]
cases = [
("Bash", "rm -rf ./tmp"),
("Bash", "npm run build"),
("Bash", "aws s3 ls"),
("Read", "./.env"),
("Read", "./secrets/prod.key"),
("Bash", "git commit -m fix"),
]
for tool, detail in cases:
outcome, rule = evaluate(deny, [], allow, tool, detail)
print(f"{tool:4} {detail:28} -> {outcome:5} ({rule})")python3 rule_check.pyCaptured output:
Bash rm -rf ./tmp -> deny (Bash(rm *))
Bash npm run build -> allow (Bash(npm run *))
Bash aws s3 ls -> deny (Bash(aws *))
Read ./.env -> deny (Read(./.env))
Read ./secrets/prod.key -> deny (Read(./secrets/**))
Bash git commit -m fix -> allow (Bash(git commit *))aws s3 ls lands on deny because Bash(aws *) wins before any hypothetical allow [1]. That is the behavior people miss when they stack allow and deny in the same file.
Pair permissions with workflow, not wishful thinking
Deny rules stop Claude Code from issuing a tool call. They do not rewrite shell history, and they are not a kernel sandbox [1]. The permissions page points at sandboxing when you need filesystem or network limits that do not depend on how the command string is written [1]. Use both layers when the repo holds customer data [1].
My working habit on agent tasks: keep credentials outside the tree or under secrets/ with a deny on read, work on a named branch, and read git diff before I commit. Standing approvals from "Yes, and don't ask again" land in .claude/settings.local.json, which settings docs keep out of git by default [2]. Commit the shared deny list; keep personal allow shortcuts local [2].
ALGO ARCADE: allow list as the mirror image
The world2/agent_loop.py level in ALGO ARCADE is not Claude Code, but it shows the same boundary idea from the other side: only listed actions run, and everything else stops [3].
git clone https://github.com/kamilikamil/algo-arcade.git
cd algo-arcade/world2
python3 agent_loop.pyCaptured output:
QUEST: Fix the failing test. allow list: edit_file, read_file, run_tests
turn 1 NOTE read_file("test_app.py")
...
turn 5 NOTE delete_file("prod.db")
STOP DENIED: not on the allow list
...
6 turns, 5 tool calls, 4 ran, 1 denied; prod.db still there: TrueClaude Code deny rules are a deny list on top of a much larger default tool surface [1]. The arcade script is a toy allow list [3]. Both teach where to draw the line before you trust an agent with production paths.
Mistakes I see in real repos
- Putting deny rules only in
~/.claude/settings.jsonwhen the team needed.claude/settings.jsonchecked in. Teammates clone the repo and never inherit your blocks [2]. - Adding
Bash(git push *)to deny but forgetting thatgit -C repo pushmight not match the same pattern. The permissions page lists cases a Bash rule does not match [1]. - Expecting
CLAUDE.mdtext like "never delete files" to replaceBash(rm *). Model instructions shape intent; deny rules enforce the tool layer [1]. - Relying on deny for network exfiltration without sandbox network settings. Deny on
curldoes not catch every path or wrapper the docs describe [1]. - Using
bypassPermissionsordontAskto speed up a session while the committed file still lists sensitive paths. The docs markbypassPermissionsfor isolated environments only [1]. - Leaving a real
.envin the working tree "just for local runs" while inviting teammates to start Claude Code. Deny onRead(./.env)only helps after the file path matches; move secrets out or undersecrets/before the first agent session [1][2].
What to open next
When context gets huge, compacting sessions is a separate knob: Claude Code compact. When your API client hits rate limits, Retry-After in Python covers polite backoff. More agent posts sit under AI agents.
Deny rules only help if the matching strings match how Claude Code spells tools and paths in your version. After you edit the project settings file, trigger the blocked tool once on purpose and confirm the deny message appears before you trust the file in CI [1]. Keep secrets out of the prompt by denying reads on env files even when the model asks politely.
Send this to the friend who is about to run Claude Code on a repo that still has .env in the tree.

Keep this guide
Questions about this guide
Do Claude Code deny rules sandbox commands?
No. Permission rules control Claude Code tool requests; use the sandbox when you need operating-system enforcement.
Where do Claude Code deny rules go?
Put them in .claude/settings.json in the project.
Sources
- Claude Code permissionscode.claude.com
- Claude Code settingscode.claude.com
- ALGO ARCADE agent loop sourcegithub.com