Skip to content
Docs

Dangerous Command Blocker

The dangerous command blocker is your safety net against catastrophic shell commands. It intercepts every Bash command before execution and blocks patterns known to cause irreversible damage — things like rm -rf /, force-pushing to main, or writing to system directories.

The plugin registers a PreToolUse hook that fires before every Bash tool call. The block-dangerous.py script checks the command against a set of regex patterns. If a match is found, the command is blocked with exit code 2 and a clear explanation of why it was stopped.

Commands that don’t match any dangerous pattern pass through untouched with zero overhead.

Commands that could wipe out large portions of the filesystem:

PatternExampleWhy It’s Blocked
rm -rf /rm -rf /Deletes the entire filesystem
rm -rf ~rm -rf ~/Deletes the user’s home directory
rm -rf ../rm -rf ../../Escapes up the directory tree
sudo rmsudo rm -rf /var/logPrivileged deletion bypasses permissions
find -exec rmfind . -exec rm {} \;Recursive deletion via find
find -deletefind /tmp -deleteBulk deletion via find

Commands that destroy or overwrite git history in ways that are difficult to recover:

PatternExampleWhy It’s Blocked
Force push to main/mastergit push --force origin mainOverwrites shared history
Bare force pushgit push -fForce push without specifying target
Hard reset to remotegit reset --hard origin/mainDiscards all local work
git clean -fgit clean -fdPermanently removes untracked files

Commands that modify critical system directories or create security vulnerabilities:

PatternExampleWhy It’s Blocked
chmod 777chmod 777 app.pyCreates world-writable files
chmod -R 777chmod -R 777 /var/wwwRecursively weakens permissions
Write to system dirs> /usr/local/bin/scriptModifies system binaries
Write to /etc/echo "config" > /etc/hostsModifies system configuration
Write to /bin/ or /sbin/> /bin/scriptModifies core system binaries

Commands that could destroy disk contents:

PatternExampleWhy It’s Blocked
mkfs.*mkfs.ext4 /dev/sda1Formats a disk partition
dd of=/dev/dd if=/dev/zero of=/dev/sdaOverwrites a device

Commands that could break container isolation:

PatternExampleWhy It’s Blocked
docker run --privilegeddocker run --privileged ubuntuAllows container escape
Mount host rootdocker run -v /:/host ubuntuExposes host filesystem
Destructive docker opsdocker rm container_idStops, removes, or kills containers and images (docker rmi)

When the blocker catches a dangerous command, you see a clear message explaining what was blocked and why:

Blocked: force push to main/master destroys history

The command never executes. Claude receives the block message and can suggest a safer alternative.

The blocker follows a “fail closed” principle for its own errors:

  • If it can’t parse the hook input JSON, it blocks the command (exit code 2) rather than allowing something it couldn’t inspect.
  • If an unexpected error occurs during pattern matching, it logs the error but allows the command through to avoid blocking legitimate work on a hook bug.

The blocker is designed to catch accidental destructive commands, not to prevent intentional operations. If you genuinely need to run a blocked command, you can use the Claude Code permission prompt to explicitly approve it. The blocker respects user intent — it’s a guardrail, not a cage.

ScriptHookMatcherPurpose
block-dangerous.pyPreToolUseBashInspects and blocks dangerous shell commands