Skip to content

Commit 0cc0073

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 9eaf48a commit 0cc0073

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
@@ -2672,9 +2672,11 @@ def makelink_with_filter(self, tarinfo, targetpath,
26722672
"makelink_with_filter: if filter_function is not None, "
26732673
+ "extraction_root must also not be None")
26742674
try:
2675-
filter_function(
2675+
filtered = filter_function(
26762676
unfiltered.replace(name=tarinfo.name, deep=False),
26772677
extraction_root)
2678+
if filtered is None:
2679+
return
26782680
filtered = filter_function(unfiltered, extraction_root)
26792681
except _FILTER_ERRORS as cause:
26802682
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
@@ -4071,9 +4071,15 @@ def test_sneaky_hardlink_fallback(self):
40714071
for filter in 'tar', 'fully_trusted':
40724072
with self.subTest(filter), self.check_context(arc.open(), filter):
40734073
if not os_helper.can_symlink():
4074-
self.expect_file("a/t/dummy")
4075-
self.expect_file("b/")
4076-
self.expect_file("c/")
4074+
if filter == 'tar':
4075+
self.expect_exception(
4076+
tarfile.LinkFallbackError,
4077+
"link 'boom' would be extracted as a copy of "
4078+
+ "'c/escape', which was rejected")
4079+
else:
4080+
self.expect_file("a/t/dummy")
4081+
self.expect_file("b/")
4082+
self.expect_file("c/")
40774083
else:
40784084
self.expect_file("a/t/dummy")
40794085
self.expect_file("b/")
@@ -4252,6 +4258,25 @@ def testing_filter(member, path):
42524258
if os_helper.can_chmod():
42534259
self.assertFalse(path.stat().st_mode & stat.S_IWUSR)
42544260

4261+
@symlink_test
4262+
def test_extract_filters_target_none(self):
4263+
# Test that when extract() falls back to extracting (rather than
4264+
# linking) a hardlink target, the member is skipped if the filter
4265+
# returns None.
4266+
with ArchiveMaker() as arc:
4267+
arc.add('a/b/s', symlink_to='../escape')
4268+
arc.add('q', hardlink_to='a/b/s')
4269+
def filter_unsafe_members(member, path):
4270+
try:
4271+
return tarfile.data_filter(member, path)
4272+
except tarfile.FilterError as error:
4273+
return None
4274+
with self.check_context(arc.open(), filter_unsafe_members):
4275+
if os_helper.can_symlink():
4276+
self.expect_file('a/b/s', symlink_to='../escape')
4277+
else:
4278+
self.expect_file('a/b/') # symlink is not extracted
4279+
42554280
def test_link_fallback_normalizes(self):
42564281
# Make sure hardlink fallbacks work for non-normalized paths for all
42574282
# 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)