| created | "2025-12-16T00:00:00.000Z" |
| modified | "2025-12-16T00:00:00.000Z" |
| reviewed | "2025-12-16T00:00:00.000Z" |
| name | git-security-checks |
| description | Pre-commit security validation and secret detection. Runs detect-secrets scan
and audit workflow, validates secrets baseline, and integrates with pre-commit
hooks to prevent credential leaks.
Use when user mentions scanning for secrets, detect-secrets, secret detection,
credential scanning, pre-commit security, or .secrets.baseline.
|
| allowed-tools | Bash, Read |
Git Security Checks
Expert guidance for pre-commit security validation and secret detection using detect-secrets and pre-commit hooks.
Core Expertise
- detect-secrets: Scan for hardcoded secrets and credentials
- Pre-commit Hooks: Automated security validation before commits
- Secrets Baseline: Manage false positives and legitimate secrets
- Security-First Workflow: Prevent credential leaks before they happen
detect-secrets Workflow
Initial Setup
pip install detect-secrets
detect-secrets scan > .secrets.baseline
detect-secrets audit .secrets.baseline
Pre-commit Scan Workflow
Run detect-secrets before every commit:
detect-secrets scan --baseline .secrets.baseline
detect-secrets audit .secrets.baseline
git add .secrets.baseline
Audit Process
When new secrets are detected:
detect-secrets audit .secrets.baseline
detect-secrets scan --baseline .secrets.baseline
Complete Pre-commit Security Flow
detect-secrets scan --baseline .secrets.baseline
detect-secrets audit .secrets.baseline
git add .secrets.baseline
pre-commit run --all-files --show-diff-on-failure
git add src/file.ts
git status
git diff --cached --stat
git commit -m "feat(auth): add authentication module"
Pre-commit Hook Integration
.pre-commit-config.yaml
Example configuration with detect-secrets:
repos:
- repo: https://github.com/Yelp/detect-secrets
rev: v1.4.0
hooks:
- id: detect-secrets
args: ['--baseline', '.secrets.baseline']
exclude: package-lock.json
Running Pre-commit Hooks
pre-commit run --all-files
pre-commit run
pre-commit run detect-secrets
pre-commit run --all-files --show-diff-on-failure
pre-commit install
Common Secret Patterns
detect-secrets scans for:
- API Keys: AWS, GitHub, Stripe, etc.
- Authentication Tokens: JWT, OAuth tokens, session tokens
- Passwords: Hardcoded passwords in config files
- Private Keys: RSA, SSH, PGP private keys
- Database Credentials: Connection strings with passwords
- Generic Secrets: High-entropy strings that look like secrets
Examples of What Gets Detected
API_KEY = "sk_live_abc123def456ghi789"
aws_access_key_id = AKIAIOSFODNN7EXAMPLE
DB_URL = "postgresql://user:Pa$$w0rd@localhost/db"
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA...
Managing False Positives
Excluding Files
In .secrets.baseline:
detect-secrets scan --exclude-files 'package-lock\.json' > .secrets.baseline
detect-secrets scan --exclude-files '.*\.lock$' > .secrets.baseline
detect-secrets scan --exclude-files 'test/.*\.py' > .secrets.baseline
Inline Ignore Comments
api_key = "test-key-1234"
password = "fake-password"
Baseline Management
detect-secrets scan --baseline .secrets.baseline --update
detect-secrets audit .secrets.baseline
cat .secrets.baseline | jq '.results'
Security Best Practices
Never Commit Secrets
- Use environment variables: Store secrets in .env files (gitignored)
- Use secret managers: AWS Secrets Manager, HashiCorp Vault, etc.
- Use CI/CD secrets: GitHub Secrets, GitLab CI/CD variables
- Rotate leaked secrets: If accidentally committed, rotate immediately
Secrets File Management
.env
.env.local
.env.*.local
*.pem
*.key
credentials.json
config/secrets.yml
.api_tokens
Handling Legitimate Secrets in Repo
For test fixtures or examples:
API_KEY = "fake-key-for-testing-only"
API_KEY = "<your-api-key-here>"
detect-secrets audit .secrets.baseline
Emergency: Secret Leaked to Git History
If a secret is committed and pushed:
Immediate Actions
git reset --soft HEAD~1
git add .
git commit -m "fix(security): remove leaked credentials"
git push --force-with-lease origin branch-name
Full History Cleanup
pip install git-filter-repo
git filter-repo --path path/to/secret/file --invert-paths
git filter-repo --replace-text <(echo "SECRET_KEY=abc123==>SECRET_KEY=REDACTED")
Prevention
pre-commit run detect-secrets
git diff --cached
echo ".env" >> .gitignore
echo ".api_tokens" >> .gitignore
Workflow Integration
Daily Development Flow
detect-secrets scan --baseline .secrets.baseline
pre-commit run --all-files
detect-secrets audit .secrets.baseline
git add .secrets.baseline
git add src/feature.ts
git diff --cached
detect-secrets scan --baseline .secrets.baseline
git commit -m "feat(feature): add new capability"
CI/CD Integration
name: Security Checks
on: [push, pull_request]
jobs:
security:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install detect-secrets
run: pip install detect-secrets
- name: Scan for secrets
run: detect-secrets scan --baseline .secrets.baseline --fail-on-unaudited
Troubleshooting
Baseline Out of Sync
detect-secrets scan > .secrets.baseline.new
detect-secrets audit .secrets.baseline.new
mv .secrets.baseline.new .secrets.baseline
Too Many False Positives
detect-secrets scan --exclude-files 'test/.*' > .secrets.baseline
detect-secrets scan --base64-limit 4.5 > .secrets.baseline
Pre-commit Hook Failing
pre-commit run detect-secrets --verbose
ls -la .secrets.baseline
pre-commit autoupdate
Secret Detected But File Not Changed
detect-secrets scan --baseline .secrets.baseline --update
detect-secrets audit .secrets.baseline
Tools Reference
detect-secrets Commands
detect-secrets scan
detect-secrets scan --baseline .secrets.baseline
detect-secrets audit .secrets.baseline
detect-secrets scan --baseline .secrets.baseline --update
detect-secrets scan --exclude-files 'pattern'
detect-secrets scan --list-all-plugins
pre-commit Commands
pre-commit install
pre-commit run --all-files
pre-commit run detect-secrets
pre-commit autoupdate
pre-commit uninstall