What each tool can read by default
- Claude Code: file reads inside the working directory and added directories need no approval (permissions). If you turn the sandbox on, its default read policy covers "most of the machine, including credential files such as
~/.sshand~/.aws/credentials" (sandboxing). Environment variables are inherited too, including secrets already in the process environment. - Codex CLI: the default
workspace-writesandbox limits where commands can write and keeps network off. Reading is governed by the permissions profile you use, and Codex's own example profile adds an explicit deny rule for.envfiles. That is the step to copy. - Cursor: it ignores
.env*files by default and anything in your.gitignore. That covers the Agent, Tab and @-mentions. Cursor's docs say terminal and MCP tools are not blocked by ignore files.
Anything an agent reads becomes part of what the model sees. A secret in a file it opens is a secret in the conversation.
Block it in Claude Code
Deny the files you know about. Then turn on the sandbox for the shell, and deny the credential folders there too. Put this in your project's .claude/settings.json. The ./ paths in the sandbox block resolve from the project root in that file, but from ~/.claude in your user settings:
{
"permissions": {
"deny": [
"Read(./.env)",
"Read(./.env.*)",
"Read(./secrets/**)",
"Read(~/.ssh/**)",
"Read(~/.aws/**)"
]
},
"sandbox": {
"enabled": true,
"allowUnsandboxedCommands": false,
"filesystem": {
"denyRead": ["./.env", "./.env.*", "~/.ssh", "~/.aws/credentials"]
}
}
}Two limits from Anthropic's docs:
- Read deny rules cover built-in file tools and some Bash commands such as
cat .env. They "aren't a complete block": they do not apply to a script that opens files itself, or to a command that reads files without naming them, likegrep -r pattern .. - The sandbox wraps shell commands only. A
denyReadentry does not stop the Read tool, so you need both layers.
Block it in Codex
Codex permission profiles can deny reads with exact paths or globs. This profile extends the built-in workspace profile and denies .env files and your SSH folder (permissions):
default_permissions = "project-edit"
[permissions.project-edit]
extends = ":workspace"
[permissions.project-edit.filesystem]
glob_scan_max_depth = 3
"~/.ssh" = "deny"
[permissions.project-edit.filesystem.":workspace_roots"]
"**/*.env" = "deny"One caveat from the docs: on Linux, WSL and native Windows, glob matches are enumerated when each sandboxed process starts. A long-running shell, script or dev server can still read a matching .env created after it started. Start a fresh process after you add secrets files.
Block it in Cursor
Add a .cursorignore at the project root, and add global ignore patterns in your user settings so you do not repeat it per project (ignore file):
**/.env
**/.env.*
**/credentials.json
**/*.pem
**/*.key
**/id_rsaThen set Read access to Workspace, so reads outside the project ask first, and keep the sandbox on for shell commands.
Test it in two minutes
- Put a fake value in a new
.env, such asFAKE_KEY=not-a-real-secret. Never use a real one for a test. - Ask the agent to print the contents of
.env. - You want a denial, a prompt or a sandbox error, not the value.
- Repeat with
cat ~/.ssh/config, and with a command that reads without naming the file, likegrep -r FAKE_KEY .. That last one is where deny rules alone fail.
If a real secret was exposed
Rotate it. Revoking and reissuing the key is the only fix that works after the fact. Then check that .env and .claude/settings.local.json are in .gitignore and not in your git history. The free Claude Code settings scanner checks pasted settings for saved secrets, wide allow rules and missing guardrails in your browser, and the command-line version never prints the secrets it finds. The full walkthrough covers that and the per-tool settings in more depth, and the 15-minute inventory shows what your agent can reach right now.
Quick follow-ups
Does a deny rule fully stop Claude Code reading .env?
No. Anthropic's docs say read deny rules are not a complete block. They cover built-in file tools and some Bash commands, but not a script that opens the file itself. Add the sandbox and keep secrets out of reach.
Do I need to worry about ~/.ssh if I only work in one project?
Yes. Anthropic's docs say the sandbox's default read policy includes credential files like ~/.ssh and ~/.aws/credentials. Add filesystem.denyRead entries, or run the agent in a container that never mounts them.
Is .cursorignore enough?
It helps, but Cursor's docs say the terminal and MCP tools cannot be blocked by it. Pair it with the sandbox, Read access set to Workspace, and approval prompts for commands.
Sources
- Claude Code: Configure permissions
- Claude Code: Settings reference
- Claude Code: Configure the sandboxed Bash tool
- Codex: Permissions
- Codex: Agent approvals and security
- Cursor: Ignore file
- Cursor: Run modes
See also the glossary and the other questions.
