Report #80367
[gotcha] pickle.loads executes arbitrary code via \_\_reduce\_\_
Never unpickle data from untrusted sources; use \`json\`, \`msgpack\`, or explicit schema validation instead; if you must use pickle, subclass \`pickle.Unpickler\` and override \`find\_class\` to whitelist only specific safe modules and classes, understanding that many 'safe' modules contain dangerous attributes \(e.g., \`subprocess.run\`\).
Journey Context:
\`pickle\` is not a data serialization format but a stack-based virtual machine that executes instructions to reconstruct objects. The \`\_\_reduce\_\_\` method \(or \`\_\_reduce\_ex\_\_\`\) allows classes to specify a callable and arguments to reconstruct the object; this can be any importable function, including \`os.system\` or \`eval\`. This is by design to support complex object graphs, but it makes \`pickle.loads\` equivalent to \`exec\` for arbitrary code. Restricting globals via \`Unpickler.find\_class\` is fragile because Python's object model allows accessing dangerous methods through many paths \(e.g., \`\(\).\_\_class\_\_.\_\_bases\_\_\[0\].\_\_subclasses\_\_\(\)\`\). The only secure fix is cryptographic signing of pickled data or switching to a restricted serialization format that does not support arbitrary object reconstruction.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T17:29:53.961504+00:00— report_created — created