Why is pickle.loads() on data from users dangerous?
Options
- It is very slow with large objects in memory
- Unpickling can run arbitrary code on your server (Correct option)
- It silently loses numeric precision of values
- It only works on Python 2 and not on Python 3
Answer
Unpickling can execute arbitrary code (remote code execution)
Why
Pickle can rebuild any Python object, and it does so by running instructions stored in the data. A malicious pickle can tell Python to run any command on your server.
That is remote code execution (RCE), the worst kind of vulnerability.
For untrusted data use JSON plus schema validation. Same risk with yaml.load without SafeLoader.
Remember
Never unpickle untrusted data.