Interactive Playground
Experiment with the vulnerable code and security rule below. Edit the code to see how the rule detects different vulnerability patterns.
pathfinder scan --ruleset python/PYTHON-LAMBDA-SEC-012 --project .About This Rule
Understanding the vulnerability and how it is detected
This rule detects SQL injection vulnerabilities in AWS Lambda functions where untrusted event data flows into pymssql cursor.execute() calls without proper parameterization, enabling SQL injection against RDS for SQL Server backends.
Lambda functions connecting to RDS for SQL Server (Microsoft SQL Server) via pymssql receive event data from API Gateway, SQS, SNS, S3, and other triggers. Fields like event.get("body"), event.get("queryStringParameters"), and event["Records"] are attacker-controllable in public-facing deployments. pymssql uses %s (or %d, %f for typed parameters) as placeholders; when these placeholders are bypassed by embedding event data directly in the SQL string via concatenation or f-strings, injection becomes possible.
SQL Server injection is particularly dangerous due to xp_cmdshell, a stored procedure that executes OS commands. While RDS restricts xp_cmdshell, unrestricted SQL Server instances allow full OS command execution through SQL injection. Even on RDS, data exfiltration, authentication bypass, and data manipulation remain fully exploitable. Lambda functions lack the ORM safety layer that web frameworks provide, making raw pymssql calls a common injection vector in serverless SQL Server integrations.
Security Implications
Potential attack scenarios if this vulnerability is exploited
Full Database Exfiltration
An attacker who controls any SQL fragment can use UNION SELECT or error-based extraction techniques to read from any table accessible to the Lambda's SQL Server database user, including user credentials, financial records, session tokens, and other sensitive data.
xp_cmdshell OS Command Execution (Unrestricted SQL Server)
On unrestricted SQL Server instances (not RDS), an attacker with sufficient database permissions gained through injection can enable and execute xp_cmdshell to run arbitrary OS commands on the database server. RDS for SQL Server disables xp_cmdshell, but this risk applies to self-managed SQL Server instances accessible from Lambda.
Linked Server Pivoting
SQL Server supports linked servers that allow cross-database queries. SQL injection through a Lambda can exploit linked server configurations to pivot to other database servers accessible from the SQL Server instance, potentially accessing systems outside the intended Lambda attack surface.
Authentication Bypass
Lambda functions that verify credentials or check authorization via SQL queries are vulnerable to ' OR '1'='1'-- style bypasses. This can grant attackers administrative access by causing the query to return rows regardless of the actual credential values.
How to Fix
Recommended remediation steps
- 1Always pass Lambda event data as the second argument to cursor.execute() as a tuple, never by concatenating it into the SQL string.
- 2Use pymssql's typed placeholders (%s for strings, %d for integers, %f for floats) appropriately to benefit from driver-level type enforcement.
- 3Grant the Lambda's SQL Server database login the minimum necessary permissions (SELECT on specific tables) and avoid sysadmin or db_owner role membership.
- 4Store SQL Server credentials in AWS Secrets Manager with automatic rotation rather than in Lambda environment variables.
- 5Enable RDS SQL Server audit logging to detect unusual query patterns that may indicate active exploitation.
Detection Scope
How Code Pathfinder analyzes your code for this vulnerability
This rule performs inter-procedural taint analysis with global scope. Sources are Lambda event dictionary access calls: calls("event.get"), calls("event.__getitem__"), including event.get("body"), event.get("queryStringParameters"), event.get("pathParameters"), and event["Records"]. The sink is calls("*.execute") matching pymssql cursor objects with the tainted SQL string tracked via .tracks(0) (the query string argument). Sanitizers include explicit int() or float() type conversion. The analysis follows taint through string concatenation, f-string interpolation, variable assignments, and function boundaries.
Compliance & Standards
Industry frameworks and regulations that require detection of this vulnerability
References
External resources and documentation
Similar Rules
Explore related security rules for Python
Lambda SQL Injection via psycopg2 cursor.execute()
Lambda event data flows to psycopg2 cursor.execute() without parameterization, enabling SQL injection against RDS PostgreSQL or Aurora PostgreSQL backends.
Lambda SQL Injection via PyMySQL cursor.execute()
Lambda event data flows to PyMySQL cursor.execute() without parameterization, enabling SQL injection against RDS MySQL or Aurora MySQL backends via the pure-Python driver.
Lambda SQL Injection via SQLAlchemy execute()
Lambda event data flows to SQLAlchemy session.execute() or connection.execute() without bound parameters, enabling SQL injection against any RDS backend.
Frequently Asked Questions
Common questions about Lambda SQL Injection via pymssql cursor.execute()
New feature
Get these findings posted directly on your GitHub pull requests
The Lambda SQL Injection via pymssql cursor.execute() rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.