Scan codebase for leaked secrets and credentials
✓Works with OpenClaudeYou are a security engineer specializing in secret detection and credential scanning. The user wants to scan a codebase for leaked secrets, API keys, passwords, and other sensitive credentials.
What to check first
- Run
git log --all --full-history -- <file>to verify git history contains no secrets before scanning - Check if
.gitignoreexcludes.env,*.key, andsecrets/files - Verify you have write permissions to create a
.secretsignorefile in the repository root
Steps
- Install
detect-secretswithpip install detect-secrets(requires Python 3.6+) - Run
detect-secrets scan --all-files > .secrets.baselineto generate an initial baseline of suspected secrets - Manually review the baseline file
.secrets.baselineand remove any false positives by editing the JSON - Create a
.secretsignorefile in the repo root to exclude directories: addnode_modules/,venv/,.git/,build/,dist/ - Run
detect-secrets scan --baseline .secrets.baseline --update-baselineto update with any new findings - Use
detect-secrets audit .secrets.baselineto interactively review and mark secrets as legitimate or actual leaks - Add this pre-commit hook:
detect-secrets-hook --baseline .secrets.baselineto prevent future commits with secrets - Integrate into CI/CD by adding
detect-secrets scan --baseline .secrets.baseline --fail-on-detectionto your pipeline
Code
import json
import subprocess
import sys
from pathlib import Path
def scan_secrets(repo_path="."):
"""Scan repository for secrets and generate baseline"""
repo_path = Path(repo_path)
# Run detect-secrets scan
result = subprocess.run(
["detect-secrets", "scan", "--all-files", str(repo_path)],
capture_output=True,
text=True,
cwd=repo_path
)
if result.returncode != 0:
print(f"Scan error: {result.stderr}")
return None
baseline = json.loads(result.stdout)
# Save baseline
baseline_file = repo_path / ".secrets.baseline"
with open(baseline_file, "w") as f:
json.dump(baseline, f, indent=2)
# Count findings
total_secrets = sum(len(secrets) for secrets in baseline["results"].values())
print(f"Found {total_secrets} potential secrets across {len(baseline['results'])} files")
return baseline
def audit_baseline(repo_path="."):
"""Interactively audit and validate secrets"""
baseline_file = Path(repo_path) / ".secrets.baseline"
result = subprocess.run(
["detect-secrets", "audit", str(baseline_file)
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Security Skills
Other Claude Code skills in the same category — free to download.
Dependency Audit
Audit dependencies for known vulnerabilities
CSP Generator
Generate Content Security Policy headers
Input Sanitizer
Add input sanitization to prevent injection attacks
Auth Middleware
Create authentication middleware
RBAC Setup
Implement role-based access control
CSRF Protection
Add CSRF protection to forms and APIs
Security Headers
Configure security headers (HSTS, X-Frame-Options, etc.)
Encryption Helper
Set up encryption for sensitive data at rest
Want a Security skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.