
Most apps keep their secret keys, such as a Stripe or Supabase key, in a file called .env. Claude doesn't need to see a key to build with it: it writes code that picks the key up while the app runs. But if you ask about the file, Claude opens it, and your keys end up in the conversation.
A deny rule stops that. In your project folder, make a file at .claude/settings.json with the text below. Or paste the text to Claude and ask it to create that file with exactly this in it. Claude normally asks before it writes in the .claude folder, so say yes when it does.
- The first rule blocks
.env. - The second rule blocks every file whose name starts with
.env., such as.env.local. - The third rule lets Claude still read
.env.example, a copy that lists the names of your keys with no values, so it knows which keys your app uses.
We tried it on a small shop project with made-up keys. Without the rule, "What are the values in my .env file?" got both keys printed back. With these three rules, Claude was refused, said it couldn't read the file, and offered to show .env.example instead. Asked to finish the checkout with the Stripe key, it read .env.example and wrote code that fetches the key when the app runs, with no key written into the code.
{
"permissions": {
"deny": [
"Read(./.env)",
"Read(./.env.*)",
"Read(!.env.example)"
]
}
}Why it works
Whatever Claude reads becomes part of the conversation, and a key there can be repeated in a reply, a summary or a file it writes. If Claude never opens the file, it has nothing to repeat. It loses nothing either, because building with a key only needs the key's name.
Watch out
This is a rule, not a lock. The docs say it stops Claude's own file tools and the common commands that open a file by name (we tried cat, and it was refused). A command that searches the whole folder can still see inside: in our test, a folder-wide search printed the key. The docs say the same of a script that opens files itself, such as a Python or Node script (we didn't test that). For a fence that holds against every command, Claude Code has a sandbox (see the sandboxing page; we didn't test that part here). In our test (Claude Code 2.1.325), the rules also blocked a key file in a subfolder, apps/web/.env, while apps/web/.env.example stayed readable. We didn't test a folder above the one you start Claude in or another project, and the docs say this kind of rule doesn't reach them, so add the file to each project. When we asked Claude to write the rules itself, it came up with 17 lines that still missed some file names, so paste the three rules above instead.
Needs Claude Code 2.1.325 or later