skip to content

Allow, Ask & Deny Rules

Allow, ask and deny rules written as Tool(specifier), evaluated deny, then ask, then allow. Asked because rules, not CLAUDE.md, are what Claude Code enforces, and a misplaced wildcard leaks access.

part ofClaude Coderead it →
on this pageshow

explore

questions

6

In a Claude Code project, which permission rules let `npm run` scripts run without a prompt while blocking Claude from reading `.env` files?

level: middleimportance: must knowfreq 30%

answer

  1. two arrays under permissions
  2. space before the trailing star
  3. Read deny also guards edits
  4. Claude's tools, not every process

basics

~10 s

Add Bash(npm run ) to permissions.allow and Read(./.env) plus Read(./.env.) to permissions.deny; deny is evaluated first, but it guards Claude's own tools and recognised commands, not a script that opens the file itself.

solid answer

~50 s

Put `"Bash(npm run *)"` in `permissions.allow` and `"Read(./.env)"` and `"Read(./.env.*)"` in `permissions.deny`, in the project's `.claude/settings.json` so the team shares them. The allow rule matches `npm run build`, `npm run test --watch` and bare `npm run`, but not `npm install`; in a compound command every subcommand must match on its own, so `npm run build && rm -rf dist` still stops at the `rm`. The deny rule removes matching files from search results, refuses reads, and also blocks Edit and Write on the path; it covers `cat`, `head`, `tail`, `sed`, `tee` and `< .env` redirects in Bash. It does not cover a process that opens the file itself, such as the very npm script you just allowed, so OS-level enforcement needs the sandbox. Allow rules in project settings apply only after you trust the workspace.

code

json · 10 lines
json
{
  "permissions": {
    "allow": ["Bash(npm run *)"],
    "deny": [
      "Read(./.env)",
      "Read(./.env.*)",
      "Read(**/.env)"
    ]
  }
}

go deeper

for a junior

Recall the shape: permissions.allow and permissions.deny hold Tool(specifier) strings, and Bash(npm run *) and Read(./.env) are the two rules this needs.

for a middle

Explain how the trailing wildcard, the space before it and compound-command splitting decide what Bash(npm run *) matches, and what a Read deny covers.

for a senior

Point out the hole: an allowed npm script can load .env itself, so the deny rule is not containment; name the sandbox as the process-level layer.

for a principal

Decide which rules ship in committed project settings, how trust gating affects new clones, and where text-matching rules stop being an adequate secrets policy.

## The two rule lists involved Claude Code enforces permissions through rules in the `permissions` object of a settings file. Three arrays hold rules: `permissions.allow` (run without asking), `permissions.ask` (always prompt) and `permissions.deny` (block). Every rule has the form `Tool` or `Tool(specifier)`, and Claude Code evaluates **deny, then ask, then allow**, taking the first match. This requirement needs only allow and deny: ```json { "permissions": { "allow": ["Bash(npm run *)"], "deny": [ "Read(./.env)", "Read(./.env.*)", "Read(**/.env)" ] } } ``` Placed in `.claude/settings.json`, the file is committed and shared. Allow rules from a project file take effect only after you accept the **workspace trust dialog**; deny and ask rules apply immediately, because they only restrict. ## Reading the allow rule A Bash rule matches the command text, with `*` standing in for any text, spaces included. | Command | Matched by `Bash(npm run *)`? | Why | | :- | :- | :- | | `npm run build` | Yes | `*` stands in for `build` | | `npm run test --watch` | Yes | `*` covers the rest of the line | | `npm run` | Yes | a trailing ` *` also matches the bare command | | `npm install` | No | the words before `*` must match as written | | `timeout 60 npm run test` | Yes | `timeout` is a wrapper Claude Code strips first | | `npm run build && rm -rf dist` | Only the first part | each subcommand must match independently | Three details matter: - **Put the `*` after the subcommand.** Everything before the first `*` limits the rule, so `Bash(npm run *)` allows only `npm run`, while `Bash(npm *)` would allow `npm publish` too. - **The space is part of the rule.** Without it, the pattern would also match any longer command that merely begins with the same letters. - **Compound commands are split** on `&&`, `||`, `;`, `|`, `|&`, `&` and newlines. An allow rule must match each part, so it cannot smuggle a second command through. ## Reading the deny rules `Read(./.env)` targets the `.env` file in the current directory. A bare filename follows gitignore semantics and matches at any depth, so `Read(**/.env)` (equivalently `Read(.env)`) also catches `packages/api/.env`. `Read(./.env.*)` covers `.env.local` and friends. A `Read` deny rule is broader than its name suggests: 1. Matching files are **excluded from file discovery and search results**. 2. **Reads are refused**, by the Read tool and on a best-effort basis by Grep, Glob and `@` file mentions. 3. The **Edit and Write tools are blocked** on the same path, including creating a new file there. 4. Recognised **Bash file commands** such as `cat`, `head`, `tail`, `sed` and `tee` are checked, as are **redirection targets** like `< .env`. An old `.claudeignore` file has no effect; its entries belong in `Read` deny rules. ## What the deny rule does not stop The rule guards Claude's tools and the commands Claude Code can recognise. It does not apply to: - **A command that reads files without naming them**, such as `grep -r API_KEY .` run in the directory holding `.env`. - **A subprocess that opens files itself**, such as a Node or Python script. Here that includes the npm scripts you just allowed: if `npm run dev` loads `.env`, the secret is read by a process Claude started, and whatever it prints reaches the conversation. For enforcement that holds for every process, Claude Code's OS-level sandbox is the layer to add; it is configured separately. A line in `CLAUDE.md` saying "never read .env" is guidance: it shapes what Claude tries, and enforces nothing. ## Checking the result A quick test is to ask Claude to show the file: the read should be refused, while `npm run lint` runs straight away. Run `/permissions` to confirm both rules loaded and see which settings file each came from. If a teammate's saved local rules or user settings add allows, the deny still wins: a deny at any scope is evaluated before any allow. ## Why the question is asked It tests whether a candidate can write rules that do exactly what was intended: an allow narrow enough not to cover `npm publish`, a deny wide enough to catch nested `.env` files, and an honest account of what text-matching rules can and cannot enforce.

  • Why not simply allow `Bash(npm *)`?
    Everything before the first `*` is what limits a Bash rule, so `Bash(npm *)` approves every npm subcommand, including `npm install` of an arbitrary package and `npm publish`. Writing the subcommand first, `Bash(npm run *)`, limits the approval to the scripts defined in `package.json`, which the team reviews. Claude Code also warns at startup about an allow rule with a `*` before the subcommand.
  • Does the `.env` deny rule stop `npm run dev` from loading the file?
    No. A `Read` deny rule applies to Claude's built-in file tools, recognised Bash file commands and redirect targets. A script that opens `.env` itself is an ordinary subprocess the rule never sees, and anything it prints enters the conversation. To keep secrets out, avoid scripts that echo them, and use the OS-level sandbox if the block must hold for every process.
  • Do these rules work as soon as a teammate clones the repository?
    The deny rules do. The allow rule in the project's `.claude/settings.json` grants capability, so Claude Code applies it only after the teammate accepts the workspace trust dialog, which lists the rules the folder would grant. In a `claude -p` run in a folder never trusted interactively, which shows no dialog, project allow rules are not used and a warning is printed.

  • Bash(npm *) is just as safe as Bash(npm run *)
  • Bash(npm run *) also approves whatever follows && on the same line
  • A Read deny on .env stops any process Claude starts from reading it
  • A CLAUDE.md line saying never read .env enforces the rule
  • A .claudeignore file keeps .env away from Claude
  • Read deny rules leave Edit and Write on that path untouched
open as a page

A Claude Code settings file denies `Bash(aws *)` and allows `Bash(aws s3 ls)` — what happens when Claude runs `aws s3 ls`, and why?

level: seniorimportance: must knowfreq 22%

basics

~20 s

The call is blocked: Claude Code checks deny rules, then ask, then allow, and the first match decides regardless of specificity, so an allow rule can never carve an exception out of a matching deny.

open as a page

When you pick 'Yes, and don't ask again' on a Claude Code permission prompt, where is that approval saved and how long does it last?

level: juniorimportance: should knowfreq 16%

basics

~20 s

For Bash commands and WebFetch domains it is saved permanently as an allow rule in .claude/settings.local.json at the git repository root (since v2.1.211); a file-edit approval is never saved and lasts only until the session ends.

open as a page

Why is a Claude Code deny rule such as `Bash(git push *)` not a security boundary, and which commands does it actually stop?

level: seniorimportance: should knowfreq 14%

basics

~20 s

A Bash rule matches the command text Claude writes, after splitting compound commands and stripping a fixed set of wrappers; it stops git push origin main but not git -C . push, git 'push' or the same push inside sh -c.

open as a page

How do Claude Code permission rules name a web domain and an MCP server's tools, and where may wildcards appear in each?

level: middleimportance: nice to knowfreq 8%

basics

~10 s

Web fetches use WebFetch(domain:host), where *.example.com covers subdomains but not example.com itself; MCP tools use mcp__server or mcp__server__tool, and an allow glob must follow a literal mcp__server__ prefix.

open as a page

In Claude Code Read and Edit permission rules, what do the `//`, `~/`, `/` and `./` path prefixes each anchor to?

level: seniorimportance: nice to knowfreq 7%

basics

~20 s

//path is absolute from the filesystem root, ~/path starts at your home directory, /path is relative to the settings source (the project for project settings, ~/.claude for user settings), and path or ./path is relative to the current directory.

open as a page