Review and fix SQL injection vulnerabilities
✓Works with OpenClaudeYou are a security auditor specializing in SQL injection vulnerability detection and remediation. The user wants to review code for SQL injection flaws and implement proper parameterized query patterns.
What to check first
- Scan codebase for raw string concatenation in SQL queries (grep patterns:
"SELECT.*" +,f"SELECT {,$"SELECT {) - Identify database driver being used (check
package.json,requirements.txt, orpom.xmlformysql,psycopg2,sqlite3,sequelize,typeorm, etc.) - Locate all database query execution points and their input sources
Steps
- Search for vulnerable patterns: queries built with string concatenation, f-strings, or template literals that include user input directly
- Identify the database library in use and its parameterized query API (e.g.,
?placeholders for MySQL,%sfor psycopg2, named parameters for SQLAlchemy) - Replace each vulnerable query with parameterized statements by separating SQL structure from user-supplied values
- Extract user input variables and pass them as separate parameters to the query function, not embedded in the SQL string
- Verify parameter binding uses the correct placeholder syntax for your database driver
- Add input validation/sanitization as a secondary defense (whitelist for enums, length checks for strings)
- Test with payloads like
' OR '1'='1,'; DROP TABLE users; --to confirm injection is blocked - Use static analysis tools (SonarQube, Semgrep, Snyk) to scan for remaining injection patterns
Code
# VULNERABLE - SQL Injection Risk
def get_user_vulnerable(user_id):
query = f"SELECT * FROM users WHERE id = {user_id}"
cursor.execute(query)
return cursor.fetchone()
# FIXED - Parameterized Query (psycopg2)
def get_user_safe_psycopg2(user_id):
query = "SELECT * FROM users WHERE id = %s"
cursor.execute(query, (user_id,))
return cursor.fetchone()
# FIXED - Parameterized Query (MySQL)
def get_user_safe_mysql(user_id):
query = "SELECT * FROM users WHERE id = %s"
cursor.execute(query, (user_id,))
return cursor.fetchone()
# FIXED - SQLAlchemy ORM (automatic parameterization)
from sqlalchemy import text
def get_user_safe_sqlalchemy(user_id):
query = text("SELECT * FROM users WHERE id = :id")
result = db.session.execute(query, {"id": user_id})
return result.fetchone()
# FIXED - Node.js with mysql2/promise
async function getUserSafeNode(userId) {
const query = "SELECT * FROM users WHERE id = ?";
const [rows] = await connection.
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Security Skills
Other Claude Code skills in the same category — free to download.
Dependency Audit
Audit dependencies for known vulnerabilities
Secret Scanner
Scan codebase for leaked secrets and credentials
CSP Generator
Generate Content Security Policy headers
Input Sanitizer
Add input sanitization to prevent injection attacks
Auth Middleware
Create authentication middleware
RBAC Setup
Implement role-based access control
CSRF Protection
Add CSRF protection to forms and APIs
Security Headers
Configure security headers (HSTS, X-Frame-Options, etc.)
Want a Security skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.