Stop AI coding agents from reading .env files and secrets
An agent that can run shell commands can usually read any file the developer can. Ignore files do not change that. Here is what actually keeps Claude Code, Codex, Cursor and Copilot away from your keys, and where each control stops.
Next step
AI Agent Blocklist: block the coding agents that do not honour your secret rules, by package, process, app ID and domain
Deny rules only work in agents you manage. Our list covers the rest, with a risk grade on every entry and daily updates.
Three ways an agent reaches a secret
Most guides only cover the first path. A real block has to cover all three, or the agent simply takes another route when a rule stops it.
The agent's file tools
The built-in read, search and edit tools. Deny rules in the agent's settings cover this path well.
Shell commands
A command such as cat or grep run in the terminal. Agents recognise some file commands; others slip through rules that match command text.
Scripts and subprocesses
A Python or Node script, a test runner or a build tool that opens files itself. Only an operating system sandbox stops this path.
Where secrets hide on a developer machine
Before you write rules, list what you are protecting. These are the usual places, and each belongs in your deny lists.
.env, .env.*App keys, database URLs, API tokens~/.sshSSH private keys and known hosts~/.aws/credentialsCloud access keys~/.config/gcloudCloud CLI sign-in data~/.kube/configCluster credentials.npmrc, .pypircPackage publishing tokenssecrets/, *.pem, *.keyCertificates and private keysShell profiles and historyExported tokens, pasted commandsHow to prevent Claude Code from reading .env and certain files
Claude Code has two layers: permission rules for its own tools and recognised commands, and a sandbox for everything else. Use both.
- Write Read deny rules. Patterns follow gitignore syntax. A bare name such as Read(.env) matches at any depth under the working folder.
- Anchor them correctly. // means the filesystem root, ~/ the home folder, ./ or no prefix the current folder. A single / anchors at the settings file's own location.
- Deliver them as managed settings and set allowManagedPermissionRulesOnly so project files cannot add their own allow rules.
- Turn on the sandbox and add the same paths to sandbox.filesystem.denyRead, so scripts and subprocesses are covered too.
- Strip credentials with the sandbox credentials settings for files and environment variables.
{
"permissions": {
"deny": [
"Read(.env)",
"Read(.env.*)",
"Read(./secrets/**)",
"Read(~/.ssh/**)",
"Read(~/.aws/**)"
]
},
"allowManagedPermissionRulesOnly": true,
"sandbox": {
"enabled": true,
"failIfUnavailable": true,
"allowUnsandboxedCommands": false,
"filesystem": {
"denyRead": ["~/**/.env"]
},
"credentials": {
"files": [
{ "path": "~/.ssh", "mode": "deny" }
]
}
}
}
How to prevent Codex from reading .env and certain files
Codex enforces file rules through its sandbox. Admins set them as requirements that users cannot weaken; developers can add their own in a permission profile.
- Admins: use requirements.toml. Under [permissions.filesystem], list deny_read paths. Absolute paths and ~ paths are allowed; paths starting with ./ are not.
- Place or deliver it: /etc/codex/requirements.toml on macOS and Linux, %ProgramData%\OpenAI\Codex\requirements.toml on Windows, macOS MDM, or cloud-managed requirements for ChatGPT business workspaces.
- Developers: use a permission profile. Add a deny entry for **/*.env under the profile's workspace roots.
- Trim environment variables with Codex's shell environment policy, so tokens in the shell do not reach the commands Codex runs. Check the current configuration reference for the keys.
# requirements.toml, set by admins [permissions.filesystem] deny_read = [ "/**/*.env", "~/.ssh", "~/.aws", ] # a developer's own profile in config.toml [permissions.project-edit.filesystem.":workspace_roots"] "**/*.env" = "deny"
Cursor and GitHub Copilot: what their ignore and exclusion features do
Both offer a way to hide files from AI features. Both vendors say clearly that it is not a security boundary, which means you need other controls around it.
| Feature | What it does | What the vendor says it does not do | What to add |
|---|---|---|---|
| .cursorignore in Cursor | Keeps listed files out of Cursor's AI features and indexing | Cursor calls it a convenience, not a security boundary; terminal commands and MCP tools can still read ignored files | Approval for terminal commands, MCP allowlist, secrets out of the workspace |
| Content exclusion in GitHub Copilot | Stops excluded files being used for suggestions and chat, on Business and Enterprise plans | GitHub says it is not a filesystem security boundary, may miss indirect access by agents, is not guaranteed with third-party MCP servers and does not apply to symlinks | Operating system permissions, agent mode limits, MCP registry |
Side by side: which control covers which path
Use this table to see where each agent is protected and where you still depend on keeping secrets out of reach. Only the last row covers every path on its own.
| Control | File tools | Shell commands | Scripts and subprocesses | Admin can enforce |
|---|---|---|---|---|
| Claude Code Read deny rules | Yes | Recognised file commands | No | Yes, managed settings |
| Claude Code sandbox denyRead | Own rules apply | Yes | Yes, macOS, Linux, WSL2 | Yes |
| Codex deny_read | Not stated by OpenAI | Yes, sandboxed | Yes, sandboxed | Yes, requirements.toml |
| .cursorignore | AI features | No | No | Not as a boundary |
| Copilot content exclusion | Suggestions and chat | Not guaranteed | No | Yes, organisation settings |
| Secrets kept out of the workspace | Yes | Yes | Yes | Through process and tooling |
Environment variables: the secret path nobody checks
Many teams move secrets out of .env files and into the shell, then forget that the agent starts from that shell. Every command it runs inherits the variables.
Load per command
Inject a credential only into the one command that needs it, from your secret manager, and let it expire.
Use the agent's filter
Claude Code's sandbox can unset listed variables, and Codex has a shell environment policy for the same job.
Tokens in profiles
Long-lived tokens exported in shell profiles are visible to every process the developer starts, agents included.
Mistakes that leave secrets readable
These come up again and again when teams review their agent settings. Each one looks like a working control until someone tests it.
| Mistake | Why it fails | Fix |
|---|---|---|
| Relying on an ignore file | Ignore files steer AI features; they do not stop shell commands or scripts | Deny rules plus a sandbox, and secrets out of the folder |
| Rules only in a project file | Project files are written by whoever controls the repository | Put deny rules in managed settings or admin requirements |
| Rules with the wrong anchor | In Claude Code, a single leading slash anchors at the settings file, not the filesystem root | Use // for absolute paths and ~/ for the home folder |
| Relative paths in Codex requirements | Paths that start with ./ are not allowed in deny_read | Use absolute patterns such as /**/*.env, or ~ paths |
| Sandbox on, but not available | Native Windows sessions of Claude Code are not sandboxed | Use WSL2, and set the sandbox to fail when it cannot run |
| Developers with local admin rights | Local admins can edit managed files on their own machine | Let MDM redeploy the files and limit admin rights where you can |
If an agent may have read a secret
Treat it as exposed. Agent sessions send context to a model provider, and you cannot recall what was sent. Work through these steps the same day.
Rotate
Revoke and reissue the credential. Start with cloud keys, publishing tokens and database passwords, which attackers use fastest.
Check use
Look at the provider's access logs for sign-ins or calls from unfamiliar places since the session started.
Close the path
Add the missing deny rule or sandbox path, rerun the four tests, and record the change so the next review sees it.
Test your rules before you trust them
Put a dummy .env file with a fake value in a test repository, then ask the agent to use it in four different ways. Every test should fail.
- Direct readAsk the agent to open the .env file and show its contents.
- Shell readAsk it to print the file from the terminal with a common command.
- Script readAsk it to write and run a short script that opens the file.
- Variable readExport a fake token in the shell and ask the agent to print the environment.
Who owns which part
Secret protection for agents fails when everyone assumes someone else set it up. Give each part a named owner.
| Team | Owns | Reviews |
|---|---|---|
| Security | The list of protected paths and variables, and which agents are approved | Every quarter, and after each incident |
| Endpoint or platform team | Managed settings, requirements files and MDM delivery to every machine | On each agent release that changes settings |
| Engineering leads | Keeping secrets out of repositories and moving teams to run-time credentials | In code review and onboarding |
| Developers | Reporting any agent that reads what it should not, and not working around the rules | Every day, as part of normal work |
The gap rules cannot close: agents you do not manage
Every control above lives inside a specific agent. A developer who installs a different agent, or a lookalike, brings none of your rules with them. That is the gap the AI Agent Blocklist closes.
| What we list | Examples from our file | Where you block it |
|---|---|---|
| Command-line coding agents by package name | @anthropic-ai/claude-code, @openai/codex | Package proxies and endpoint rules |
| Agent processes | cursor-agent, gemini | Intune, Jamf, Defender |
| Editor extensions, AI editors, MCP servers | Thousands of IDs and names in the full file | Editor policies and MDM |
| Agent domains | 2,600+ AI agent domains | Firewall, DNS filter, gateway |
Rules for the agents you allow, a block for the rest
The AI Agent Blocklist covers coding agents, AI editors, MCP servers, AI browsers and agent extensions, with a risk grade on every entry.
From $99 a month for one organisation. Download access is set up within one business day of your order.
See plans Ask a question- Package and process names of coding agents
- Bundle IDs and package IDs for Intune and Jamf
- Extension IDs for VS Code and JetBrains
- Domains in firewall, DNS, hosts and PAC formats
- CSV and JSON, updated every day
Ask us or order a plan
Tell us what you need to block and where your rules live. We reply within one business day.
- Files for firewalls, DNS filters, secure web gateways, MDM and browser policies
- Updated every day as new AI browsers, agents and extensions appear
- Plans for one organisation, for MSPs and for product makers, or a hosted filter we run for you
Questions about agents and secrets
How do I prevent Claude Code from reading .env files?
Does .claudeignore stop Claude Code reading files?
How do I prevent Codex from reading .env files?
How do I prevent Claude Code or Codex from reading certain files?
Is .cursorignore a security control?
Can an agent still see secrets in environment variables?
Should .env files be in the project folder at all?
What is the safest way to keep secrets from coding agents?
Claude Code is a trademark of Anthropic, PBC. Codex is a trademark of OpenAI. Cursor is a trademark of Anysphere, Inc. GitHub and GitHub Copilot are trademarks of GitHub, Inc.
Gemini is a trademark of Google LLC. Intune, Defender and Windows are trademarks of Microsoft Corporation. Jamf is a trademark of Jamf.
macOS is a trademark of Apple Inc. VS Code is a trademark of Microsoft Corporation. ChatGPT is a trademark of OpenAI. JetBrains is a trademark of JetBrains s.r.o. Linux is a registered trademark of Linus Torvalds.
Names are used only to identify the products discussed. AI Agent Blocklist is not affiliated with, sponsored or endorsed by these companies.