codemod/secure-sequelize-connection-strings
Parses hardcoded credentials from Sequelize connection URIs and replaces with secure env var references
# Secure Sequelize Connection Strings
> **Security Critical**: Automatically removes hardcoded database credentials from Sequelize connection strings and replaces them with secure environment variables.
## Problem
Hardcoded database credentials in Sequelize connection strings create serious security vulnerabilities:
- Credentials are exposed in source code and version control
- Passwords are visible to anyone with repository access
- Risk of credential leakage in logs, error messages, and backups
- Violation of security best practices and compliance requirements
## Solution
This codemod automatically:
- Detects hardcoded credentials in Sequelize connection strings and configuration objects
- Replaces them with secure environment variable references
- Maintains functionality while improving security posture
- Supports multiple database types (PostgreSQL, MySQL, MariaDB, SQLite)
## Installation
```bash
# Run directly from registry
npx codemod@latest run @codemod/secure-sequelize-connection-strings
# Or run locally
npx codemod@latest run -w workflow.yaml
```
## Transformations
### Connection String URIs
**Before:**
```javascript
const sequelize = new Sequelize('postgres://username:password@localhost:5432/database');
```
**After:**
```javascript
const sequelize = new Sequelize(process.env.DATABASE_URL);
```
### Configuration Objects
**Before:**
```javascript
const sequelize = new Sequelize({
dialect: 'postgres',
host: 'localhost',
username: 'dbuser',
password: 'mypassword',
database: 'mydb'
});
```
**After:**
```javascript
const sequelize = new Sequelize({
dialect: 'postgres',
host: process.env.DB_HOST || 'localhost',
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME
});
```
### Connection String Properties
**Before:**
```javascript
const config = {
connectionString: 'postgresql://admin:pass123@prod-db:5432/app'
};
```
**After:**
```javascript
const config = {
connectionString: process.env.DATABASE_URL
};
```
## Security Features
- **Smart Detection**: Only transforms files that actually use Sequelize
- **Test Exclusion**: Automatically skips test files and mock credentials
- **Credential Recognition**: Detects various database URI formats
- **Context Aware**: Preserves non-sensitive configuration while securing credentials
## Environment Variables
After running this codemod, ensure you set the following environment variables:
```bash
# For connection string approach
DATABASE_URL=postgres://username:password@host:port/database
# For configuration object approach
DB_HOST=your-database-host
DB_USER=your-database-username
DB_PASSWORD=your-database-password
DB_NAME=your-database-name
```
## Security References
- [CWE-798: Use of Hard-coded Credentials](https://cwe.mitre.org/data/definitions/798.html)
- [OWASP Top 10 A07:2021 Identification and Authentication Failures](https://owasp.org/Top10/A07_2021-Identification_and_Authentication_Failures/)
## Development
```bash
# Run tests
pnpm test
# Test specific case
pnpm test --filter postgres-connection-string
# Validate implementation
npx codemod@latest validate -w workflow.yaml
```
## License
MIT