Fix insecure deserialization vulnerability by replacing pickle with JSON#6
Merged
Merged
Conversation
Replace unsafe `pickle.loads()` deserialization with safe `json.loads()` to prevent remote code execution.
## Changes
- Replaced `import pickle` with `import json`
- Changed `pickle.loads(data)` to `json.loads(data.decode('utf-8'))` in the `/deserialized_descr` endpoint
- Renamed variable `pickled` to `encoded` for clarity
## Why
The `pickle` module can execute arbitrary Python code during deserialization. When user-controlled data is passed to `pickle.loads()`, an attacker can craft a malicious payload to achieve remote code execution (RCE), denial of service (DoS), or bypass access controls.
JSON is a safe alternative that only deserializes basic data types (strings, numbers, booleans, lists, and dictionaries) without executing any code.
## Semgrep Finding Details
The application may convert user-controlled data into an object, which can lead to an insecure deserialization vulnerability. An attacker can create a malicious serialized object, pass it to the application, and take advantage of the deserialization process to perform Denial-of-service (DoS), Remote code execution (RCE), or bypass access control measures. The C implementations of the `pickle` module, called `cPickle` or `_pickle`, are also considered insecure.
@267212124 requested Semgrep Assistant generate this pull request to fix [a finding](https://semgrep.dev/orgs/studentsca023_personal_org/findings/722169012) from the detection rule [python.flask.deserialization.tainted-pickle-flask.tainted-pickle-flask](https://semgrep.dev/r/python.flask.deserialization.tainted-pickle-flask.tainted-pickle-flask).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace unsafe
pickle.loads()deserialization with safejson.loads()to prevent remote code execution.Changes
import picklewithimport jsonpickle.loads(data)tojson.loads(data.decode('utf-8'))in the/deserialized_descrendpointpickledtoencodedfor clarityWhy
The
picklemodule can execute arbitrary Python code during deserialization. When user-controlled data is passed topickle.loads(), an attacker can craft a malicious payload to achieve remote code execution (RCE), denial of service (DoS), or bypass access controls.JSON is a safe alternative that only deserializes basic data types (strings, numbers, booleans, lists, and dictionaries) without executing any code.
Semgrep Finding Details
The application may convert user-controlled data into an object, which can lead to an insecure deserialization vulnerability. An attacker can create a malicious serialized object, pass it to the application, and take advantage of the deserialization process to perform Denial-of-service (DoS), Remote code execution (RCE), or bypass access control measures. The C implementations of the
picklemodule, calledcPickleor_pickle, are also considered insecure.@267212124 requested Semgrep Assistant generate this pull request to fix a finding from the detection rule python.flask.deserialization.tainted-pickle-flask.tainted-pickle-flask.