Skip to content

Latest commit

 

History

History

block-dangerous-commands

Folders and files

README.md

block-dangerous-commands

PreToolUse guard for Bash: blocks dangerous shell commands (rm -rf ~, fork bombs, curl|sh, force push to main) before they run.

A PreToolUse hook fires before every Bash call and tests the command against a curated pattern list: rm targeting home (~, $HOME, ${HOME}, /home/<user>, /Users/<user>), root or system directories, the cwd or its parent (..), dd to disk devices (including macOS /dev/disk*), mkfs, fork bombs, curl/wget piped to any shell or run through bash <(curl ...) / sh -c "$(curl ...)", force push to main/master (flag before or after the branch, or a +main refspec), git reset --hard, git clean -f, chmod 777, docker volume deletion, and more. On a match it denies the call and shows the pattern id and reason, so Claude knows exactly why. Three safety levels control how much is blocked, and per-level ask mode turns a hard deny into a prompt you approve or reject. Every block/ask is logged to ~/.claude/hooks-logs/<date>.jsonl.

Install

/plugin marketplace add karanb192/claude-code-hooks   # once per machine
/plugin install block-dangerous-commands@claude-code-hooks

Restart Claude Code, done. (Or from a shell: claude plugin install block-dangerous-commands@claude-code-hooks.)

Codex CLI

codex plugin marketplace add karanb192/claude-code-hooks
codex plugin add block-dangerous-commands@claude-code-hooks

Open /hooks in Codex and approve this plugin. Re-approve after every update; Codex skips unapproved or changed hooks. Do not install it alongside guard-pack, which includes this guard.

This entry point checks Bash and native apply_patch calls. Patch files are checked individually; unreadable patches are blocked. Shell patches must use a plain command with a quoted heredoc. Literal searches and commit messages mentioning apply_patch pass. See the shared adapter's limits, including shell sessions and dynamically constructed commands.

Export configuration variables before launching Codex. An ask-mode match becomes a block because Codex hooks cannot ask. Logs stay in ~/.claude/hooks-logs/ with host: "codex", including on Codex-only machines.

What it does

Event Runs What happens
PreToolUse (Bash) sync (must finish before the command runs) Tests the command against every pattern at or below the configured safety level; on a match returns permissionDecision: "deny" (or "ask" in ask mode) with the pattern id and reason, and logs the event. Safe commands pass through untouched.

Safety levels

Level What's blocked Use case
critical Catastrophic only (rm -rf ~, fork bombs, dd to disk) Maximum flexibility
high + Risky (force push main, git reset --hard, curl|sh) Recommended, default
strict + Cautionary (any force push, sudo rm, docker prune) Maximum safety

Set the level via HOOK_SAFETY_LEVEL (see below). Don't edit the installed script: plugin updates overwrite it.

Configuration

All optional, set via environment variables:

Variable Default Purpose
HOOK_SAFETY_LEVEL high Safety level: critical, high, or strict. Invalid values fall back to high.
HOOK_ASK_CRITICAL unset (deny) The literal string true makes critical-level matches prompt instead of deny.
HOOK_ASK_HIGH unset (deny) Same, for high-level matches.
HOOK_ASK_STRICT unset (deny) Same, for strict-level matches.
HOOK_FAIL_OPEN unset (deny) The literal string true lets the call through when the guard's own check throws, instead of denying it. See When the guard fails.

Ask mode is strictly opt-in and per level: only the literal string true enables it ("1" does not), and enabling it for one level never softens another. A common setup: keep critical on deny, set HOOK_ASK_STRICT=true so cautionary patterns prompt instead of blocking.

Pair it with protect-secrets for coverage of secrets exposure (cat .env, printenv, ~/.ssh reads): that lives in a separate hook by design.

When the guard fails

From 1.2.0, a guard that cannot finish its check refuses the call instead of allowing it. A check that throws is denied with a [guard-error] reason. A timeout (10 s) or a missing node is blocked by "onFailure": "block" on Claude Code v2.1.295 or later; older versions ignore that key and let the call run. On Codex a missing node is blocked by the command's || exit 2, but a timeout still lets the call run, because Codex has no setting for it. Set HOOK_FAIL_OPEN=true to let the call through when the check throws; When a guard fails covers the rest of the way back.

Data & privacy

Blocked/asked commands are logged (with pattern id, level, decision, and session metadata) to ~/.claude/hooks-logs/<date>.jsonl. It makes no network calls (the script only uses fs and path), so everything stays on your local machine.

Uninstall

/plugin uninstall block-dangerous-commands@claude-code-hooks

Part of claude-code-hooks.