Stop yourself from pasting a private key into Claude Code with a UserPromptSubmit hook
Deny rules and write guards protect files on disk. They do nothing if you paste a private key, seed phrase, or API token into the prompt itself. A UserPromptSubmit hook can catch that shape before the model ever sees it. The hook Save this as .claude/hooks/block-secret-prompts.sh and make it executable ( chmod +x ): #!/usr/bin/env bash # Reads the hook event as JSON on stdin; exit code 2 blocks…
Implement a UserPromptSubmit hook to prevent private keys, seed phrases, or API tokens from being pasted into Claude Code prompts. This hook reads the prompt text and checks for patterns indicating sensitive information. If a match is found, the hook blocks the prompt and displays an error message. The hook should be saved as a script file, made executable, and registered in the Claude Code settings file.
Test the hook with dummy examples before using it with real secrets. Pair the hook with other security measures like deny rules and write guards for comprehensive protection.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.
This story
This is one outlet's version. Read the fullest account.