codemod/fix-sequelize-empty-password-args
Detects Sequelize instances with empty password arguments and replaces with environment variable references
# @codemod/fix-sequelize-empty-password-args
**Security Fix**: Detects Sequelize instances with empty password arguments and replaces them with environment variable references to prevent authentication bypass vulnerabilities.
## Overview
Sequelize connections with empty password arguments (empty strings, `null`, or `undefined`) pose significant security risks by potentially allowing unauthorized database access. This codemod identifies such instances and automatically replaces them with secure environment variable references.
## Security Impact
This codemod addresses:
- **CWE-287**: Improper Authentication - Empty passwords can bypass authentication mechanisms
- **OWASP A6**: Security Misconfiguration - Improper database authentication setup
- **Authentication Bypass**: Prevents unauthorized database access through empty credentials
## Installation
```bash
# Run from registry
npx codemod@latest @codemod/fix-sequelize-empty-password-args
# Or run locally
npx codemod@latest jssg run -l typescript ./scripts/codemod.ts <target-directory>
```
## Transformations
### Before/After Examples
#### Positional Arguments
```javascript
// Before
new Sequelize('database', 'username', '', options)
new Sequelize('database', 'username', null, options)
new Sequelize('database', 'username', undefined, options)
// After
new Sequelize('database', 'username', process.env.DB_PASSWORD || '', options)
new Sequelize('database', 'username', process.env.DB_PASSWORD || null, options)
new Sequelize('database', 'username', process.env.DB_PASSWORD, options)
```
#### Object Configuration
```javascript
// Before
new Sequelize({
database: 'db',
username: 'user',
password: ''
})
new Sequelize({
database: 'db',
username: 'user',
password: null
})
// After
new Sequelize({
database: 'db',
username: 'user',
password: process.env.DB_PASSWORD || ''
})
new Sequelize({
database: 'db',
username: 'user',
password: process.env.DB_PASSWORD || null
})
```
## What Gets Transformed
✅ **Transforms:**
- `new Sequelize('db', 'user', '')`
- `new Sequelize('db', 'user', null)`
- `new Sequelize('db', 'user', undefined)`
- `new Sequelize({ password: '' })`
- `new Sequelize({ password: null })`
❌ **Doesn't Transform:**
- `new Sequelize('db', 'user', 'actualpassword')` (already secure)
- `new Sequelize('db', 'user', process.env.DB_PASSWORD)` (already using env vars)
- Files without Sequelize imports
- Commented code
## Requirements
- Files must import Sequelize from the 'sequelize' package
- Supports both default imports (`import Sequelize from 'sequelize'`) and named imports (`import { Sequelize } from 'sequelize'`)
- Works with JavaScript, TypeScript, JSX, and TSX files
## Post-Transformation Steps
After running this codemod:
1. **Set Environment Variable**: Ensure `DB_PASSWORD` is set in your environment:
```bash
export DB_PASSWORD="your-secure-password"
```
2. **Update Environment Files**: Add to your `.env` file:
```
DB_PASSWORD=your-secure-password
```
3. **Review Changes**: Verify all transformations are correct
4. **Test Database Connections**: Ensure your application still connects properly
## Development
```bash
# Test the transformation
pnpm test
# Run on specific test case
pnpm test --filter positive-empty-string
# Validate the workflow
npx codemod@latest validate -w workflow.yaml
```
## References
- [CWE-287: Improper Authentication](https://cwe.mitre.org/data/definitions/287.html)
- [OWASP A6: Security Misconfiguration](https://owasp.org/www-project-top-ten/2017/A6_2017-Security_Misconfiguration.html)
- [Sequelize Authentication Documentation](https://sequelize.org/docs/v6/getting-started/#connecting-to-a-database)
- [OWASP Database Security Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Database_Security_Cheat_Sheet.html)
## License
MIT