# Claude Code deny rules

> Add Claude Code deny rules for rm, .env, and secrets, then work from a reviewable branch.

Source: https://fullbytedeveloper.com/blog/claude-code-deny-rules
Keyword: AGENT
Published here: 2026-11-25, updated 2026-12-03

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:

```json title=".claude/settings.json"
{
  "$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].

<figure>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 720 300" role="img" aria-label="Claude Code permission order: deny, then ask, then allow; first match wins">
  <defs>
    <marker id="arrowClaudeDeny" markerWidth="10" markerHeight="10" refX="8" refY="3" orient="auto">
      <path d="M0,0 L8,3 L0,6 Z" fill="#14b8a6"/>
    </marker>
  </defs>
  <rect x="0" y="0" width="720" height="300" rx="12" fill="#0b1f3a"/>
  <text x="24" y="36" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="16" font-weight="700">Tool call: Bash rm -rf ./tmp</text>
  <rect x="24" y="56" width="200" height="56" rx="8" fill="#123052" stroke="#f59e0b" stroke-width="2"/>
  <text x="36" y="92" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">1 deny list</text>
  <path d="M224 84 H280" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#arrowClaudeDeny)"/>
  <rect x="288" y="56" width="180" height="56" rx="8" fill="#123052" stroke="#14b8a6" stroke-width="2"/>
  <text x="300" y="84" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">match Bash(rm *)</text>
  <text x="300" y="104" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">stop: blocked</text>
  <path d="M134 112 V140" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#arrowClaudeDeny)"/>
  <rect x="24" y="148" width="200" height="56" rx="8" fill="#123052" stroke="#64748b" stroke-width="2"/>
  <text x="36" y="184" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">2 ask list</text>
  <path d="M224 176 H288" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#arrowClaudeDeny)"/>
  <text x="296" y="180" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">prompt if matched</text>
  <path d="M134 204 V232" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#arrowClaudeDeny)"/>
  <rect x="24" y="240" width="200" height="44" rx="8" fill="#123052" stroke="#64748b" stroke-width="2"/>
  <text x="36" y="270" fill="#f8fafc" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">3 allow list</text>
  <path d="M224 262 H440" stroke="#14b8a6" stroke-width="3" fill="none" marker-end="url(#arrowClaudeDeny)"/>
  <text x="448" y="266" fill="#cbd5e1" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">auto-approve only if deny and ask missed</text>
  <text x="480" y="100" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">Deny cannot be</text>
  <text x="480" y="124" fill="#f59e0b" font-family="ui-sans-serif, system-ui, sans-serif" font-size="14">overridden by allow [1]</text>
</svg>
</figure>

*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]:

```python title="rule_check.py"
#!/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})")
```

```bash
python3 rule_check.py
```

Captured output:

```text
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].

```bash
git clone https://github.com/kamilikamil/algo-arcade.git
cd algo-arcade/world2
python3 agent_loop.py
```

Captured output:

```text
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: True
```

Claude 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.json` when the team needed `.claude/settings.json` checked in. Teammates clone the repo and never inherit your blocks [2].
- Adding `Bash(git push *)` to deny but forgetting that `git -C repo push` might not match the same pattern. The permissions page lists cases a Bash rule does not match [1].
- Expecting `CLAUDE.md` text like "never delete files" to replace `Bash(rm *)`. Model instructions shape intent; deny rules enforce the tool layer [1].
- Relying on deny for network exfiltration without sandbox network settings. Deny on `curl` does not catch every path or wrapper the docs describe [1].
- Using `bypassPermissions` or `dontAsk` to speed up a session while the committed file still lists sensitive paths. The docs mark `bypassPermissions` for isolated environments only [1].
- Leaving a real `.env` in the working tree "just for local runs" while inviting teammates to start Claude Code. Deny on `Read(./.env)` only helps after the file path matches; move secrets out or under `secrets/` before the first agent session [1][2].

## What to open next

When context gets huge, compacting sessions is a separate knob: [Claude Code compact](/blog/claude-code-compact). When your API client hits rate limits, [Retry-After in Python](/blog/retry-after-header-python) covers polite backoff. More agent posts sit under [AI agents](https://fullbytedeveloper.com/blog/topic/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.

Image: A protective deny wall of amber bars blocking paths to a shredder, a secret-file vault, and an env crystal, while allowed work flows through a reviewable branch channel toward a work cube, shown as unmarked geometric solids with no typography or filenames.
https://fullbytedeveloper.com/media/claude-code-deny-rules/claude-code-deny-rules-venice.webp
Deny dangerous paths first. Do the rest of the work on a branch you can review.

## Sources

1. [Claude Code permissions](https://code.claude.com/docs/en/permissions)
2. [Claude Code settings](https://code.claude.com/docs/en/settings)
3. [ALGO ARCADE agent loop source](https://github.com/kamilikamil/algo-arcade/blob/main/world2/agent_loop.py)
