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-023 --project .About This Rule
Understanding the vulnerability and how it is detected
This rule detects unsafe deserialization vulnerabilities in AWS Lambda functions where attacker-controlled event data flows into pickle.loads(), pickle.load(), cPickle.loads(), or related pickle deserialization functions.
Python's pickle module serializes and deserializes arbitrary Python objects. The pickle protocol can encode __reduce__() method calls on arbitrary classes, allowing a crafted pickle payload to execute any Python code during deserialization. This is documented behavior: the pickle documentation explicitly states "The pickle module is not secure. Only unpickle data you trust."
Lambda functions that receive pickle-encoded data via the event dictionary are completely compromised if an attacker can control the bytes being deserialized. Event sources including API Gateway request bodies (event.get("body")), SQS message bodies (event["Records"][0]["body"]), S3 object contents fetched based on event metadata, and SNS message bodies are all attacker-controllable in public-facing deployments.
In the Lambda environment, pickle deserialization of attacker-controlled data immediately gives the attacker the full capabilities of the execution environment: the execution role's AWS credentials, boto3 SDK access, the /tmp filesystem, and outbound network access. The attack completes during the deserialization call, before any application logic runs.
Security Implications
Potential attack scenarios if this vulnerability is exploited
Code Execution During Deserialization
Pickle deserialization executes Python code encoded in the payload during the loads() call itself. No application logic needs to run; the attacker's code executes before the deserialized object is even used. This makes pickle deserialization of event data an unconditional RCE with no mitigating conditions.
Immediate AWS Credential Exfiltration
A crafted pickle payload can import os, read the Lambda's environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN), and exfiltrate them via an outbound HTTP request — all within the deserialization call before lambda_handler() returns. The execution role's permissions are compromised immediately.
Persistent Compromise of Warm Execution Environments
Pickle deserialization can modify the Lambda process's global state, install hooks in imported modules, or write files to /tmp that persist across warm invocations. An attacker can install persistence mechanisms that affect all subsequent requests processed by the same execution environment until it is recycled.
SQS and SNS Trigger Exploitation
Lambda functions triggered by SQS queues or SNS topics may process base64-encoded message bodies. If the Lambda decodes and deserializes message bodies with pickle, an attacker who can publish to the SQS queue or SNS topic (which may be publicly accessible) can execute arbitrary code in the Lambda without ever sending an API Gateway request.
How to Fix
Recommended remediation steps
- 1Replace all pickle deserialization of Lambda event data with json.loads() or another safe serialization format (msgpack, protobuf, avro) that does not execute code during parsing.
- 2Never use pickle to deserialize data received from any AWS event source (API Gateway, SQS, SNS, S3, DynamoDB Streams) regardless of whether the source appears trusted.
- 3For internal Lambda-to-Lambda communication that currently uses pickle, replace with JSON serialization or AWS-native formats (EventBridge schema registry, SQS with JSON).
- 4If pickle must be used for internal state (e.g., ML model objects in /tmp), ensure the pickle data is written by the Lambda itself and never derived from event input.
- 5Apply least-privilege IAM policies to the Lambda execution role to limit the AWS APIs accessible if exploitation occurs.
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"]. Sinks are calls("pickle.loads"), calls("pickle.load"), calls("cPickle.loads"), and calls("cPickle.load") with tainted input tracked via .tracks(0). There are no recognized sanitizers for pickle deserialization — any Lambda event data reaching pickle.loads() is a confirmed critical vulnerability. The analysis follows taint through base64.b64decode(), bytes conversions, variable assignments, and module 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 Code Injection via eval() or exec()
Lambda event data flows to eval() or exec(), enabling arbitrary Python code execution with the full permissions of the Lambda execution environment.
Lambda XSS via Tainted HTML Response Body
Lambda event data is embedded directly in an HTML response body returned to API Gateway, enabling Cross-Site Scripting attacks against end users.
Frequently Asked Questions
Common questions about Lambda Remote Code Execution via Pickle Deserialization
New feature
Get these findings posted directly on your GitHub pull requests
The Lambda Remote Code Execution via Pickle Deserialization rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.