Key Exposure Detection
What you'll learn: How to catch API keys and credentials before they reach your repository or production logs, using automated scanning and monitoring tools.
Why Detection Matters
Even with best practices, developers occasionally commit secrets to repositories or log them accidentally. Key exposure detection acts as your safety net—catching these mistakes before attackers find them. Think of it as a smoke detector for your codebase: it doesn't prevent fires, but it alerts you immediately when one starts.
GitHub Secret Scanning
GitHub automatically scans public repositories for known secret patterns (API keys from OpenAI, AWS, etc.). For private repositories, enable push protection in your repository settings:
- Blocks commits containing detected secrets
- Notifies developers immediately
- Supports custom patterns for your internal keys
Configure alerts in Settings → Code security and analysis → Secret scanning.
Pre-Commit Hooks
Pre-commit hooks run locally before code reaches your repository. Tools like detect-secrets or git-secrets scan staged files:
- Catch secrets before they're committed
- Run instantly in your developer workflow
- Block commits containing high-entropy strings or known patterns
Install these in your project's .git/hooks/pre-commit or use frameworks like pre-commit to manage them team-wide.
Production Monitoring
Monitor application logs and error traces for accidentally logged credentials:
- Scan logs for patterns matching API key formats
- Alert on environment variable dumps in error messages
- Review third-party logging services for exposed secrets
Set up automated alerts that trigger immediate key rotation when exposure is detected.
Responding to Detection
When a secret is detected: rotate the key immediately, audit access logs for unauthorized usage, and review how it was exposed to prevent recurrence.
Key Takeaway: Key exposure detection creates multiple safety layers—repository scanning, pre-commit hooks, and log monitoring—ensuring secrets that slip through other defenses are caught quickly before exploitation.