In the documentation of Python's jsonpickle module for JSON serialization and deserialization it states that
Loading a JSON string from an untrusted source represents a potential security vulnerability. jsonpickle makes no attempt to sanitize the input
But I wonder how is it possible for an attacker to execute arbitrary code via JSON messages?
Also, what is the best way to sanitize the input as suggested in the documentation? JSON data in my application is not trust-worthy (it came from the clients that send JSON messages).
JSON alone is not much of a threat. After all, it's only a data-interchange format. The real security concerns with JSON arise in the way that it is used. If misused, JSON-based applications can become vulnerable to attacks such as JSON hijacking and JSON injection.
JavaScript Object Notation (JSON) security performs deep inspection of incoming packets/requests for web applications that use the JSON protocol to exchange data over HTTP.
The built-in functions (JSON. parse()) are not vulnerable so you can use them safely. However, custom deserialization packages for JavaScript have different types of vulnerabilities, depending on the approach used to deserialize data.
loads() method can be used to parse a valid JSON string and convert it into a Python Dictionary. It is mainly used for deserializing native string, byte, or byte array which consists of JSON data into Python Dictionary.
jsonpickle
is not JSON. jsonpickle
allows to create arbitrary Python-Objects that potentially do harmful things. Sanitizing means, that the JSON objects only contain data, that can be interpreted by jsonpickle
. Normally wrong data would lead to exceptions, but can may be used to trigger unwanted behavior.
The __reduce__
exploit (see, for example Into The Jar | Exploitation of jsonpickle)
jsonpickle.decode('{"py/object": "list", "py/reduce":[{"py/type": "subprocess.Popen"}, ["ls"], null, null, null]}')
is only one direct way to execute any command. More subtle ways depend on your actual code.
So the short answer is, not to use jsonpickle
in an untrusted environment. Use normal JSON and check the input before using it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With