API key leaks are the #1 breach cause in 2026. Core rules: 1) Never commit keys to Git (use .env + .gitignore), 2) Rotate regularly (quarterly + on incident), 3) Scoped keys — minimum necessary permissions, 4) Secrets manager for production (Vault, AWS SM, GCP SM). GitHub Push Protection automatically blocks pushes with detected secrets.
Below: step-by-step, working examples, common pitfalls, FAQ.
.env file + add to .gitignore.env.example in git without actual valuesdetect-secrets, git-secrets (AWS), trufflehoggit filter-branch or BFG Repo-Cleaner| Scenario | Config |
|---|---|
| .env file | # .env (in .gitignore)
STRIPE_API_KEY=sk_live_abc123
AWS_ACCESS_KEY_ID=AKIA...
OPENAI_API_KEY=sk-proj-xxx
# .env.example (in git)
STRIPE_API_KEY=sk_live_xxx
AWS_ACCESS_KEY_ID=xxx |
| Node.js reading .env | require('dotenv').config();
const stripe = require('stripe')(process.env.STRIPE_API_KEY); |
| pre-commit hook (detect-secrets) | pip install detect-secrets
detect-secrets scan > .secrets.baseline
# .pre-commit-config.yaml:
repos:
- repo: https://github.com/Yelp/detect-secrets
rev: v1.4.0
hooks: [{id: detect-secrets}] |
| Emergency: remove key from git history | # If key already in git — rotate IMMEDIATELY (key compromised)
# Then clean history:
git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch .env" \
--prune-empty --tag-name-filter cat -- --all
git push --force origin main |
| AWS Secrets Manager | import boto3, json
client = boto3.client('secretsmanager')
secret = json.loads(client.get_secret_value(SecretId='prod/stripe')['SecretString'])
api_key = secret['api_key'] |
console.log(req) dumps headers with Authorization. Mask secrets in loggerThe tool checks HTTP security headers, SSL/TLS configuration, server info leaks, and protection against common attacks (XSS, clickjacking, MIME sniffing). A grade fromA to F shows overall security level.
Checking Content-Security-Policy, HSTS, X-Frame-Options, X-Content-Type-Options, Referrer-Policy, and more.
TLS version, certificate expiry, chain of trust, HSTS support.
Finding exposed server versions, debug modes, open configs, and directories.
Detailed report explaining each issue with specific steps to fix it.
HTTP header audit
config verification
CSP & HSTS setup
compliance checks
Strict-Transport-Security.Server: Apache/2.4.52 helps attackers find exploits. Hide the version.DENY or SAMEORIGIN.nosniff, browsers may misinterpret file types (MIME sniffing).Content-Security-Policy-Report-Only, monitor violations, then enforce.Server, X-Powered-By, X-AspNet-Version from responses.Security check history and HTTP security header monitoring.
Sign up freeBloom filter + regex detection on commits at push. Catches AWS, Stripe, Google, OpenAI keys and other common patterns. Enable in repo settings.
Scan history: trufflehog. Monitor provider alerts (AWS CloudTrail, Stripe webhook). Enable Secret Scanning in GitHub (free for public repos).
.env for local dev + staging. Vault/SM for production — enable audit, rotation, fine-grained access. For early-stage — AWS SM cheap and safe.
Restrict by referer/origin. Google Maps key bound to *.example.com — attacker domain useless. Not 100% protection but raises bar.