Claude Code hooks that block destructive shell commands and protect secret files
A drop-in .claude/settings.json with two PreToolUse hooks: one denies dangerous Bash commands (recursive deletes of root or home, force pushes to main, disk wipes, piping curl to a shell), the other blocks edits to .env files, keys and lockfiles.
Automation in Security by AISkillHub Library
What it does
Permissions in Claude Code decide which tools can run; hooks let you add your own checks on the actual arguments. This listing ships two small `PreToolUse` hooks that run before every relevant tool call and block a short list of actions you almost never want an agent to take.
**Bash guard** (`guard-bash.sh`, runs before every `Bash` tool call) denies: - `rm -rf` targeting `/`, `~`, `$HOME` or `*` at the top level. - `git push --force` or `-f` (it allows `--force-with-lease`) and any push to `main` or `master`. - `git reset --hard` and `git clean -fd` style commands that destroy uncommitted work. - Disk-level commands: `mkfs`, `dd` writing to a device, `shred` on devices. - Piping a downloaded script straight into a shell (`curl ... | sh`, `wget ... | bash`). - `chmod -R 777`.
**File guard** (`protect-files.sh`, runs before `Edit` and `Write`) blocks changes to `.env` files (but not `.env.example`), private keys and certificates, `.git/` internals, and lockfiles that should only change through a package manager.
When a hook blocks an action it returns a `permissionDecision` of `deny` with a reason. Claude sees the reason and can choose another approach or ask you.
**Limits** These are guardrails against mistakes, not a security boundary. Pattern matching on shell commands can be bypassed by unusual quoting or by running a script that does the same thing. Keep normal permission settings and review commands in sensitive repositories.
Both scripts require `jq`.
How to use it
1. Install `jq` if you do not have it (`brew install jq`, `apt install jq`, etc.). 2. Save the three files below at the paths shown, in your repository. 3. Make the scripts executable: `chmod +x .claude/hooks/guard-bash.sh .claude/hooks/protect-files.sh`. 4. If you already have a `.claude/settings.json`, merge the `hooks.PreToolUse` entries into it instead of replacing the file. 5. Start Claude Code and run `/hooks` to confirm both hooks are registered. 6. Test: ask Claude to run `git push --force origin main`; it should be blocked with the reason shown. 7. Edit the pattern lists at the top of each script to match your team's needs.
Files
Save each file at the path shown, relative to your project root.