@@ -1935,6 +1935,21 @@ def _loads(s, /, *, fix_imports=True, encoding="ASCII", errors="strict",
19351935def _main (args = None ):
19361936 import argparse
19371937 import pprint
1938+
1939+ _SAFE_BUILTINS = frozenset ({
1940+ 'range' , 'complex' , 'set' , 'frozenset' , 'slice' ,
1941+ 'list' , 'tuple' , 'dict' , 'int' , 'float' , 'bool' ,
1942+ 'bytes' , 'bytearray' , 'str' , 'NoneType' ,
1943+ })
1944+
1945+ class _SafeUnpickler (_Unpickler ):
1946+ def find_class (self , module , name ):
1947+ if module == 'builtins' and name in _SAFE_BUILTINS :
1948+ return super ().find_class (module , name )
1949+ raise UnpicklingError (
1950+ f"Global '{ module } .{ name } ' is forbidden in CLI mode"
1951+ )
1952+
19381953 parser = argparse .ArgumentParser (
19391954 description = 'display contents of the pickle files' ,
19401955 )
@@ -1944,10 +1959,10 @@ def _main(args=None):
19441959 args = parser .parse_args (args )
19451960 for fn in args .pickle_file :
19461961 if fn == '-' :
1947- obj = load (sys .stdin .buffer )
1962+ obj = _SafeUnpickler (sys .stdin .buffer ). load ( )
19481963 else :
19491964 with open (fn , 'rb' ) as f :
1950- obj = load ( f )
1965+ obj = _SafeUnpickler ( f ). load ( )
19511966 pprint .pprint (obj )
19521967
19531968
0 commit comments