<?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: redpa</title>
    <description>The latest articles on DEV Community by redpa (@redpa).</description>
    <link>https://dev.to/redpa</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3950206%2Fc72f1cb7-183c-4177-82fe-2c4b486e7e95.png</url>
      <title>DEV Community: redpa</title>
      <link>https://dev.to/redpa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/redpa"/>
    <language>en</language>
    <item>
      <title>Your Claude Code hooks probably fail open — here's why that's dangerous</title>
      <dc:creator>redpa</dc:creator>
      <pubDate>Mon, 25 May 2026 08:04:09 +0000</pubDate>
      <link>https://dev.to/redpa/your-claude-code-hooks-probably-fail-open-heres-why-thats-dangerous-2nfi</link>
      <guid>https://dev.to/redpa/your-claude-code-hooks-probably-fail-open-heres-why-thats-dangerous-2nfi</guid>
      <description>&lt;p&gt;A few weeks ago I added a Claude Code hook to block destructive shell commands — the usual &lt;code&gt;rm -rf&lt;/code&gt;, force-pushes, that kind of thing. Felt good. Slept better.&lt;/p&gt;

&lt;p&gt;Then one day I changed something small in the hook and made a typo. The hook threw on startup. And here's the part that got me: Claude Code just... ran the command anyway. No error, no warning, nothing. The guard had quietly stopped guarding, and I only noticed because I happened to read the logs for an unrelated reason.&lt;/p&gt;

&lt;p&gt;That's the failure mode I want to talk about, because almost every hook I've seen shared online has it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a hook actually is
&lt;/h2&gt;

&lt;p&gt;A Claude Code hook is a tiny program. It reads a JSON event on stdin, decides what to do, and writes a JSON decision on stdout. Most examples look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isDangerous&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;decision&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;block&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// swallow, don't crash&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look at that catch. If &lt;code&gt;isDangerous&lt;/code&gt; throws — bad input, a regex bug, a typo, anything — the hook prints nothing and exits 0. To Claude Code that reads as "no objection." The action goes through.&lt;/p&gt;

&lt;p&gt;So the hook fails &lt;em&gt;open&lt;/em&gt;. The one moment you most need the guard — when something unexpected happened — is exactly when it lets go.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix is boring: fail closed
&lt;/h2&gt;

&lt;p&gt;For a protection hook, an error should mean &lt;strong&gt;block&lt;/strong&gt;, not allow. If you're not sure whether something is safe, the safe default is no.&lt;/p&gt;

&lt;p&gt;I ended up writing a few base classes around this idea and put them on npm as &lt;code&gt;claude-hook-guard&lt;/code&gt;. The core rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A protection hook &lt;strong&gt;blocks&lt;/strong&gt; if your check throws or the hook itself crashes.&lt;/li&gt;
&lt;li&gt;Every run appends a line to an NDJSON audit log, so you can always answer "did the guard run, and what did it decide?"&lt;/li&gt;
&lt;li&gt;A hard 5-second timeout, so a hung hook can't wedge your session.&lt;/li&gt;
&lt;li&gt;The base owns stdout, so you can't accidentally emit two conflicting decisions.&lt;/li&gt;
&lt;li&gt;A kill-switch and a short-lived bypass token, because a guard you can't turn off in an emergency is its own hazard.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A guard then looks like this — no try/catch, no stdout plumbing, just the rule:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ProtectionBase&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PolicyViolation&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;claude-hook-guard&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProtectPaths&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;ProtectionBase&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;tool_input&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;file_path&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;^|&lt;/span&gt;&lt;span class="se"&gt;\/)\.&lt;/span&gt;&lt;span class="sr"&gt;env&lt;/span&gt;&lt;span class="se"&gt;(\.&lt;/span&gt;&lt;span class="sr"&gt;|$&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;|&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="sr"&gt;pem$|id_rsa$/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;PolicyViolation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Refusing to edit a secret file: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ProtectPaths&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;hookName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;protect-paths&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;execute&lt;/code&gt; returns, the action is allowed. If it throws — or if the file has a bug and crashes — it's blocked, and a line gets written to the audit log either way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I built it
&lt;/h2&gt;

&lt;p&gt;I'm not a tooling company. I run an English academy and I've automated a big chunk of it on Claude Code — generating exams, QA-ing content, building pages. When you let an agent run real commands against real files every day, "the guard quietly stopped working" is not a hypothetical. The two guards I bundled (&lt;code&gt;dangerous-command-guard&lt;/code&gt; and a &lt;code&gt;cloud-sync-git-guard&lt;/code&gt; that catches git repos mangled by Dropbox/OneDrive/Synology sync) both came straight out of incidents that actually bit me.&lt;/p&gt;

&lt;p&gt;If you're running hooks you'd be sad to see fail silently, it's worth checking whether yours fail open. And if you want the base classes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;npm: &lt;a href="https://www.npmjs.com/package/claude-hook-guard" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/claude-hook-guard&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;repo: &lt;a href="https://github.com/misty7kr/claude-hook-guard" rel="noopener noreferrer"&gt;https://github.com/misty7kr/claude-hook-guard&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Zero dependencies, MIT. Curious whether other people have hit the same thing.&lt;/p&gt;

</description>
      <category>claude</category>
      <category>opensource</category>
      <category>ai</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
