In a Claude Code project, which permission rules let `npm run` scripts run without a prompt while blocking Claude from reading `.env` files?
answer
- two arrays under permissions
- space before the trailing star
- Read deny also guards edits
- Claude's tools, not every process
basics
~10 sAdd 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 sPut `"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{
"permissions": {
"allow": ["Bash(npm run *)"],
"deny": [
"Read(./.env)",
"Read(./.env.*)",
"Read(**/.env)"
]
}
}go deeper
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.
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.
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.
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