<?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: Keelwork</title>
    <description>The latest articles on DEV Community by Keelwork (@keelworkdev).</description>
    <link>https://dev.to/keelworkdev</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%2F4258426%2F02a4927d-c090-400c-8ba4-38d322439133.png</url>
      <title>DEV Community: Keelwork</title>
      <link>https://dev.to/keelworkdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/keelworkdev"/>
    <language>en</language>
    <item>
      <title>Five shell commands I don't let an AI coding agent run - and the small hook that stops them</title>
      <dc:creator>Keelwork</dc:creator>
      <pubDate>Sun, 22 Nov 2026 08:08:52 +0000</pubDate>
      <link>https://dev.to/keelworkdev/five-shell-commands-i-dont-let-an-ai-coding-agent-run-and-the-small-hook-that-stops-them-2750</link>
      <guid>https://dev.to/keelworkdev/five-shell-commands-i-dont-let-an-ai-coding-agent-run-and-the-small-hook-that-stops-them-2750</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclosure: this article was written with AI assistance. It was drafted by a Claude Code agent for the Keelwork account, and the account owner approved publishing it. The commands and outputs below were run and copied from real sessions; the hook messages are quoted verbatim.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Coding agents are useful because they can run commands. They are risky for the same reason. Claude Code asks before it runs most things, but after the tenth harmless &lt;code&gt;ls&lt;/code&gt; it is tempting to switch to auto-accept, and that is exactly when a single bad command can slip through.&lt;/p&gt;

&lt;p&gt;Claude Code has a feature built for this: &lt;strong&gt;hooks&lt;/strong&gt;. A &lt;code&gt;PreToolUse&lt;/code&gt; hook is a program that Claude Code runs &lt;em&gt;before&lt;/em&gt; a tool call. It receives the call as JSON on standard input. If the hook exits with code &lt;code&gt;2&lt;/code&gt;, the call is cancelled and the hook's message is shown to the agent, which then has to find another way. Exit code &lt;code&gt;0&lt;/code&gt; lets the call run.&lt;/p&gt;

&lt;p&gt;That is the whole contract. Everything below is about what to put inside it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The five commands
&lt;/h2&gt;

&lt;p&gt;These are the commands I treat as "never without a human":&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;rm -rf&lt;/code&gt; outside the project&lt;/strong&gt; (or of &lt;code&gt;~&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt;, the project root, &lt;code&gt;.git&lt;/code&gt;). One wrong variable and a cleanup step becomes a disaster.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;git push --force&lt;/code&gt;&lt;/strong&gt; and deleting &lt;code&gt;main&lt;/code&gt; on the remote. Rewrites history other people depend on.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;git reset --hard&lt;/code&gt;, &lt;code&gt;git clean -f&lt;/code&gt;, &lt;code&gt;git checkout .&lt;/code&gt;&lt;/strong&gt; - silently throws away uncommitted work, often the agent's own work from the last hour.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;curl ... | sh&lt;/code&gt;&lt;/strong&gt; - runs code nobody has read. Agents love copying install one-liners from READMEs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;sudo&lt;/code&gt;&lt;/strong&gt; - an agent should never need root to edit your project.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The minimal hook
&lt;/h2&gt;

&lt;p&gt;Here is the core idea in a few lines of Python. &lt;code&gt;.claude/settings.json&lt;/code&gt;:&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;"hooks"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;"PreToolUse"&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="nl"&gt;"matcher"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Bash"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"hooks"&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="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;"command"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"python3 \"$CLAUDE_PROJECT_DIR/.claude/hooks/guard.py\""&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="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;&lt;code&gt;.claude/hooks/guard.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;command&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&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="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"command"&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="no"&gt;RULES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"\bgit\s+push\b.*(--force|-f\b|--mirror)"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"force push rewrites shared history"&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="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"\bgit\s+reset\s+--hard\b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"git reset --hard throws away uncommitted work"&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="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"\b(curl|wget)\b[^|]*\|\s*(sh|bash|zsh)\b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"piping a download into a shell runs unread code"&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="n"&gt;r&lt;/span&gt;&lt;span class="s2"&gt;"(^|[;&amp;amp;|]\s*)sudo\b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"sudo is not needed to edit a project"&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;for&lt;/span&gt; &lt;span class="n"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="no"&gt;RULES&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;re&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"BLOCKED - {reason}. Ask the human to run it."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exit&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Test it without starting Claude Code at all - just pipe in the JSON that Claude Code would send:&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="s1"&gt;'{"tool_name":"Bash","tool_input":{"command":"git reset --hard HEAD~3"}}'&lt;/span&gt; \
  &lt;span class="o"&gt;|&lt;/span&gt; python3 .claude/hooks/guard.py&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"exit=$?"&lt;/span&gt;
&lt;span class="c"&gt;# BLOCKED - git reset --hard throws away uncommitted work. Ask the human to run it.&lt;/span&gt;
&lt;span class="c"&gt;# exit=2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is worth doing for every rule you add. A hook you have never seen block anything is a hook you are only hoping works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the minimal version is not enough
&lt;/h2&gt;

&lt;p&gt;Regular expressions over the raw command string break quickly. Once you add rules for deletes and redirects, these are the cases a fuller version has to handle. Some slip past the minimal hook (for example &lt;code&gt;git -C x push --force&lt;/code&gt; and &lt;code&gt;env sudo ls&lt;/code&gt; both exit 0), and the last two cause wrong decisions in either direction:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;git -C other/dir push --force&lt;/code&gt;&lt;/strong&gt; - git's global options come &lt;em&gt;before&lt;/em&gt; the subcommand, so &lt;code&gt;git\s+push&lt;/code&gt; never matches.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;env sudo ls&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;nice -n 10 sudo ...&lt;/code&gt;&lt;/strong&gt; - prefixes hide the real command from any rule that expects it at the start of the line (the minimal hook's &lt;code&gt;sudo&lt;/code&gt; rule lets &lt;code&gt;env sudo ls&lt;/code&gt; through: exit 0). Unanchored rules have the opposite problem: they also match the words inside quoted text or file names.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;bash -c 'rm -rf ~/old'&lt;/code&gt;&lt;/strong&gt; - the dangerous part is inside a quoted string.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;rm -rf build &amp;amp;&amp;amp; rm -rf ../shared&lt;/code&gt;&lt;/strong&gt; - the first part is fine, the second is not; you have to split at &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;, &lt;code&gt;;&lt;/code&gt;, &lt;code&gt;|&lt;/code&gt; and check each piece.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;cat &amp;gt; notes.md &amp;lt;&amp;lt;'EOF' ... a &amp;gt; b ... EOF&lt;/code&gt;&lt;/strong&gt; - the opposite problem: heredoc &lt;em&gt;text&lt;/em&gt; looks like a redirect and causes false blocks, which trains people to turn the hook off.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Paths&lt;/strong&gt;: &lt;code&gt;rm -rf ./build&lt;/code&gt; is fine, &lt;code&gt;rm -rf ./build/../../&lt;/code&gt; is not. You have to resolve the path against the project folder, including &lt;code&gt;~&lt;/code&gt; and symlinks, before deciding.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The robust approach is to tokenise the command like a shell does (Python's &lt;code&gt;shlex&lt;/code&gt; gets you most of the way), split it into simple commands, strip known prefixes and git global options, expand &lt;code&gt;~&lt;/code&gt; and wildcards, resolve paths, and only then apply rules. The hook used in the next section is about 680 lines of Python across two files, plus a 21-case test file that covers the cases above (each one is a single example, not proof that every variant is caught).&lt;/p&gt;

&lt;h2&gt;
  
  
  What it looks like in a real session
&lt;/h2&gt;

&lt;p&gt;I installed a hook built this way as a plugin (&lt;code&gt;claude -p --plugin-dir ...&lt;/code&gt;, Claude Code 2.1.330, Haiku model) and asked the agent to run &lt;code&gt;git reset --hard HEAD&lt;/code&gt; in a repository with one uncommitted line. The call never ran. Claude Code returned the hook's message to the agent:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;BLOCKED - &lt;code&gt;git reset --hard&lt;/code&gt; throws away uncommitted work. Commit or stash first, or run it yourself.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The agent reported that the command was blocked, quoted that message, and did not try another way to discard the change; the uncommitted line was still in the file afterwards. That matters: a good block message tells the agent what to do instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limits you should know
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A hook reads the &lt;strong&gt;command text&lt;/strong&gt;. A Python script that the agent writes and then runs can still delete whatever it likes; the hook only sees &lt;code&gt;python3 script.py&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;An agent can edit the hook file itself unless you also block writes to &lt;code&gt;.claude/hooks/&lt;/code&gt; and &lt;code&gt;.claude/settings.json&lt;/code&gt;. Commit &lt;code&gt;.claude/&lt;/code&gt; to git so any change shows up in &lt;code&gt;git status&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Hooks are a safety net for accidents, not a sandbox against an adversary. Backups and version control still do the heavy lifting.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Source code
&lt;/h2&gt;

&lt;p&gt;The free, MIT-licensed hook I used above - with the parsing described in this article and a 21-case self-test - is on GitHub: &lt;strong&gt;&lt;a href="https://github.com/keelwork-dev/claude-code-guardrails-free" rel="noopener noreferrer"&gt;https://github.com/keelwork-dev/claude-code-guardrails-free&lt;/a&gt;&lt;/strong&gt; (installable as a Claude Code plugin).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Independent project; not affiliated with or endorsed by Anthropic. "Claude" and "Claude Code" are trademarks of Anthropic.&lt;/em&gt;&lt;/p&gt;

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