Security: Replace pickle serialization with safer alternative in Embedding model #15865

Closed
opened 2026-02-21 19:23:43 -05:00 by yindo · 2 comments
Owner

Originally created by @lyzno1 on GitHub (Aug 4, 2025).

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

main

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

I noticed that the Embedding model in the codebase uses pickle for serialization, which was flagged in PR #16461 but bypassed with a noqa comment:

https://github.com/langgenius/dify/blob/main/api/models/dataset.py/#L938-L942

def set_embedding(self, embedding_data: list[float]):
    self.embedding = pickle.dumps(embedding_data, protocol=pickle.HIGHEST_PROTOCOL)

def get_embedding(self) -> list[float]:
    return cast(list[float], pickle.loads(self.embedding))  # noqa: S301

The S301 rule was added in PR #16461 to enhance security checks, but this specific usage was bypassed rather than addressed.

✔️ Expected Behavior

I'm wondering if there's a specific reason why pickle is necessary here, or if this could be replaced with a safer serialization method like JSON or MessagePack? Since embedding data is typically just float arrays, it seems like safer alternatives might work just as well.

Actual Behavior

The current implementation uses pickle.loads() on data stored in the database, which security tools flag as potentially unsafe for deserialization of untrusted data.

Originally created by @lyzno1 on GitHub (Aug 4, 2025). ### Self Checks - [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542). - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit this report, otherwise it will be closed. - [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version main ### Cloud or Self Hosted Self Hosted (Source) ### Steps to reproduce I noticed that the Embedding model in the codebase uses pickle for serialization, which was flagged in PR #16461 but bypassed with a noqa comment: https://github.com/langgenius/dify/blob/main/api/models/dataset.py/#L938-L942 ```python def set_embedding(self, embedding_data: list[float]): self.embedding = pickle.dumps(embedding_data, protocol=pickle.HIGHEST_PROTOCOL) def get_embedding(self) -> list[float]: return cast(list[float], pickle.loads(self.embedding)) # noqa: S301 ``` The S301 rule was added in PR #16461 to enhance security checks, but this specific usage was bypassed rather than addressed. ### ✔️ Expected Behavior I'm wondering if there's a specific reason why pickle is necessary here, or if this could be replaced with a safer serialization method like JSON or MessagePack? Since embedding data is typically just float arrays, it seems like safer alternatives might work just as well. ### ❌ Actual Behavior The current implementation uses pickle.loads() on data stored in the database, which security tools flag as potentially unsafe for deserialization of untrusted data.
yindo closed this issue 2026-02-21 19:23:43 -05:00
Author
Owner

@lyzno1 commented on GitHub (Aug 4, 2025):

@dosubot validate issue

@lyzno1 commented on GitHub (Aug 4, 2025): @dosubot validate issue
Author
Owner

@dosubot[bot] commented on GitHub (Aug 4, 2025):

This issue is valid. The Embedding model currently uses pickle for serializing and deserializing embedding data, which is flagged as insecure because pickle.loads can execute arbitrary code if the data is untrusted. There is no technical or performance justification in the codebase for using pickle over safer alternatives like JSON or MessagePack—embedding data is just a list of floats, and other serialization methods would work just as well. The use of pickle appears to be for convenience and is currently bypassing security linting with a noqa comment. Migration to a safer format is advisable and should be straightforward for this use case. See the implementation and context here: Embedding model code and related linting proposal.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Aug 4, 2025): <!-- Answer --> This issue is valid. The Embedding model currently uses pickle for serializing and deserializing embedding data, which is flagged as insecure because pickle.loads can execute arbitrary code if the data is untrusted. There is no technical or performance justification in the codebase for using pickle over safer alternatives like JSON or MessagePack—embedding data is just a list of floats, and other serialization methods would work just as well. The use of pickle appears to be for convenience and is currently bypassing security linting with a noqa comment. Migration to a safer format is advisable and should be straightforward for this use case. See the implementation and context here: [Embedding model code](https://github.com/langgenius/dify/blob/8041808b53610275283bf6cbc0a44f8a163da254/api/models/dataset.py) and [related linting proposal](https://github.com/langgenius/dify/issues/16458). <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/81284072-abd6-4634-aac1-39e345f0dae4?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/81284072-abd6-4634-aac1-39e345f0dae4?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/81284072-abd6-4634-aac1-39e345f0dae4?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/81284072-abd6-4634-aac1-39e345f0dae4?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/81284072-abd6-4634-aac1-39e345f0dae4?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/81284072-abd6-4634-aac1-39e345f0dae4?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/81284072-abd6-4634-aac1-39e345f0dae4?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/23369)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#15865