Closed pixeebot[bot] closed 5 months ago
I'm confident in this change, and the CI checks pass, too!
If you see any reason not to merge this, or you have suggestions for improvements, please let me know!
This change may not be a priority right now, so I'll close it. If there was something I could have done better, please let me know!
You can also customize me to make sure I'm working with you in the way you want.
Remediation
This change fixes "python.django.security.audit.avoid-insecure-deserialization.avoid-insecure-deserialization" (id = python.django.security.audit.avoid-insecure-deserialization.avoid-insecure-deserialization) identified by DefectDojo.
Remaining Findings
The following findings are not fixed by this change:
fickling
does not yet supportpickle.loads
)fickling
does not yet supportpickle.loads
)fickling
does not yet supportpickle.loads
)Details
Use of insecure deserialization can potentially lead to arbitrary code execution. This codemod addresses this issue with two different serialization providers:
yaml
(via PyYAML)pickle
(via the standard library pickle module)Each is described in more detail below.
PyYAML
The default loaders in PyYAML are not safe to use with untrusted data. They potentially make your application vulnerable to arbitrary code execution attacks. If you open a YAML file from an untrusted source, and the file is loaded with the default loader, an attacker could execute arbitrary code on your machine.
Calling
yaml.load()
without an explicit loader argument is equivalent to calling it withLoader=yaml.Loader
, which is unsafe. This usage has been deprecated since PyYAML 5.1. This codemod will add an explicitSafeLoader
argument to allyaml.load()
calls that don't use an explicit loader.The changes from this codemod look like the following:
Pickle
Python's
pickle
module is notoriouly insecure. While it is very useful for serializing and deserializing Python objects, it is not safe to usepickle
to load data from untrusted sources. This is becausepickle
can execute arbitrary code when loading data. This can be exploited by an attacker to execute arbitrary code on your system. Unlikeyaml
there is no concept of a "safe" loader inpickle
. Therefore, it is recommended to avoidpickle
and to use a different serialization format such asjson
oryaml
when working with untrusted data.However, if you must use
pickle
to load data from an untrusted source, we recommend using the open-sourcefickling
library.fickling
is a drop-in replacement forpickle
that validates the data before loading it and checks for the possibility of code execution. This makes it much safer (although still not entirely safe) to usepickle
to load data from untrusted sources.This codemod replaces calls to
pickle.load
withfickling.load
in Python code. It also adds an import statement forfickling
if it is not already present.The changes look like the following:
I have additional improvements ready for this repo! If you want to see them, leave the comment:
... and I will open a new PR right away!
🧚🤖Powered by Pixeebot (codemod ID: defectdojo:python/avoid-insecure-deserialization)