Skip to main content

Common Misconfiguration

Hardcoded database connection strings expose your database to unauthorized access and data breaches.

Vulnerable Example

Secure Example

Detection Patterns

  • MySQL: `mysql://[^:]+:[^@]+@`
  • PostgreSQL: `postgres(ql)?://[^:]+:[^@]+@`
  • SQL Server: `(Server|Data Source)=[^;]+;(Password|Pwd)=[^;]+`
  • Oracle: `jdbc:oracle:thin:[^/]+/[^@]+@`
  • MongoDB: `mongodb(\+srv)?://[^:]+:[^@]+@`

Prevention Best Practices

  1. Use Environment Variables: Never hardcode credentials. Load them from environment variables at runtime.
  2. Use Secrets Management: Store credentials securely in a dedicated service like HashiCorp Vault, AWS Secrets Manager, Azure Key Vault, or GCP Secret Manager.
  3. Use Connection Pooling: Use a connection pool (like HikariCP) to manage database connections efficiently.
  4. Enable SSL/TLS: Encrypt data in transit by enforcing SSL/TLS connections to your database.
  5. Minimal Privileges: Use dedicated database service accounts with the absolute minimum (least-privilege) permissions they need (e.g., SELECT, INSERT on specific tables, not root or sa).
  6. Rotate Passwords: Regularly rotate all database passwords.
  7. Implement Audit Logging: Enable database audit logging to monitor for suspicious activity.