A pre-commit hook to check for exposed API keys in your code. Supports configurable regex patterns to catch different types of API keys.
Add this to your .pre-commit-config.yaml
:
repos:
- repo: https://github.com/ericmjl/api-key-precommit
rev: v0.1.0 # Use the latest tag/release
hooks:
- id: api-key-checker
args:
- "--pattern"
- "[a-zA-Z0-9]{32,}" # Generic API key pattern (32+ chars)
- "--pattern"
- "api[_-]key[_-][a-zA-Z0-9]{16,}" # API key with prefix
- "--pattern"
- "^sk-proj-[A-Za-z0-9_-]+-rev[A-Za-z0-9_\\-\\.]+$" # Project-specific key pattern
The hook accepts patterns through command-line arguments using the --pattern
or -p
flag. Each pattern must be preceded by its own flag.
Some example patterns you might want to use:
args:
- "--pattern"
- "[A-Z0-9]{32}" # Generic 32-char API key
- "--pattern"
- "sk-[A-Za-z0-9]{48}" # OpenAI-style key
- "--pattern"
- "ghp_[A-Za-z0-9]{36}" # GitHub personal access token
- "--pattern"
- "xox[baprs]-[A-Za-z0-9-]{10,}" # Slack tokens
If no patterns are specified, the hook will use a default pattern that matches common API key formats:
- 32 or more alphanumeric characters (
[a-zA-Z0-9]{32,}
)
When an API key is detected, the hook will raise an exception with details about the match:
Found potential API key matching pattern '[A-Z0-9]{32}' in path/to/file.py:123-155
- Clone the repository
- Install development dependencies:
pip install -e .
- Run tests:
pytest
MIT
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request