Skip to content

Commit 05f187c

Browse files
encukouStanFromIreland
authored andcommitted
gh-157265: tarfile: Honor None result of filter for link fallbacks (GH-157266)
(cherry picked from commit fb2f0bb) Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent 9f9dd10 commit 05f187c

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

Lib/tarfile.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2764,9 +2764,11 @@ def makelink_with_filter(self, tarinfo, targetpath,
27642764
"makelink_with_filter: if filter_function is not None, "
27652765
+ "extraction_root must also not be None")
27662766
try:
2767-
filter_function(
2767+
filtered = filter_function(
27682768
unfiltered.replace(name=tarinfo.name, deep=False),
27692769
extraction_root)
2770+
if filtered is None:
2771+
return
27702772
filtered = filter_function(unfiltered, extraction_root)
27712773
except _FILTER_ERRORS as cause:
27722774
raise LinkFallbackError(tarinfo, unfiltered.name) from cause

Lib/test/test_tarfile.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4418,9 +4418,15 @@ def test_sneaky_hardlink_fallback(self):
44184418
for filter in 'tar', 'fully_trusted':
44194419
with self.subTest(filter), self.check_context(arc.open(), filter):
44204420
if not os_helper.can_symlink():
4421-
self.expect_file("a/t/dummy")
4422-
self.expect_file("b/")
4423-
self.expect_file("c/")
4421+
if filter == 'tar':
4422+
self.expect_exception(
4423+
tarfile.LinkFallbackError,
4424+
"link 'boom' would be extracted as a copy of "
4425+
+ "'c/escape', which was rejected")
4426+
else:
4427+
self.expect_file("a/t/dummy")
4428+
self.expect_file("b/")
4429+
self.expect_file("c/")
44244430
else:
44254431
self.expect_file("a/t/dummy")
44264432
self.expect_file("b/")
@@ -4617,6 +4623,25 @@ def testing_filter(member, path):
46174623
if os_helper.can_chmod():
46184624
self.assertFalse(path.stat().st_mode & stat.S_IWUSR)
46194625

4626+
@symlink_test
4627+
def test_extract_filters_target_none(self):
4628+
# Test that when extract() falls back to extracting (rather than
4629+
# linking) a hardlink target, the member is skipped if the filter
4630+
# returns None.
4631+
with ArchiveMaker() as arc:
4632+
arc.add('a/b/s', symlink_to='../escape')
4633+
arc.add('q', hardlink_to='a/b/s')
4634+
def filter_unsafe_members(member, path):
4635+
try:
4636+
return tarfile.data_filter(member, path)
4637+
except tarfile.FilterError as error:
4638+
return None
4639+
with self.check_context(arc.open(), filter_unsafe_members):
4640+
if os_helper.can_symlink():
4641+
self.expect_file('a/b/s', symlink_to='../escape')
4642+
else:
4643+
self.expect_file('a/b/') # symlink is not extracted
4644+
46204645
def test_link_fallback_normalizes(self):
46214646
# Make sure hardlink fallbacks work for non-normalized paths for all
46224647
# filters
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
In :mod:`tarfile`, when extracting a link falls back to extracting a member
2+
of the archive, skip the member when the filter function returns None when
3+
called with the extracted member's name replaced with the link's.

0 commit comments

Comments
 (0)