Skip to content

hookSecurity

Block Secrets in Prompts

A Claude Code hook that blocks a prompt before it is sent when it holds an AWS, GitHub, Anthropic, OpenAI, Stripe or Slack key, or a private key block.

npx workspacetemplates add block-secrets-in-prompts --tool claude-code

Writes 1 file to .claude/

.claude/settings.jsonmerge into your file15 lines · 1.1 KBFile
{
  "hooks": {
    "UserPromptSubmit": [
      {
        "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 q=String.fromCharCode(34),sq=String.fromCharCode(39),bs=String.fromCharCode(92),nl=String.fromCharCode(10);const t=String(j.prompt||'');const pats=[['an AWS access key',/AKIA[0-9A-Z]{16}/],['a GitHub token',/(gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{30,})/],['an Anthropic or OpenAI key',/sk-(ant-)?[A-Za-z0-9_-]{24,}/],['a Stripe live key',/[rs]k_live_[A-Za-z0-9]{16,}/],['a Slack token',/xox[abprs]-[A-Za-z0-9-]{10,}/],['a Google API key',/AIza[0-9A-Za-z_-]{35}/],['a private key',/-----BEGIN [A-Z ]*PRIVATE KEY-----/]];const h=pats.find(p=>p[1].test(t));if(h){console.error('Prompt not sent: it contains '+h[0]+'. Remove the secret, refer to it by variable name instead, and send the prompt again.');process.exit(2)}})\"",
            "timeout": 10
          }
        ]
      }
    ]
  }
}

What it does

Pasting a stack trace often drags a live token along with it. This UserPromptSubmit hook scans your message for well-known key shapes and stops it before the model sees anything.

The warning names the kind of secret and never repeats the value. Remove the key, mention it by variable name, and send the prompt again. Everything is checked locally with regular expressions.

Example

you › Why does this fail? AWS_ACCESS_KEY_ID is set to a key beginning with AKIA followed by sixteen characters.

agent › The prompt is stopped with a note that it contains an AWS access key. Nothing is sent until you remove it.

Questions

Does it find every kind of secret?
No. It knows common token formats and PEM private key headers. Passwords, database URLs and custom tokens are not recognized.
Is my prompt sent anywhere to be checked?
No. The hook is a local node script reading the prompt from standard input, with no network access.
  • Block Chmod 777

    HookSecurity

    Free
  • Block Curl Pipe Shell

    HookSecurity

    Free
  • Block Dangerous Commands

    HookSecurity

    Free
  • Block Env in Git Add

    HookSecurity

    Free