Express PostgreSQL Parameterized Queries
⚠️ Critical Security Fix: Transform PostgreSQL queries to use parameterized queries and prevent SQL injection vulnerabilities (CWE-89).
This codemod automatically transforms PostgreSQL queries in Express applications that use string concatenation or template literals into safe parameterized queries using the pg (node-postgres) library.
🔒 Security Impact
Vulnerability: CWE-89 - SQL Injection
OWASP: A03:2021 - Injection
Severity: Critical
SQL injection occurs when user input is directly concatenated into SQL query strings, allowing attackers to manipulate the query structure and potentially:
- Access unauthorized data
- Modify or delete database records
- Execute administrative operations
- Bypass authentication
🚀 Installation
bash
✨ Transformations
Template Literals → Parameterized Queries
Before:
javascript
After:
javascript
String Concatenation → Parameterized Queries
Before:
javascript
After:
javascript
Complex Multi-Parameter Queries
Before:
javascript
After:
javascript
DELETE and UPDATE Operations
Before:
javascript
After:
javascript
🎯 Scope
✅ Transforms
- Files importing
pgornode-postgres - Express route handlers and middleware
.query()method calls with:- Template literals containing variables (
${variable}) - String concatenation with
+operator - Mixed quote styles and complex expressions
- Template literals containing variables (
❌ Ignores
- Already parameterized queries (using
$1,$2syntax) - Static queries without dynamic content
- Query builders (Knex, Sequelize, Prisma)
- Test files (
*.test.*,*.spec.*) - Database migration files
- Comments and documentation
🔧 Usage Examples
Express Route Handler
javascript
Middleware Function
javascript
🔍 Why This Matters
Vulnerable Code Example
javascript
Secure Code After Transformation
javascript
🏗️ Development
bash
📚 References
- CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
- OWASP SQL Injection Prevention Cheat Sheet
- PostgreSQL Extended Query Protocol
- node-postgres Parameterized Queries
📄 License
MIT