Skip to content

Fix insecure deserialization vulnerability by replacing pickle with JSON#6

Merged
studentsca023-rgb merged 1 commit into
masterfrom
semgrep-autofix/1773728728
Mar 17, 2026
Merged

Fix insecure deserialization vulnerability by replacing pickle with JSON#6
studentsca023-rgb merged 1 commit into
masterfrom
semgrep-autofix/1773728728

Conversation

@semgrep-code-studentsca023-rgb

Copy link
Copy Markdown

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 from the detection rule python.flask.deserialization.tainted-pickle-flask.tainted-pickle-flask.


⚠️ Review carefully before merging. This PR was generated by AI and may cause breaking changes or introduce new vulnerabilities.

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).
@studentsca023-rgb studentsca023-rgb marked this pull request as ready for review March 17, 2026 06:27
@studentsca023-rgb studentsca023-rgb merged commit 49620ff into master Mar 17, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant