Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2841,9 +2841,11 @@ def makelink_with_filter(self, tarinfo, targetpath,
"makelink_with_filter: if filter_function is not None, "
+ "extraction_root must also not be None")
try:
filter_function(
filtered = filter_function(
unfiltered.replace(name=tarinfo.name, deep=False),
extraction_root)
if filtered is None:
return
filtered = filter_function(unfiltered, extraction_root)
except _FILTER_ERRORS as cause:
raise LinkFallbackError(tarinfo, unfiltered.name) from cause
Expand Down
31 changes: 28 additions & 3 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4621,9 +4621,15 @@ def test_sneaky_hardlink_fallback(self):
for filter in 'tar', 'fully_trusted':
with self.subTest(filter), self.check_context(arc.open(), filter):
if not os_helper.can_symlink():
self.expect_file("a/t/dummy")
self.expect_file("b/")
self.expect_file("c/")
if filter == 'tar':
self.expect_exception(
tarfile.LinkFallbackError,
"link 'boom' would be extracted as a copy of "
+ "'c/escape', which was rejected")
else:
self.expect_file("a/t/dummy")
self.expect_file("b/")
self.expect_file("c/")
else:
self.expect_file("a/t/dummy")
self.expect_file("b/")
Expand Down Expand Up @@ -4820,6 +4826,25 @@ def testing_filter(member, path):
if os_helper.can_chmod():
self.assertFalse(path.stat().st_mode & stat.S_IWUSR)

@symlink_test
def test_extract_filters_target_none(self):
# Test that when extract() falls back to extracting (rather than
# linking) a hardlink target, the member is skipped if the filter
# returns None.
with ArchiveMaker() as arc:
arc.add('a/b/s', symlink_to='../escape')
arc.add('q', hardlink_to='a/b/s')
def filter_unsafe_members(member, path):
try:
return tarfile.data_filter(member, path)
except tarfile.FilterError as error:
return None
with self.check_context(arc.open(), filter_unsafe_members):
if os_helper.can_symlink():
self.expect_file('a/b/s', symlink_to='../escape')
else:
self.expect_file('a/b/') # symlink is not extracted

def test_link_fallback_normalizes(self):
# Make sure hardlink fallbacks work for non-normalized paths for all
# filters
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
In :mod:`tarfile`, when extracting a link falls back to extracting a member
of the archive, skip the member when the filter function returns None when
called with the extracted member's name replaced with the link's.
Loading