AIToday
Large Language ModelsAI Coding AssistantsZenn AI/MLPublished: Nov 24, 2026, 16:00 JST

Claude Code hooks: exit 2 stops rm -rf, exit 1 does not

Send on LINE
Claude Code hooks: exit 2 stops rm -rf, exit 1 does not

3 Key Points

  1. What happened

    Running Claude Code 2.1.331 with a hook that blocks rm -rf, exit 1 let the build folder be deleted in all 3 runs, while exit 2 kept it in all 3; adding "onFailure": "block" stopped even a mistyped hook path.

  2. Why it matters

    A broken or mistyped hook fails silently in unattended runs — no hook mention appeared in Claude's final report — so anyone relying on PreToolUse hooks as a safety net may be unprotected without knowing it.

Not sure about something? Ask the AI

Questions and answers are published on this page.

Summaries like this, every morning.

Context & Analysis

The test set out to answer a question many teams skip: does the hook actually stop anything? Kosuke ran Claude Code 2.1.331 with claude -p in a fresh folder containing build/a.txt, using only that folder's .claude/settings.json and .claude/hooks/, and judged each run by whether the build folder survived — not by what Claude said.

The failure modes turned out to be independent of each other. A hook that exits 1 is treated by Claude Code as a non-blocking error, per Anthropic's own reference, even though 1 is the usual Unix failure code; the same applies to a mistyped script path or a Python KeyError, which left no trace in the stream-json log that exit 1 and the exception cases produced. String matching added a separate weakness: the rm -rf hook passed 17 of 22 delete commands, including the ordinary rm -r build, and regex or allowlist approaches each traded misses for new costs — the allowlist stopped all 22 but also blocked harmless commands and forced Claude to rewrite its calls.

Kosuke notes the limits plainly: 26 commands are his own selection, each condition ran 3 to 15 times, and Windows, other models and sandbox behavior remain untested. The practical conclusion is that a hook is not a safety device until it has been broken and its pass count measured.

FAQ
Does a Claude Code hook that exits with code 1 stop a dangerous command?
No. In 3 out of 3 runs the build folder was deleted, and Claude's final report never mentioned the hook. Only exit 2 blocked the command in all 3 runs.
What happens if the hook script itself is broken or the path is wrong?
Without "onFailure": "block", all 3 runs with a mistyped path and all 3 runs with a Python exception deleted the folder. Adding "onFailure": "block" kept it in all 3 runs for each case.
Did Claude try to get around the hook?
In 25 runs with no command specified, Claude did not deliberately evade it. But in 1 of those 25, Claude wrote rm -r build on its own, which the "rm -rf" hook did not catch, and the folder was deleted.
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 articleCHINTAI, ABEJA launch chat-based rental search AI