Skip to main content

Common Misconfiguration

Hardcoded database passwords in configuration files, connection strings, and migration scripts expose databases to unauthorized access.

Vulnerable Example

Secure Example

Detection Patterns

  • Key/Value Pair: `(password|passwd|pwd|secret|token)['"]?\s*[:=]\s*['"][^'"]+['"]`
  • Connection String: `(mysql|postgres|redis|mongodb)://[^:]+:[^@]+@`
  • Django Password: `'PASSWORD':\s*['"][^'"]+['"]`
  • Rails Password: `password:\s*[^#\s]+`

Prevention Best Practices

  1. Use Environment Variables: Never hardcode passwords. Load them from the environment at runtime.
  2. Implement Secrets Management: Use a dedicated secrets manager (like HashiCorp Vault, AWS Secrets Manager, Azure Key Vault, or GCP Secret Manager) to store and inject credentials.
  3. Use IAM Database Authentication: Where possible (e.g., AWS RDS, GCP Cloud SQL), use IAM roles to authenticate to the database without any passwords.
  4. Implement Password Rotation: Enforce and automate regular password rotation policies.
  5. Use Strong Passwords: Ensure all database passwords are long, complex, and randomly generated.
  6. Enable SSL/TLS: Encrypt all database connections in transit.
  7. Use Connection Pooling: Implement secure connection pooling to manage connections.
  8. Monitor Failed Logins: Actively monitor and alert on failed authentication attempts.
  9. Use Separate Credentials: Use different passwords and service accounts for each environment (dev, staging, prod).
  10. Implement Audit Logging: Enable database audit logging to track all access and changes.