Prompt
How do I detect leaked API keys in repos and logs?
Latest observation
Detecting leaked API keys is usually a mix of preventive scanning, log inspection, and revocation/monitoring. Here’s a practical approach.
1) Scan repos for secrets
Use secret scanners in CI and locally:
- gitleaks
- trufflehog
- git-secrets
- detect-secrets (Yelp)
Examples:
gitleaks detect --source . --report-format json --report-path gitleaks.json
trufflehog git file:///path/to/repo
What they look for:
- Common key patterns
- High-entropy strings
- Known prefixes like
AKIA...,sk-...,AIza..., etc. - Secrets in history, not just current files
Also scan:
.env, config files, test fixtures- Commit history
- Forks and PR branches
- Docker images and build artifacts
2) Search logs for secrets
Logs often accidentally capture:
- Request headers
- Debug payloads
- Stack traces
- Environment dumps
Search for patterns and suspicious terms:
AuthorizationBearerapi_keysecrettokenpassword
Examples:
grep -RInE 'Authorization:|Bearer |api[_-]?key|secret|token|password' /var/log
For large log systems, use your SIEM/query tools:
- Splunk
- ELK/OpenSearch
- CloudWatch Logs Insights
- Datadog Logs
Example search idea:
- regex for key formats
- high-entropy string detection
- headers containing
Bearer
3) Use pattern-based detection plus entropy
Not all secrets match known prefixes. Good detection combines:
- Regex rules for known formats
- Entropy checks for random-looking strings
- Context keywords near the value
This reduces false positives.
4) Check Git history, not just current code
A secret may have been deleted from the latest commit but still exist in history.
Use:
git log -p -S 'your_secret_or_fragment'
git rev-list --all | xargs git grep -n 'pattern'
Or better, use a secret scanner that inspects all commits.
5) Monitor source control events
Set up alerts for:
- New secrets pushed
- Force-pushes after secret removal
- New
.envor credential files - Changes in files with auth/config data
6) Automate in CI/CD
Block merges when secrets are found:
- Pre-commit hooks
- Pull request checks
- Pipeline gates
Typical policy:
- Fail build on high-confidence secrets
- Allow exception process for false positives
- Notify security/channel owner immediately
7) Use cloud/provider-specific detection
Many providers offer native detection:
- GitHub Secret Scanning
- GitLab Secret Detection
- AWS Access Analyzer / CloudTrail alerts
- Google Cloud Secret Manager audit logs
- Azure Key Vault logging/alerts
8) If you find a leaked key
- Revoke/rotate immediately
- Audit usage for abuse
- Remove from repo/logs
- Purge sensitive logs if possible
- Invalidate caches and downstream copies
- Add detection rule so it doesn’t recur
9) Good prevention habits
- Never log secrets
- Redact headers and tokens
- Store secrets in a secret manager
- Use short-lived tokens
- Scope keys narrowly
- Separate dev/test/prod keys
10) A simple detection strategy to start with
If you want a quick baseline:
- Run gitleaks on all repos
- Enable GitHub/GitLab secret scanning
- Add pre-commit secret checks
- Search logs for
Authorization,Bearer,api_key,secret,token - Set alerts for high-confidence matches
- Rotate anything suspicious
If you want, I can also give you:
- a ready-to-use regex set
- a CI pipeline example
- or a Python script to scan repos and logs for likely API keys.