Walkthrough for auditing secrets management to find hard‑coded credentials, exposed .env files, weak rotation and encryption, with evidence, safe PoCs, and prioritized remediation steps.
In this walkthrough we perform a secrets management audit to identify hard-coded credentials, insecure environment handling, and gaps in rotation and encryption practices. The goal: produce reproducible findings with evidence, safe proofs-of-concept, and prioritized remediation steps you can track in issues or tickets.
Prevents leaked credentials in VCS and public mirrors
Verify environment variable usage; ensure .env is not committed
Builds separation of config from code and avoids plaintext secrets
Check secret rotation capability for DB passwords, API keys, certs
Limits blast radius after exposure
Review encryption and key management (KDFs, salts, key storage)
Ensures cryptographic primitives are configured correctly
Ensure startup environment validation (fail fast in production)
Prevents deploying weak default secrets to prod
Produce structured findings (severity, evidence, remediation snippet)
Enables prioritized remediation and tracking
Use both automated scanners (trufflehog, git-secrets) and targeted repository searches for keywords like JWT_SECRET, API_KEY, password, env, encrypt, salt, bcrypt, and rotate to collect evidence before manual verification.
Default/placeholder JWT secret present and referenced in code
Enables token forgery if secret is unchanged
Critical
.env tracked in git with DB credentials in plaintext
Exposed credentials and compliance risk
High
No secret rotation mechanism or revocation support
Long-lived secrets increase exposure window
Medium
Password hashing cost may be too low
Easier to brute-force/accelerate attacks
High
Missing environment validation on startup
Weak dev/test secrets could be deployed to production
If a .env (or any secrets file) is committed, treat the repository as potentially compromised. Remove secrets from version control, rotate them immediately, and enable CI secret scanning. Follow a documented rotation procedure.
Proof-of-concept — JWT forgery with known placeholder secret
This safe PoC demonstrates how an attacker can forge a token when the placeholder secret is used. Do not use forged tokens against live systems.
Copy
// PoC: Forge a JWT when secret is the known placeholderconst jwt = require('jsonwebtoken');const forgedToken = jwt.sign( { userId: 1, email: 'admin@example.com' }, 'your_jwt_secret_key_here' // Known default secret from repo placeholder);console.log('Forged token:', forgedToken);
How to remove .env from git and add it to .gitignore:
Copy
# Remove .env from index, keep local file, and add to .gitignoregit rm --cached .envecho ".env" >> .gitignoregit add .gitignoregit commit -m "Remove .env from repo and add to .gitignore"
Generate a cryptographically secure JWT secret (example):
Copy
# Generate a 64-byte hex secret with Nodenode -e "console.log(require('crypto').randomBytes(64).toString('hex'))"
Replace placeholder JWT_SECRET with a secure, random secret (64+ chars). Remove .env from git, rotate exposed credentials, add startup checks to refuse weak production secrets.
Priority 2 — This week
Implement token revocation/refresh flow or short-lived access tokens. Increase bcrypt rounds for new passwords and plan re-hashing strategy. Add environment-specific validation.
Priority 3 — Next sprint
Integrate a secrets manager and migrate secrets. Add rotation procedures and documentation. Enable automated secret scanning in CI/CD (e.g., GitHub secret scanning, TruffleHog, git-secrets).
The audit highlights substantial risk from default secrets and tracked environment files. Immediate actions: remove secrets from version control, rotate exposed credentials, enable startup validation to prevent weak production configs, and integrate automated secret scanning into developer workflows. Consider adding logging and monitoring to detect and respond to leaked or abused credentials.Links and references