@@ -3338,7 +3338,7 @@ class TestExtractionFilters(unittest.TestCase):
3338
3338
destdir = outerdir / 'dest'
3339
3339
3340
3340
@contextmanager
3341
- def check_context (self , tar , filter , * , check_flag = True ):
3341
+ def check_context (self , tar , filter , * , check_flag = True , ignored_trees = () ):
3342
3342
"""Extracts `tar` to `self.destdir` and allows checking the result
3343
3343
3344
3344
If an error occurs, it must be checked using `expect_exception`
@@ -3351,6 +3351,10 @@ def check_context(self, tar, filter, *, check_flag=True):
3351
3351
A file called 'flag' is made in outerdir (i.e. outside destdir)
3352
3352
before extraction; it should not be altered nor should its contents
3353
3353
be read/copied.
3354
+
3355
+ *ignored_trees* is a set of directories to remove (including their
3356
+ contents) right after the archive is extracted. It is a workaround
3357
+ for Path.glob() failing to get all files in Python 3.10 and below.
3354
3358
"""
3355
3359
with os_helper .temp_dir (self .outerdir ):
3356
3360
flag_path = self .outerdir / 'flag'
@@ -3362,6 +3366,8 @@ def check_context(self, tar, filter, *, check_flag=True):
3362
3366
self .reraise_exception = True
3363
3367
self .expected_paths = set ()
3364
3368
else :
3369
+ for ignored_tree in ignored_trees :
3370
+ os_helper .rmtree ((self .destdir / ignored_tree ).resolve ())
3365
3371
self .raised_exception = None
3366
3372
self .reraise_exception = False
3367
3373
self .expected_paths = set (self .outerdir .glob ('**/*' ))
@@ -3573,15 +3579,14 @@ def test_realpath_limit_attack(self):
3573
3579
3574
3580
with (self .subTest ('fully_trusted' ),
3575
3581
self .check_context (arc .open (), filter = 'fully_trusted' ,
3576
- check_flag = False )):
3582
+ check_flag = False , ignored_trees = { component } )):
3577
3583
if sys .platform == 'win32' :
3578
3584
self .expect_exception ((FileNotFoundError , FileExistsError ))
3579
3585
elif self .raised_exception :
3580
3586
# Cannot symlink/hardlink: tarfile falls back to getmember()
3581
3587
self .expect_exception (KeyError )
3582
3588
# Otherwise, this block should never enter.
3583
3589
else :
3584
- self .expect_any_tree (component )
3585
3590
self .expect_file ('flaglink' , content = 'overwrite' )
3586
3591
self .expect_file ('../newfile' , content = 'new' )
3587
3592
self .expect_file ('escape' , type = tarfile .SYMTYPE )
0 commit comments