Skip to content

hookSecurity

Block Dangerous Commands

A Claude Code hook that refuses rm -rf on the root or home folder, force pushes to main or master, and DROP DATABASE before the shell runs.

npx workspacetemplates add block-dangerous-commands --tool claude-code

Writes 1 file to .claude/

.claude/settings.jsonmerge into your file16 lines · 919 BFile
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "node -e \"let d='';process.stdin.on('data',c=>d+=c).on('end',()=>{let j={};try{j=JSON.parse(d)}catch(e){process.exit(0)}const i=j.tool_input||{};const c=String(i.command||'')+' ';const bad=[[/rm[ ]+(-[a-zA-Z-]+[ ]+)+(\\/|~[/]?)[*]?[ ;&|]/.test(c)&&/rm[ ]+.*-[a-zA-Z]*[rR]/.test(c),'recursive delete of / or ~'],[/git[ ]+push[ ]/.test(c)&&/(--force|[ ]-f[ ])/.test(c)&&/(main|master)[ ]/.test(c),'force push to main'],[/git[ ]+push[ ].*[+](main|master)[ ]/.test(c),'force push to main'],[/drop[ ]+(database|schema)/i.test(c),'DROP DATABASE']];const h=bad.find(b=>b[0]);if(h){console.error('Blocked: '+h[1]+'. Ask the user to run this themselves if it is really intended.');process.exit(2)}})\"",
            "timeout": 10
          }
        ]
      }
    ]
  }
}

What it does

Most shell mistakes are cheap, but a few are not. This PreToolUse hook reads each Bash command and stops the ones that wipe a home folder, rewrite the main branch or drop a database.

The check is a plain pattern match on the command text. It is a guard rail for slips, not a sandbox, so keep your normal permission rules too.

Example

you › Clean up everything with rm -rf ~/ and start fresh.

agent › Command blocked: recursive delete of / or ~. The agent asks which project folder you actually want removed.

Questions

Does it stop a force push to a feature branch?
No. Only pushes that name main or master with a force flag or a plus refspec are refused.
Can the agent work around it?
A determined command built from variables could slip past pattern matching. Use it with sandboxing for real protection.
  • Block Chmod 777

    HookSecurity

    Free
  • Block Curl Pipe Shell

    HookSecurity

    Free
  • Block Env in Git Add

    HookSecurity

    Free
  • Block Force Push

    HookSecurity

    Free