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/.

