@@ -3299,7 +3299,7 @@ class TestExtractionFilters(unittest.TestCase):
32993299 destdir = outerdir / 'dest'
33003300
33013301 @contextmanager
3302- def check_context (self , tar , filter , * , check_flag = True ):
3302+ def check_context (self , tar , filter , * , check_flag = True , ignored_trees = () ):
33033303 """Extracts `tar` to `self.destdir` and allows checking the result
33043304
33053305 If an error occurs, it must be checked using `expect_exception`
@@ -3312,6 +3312,10 @@ def check_context(self, tar, filter, *, check_flag=True):
33123312 A file called 'flag' is made in outerdir (i.e. outside destdir)
33133313 before extraction; it should not be altered nor should its contents
33143314 be read/copied.
3315+
3316+ *ignored_trees* is a set of directories to remove (including their
3317+ contents) right after the archive is extracted. It is a workaround
3318+ for Path.glob() failing to get all files in Python 3.10 and below.
33153319 """
33163320 with support .temp_dir (self .outerdir ):
33173321 flag_path = self .outerdir / 'flag'
@@ -3323,6 +3327,8 @@ def check_context(self, tar, filter, *, check_flag=True):
33233327 self .reraise_exception = True
33243328 self .expected_paths = set ()
33253329 else :
3330+ for ignored_tree in ignored_trees :
3331+ os_helper .rmtree ((self .destdir / ignored_tree ).resolve ())
33263332 self .raised_exception = None
33273333 self .reraise_exception = False
33283334 self .expected_paths = set (self .outerdir .glob ('**/*' ))
@@ -3534,15 +3540,14 @@ def test_realpath_limit_attack(self):
35343540
35353541 with (self .subTest ('fully_trusted' ),
35363542 self .check_context (arc .open (), filter = 'fully_trusted' ,
3537- check_flag = False )):
3543+ check_flag = False , ignored_trees = { component } )):
35383544 if sys .platform == 'win32' :
35393545 self .expect_exception ((FileNotFoundError , FileExistsError ))
35403546 elif self .raised_exception :
35413547 # Cannot symlink/hardlink: tarfile falls back to getmember()
35423548 self .expect_exception (KeyError )
35433549 # Otherwise, this block should never enter.
35443550 else :
3545- self .expect_any_tree (component )
35463551 self .expect_file ('flaglink' , content = 'overwrite' )
35473552 self .expect_file ('../newfile' , content = 'new' )
35483553 self .expect_file ('escape' , type = tarfile .SYMTYPE )
0 commit comments