# How to stop Claude Code from reading secrets and .env files

DevFeed: [How to stop Claude Code from reading secrets and .env files](<https://devfeed.tech/articles/how-to-stop-claude-code-from-reading-secrets-and-env-files-119445.md>)

Original publisher: [Read original article](<https://nirmata.com/2026/10/27/how-to-stop-claude-code-from-reading-secrets-and-env-files/>)

Author: Ritesh Patel

Published: 2026-10-27T19:00:00Z

Content type: tutorial

Language: en

Sources: [Nirmata](<https://devfeed.tech/sources/nirmata.md>)

Topics: [Claude Code](<https://devfeed.tech/topics/claude-code.md>), [code security](<https://devfeed.tech/topics/code-security.md>), [.env](<https://devfeed.tech/topics/dotenv.md>)

Tags: [ai](<https://devfeed.tech/tags/ai.md>), [ai-agents](<https://devfeed.tech/tags/ai-agents.md>), [ai-coding-agents](<https://devfeed.tech/tags/ai-coding-agents.md>), [ai-governance](<https://devfeed.tech/tags/ai-governance.md>), [bash](<https://devfeed.tech/tags/bash.md>), [block](<https://devfeed.tech/tags/block.md>), [cat](<https://devfeed.tech/tags/cat.md>), [chmod](<https://devfeed.tech/tags/chmod.md>), [claude-code](<https://devfeed.tech/tags/claude-code.md>), [cli](<https://devfeed.tech/tags/cli.md>), [commands](<https://devfeed.tech/tags/commands.md>), [credentials](<https://devfeed.tech/tags/credentials.md>), [environment](<https://devfeed.tech/tags/environment.md>), [files](<https://devfeed.tech/tags/files.md>), [filesystem](<https://devfeed.tech/tags/filesystem.md>), [layer](<https://devfeed.tech/tags/layer.md>), [pretooluse-hook](<https://devfeed.tech/tags/pretooluse-hook.md>), [reading](<https://devfeed.tech/tags/reading.md>), [rules](<https://devfeed.tech/tags/rules.md>), [sandboxing](<https://devfeed.tech/tags/sandboxing.md>), [secret](<https://devfeed.tech/tags/secret.md>), [secrets](<https://devfeed.tech/tags/secrets.md>), [security](<https://devfeed.tech/tags/security.md>), [stop](<https://devfeed.tech/tags/stop.md>)

## AI overview

Use `Read` deny rules in project settings as the main control: they block the Read tool, recognized Bash file commands such as `cat`, and writes to denied paths, but miss indirect reads such as `grep -r` and scripts opening files themselves. A `PreToolUse` hook can block matching Read calls before permission checks; it needs separate matchers for other tools. Enable sandboxing to restrict shell commands and subprocesses, adding project paths to `sandbox.filesystem.denyRead` when needed. Sandboxing is off by default, excludes the Read tool, MCP servers and hooks, and is unavailable for native Windows commands. The excerpt recommends keeping production credentials outside the project and checking loaded settings with `/status` and `claude doctor`.

## Source excerpt

Block Claude Code from reading .env and secret files with Read deny rules, a PreToolUse hook and sandboxing, and see which reads each layer misses, and why.