Open a coding agent in a repo that has a .env file and ask it to “check why the API call fails.” There’s a good chance the first thing it does is read .env. Your database password, your Stripe key and your OpenAI token are now in the agent’s context, and from there in the transcript, maybe in a log line, and sometimes in a test fixture it writes for you.
The agent isn’t doing anything wrong. It runs as you, and .env is just a file in the working directory.
This post covers what Claude Code and Codex give you to stop that, where each option leaks, and the setup that actually works: the agent can use the secrets but never sees them.
Why .gitignore doesn’t help
.gitignore tells git what to skip. It says nothing to the agent. A file can be ignored by git and still sit in the working directory, readable by every process you start, including your agent.
Some agents skip ignored files when they search the repo, which makes it look like they respect it. But “not indexed” is not “not readable.” Ask the agent to open .env directly, or have it run a command that does, and the contents come back.
Claude Code: deny rules
Claude Code lets you deny file access in .claude/settings.json:
{
"permissions": {
"deny": [
"Read(./.env)",
"Read(./.env.*)"
]
}
}
This blocks Claude Code’s own file tools from opening those files. Claude Code also applies the rule to shell commands it can parse, so a plain cat .env or head .env gets blocked too. It’s worth adding, and it takes a minute.
The gap is commands that read the file without naming it. grep -r KEY . scans the whole directory, and a Python or Node script can open .env on its own. Claude Code’s docs say outright that deny rules don’t cover these. A rule that matches command text can always be stepped around.
Deny rules are a good guardrail against the agent wandering into .env. They don’t hold up against an agent that’s been told to get the value, for example by a prompt injection in an issue or README it read earlier.
Claude Code’s sandboxing closes that gap, because the OS enforces the restriction for every process. It’s off by default, and its read rules (sandbox.filesystem.denyRead) are separate from the permission rules above. You need both: the sandbox covers shell commands, the deny rules cover Claude’s own file tools. Also set allowUnsandboxedCommands to false, otherwise Claude can retry a blocked command outside the sandbox.
Codex: sandbox and permission profiles
Codex CLI runs commands in a sandbox by default. The usual mode, workspace-write, lets the agent edit files in the repo and keeps network access off unless you turn it on.
The default mode doesn’t hide files inside the workspace. Your .env lives in the repo, and the repo is where the agent works. To block it, you define a permission profile in config.toml that denies the path, for example a "**/*.env" = "deny" entry on the workspace roots. That denies both reads and writes. It’s opt-in, so you have to set it up per machine or per project. See the Codex config reference for the exact format.
The real problem: the agent still needs the secret
Say you lock it down properly and the agent can’t read .env at all. Now it can’t run the integration tests, can’t hit the staging API, can’t run the migration. So you paste the key into the chat “just this once,” and you’re worse off than before.
Blocking the file only solves half of it. The other half is: how does the agent use a credential without holding it?
The answer is to give the agent a placeholder instead of the real value, and swap in the real one outside the agent’s reach, only for requests you’ve allowed. The agent’s environment has STRIPE_KEY set to a placeholder. When it calls api.stripe.com, a local proxy sees the placeholder, checks the request against your rules, and replaces it with the real key on the way out. The agent never sees the real value, so it can’t leak it, paste it, or be tricked into sending it somewhere else.
Setup: block the file, keep the secret usable
I’ll use Stashbase because it’s what we build and it does both halves in one profile: sandboxing for the file, and a local proxy for the secret. The CLI is open source and works with any coding agent you start from the terminal, not just Claude Code and Codex.
Keep your secrets where they are, in .env. Then create .stashbase/agents/claude.toml (or codex.toml):
egress_hosts = ["api.anthropic.com", "platform.claude.com"]
file = ".env"
[filesystem]
deny_read = [".env*", "~/.ssh", "~/.aws"]
deny_write = [".env*", ".git/hooks"]
[secrets.STRIPE_KEY]
[[secrets.STRIPE_KEY.rules]]
effect = "allow"
hosts = ["api.stripe.com"]
[sandbox]
backend = "native"
What this does:
deny_read = [".env*"]is enforced by the OS sandbox, so it applies to every process the agent starts.cat,grep, Python, all of them get “permission denied.”file = ".env"tells Stashbase where the real values live. Stashbase reads the file; the agent can’t.[secrets.STRIPE_KEY]gives the agent a placeholder in its environment. The real key is only injected for requests toapi.stripe.com. If something convinces the agent to send that placeholder to another host, the proxy refuses the request, because no rule allows that credential there.egress_hostskeeps the agent from talking to anything you didn’t list.
For Codex or any other agent, replace egress_hosts with the hosts that agent needs for its API and your login method (an API key and a subscription login can use different hosts). Everything else stays the same.
Quick checklist
In order of effort:
- Deny
.env*in the agent’s own config.Read(...)deny rules in Claude Code, a deny entry in a Codex permission profile. Free, and it stops the accidental reads. - Turn on sandboxing so file restrictions apply to every shell command, not just the ones the agent’s rules can parse.
- Stop handing agents real values. Give them placeholders and let a proxy inject the real ones per request.
The first two keep the agent out of the file. The third is what lets you stop babysitting it, because even if something goes wrong, there’s nothing in the agent’s context worth stealing.
For a deeper walkthrough with a GitHub token scoped to read-only issue access, see How to secure API keys when using Claude Code.