1111
1212from __future__ import annotations
1313
14+ import os
15+ import tarfile
1416import tempfile
1517import unittest
1618import zipfile
17- import tarfile
1819from pathlib import Path
1920from urllib .error import ContentTooShortError , HTTPError
2021
@@ -80,7 +81,7 @@ def test_valid_zip_extraction(self):
8081 extract_dir .mkdir ()
8182
8283 # Create zip with normal file structure
83- with zipfile .ZipFile (zip_path , 'w' ) as zf :
84+ with zipfile .ZipFile (zip_path , "w" ) as zf :
8485 zf .writestr ("normal_file.txt" , "This is a normal file" )
8586 zf .writestr ("subdir/nested_file.txt" , "This is a nested file" )
8687 zf .writestr ("another_file.json" , '{"key": "value"}' )
@@ -95,7 +96,7 @@ def test_valid_zip_extraction(self):
9596 self .assertTrue ((extract_dir / "another_file.json" ).exists ())
9697
9798 # Verify content
98- with open (extract_dir / "normal_file.txt" ) as f :
99+ with open (extract_dir / "normal_file.txt" , "r" ) as f :
99100 self .assertEqual (f .read (), "This is a normal file" )
100101
101102 except Exception as e :
@@ -110,7 +111,7 @@ def test_malicious_zip_path_traversal(self):
110111 extract_dir .mkdir ()
111112
112113 # Create zip with malicious paths
113- with zipfile .ZipFile (zip_path , 'w' ) as zf :
114+ with zipfile .ZipFile (zip_path , "w" ) as zf :
114115 # Try to write outside extraction directory
115116 zf .writestr ("../../../etc/malicious.txt" , "malicious content" )
116117 zf .writestr ("normal_file.txt" , "normal content" )
@@ -130,7 +131,7 @@ def test_valid_tar_extraction(self):
130131 extract_dir .mkdir ()
131132
132133 # Create tar with normal file structure
133- with tarfile .open (tar_path , ' w:gz' ) as tf :
134+ with tarfile .open (tar_path , " w:gz" ) as tf :
134135 # Create temporary files to add to tar
135136 temp_file1 = Path (tmp_dir ) / "temp1.txt"
136137 temp_file1 .write_text ("This is a normal file" )
@@ -149,7 +150,7 @@ def test_valid_tar_extraction(self):
149150 self .assertTrue ((extract_dir / "subdir" / "nested_file.txt" ).exists ())
150151
151152 # Verify content
152- with open (extract_dir / "normal_file.txt" ) as f :
153+ with open (extract_dir / "normal_file.txt" , "r" ) as f :
153154 self .assertEqual (f .read (), "This is a normal file" )
154155
155156 except Exception as e :
@@ -164,7 +165,7 @@ def test_malicious_tar_path_traversal(self):
164165 extract_dir .mkdir ()
165166
166167 # Create tar with malicious paths
167- with tarfile .open (tar_path , ' w:gz' ) as tf :
168+ with tarfile .open (tar_path , " w:gz" ) as tf :
168169 # Create a temporary file
169170 temp_file = Path (tmp_dir ) / "temp.txt"
170171 temp_file .write_text ("malicious content" )
@@ -186,9 +187,9 @@ def test_absolute_path_protection(self):
186187 extract_dir = Path (tmp_dir ) / "extract"
187188 extract_dir .mkdir ()
188189
189- with zipfile .ZipFile (zip_path , 'w' ) as zf :
190+ with zipfile .ZipFile (zip_path , "w" ) as zf :
190191 # Try to use absolute path
191- zf .writestr ("/etc/passwd " , "malicious content" )
192+ zf .writestr ("/etc/passwd_bad " , "malicious content" )
192193
193194 # This should raise ValueError due to absolute path detection
194195 with self .assertRaises (ValueError ) as context :
@@ -205,14 +206,14 @@ def test_malicious_symlink_protection(self):
205206 extract_dir .mkdir ()
206207
207208 # Create tar with malicious symlink
208- with tarfile .open (tar_path , ' w:gz' ) as tf :
209+ with tarfile .open (tar_path , " w:gz" ) as tf :
209210 temp_file = Path (tmp_dir ) / "normal.txt"
210211 temp_file .write_text ("normal content" )
211212 tf .add (temp_file , arcname = "normal.txt" )
212213
213214 symlink_info = tarfile .TarInfo (name = "malicious_symlink.txt" )
214215 symlink_info .type = tarfile .SYMTYPE
215- symlink_info .linkname = "../../../etc/passwd "
216+ symlink_info .linkname = "../../../etc/passwd_bad "
216217 symlink_info .size = 0
217218 tf .addfile (symlink_info )
218219
@@ -231,14 +232,14 @@ def test_malicious_hardlink_protection(self):
231232 extract_dir .mkdir ()
232233
233234 # Create tar with malicious hard link
234- with tarfile .open (tar_path , ' w:gz' ) as tf :
235+ with tarfile .open (tar_path , " w:gz" ) as tf :
235236 temp_file = Path (tmp_dir ) / "normal.txt"
236237 temp_file .write_text ("normal content" )
237238 tf .add (temp_file , arcname = "normal.txt" )
238239
239240 hardlink_info = tarfile .TarInfo (name = "malicious_hardlink.txt" )
240241 hardlink_info .type = tarfile .LNKTYPE
241- hardlink_info .linkname = "/etc/passwd "
242+ hardlink_info .linkname = "/etc/passwd_bad "
242243 hardlink_info .size = 0
243244 tf .addfile (hardlink_info )
244245
@@ -249,6 +250,5 @@ def test_malicious_hardlink_protection(self):
249250 self .assertTrue ("unsafe path" in error_msg or "hardlink" in error_msg )
250251
251252
252-
253253if __name__ == "__main__" :
254254 unittest .main ()
0 commit comments