I had Read(./.env) in my Claude Code deny list. The agent still printed my database password.
It's not just me. In the last month alone, people have opened issues on the Claude Code repo saying it tries to read .env files despite instructions not to, that deny rules are bypassed by command substitution, subshells, source and interpreters, and that a Read deny rule stops working when the command contains $? or $$. Cursor's forum has the same threads about .cursorignore.
I spent the last few weeks building a hook that sits between the agent and the shell, and testing what actually reaches it. Here's what I found.
1. Deny rules match strings. The shell doesn't care.
A deny rule says "don't run cat .env". The agent doesn't have to run cat .env. Every one of these prints the same file:
echo "$(cat .env)"
( cat .env )
source .env && echo $DB_PASSWORD
python3 -c "print(open('.env').read())"
cat .env; echo $?
You can keep adding patterns, but you're playing whack-a-mole with a programming language.
2. Hook paths arrive absolute
Claude Code's PreToolUse hook gets the tool call as JSON on stdin before it runs. When you ask the agent to read .env, the hook doesn't see .env. It sees this:
{
"hook_event_name": "PreToolUse",
"tool_name": "Read",
"tool_input": { "file_path": "/Users/you/project/.env" }
}
Any check that compares against the relative name misses it. Match on the file name at the end of the path, or resolve both sides.
3. A hook that crashes lets the read through, silently
This was the one that surprised me. If your hook exits with code 2, Claude Code blocks the call. If it exits with code 1, because of a bug, a panic or a missing dependency, the call goes through. In my test the model saw the whole fake .env, and nothing told me the hook had failed.
So a security hook has to fail closed on its own: catch its own errors and answer "deny" for anything that looks like a secret file. Newer Claude Code versions also accept "onFailure": "block" on a hook entry, so a missing or hung binary blocks instead of allowing.
4. Denying isn't always the best answer
If you deny cat .env, the agent often just tries another way, and you're back to point 1. What worked better was letting the command run and masking its output. A PreToolUse hook can return allow with an updatedInput that pipes the command through a redactor, so the agent gets:
STRIPE_SECRET_KEY=[REDACTED:env-value:b877a4c2b420]
DB_PASSWORD=[REDACTED:env-value:7de279f08763]
It can see which variables exist and keep working. It never sees the values.
5. What it already read stays on disk
Whatever the agent saw is written to its session transcript under ~/.claude/projects/, in plain text. Approvals you clicked "don't ask again" on are saved in .claude/settings.local.json, sometimes with the key inside the command. Apiiro reported 2,539 secrets leaked through public copies of that file.
Even if you fix the reads today, check those files and rotate anything you find.
6. Hooks are cheap, but not free
A do-nothing hook cost about 10–15 ms per tool call in my measurements, and most of that is Claude Code's own overhead for running a hook (process start is about 3 ms). Fine for a security check, but not if your hook does a network call each time.
What I built
I turned all of this into DotShield, a small Go binary that installs as Claude Code and Cursor hooks. Here's what it does with the tricks from point 1:
$ dotshield guard check --command 'echo "$(cat .env)"'
rewrite (secret-file-read): the command runs with its output masked
$ dotshield guard check --command 'source .env && echo $DB_PASSWORD'
rewrite (secret-env-var): the command runs with its output masked
$ dotshield guard check --read /Users/you/project/.env
deny (secret-file-read)
It also has an offline scanner for the secrets already sitting in transcripts, agent settings, .env files and git history:
npx dotshield@beta scan # never prints values, sends nothing anywhere
dotshield guard install # starts in watch mode: logs, blocks nothing
Being upfront: it's free to use but not open source, it's in beta, and it's a one-person project, not affiliated with Anthropic or Cursor. If you try it and it blocks something it shouldn't, or misses something it should catch, I'd really like to hear about it.
Whatever you use, the takeaways hold: don't trust deny rules alone, make your hooks fail closed, and go look at what's already in ~/.claude/projects/.
Top comments (5)
Masking beats denying, agreed, the agent keeps working. One question on the redacted form: if the suffix after env-value: is a plain hash of the secret, a short password can be brute-forced from the transcript. Is it keyed (an HMAC with a per-machine key), or just a stable id?
Good catch, and you’re right. Today it’s a stable id: a truncated, unkeyed SHA-256 of the value. It never leaves the machine (the events DotShield sends carry no value fingerprints), but anyone who can read the transcript could brute-force a short password from it. I’m changing it to an HMAC-SHA256 under a random per-machine key kept in ~/.dotshield, readable only by you. It will stay stable on your machine, so allowlists and “found in 3 places” still work, but it can’t be checked without that key. It’ll ship in the next beta, and I’ll update this thread when it does. Thanks for pushing on it.
On my side, Tokenade sits between the tool output and the model, so this gap is one I look at: a deny rule matches the tool call, and a shell that runs cat on the file never triggers it. Only a check on the bytes that come back sees the file.
Agreed, a deny rule on the tool call alone misses cat in a shell. That’s why DotShield doesn’t stop there. For Bash it rewrites the command so its output goes through dotshield redact before the model sees it, so cat .env, grep -r KEY . or python -c "print(open('.env').read())" come back with the values masked in the bytes themselves.
The honest limit is what triggers the rewrite. The guard decides from the command, so a script that reads .env itself and prints it (say npm run debug-config) isn’t routed through the masker. Sitting on the output stream the way Tokenade does closes that by design. Checking every tool result after the fact is the next thing we’re looking at for exactly that reason. Curious how you handle latency on large outputs. 👀
That shell bypass is why I built my envpeek.