Omniscio

OmniscioGuidesClaude Code permissions for secrets

Claude Code permissions: block .env and secrets files

An .env file is the one file a coding agent should not read. Claude Code has a rule for exactly this, and the rule has edges worth knowing before you rely on it.

Here is the block, what each pattern covers, and the gaps a permission rule leaves.

Checked against Anthropic’s Claude Code permission documentation. By the Omniscio team. Updated .

The block

{
  "permissions": {
    "deny": [
      "Read(.env)",
      "Read(.env.*)",
      "Read(secrets/**)",
      "Read(**/*.pem)",
      "Read(~/.ssh/**)",
      "Read(config/credentials.json)"
    ]
  }
}

Six rules, and each one does something the others do not. Read(.env) covers the file Claude would find in a directory listing. Read(.env.*) covers .env.local, .env.production and the rest of the family. Read(secrets/**) blocks a whole directory. Read(**/*.pem) catches key material anywhere under the project. Read(~/.ssh/**) reaches outside the project. The last one is the file people forget: its name looks ordinary, so it sits in a config folder and is never noticed.

A Read deny does two more things beyond refusing the read: Claude Code excludes matching files from file discovery and search results, and it blocks the Edit and Write tools on the same path, including creating a new file there. NotebookEdit is not covered by a Read deny, so a path no tool may change also needs an Edit rule.

What each pattern matches

RuleBlocksDoes not block
Read(.env)Any .env at or under the current directory.env in a parent directory or another project
Read(.env.*).env.local, .env.production, .env.testA file named env.local with no leading dot
Read(secrets/**)A secrets directory at any depth under the current directoryA file called secrets.md
Read(~/.ssh/**)Every key and config under your home directory’s .ssh~/.aws/credentials or any other home-directory secret, which needs its own rule
Read(**/*.pem)A .pem file at any depth under the current directoryA .pem outside the working directory; add a ~/ or // rule for that
Read(config/credentials.json)That one path under the current directoryA credentials.json in another folder

Path rules use gitignore syntax, with four anchors:

  • path or ./path is relative to the current directory.
  • /path is relative to the settings source that defines it, not to the filesystem root. A single leading slash is not an absolute path.
  • ~/path starts at your home directory.
  • //path is an absolute filesystem path.

A bare filename with no slash matches at any depth, which is why Read(.env) and Read(**/.env) are equivalent, and why Read(//**/.env) is the shape for a rule meant to reach every .env on the filesystem. A relative pattern with one directory segment behaves differently by list: as a deny rule, Read(secrets/**) matches a directory named secrets at any depth, while the same pattern in allow would match only ./secrets. Use **/secrets/** when you want every depth in every list.

The single-slash anchor catches people who write a rule in their user settings. Read(/secrets/**) in ~/.claude/settings.json resolves to ~/.claude/secrets/**, not your project’s secrets folder. A rule meant to follow you into every project needs // or ~/.

On Windows, paths are normalized to POSIX form before matching, so C:\Users\[user] becomes /c/Users/[user]. Use //c/**/.env for that drive and //**/.env across all drives.

What a Read deny covers in the shell

This is the part that decides whether the rule is worth anything. Claude Code applies Read deny rules to file commands it recognizes in a Bash command, such as cat, head, tail, sed and tee, and to the target of an input redirect such as < .env. So cat .env is refused even though it is a Bash command rather than a Read call.

It does not apply to every way of reaching a file:

  • A command that reads without naming the file. grep -r SECRET . run from the directory that holds .env is not matched, because the rule matches a path and the command names none.
  • A subprocess that opens files itself. A Python or Node script that reads .env is not a file command Claude Code can analyze.

A network path is a third case, with its own check rather than a gap: a read from a UNC path or a /net automount gets a permission check of its own, an allow rule does not pre-approve it, and in dontAsk mode the read is denied.

For a boundary that holds whatever the command looks like, enable sandboxing and turn off the unsandboxed retry. The sandbox enforces filesystem access at the OS level for shell commands and their child processes, and a Read deny path is added to what sandboxed commands cannot read.

The exception rule, and its two limits

One file in the family is usually safe to read: the example file kept in version control, which is how Claude scaffolds a new variable without seeing a real value. A deny pattern that starts with ! is a gitignore negation, and it carves paths out of the path rules listed before it in the same list:

"deny": [
  "Read(*.env)",
  "Read(!sample.env)"
]

That blocks every file whose name ends in .env at any depth, except a file named sample.env. The same shape lets this page’s block through its example file: with Read(.env.*) listed first, a Read(!.env.example) after it carves that one file out. Two limits narrow any carve-out: a ! pattern is read relative to the current directory even when the pattern starts with /, ~/ or //, so it cannot reach a rule anchored that way; and a carve-out cannot reopen a file inside a directory that a rule blocks as a whole. With Read(secrets/**) and Read(!secrets/public/**), secrets/public stays blocked.

A carve-out also reaches only rules from the same settings source. A Read(!.env) in a project file does not cancel a Read(./.env) deny from managed settings or any other file.

Where the block goes

Put it in .claude/settings.json and commit it. Deny rules in a project file apply to everyone who works in the folder, and unlike allow rules they do not wait for the workspace trust dialog, because they only restrict.

Add the project-specific secrets file to .gitignore too. A .claudeignore file has no effect on Claude Code; if your project has one, move its entries into Read deny rules.

If a project is already covered by a managed settings file, a deny there outranks anything a developer adds below it. The managed settings guide covers that layer, and the settings.json locations page covers the four files.

Confirm it took effect

  1. Run /permissions and find the six rules; the dialog names the settings file each came from.
  2. Ask Claude to read .env. It should be refused.
  3. Ask it to run cat .env. That should be refused too. Ask for grep -r SECRET .: if that runs, you have just seen the gap above.
  4. Ask Claude to search the repository for a variable name and check that the matching files are missing from its results.

Edits to settings files reach a running session without a restart. When a rule is dropped rather than applied, claude doctor lists the entries Claude Code rejected.

The common mistake

Believing the deny list makes the file unreadable. It stops Claude’s file tools and the file commands it recognizes; it does not stop a recursive search, a script that opens the file itself, or a command it does not analyze. Anthropic’s own note on this is blunt: for OS-level enforcement, enable the sandbox.

The second mistake is writing the rule in user settings with a single slash, where it anchors under ~/.claude and protects nothing in your project. The third is a symlink: a deny rule applies when either the requested path or the file it resolves to matches, so a link that points into a denied folder is blocked. An allow rule is the other way round — it applies only when both match — so a link inside an allowed folder that points outside it is no longer covered. Check the links in a repository before assuming either rule reaches the file.

Where these answers come from

  • Read and Edit rules, the four path anchors, depth, negation and symlinks: Configure permissions, read November 25, 2026.
  • That a Read deny also blocks Edit and Write, and hides files from discovery: the same page, “What Read and Edit rules cover”.
  • The Anthropic example this block starts from: Settings reference, permissions.deny.
  • The sandbox as the OS-level layer: Sandboxing.

Common questions

Does Read(.env) cover .env.local?

No, that is a different filename. Add Read(.env.*) for the family, which covers .env.local, .env.production and similar.

Is one deny rule enough, or do I need Edit too?

A Read deny also blocks the Edit and Write tools on the same path, including creating a file there. NotebookEdit is the exception: add an Edit deny for a path no tool may change.

Can Claude still see that the file exists?

Claude Code excludes matching files from file discovery and search results, and leaves them out of a directory listing it receives. The file can still be named by a command that does not go through those checks.

Do these rules protect keys outside the project?

Only if a rule names that path. Project rules anchor at the project, so a home-directory path needs ~/, or // for an absolute one.