Guard Claude Code against destructive shell commands with a 40-line PreToolUse hook
The author runs Claude Code unattended for overnight jobs and scheduled runs, where a single destructive command could be costly. Claude Code supports hooks, and a script that exits with code 2 blocks a tool call while feeding its stderr message back to the model. The post provides a Node.js hook that uses regex patterns to block commands such as recursive deletes of root or home, force-pushes to protected branches, git reset --hard, and unfiltered SQL deletes or drops. The hook is registered in settings.json, with notes on Windows path handling and a shell snippet for testing it with simulated tool calls. The author stresses that this is a regex-based seatbelt rather than a true sandbox, and recommends complementing it with behavioral rules in CLAUDE.md.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in