Ccodemod

codemod/secure-postgresql-config-objects

Transforms pg.Client and pg.Pool configuration objects to use environment variables for sensitive data

securitytransformationpostgresqlcredentialsenvironment-variables
Public
0 executions

Run locally

npx codemod @codemod/secure-postgresql-config-objects

Secure PostgreSQL Configuration Objects

Security Badge
Version

A security-focused codemod that automatically transforms PostgreSQL client configurations to use environment variables instead of hardcoded credentials, preventing credential leakage in source code.

Security Impact

CWE-798: Use of Hard-coded Credentials
CWE-256: Unprotected Storage of Credentials
OWASP Top 10 A07:2021 - Identification and Authentication Failures

Hardcoded database credentials in source code pose severe security risks:

  • Credentials are exposed in version control systems
  • Difficulty in credential rotation without code changes
  • Risk of credentials being leaked through code sharing
  • Compliance violations with security standards

What This Codemod Does

This codemod automatically identifies and transforms hardcoded database credentials in PostgreSQL client configurations (pg.Client and pg.Pool) to use environment variables with appropriate fallbacks.

Before

javascript

After

javascript

Transformation Rules

FieldEnvironment VariableHas FallbackNotes
hostDB_HOSTMaintains original value as fallback
portDB_PORTUses parseInt() wrapper
userDB_USERMaintains original value as fallback
passwordDB_PASSWORDNo fallback for security
databaseDB_NAMEMaintains original value as fallback

Installation

bash

Usage

Interactive Mode

bash

Target Specific Directory

bash

Dry Run (Preview Changes)

bash

Supported Patterns

Direct Constructor Calls

javascript

Configuration Variables

javascript

What Gets Skipped

This codemod intelligently skips transformations in the following cases:

  1. Test Files: Files with .test.ts, .spec.js, etc.
  2. Already Secured: Configurations already using process.env
  3. No Hardcoded Values: Configurations without sensitive hardcoded data
  4. Dynamic Values: Configurations using function calls or variables

After Running the Codemod

1. Set Environment Variables

Create a .env file or set environment variables:

bash

2. Load Environment Variables

For Node.js applications, use a package like dotenv:

bash
javascript

3. Verify Security

Ensure your .env file is in .gitignore:

gitignore

Environment Variable Reference

VariableDescriptionRequiredExample
DB_HOSTDatabase server hostnameNolocalhost or db.example.com
DB_PORTDatabase server portNo5432
DB_USERDatabase usernameNopostgres or myuser
DB_PASSWORDDatabase passwordYesyour-secure-password
DB_NAMEDatabase nameNomyapp or production_db

⚠️ Security Note: DB_PASSWORD has no fallback value. Your application will fail if this environment variable is not set, which is intentional for security.

Security Benefits

Credentials removed from source code
Environment-specific configuration
Easier credential rotation
Compliance with security best practices
Version control safety
Reduced risk of credential leakage

Related Security Resources

Development

bash

License

MIT

Ready to contribute?

Build your own codemod and share it with the community.