9.6 Focus on Security: pickle Serialization and Deserialization

  • Python Standard Library’s pickle module can serialize objects into in a Python-specific data format
  • Caution: The Python documentation provides the following warnings about pickle:

Pickle files can be hacked. If you receive a raw pickle file over the network, don’t trust it! It could have malicious code in it, that would run arbitrary Python when you try to de-pickle it. However, if you are doing your own pickle writing and reading, you’re safe (provided no one else has access to the pickle file, of course.)”

“Pickle is a protocol which allows the serialization of arbitrarily complex Python objects. As such, it is specific to Python and cannot be used to communicate with applications written in other languages. It is also insecure by default: deserializing pickle data coming from an untrusted source can execute arbitrary code, if the data was crafted by a skilled attacker.”

  • We do not recommend using pickle, but it’s been used for many years, so you’re likely to encounter it in legacy code—old code that’s often no longer supported.

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.