A guard hook for Claude Code: 589 stops in 41 days

A guard hook for Claude Code: 589 stops in 41 days

Want Claude Code to work without asking questions, but without deleting something important or writing an unnecessary email to a client? I gave it a guard - a hook that stopped it 589 times between September 24 and November 4. Here is what it catches, how it works and where its holes are.

Claude Code fears nothing. It's great to give it bypass permissions so it never asks about anything. But everything is fine only until it accidentally deletes something important and writes an unnecessary email to a client. You can't take an email back!

On the other hand, in normal mode you run between windows with different tasks and keep answering "yes" to every little thing.

Anthropic itself says in the documentation that bypass permissions should only be turned on in an isolated environment like a container or a virtual machine, where Claude Code can't damage anything. Mine runs on a regular computer, next to live servers and client sites. So I added an extra check, although it doesn't replace isolation.

💂 What this guard is

A hook is an action that Claude Code runs on its own at a set moment. My hook runs a script at the PreToolUse moment: the AI has already decided what to do and put the command together, but hasn't run it yet. The script looks at the command and decides whether to let it through or stop it.

The key point is that this is an ordinary script, not AI. You don't have to ask it to be careful. It holds a list of critical work written in advance. Everything else Claude Code does without asking: reads files, edits code, runs checks. But a command from the list gets rejected by the script until my message contains permission for that action.

Claude Code can't write that "yes" for itself: an "allowed" note inside the command doesn't count. And a second hook stops edits to the guard itself and to the hook settings made through the file editing tool, while in terminal commands the guard catches such edits on its own.

🤔 Why not the built-in rules

Claude Code has its own ask and deny rules, and they work even in bypass permissions. The most dangerous deletions, for example rm -rf /, it also won't run without confirmation by default. If you just need to forbid a couple of commands, start with that, no script needed. There is also auto mode, where a separate model reviews the actions.

I wanted my own list in an ordinary script, with no second model, plus three more things: a log of all stops (the numbers below come from it), parsing of the command itself (a rsync trial run is not the real thing) and permission per task: I write "yes" once, and for that folder on that server it doesn't ask again for 12 hours.

🛑 What it stopped

I count from the guard's log, September 24 to November 4, 589 stops in total:

Deploying to a server - 213
Editing a live database - 105
Recursive deletion - 81
Emails and messages - 29
Everything else - 161

Everything else includes publishing on client sites, rsync with deletion on a server, destructive commands on a server, overwriting system settings and rewriting git history.

To be honest right away: these are stops, not rescued disasters. Some of them are my own checks and false alarms. But the point isn't for it to save the day every time, it's for the decision before such actions to stay with me.

🛠 How to set one up for yourself

A warning first: one sentence only gets you a first version, not finished protection. It will catch what you named and what the script managed to recognize. You can ask Claude Code itself to build the first version:

set up a hook for yourself that stops recursive deletion, deploying to a server, editing a live database and sending emails until I personally write "yes" in the chat for that action. Trust only my message, don't count notes inside the command itself. If the hook itself crashes or hangs, stop too (onFailure: "block"). Everything else should go without questions. Then create a test folder and show me on it: without my "yes" the deletion stopped, after "yes" it went through

onFailure works starting with Claude Code v2.1.295. If your version is older, update. There is a downside too: a broken guard will stop all the commands it is set on.

Hooks live in the settings file: ~/.claude/settings.json applies to all projects, .claude/settings.json to just one. Details in the documentation: https://code.claude.com/docs/en/hooks

Then check each category separately on safe examples: a test folder, a test database, an email to yourself. The script has to forbid like this: exit code 2, or a successful exit with valid JSON (permissionDecision: "deny"). Any other error code lets the command through by default, and only with onFailure: "block" will such an error stop it too.

The minimum check is passed when it's shown in practice: a critical command stopped without your "yes", went through after "yes", an ordinary one runs without questions, and a deliberate error and a hung hook also stop a safe test command.

⚠️ Where its holes are

It's an alarm, not a lock. The documentation says it directly: if a hook hangs and times out, the call isn't blocked, and a hook that fails with another error lets the action through too. So without onFailure: "block", if the guard crashes or hangs, the command goes through. I haven't turned this setting on myself yet, so this hole is open on my side.

The guard sees only what it's set up for. Mine sits on terminal commands and file edits. If your email is connected to Claude directly, as a separate tool, such an email will pass by it - those tools have to be added separately. I already wrote about the risks of connected extensions: https://myitnotes.dev/en/post/budte-ostorozhny-s-mcp-na-desktopnom-klaude-8

It works from its own list: a command that isn't on it, it won't recognize.

The guard can also simply be turned off: the disableAllHooks setting in the settings file disables all hooks. My guard catches writes to these files, but again only in the forms described in it.

The guard itself makes mistakes too. On October 13, because of an unfinished edit in its own code, it stopped four commands in a row within seconds. Since then I edit it only in a copy: I run it over the log of old commands and then replace the live file in one go.

There were false stops as well: it took downloading a file from a server for a deployment, a rsync trial run for the real thing, and an SQL word in an ordinary search for a database edit. Now it tells them apart.

So you don't have to choose between "the AI asks about every little thing" and "the AI can do anything". Decide in advance which actions can't be undone and put a stop only on those. Backups are still a must.

Let it work all night if it wants, the last "yes" is still mine :)

I write working notes like these about AI every day in my Telegram channel: https://t.me/moi_it_zametki