Ccodemod

codemod/secure-knex-connection-strings

Secures hardcoded credentials in Knex connection configurations by replacing them with environment variable references

securityknexdatabasecredentialsenvironment-variableshardcoded-secrets
Public
0 executions

Run locally

npx codemod @codemod/secure-knex-connection-strings

Secure Knex Connection Strings

A security-focused codemod that identifies and replaces hardcoded credentials in Knex.js connection configurations with environment variable references, following security best practices and preventing credential leaks in version control.

Security Issue

CWE-798: Use of Hard-coded Credentials
OWASP A07:2021 - Identification and Authentication Failures

Connection strings with embedded credentials in Knex configurations expose database secrets in source code, violating security best practices and potentially leading to credential leaks in version control systems.

Installation

bash

What it transforms

Connection Strings

Before:

javascript

After:

javascript

Localhost Connection Strings (with fallback)

Before:

javascript

After:

javascript

Object-style Connection Configuration

Before:

javascript

After:

javascript

What it doesn't transform

  • Connections already using environment variables (process.env.*)
  • Localhost connections with common test credentials (postgres:postgres, root:root)
  • Placeholder values (username, your_password)
  • SQLite file-based connections (no network credentials)
  • Template literals already using environment variables

Supported File Types

  • JavaScript (.js)
  • TypeScript (.ts)
  • Configuration files (.json)
  • Knex files (knexfile.*, *config*)

Environment Variables Used

  • DATABASE_URL - Complete connection string
  • DB_HOST - Database host
  • DB_USER - Username
  • DB_PASSWORD - Password (no fallback for security)
  • DB_NAME - Database name
  • DB_PORT - Port number

Development

bash

Security References

License

MIT

Ready to contribute?

Build your own codemod and share it with the community.