PC-005CRITICALSecrets & API Keys

Database Connection String with Embedded Password

Flags PostgreSQL, MySQL, MongoDB, and Redis connection URIs that embed plaintext usernames and passwords directly inside source code, ORM configs, or migration scripts.

Rule Slug
database-connection-uri-credentials
Affected Files
*
Detection Method
Regex + Entropy (>= 3)
Live Rule Tester100% Browser-Local
CRITICAL: Detected 1 violation
Line 5:postgresql://prod_admin:X9kP••••••••••[email protected](entropy: 4)

Vulnerable Pattern vs. Hardened Fix

Vulnerable Pattern
// db/index.ts
import { Pool } from 'pg';

export const pool = new Pool({
  connectionString: 'postgresql://prod_admin:[email protected]:5432/billing?sslmode=require',
});
Recommended Fix
// db/index.ts
import { Pool } from 'pg';

if (!process.env.DATABASE_URL) {
  throw new Error('DATABASE_URL environment variable is not configured');
}

export const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
  ssl: process.env.NODE_ENV === 'production' ? { rejectUnauthorized: true } : false,
});

Why This Fails in Production

Database connection strings combine the network hostname, port, database name, username, and password into a single compact URI (for example, postgres://admin:[email protected]:5432/app). Committing a live URI with embedded credentials hands an attacker everything required to authenticate directly against your primary data store, bypassing application-layer authorization, rate limiting, and audit trails completely.

Managed cloud databases such as MongoDB Atlas, Neon, Supabase, PlanetScale, Render, and Railway frequently expose public TLS-encrypted endpoints by default so serverless functions can connect without VPC peering. When a connection string targeting one of these hosts leaks into a repository, client bundle, or CI log, any external attacker can run pg_dump, mongodump, or FLUSHALL within seconds to exfiltrate user tables, steal password hashes, or hold the database for ransom.

Even internal VPC-only database URIs create substantial risk when committed to Git. An attacker who gains a foothold through a secondary SSRF flaw, compromised CI runner, or leaked developer laptop can pivot straight into the production database using credentials harvested from repository history. Furthermore, developers routinely reuse database passwords across staging and production clusters, turning a seemingly harmless staging URI leak into a full production compromise.

Always inject DATABASE_URL or REDIS_URL exclusively through your runtime environment, enforce SSL/TLS verification (sslmode=require), restrict network ingress where possible, and commit only sanitized templates like .env.example with placeholder values.

Remediation Steps

  1. 1Rotate the exposed database user password immediately in your database provider console or via ALTER USER / db.changeUserPassword().
  2. 2Review database connection logs and active sessions for unauthorized IP addresses or anomalous bulk SELECT queries.
  3. 3Replace hardcoded URIs in prisma/schema.prisma, drizzle.config.ts, or server initialization files with process.env.DATABASE_URL.
  4. 4Keep local development URIs in an ignored .env file and document required keys using non-functional placeholders in .env.example.

Frequently Asked Questions

Will PushCheck flag local development strings like postgres://postgres:postgres@localhost:5432/dev?
PushCheck applies placeholder and entropy heuristics so obvious dummy credentials like password, changeme, or example are suppressed, while high-entropy or realistic passwords embedded in URIs are flagged immediately.
Should I put DATABASE_URL in docker-compose.yml?
Avoid hardcoding production passwords in docker-compose.yml. Reference ${DATABASE_URL} via environment variable interpolation and keep actual secrets in an ignored .env file or Docker secrets.
Is Redis safe if its URI leaks without a username?
No. Redis ACL or default-user URLs (redis://default:secret@host:6379) allow attackers to dump cached session tokens, tamper with job queues, or wipe volatile state.

Related Secrets & API Keys Rules

Scan local project folder →