@@ -23,6 +23,14 @@ def _make_tar_with_member(tar_path: str, member_name: str, content: bytes = b"da
2323 tf .addfile (info , io .BytesIO (content ))
2424
2525
26+ def _make_tar_with_symlink (tar_path : str , member_name : str , link_target : str ) -> None :
27+ with tarfile .open (tar_path , "w" ) as tf :
28+ info = tarfile .TarInfo (name = member_name )
29+ info .type = tarfile .SYMTYPE
30+ info .linkname = link_target
31+ tf .addfile (info )
32+
33+
2634def test_unpack_zip_file_to_temporary_directory_rejects_path_traversal ():
2735 # Zip Slip: a malicious archive entry named with ../ segments must not be extracted
2836 # outside of the target directory, even though zipfile.extractall would otherwise allow it.
@@ -69,6 +77,15 @@ def test_unpack_tar_file_to_temporary_directory_rejects_path_traversal():
6977 assert not os .path .exists ("/tmp/dcicutils_tar_slip_poc.txt" )
7078
7179
80+ def test_unpack_tar_file_to_temporary_directory_rejects_escaping_symlink ():
81+ with temporary_directory () as work_dir :
82+ tar_path = os .path .join (work_dir , "evil_symlink.tar" )
83+ _make_tar_with_symlink (tar_path , "escape" , "../../../tmp" )
84+ with pytest .raises (tarfile .FilterError ):
85+ with unpack_tar_file_to_temporary_directory (tar_path ):
86+ pass
87+
88+
7289def test_unpack_tar_file_to_temporary_directory_extracts_benign_archive ():
7390 with temporary_directory () as work_dir :
7491 tar_path = os .path .join (work_dir , "benign.tar" )
0 commit comments