Do Pickle and Dill have similar levels of risk of containing malicious script?

前端 未结 3 1595
伪装坚强ぢ
伪装坚强ぢ 2020-12-21 23:57

Dill is obviously a very useful module, and it seems as long as you manage the files carefully it is relatively safe. But I was put off by the statement:

3条回答
  •  无人及你
    2020-12-22 00:04

    Yes

    Because Pickle allows you to override the object serialization and deserialization, via

    object.__getstate__()
    

    Classes can further influence how their instances are pickled; if the class defines the method __getstate__(), it is called and the returned object is pickled as the contents for the instance, instead of the contents of the instance’s dictionary. If the __getstate__() method is absent, the instance’s __dict__ is pickled as usual.

    object.__setstate__(state)
    

    Upon unpickling, if the class defines __setstate__(), it is called with the unpickled state. In that case, there is no requirement for the state object to be a dictionary. Otherwise, the pickled state must be a dictionary and its items are assigned to the new instance’s dictionary.

    Because these functions can execute arbitrary code at the user's permission level, it is relatively easy to write a malicious deserializer -- e.g. one that deletes all the files on your hard disk.

提交回复
热议问题