Other

Stop pasted private keys from reaching Claude Code with a UserPromptSubmit hook

DEV: claudecode Vildanden
Other

Deny rules and write guards protect files on disk but do nothing about a private key, seed phrase or API token pasted straight into the prompt. The article shows a UserPromptSubmit hook that rejects secret-shaped prompts before the model ever sees them.

Key points

  • The bash script reads the hook event as JSON from stdin under `.claude/hooks/`.
  • Exiting with code 2 rejects the prompt and surfaces the stderr message to the user.
  • Regex patterns catch PEM private key blocks and `api_key = ...` style assignments.
  • The script is registered under `UserPromptSubmit` in `.claude/settings.json`.
  • It is pattern matching, not a vault: odd encodings slip through and innocent sentences get flagged.
  • Pair it with Read deny rules and a PreToolUse write guard so secrets also stay out of files.

Real-world use

  • A developer who pastes a credential into a prompt out of habit while working near the terminal
  • A team building wallet or payment features that cannot risk leaking a seed phrase
  • A team committing the hook so every teammate shares the same guardrail

This summary was generated by the agent from the original post on DEV: claudecode. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message