AIToday
AI Coding AssistantsZenn AI/MLPublished: Oct 22, 2026, 13:00 JST

Claude Code hooks fail open on typos; onFailure blocks

Send on LINE
Claude Code hooks fail open on typos; onFailure blocks

3 Key Points

  1. What happened

    With gaurd.mjs typo'd in settings.json, the guard hook returned status 127 and the rm command deleted the fake key; adding "onFailure": "block" kept it.

  2. Why it matters

    A guard hook that never runs leaves only the appearance of protection, so the author adds a ~30-line check-hooks.mjs script that names the bad path.

Not sure about something? Ask the AI

Questions and answers are published on this page.

Summaries like this, in your inbox every morning.

Context & Analysis

The author follows up on earlier work using a Claude Code hook to stop the tool from deleting .env or key files. That setup had a blind spot: the official hooks reference states that a path typo in settings.json means the hook never runs at all, and a broken guard hook lets everything through. In the test, Claude Code passed the command as JSON to a guard script, which exits with code 2 to block and 0 to allow; anything else counts as a non-blocking failure. A one-letter typo in the registered path produced exit status 127 (file not found), so the rm command went ahead.

The fix is a single setting, "onFailure": "block", which flips the guard from failing open to failing closed. The trade-off is deliberate: any hook failure, including a crash or timeout, blocks all Bash use until the path is corrected in settings.json and Claude Code is reopened. Because of that, the author recommends applying it only to guard hooks protecting something you would rather block than lose.

The companion ~30-line checker scans the registered commands, expands $CLAUDE_PROJECT_DIR and ~, and lists any hook file that does not exist, with exit code 1. It is a quick way to catch a misspelled path before relying on the guard, though it only tests existence, not whether the script's blocking condition is correct.

FAQ
What version of Claude Code do I need for onFailure?
The author says it works on Claude Code 2.1.295 and later, and tested on 2.1.307. The hook behavior can change between versions, so check with claude --version.
What is the downside of setting "onFailure": "block"?
While the hook cannot run, all Bash commands are blocked, including harmless ones like ls. You have to fix the path in settings.json by hand and reopen Claude Code.
What does the check-hooks.mjs script actually check?
It only confirms that registered hook files exist. It does not catch scripts that crash mid-run or wrong blocking conditions, and it skips script types other than .mjs, .js, .sh and .py.
Send on LINE

AI news that matters for your work, delivered every morning.

Pick your industry and the AI tools you use, and get news related to your work every day.

Free · 30 seconds with Google · unsubscribe anytimeWhat is AIToday? →

Ask AI

Ask AI anything about this article. The AI reads this article, earlier AIToday articles, and Wikipedia, and cites its sources. Q&As are published on this page for other readers too.

Questions and answers are published on this page.

Related Articles

Next articleOpenAI AI disabled its own monitors; GPT-6 Astra denied it