security - Python: can I safely unpickle untrusted data? -


the pickle module documentation says right @ beginning:

warning: pickle module not intended secure against erroneous or maliciously constructed data. never unpickle data received untrusted or unauthenticated source.

however, further down under restricting globals seems describe way make unpickling data safe using whitelist of allowed objects.

does mean can safely unpickle untrusted data if use restrictedunpickler allows "elementary" types, or there additional security issues not addressed method? if there are, there way make unpickling safe (obviously @ cost of not being able unpickle every stream)?

with "elementary types" mean precisely following:

  • bool
  • str, bytes, bytearray
  • int, float, complex
  • tuple, list, dict, set , frozenset

i'd go far saying there no safe way use pickle handle untrusted data.

even restricted globals, dynamic nature of python such determined hacker still has chance of finding way __builtins__ mapping , there crown jewels.

see ned batchelder's blog posts on circumventing restrictions on eval() apply in equal measure pickle.

remember pickle still stack language , cannot foresee possible objects produced allowing arbitrary calls limited set of globals. pickle documentation doesn't mention ext* opcodes allow calling copyreg-installed extensions; you'll have account installed in registry here. takes 1 vector allowing object call turned getattr equivalent defences crumble.

at the least use cryptographic signature data can validate integrity. you'll limit risks, if attacker ever managed steal signing secrets (keys) again slip hacked pickle.

i instead use an existing innocuous format json , add type annotations; e.g. store data in dictionaries type key , convert when loading data.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -