Skip to main content

Common Misconfiguration

Committed .env files expose all application secrets including API keys, database passwords, and encryption keys. 😱

Vulnerable Example

Secure Example


Detection Patterns

  • Generic Secret: `(?i)(password|passwd|pwd|secret|token|api.?key)\s*[:=]\s*['"]?[^'"\s]+['"]?`
  • AWS Key: `(AWS|aws|Aws)_(ACCESS|access|Access)_KEY(_ID)?\s*[:=]\s*['"]?(AKIA|ASIA)[0-9A-Z]{16}['"]?`
  • AWS Secret: `(AWS|aws|Aws)_(SECRET|secret|Secret)_ACCESS_KEY\s*[:=]\s*['"]?[A-Za-z0-9/+=]{40}['"]?`
  • Stripe Key: `STRIPE_(SECRET|secret|Secret)_KEY\s*[:=]\s*['"]?sk_(live|test)_[0-9a-zA-Z]{24,}['"]?`
  • Google OAuth Secret: `(GOOGLE|google|Google)_CLIENT_SECRET\s*[:=]\s*['"]?GOCSPX-[0-9a-zA-Z-]{30,}['"]?`

Prevention Best Practices

  1. Never Commit .env Files: This is the absolute most important rule 🚫. Add .env (and similar files like .envrc, .flaskenv) to your .gitignore file immediately. Secrets should never exist in your code repository’s history.
  2. Use .env.example: Commit a template file (e.g., .env.example) that lists all required environment variables but without their actual values. This guides other developers (and your future self) on what needs to be configured.
  3. Validate Variables on Startup: Your application should check for the presence and potentially the format or strength of required environment variables when it starts. Fail fast if critical secrets are missing or invalid.
  4. Use Secrets Management in Production: While .env files are okay for local development, they are not suitable for production. Use platform-native solutions (like AWS Secrets Manager, Azure Key Vault, GCP Secret Manager, Kubernetes Secrets, Docker Secrets, Heroku Config Vars) to inject secrets securely into your production environment.
  5. Encrypt Sensitive Variables: For secrets stored at rest (e.g., in some deployment configurations or backups), ensure they are encrypted. Tools like sops can encrypt secrets within configuration files, decrypting them only at runtime.
  6. Implement Proper Access Controls: Limit who can access the production environment where secrets are stored or injected. Use Role-Based Access Control (RBAC) on your cloud platform or orchestrator.
  7. Rotate Secrets Regularly: All secrets (database passwords, API keys, encryption keys) should have a defined lifespan and be rotated periodically. Automate this process using secrets management tools.
  8. Use Different Secrets Per Environment: Never share secrets between development, staging, and production. Each environment must have its own unique set of credentials.
  9. Scan Repositories for Secrets: Use automated tools (like git-secrets, truffleHog, or GitHub Advanced Security secret scanning) in your CI/CD pipeline to detect accidentally committed secrets before they are merged.