<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Murshid Ansari</title>
    <description>The latest articles on DEV Community by Murshid Ansari (@murshid_ansari_a3ead0a95f).</description>
    <link>https://dev.to/murshid_ansari_a3ead0a95f</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3572334%2F92dc3d36-054b-4084-83cf-044a0c4ef5d4.png</url>
      <title>DEV Community: Murshid Ansari</title>
      <link>https://dev.to/murshid_ansari_a3ead0a95f</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/murshid_ansari_a3ead0a95f"/>
    <language>en</language>
    <item>
      <title>Claude Code read my .env even with deny rules. Here's why.</title>
      <dc:creator>Murshid Ansari</dc:creator>
      <pubDate>Tue, 17 Nov 2026 10:00:50 +0000</pubDate>
      <link>https://dev.to/murshid_ansari_a3ead0a95f/claude-code-read-my-env-even-with-deny-rules-heres-why-52m4</link>
      <guid>https://dev.to/murshid_ansari_a3ead0a95f/claude-code-read-my-env-even-with-deny-rules-heres-why-52m4</guid>
      <description>&lt;p&gt;I had &lt;code&gt;Read(./.env)&lt;/code&gt; in my Claude Code deny list. The agent still printed my database password.&lt;/p&gt;

&lt;p&gt;It's not just me. In the last month alone, people have opened issues on the Claude Code repo saying &lt;a href="https://github.com/anthropics/claude-code/issues/107031" rel="noopener noreferrer"&gt;it tries to read .env files despite instructions not to&lt;/a&gt;, that &lt;a href="https://github.com/anthropics/claude-code/issues/103241" rel="noopener noreferrer"&gt;deny rules are bypassed by command substitution, subshells, &lt;code&gt;source&lt;/code&gt; and interpreters&lt;/a&gt;, and that &lt;a href="https://github.com/anthropics/claude-code/issues/107987" rel="noopener noreferrer"&gt;a Read deny rule stops working when the command contains &lt;code&gt;$?&lt;/code&gt; or &lt;code&gt;$$&lt;/code&gt;&lt;/a&gt;. Cursor's forum has the same threads about &lt;code&gt;.cursorignore&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Deny rules match strings. The shell doesn't care.
&lt;/h2&gt;

&lt;p&gt;A deny rule says "don't run &lt;code&gt;cat .env&lt;/code&gt;". The agent doesn't have to run &lt;code&gt;cat .env&lt;/code&gt;. Every one of these prints the same file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"$(cat .env)"&lt;/span&gt;
&lt;span class="o"&gt;(&lt;/span&gt; cat .env &lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;source&lt;/span&gt; .env &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$DB_PASSWORD&lt;/span&gt;
python3 -c &lt;span class="s2"&gt;"print(open('.env').read())"&lt;/span&gt;
cat .env&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can keep adding patterns, but you're playing whack-a-mole with a programming language.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Hook paths arrive absolute
&lt;/h2&gt;

&lt;p&gt;Claude Code's &lt;code&gt;PreToolUse&lt;/code&gt; hook gets the tool call as JSON on stdin before it runs. When you ask the agent to read &lt;code&gt;.env&lt;/code&gt;, the hook doesn't see &lt;code&gt;.env&lt;/code&gt;. It sees this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"hook_event_name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"PreToolUse"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"tool_name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Read"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"tool_input"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"file_path"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"/Users/you/project/.env"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. A hook that crashes lets the read through, silently
&lt;/h2&gt;

&lt;p&gt;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 &lt;strong&gt;goes through&lt;/strong&gt;. In my test the model saw the whole fake &lt;code&gt;.env&lt;/code&gt;, and nothing told me the hook had failed.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;"onFailure": "block"&lt;/code&gt; on a hook entry, so a missing or hung binary blocks instead of allowing.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Denying isn't always the best answer
&lt;/h2&gt;

&lt;p&gt;If you deny &lt;code&gt;cat .env&lt;/code&gt;, 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 &lt;code&gt;PreToolUse&lt;/code&gt; hook can return &lt;code&gt;allow&lt;/code&gt; with an &lt;code&gt;updatedInput&lt;/code&gt; that pipes the command through a redactor, so the agent gets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight conf"&gt;&lt;code&gt;&lt;span class="na"&gt;STRIPE_SECRET_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;REDACTED&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;b877a4c2b420&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="na"&gt;DB_PASSWORD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;REDACTED&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;7de279f08763&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It can see which variables exist and keep working. It never sees the values.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. What it already read stays on disk
&lt;/h2&gt;

&lt;p&gt;Whatever the agent saw is written to its session transcript under &lt;code&gt;~/.claude/projects/&lt;/code&gt;, in plain text. Approvals you clicked "don't ask again" on are saved in &lt;code&gt;.claude/settings.local.json&lt;/code&gt;, sometimes with the key inside the command. Apiiro &lt;a href="https://letsdatascience.com/news/apiiro-finds-claude-code-approvals-leaking-2539-secrets-on-g-e4f20dbc" rel="noopener noreferrer"&gt;reported 2,539 secrets&lt;/a&gt; leaked through public copies of that file.&lt;/p&gt;

&lt;p&gt;Even if you fix the reads today, check those files and rotate anything you find.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Hooks are cheap, but not free
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I built
&lt;/h2&gt;

&lt;p&gt;I turned all of this into &lt;a href="https://www.npmjs.com/package/dotshield" rel="noopener noreferrer"&gt;DotShield&lt;/a&gt;, a small Go binary that installs as Claude Code and Cursor hooks. Here's what it does with the tricks from point 1:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ dotshield guard check --command 'echo "$(cat .env)"'
rewrite (secret-file-read): the command runs with its output masked

$ dotshield guard check --command 'source .env &amp;amp;&amp;amp; 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)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It also has an offline scanner for the secrets already sitting in transcripts, agent settings, &lt;code&gt;.env&lt;/code&gt; files and git history:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx dotshield@beta scan        &lt;span class="c"&gt;# never prints values, sends nothing anywhere&lt;/span&gt;
dotshield guard install        &lt;span class="c"&gt;# starts in watch mode: logs, blocks nothing&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;~/.claude/projects/&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>claudecode</category>
      <category>security</category>
      <category>ai</category>
      <category>devtools</category>
    </item>
  </channel>
</rss>
