NewsCrawler.org

Story · 2 entries · –

A Claude Code hook meant to block dangerous commands let them through

危険コマンドを止める Claude Code hook を書いたのに、素通ししていた

Latest in the story ·

On Zenn, the author minewo describes a shell script that works as a PreToolUse hook[1] in Claude Code, a check that runs before a tool call. The script had a list of dangerous patterns, such as rm -rf, and detected pipe-to-shell commands, but with the data format that Claude Code actually sends, it let rm -rf / pass with exit 0.

The script read the command from the top level of the JSON, while the real data places it one level deeper, under tool_input. When extraction returned an empty string, an early exit 0 let the command pass without any check. According to the documentation the author reviewed on 10 September 2026, in PreToolUse only exit code 2 blocks a tool call[2], and other non-zero codes show a non-blocking error while the call goes ahead. A version written with jq[3] had the same gap: broken JSON returned code 5 and the tool still ran. The author's fix separates an empty command from a failed extraction and exits with 2 when extraction fails, which is called fail-closed[4].

In tests with six inputs, the original version blocked only one of the five dangerous cases, and only in a form that does not occur in practice; the fixed version blocked all five. Earlier versions in the git history also let the real data format through. The author also added an EXIT trap to stop unexpected crashes from passing, and kept a sed fallback for systems without node, now with a stated limit and a condition for use.

Open the entry

All entries of the story

Open as a page