Skip to content

Commit c1f106d

Browse files
miss-islingtonencukouStanFromIreland
authored
[3.10] gh-157265: tarfile: Honor None result of filter for link fallbacks (GH-157266) (#157304)
* 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> * gh-157265: Adjust test for Windows (GH-157334) gh-157266: Adjust test for Windows On Windows (no symlinks, no hardlinks), the behaviour is the same as without the fix in GH-157266: - a/t/dummy is extracted - b/ is extracted - c/ is *not* created (the target, a/t, is not in the archive) - c/escape: c/ is created; escape is skipped (target, c/../../link_here, is not in archive) - c is not recreated as a directory - boom is not created (target is c/escape, which falls back to ..\..\link_here, which does not exist in archive) * Remove @symlink_test (which is there for WASI in 3.11+) --------- Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent 2af3ca5 commit c1f106d

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

‎Lib/tarfile.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2674,9 +2674,11 @@ def makelink_with_filter(self, tarinfo, targetpath,
26742674
"makelink_with_filter: if filter_function is not None, "
26752675
+ "extraction_root must also not be None")
26762676
try:
2677-
filter_function(
2677+
filtered = filter_function(
26782678
unfiltered.replace(name=tarinfo.name, deep=False),
26792679
extraction_root)
2680+
if filtered is None:
2681+
return
26802682
filtered = filter_function(unfiltered, extraction_root)
26812683
except _FILTER_ERRORS as cause:
26822684
raise LinkFallbackError(tarinfo, unfiltered.name) from cause

‎Lib/test/test_tarfile.py‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3977,9 +3977,15 @@ def test_sneaky_hardlink_fallback(self):
39773977
for filter in 'tar', 'fully_trusted':
39783978
with self.subTest(filter), self.check_context(arc.open(), filter):
39793979
if not os_helper.can_symlink():
3980-
self.expect_file("a/t/dummy")
3981-
self.expect_file("b/")
3982-
self.expect_file("c/")
3980+
if filter == 'fully_trusted' or sys.platform == "win32":
3981+
self.expect_file("a/t/dummy")
3982+
self.expect_file("b/")
3983+
self.expect_file("c/")
3984+
else:
3985+
self.expect_exception(
3986+
tarfile.LinkFallbackError,
3987+
"link 'boom' would be extracted as a copy of "
3988+
+ "'c/escape', which was rejected")
39833989
else:
39843990
self.expect_file("a/t/dummy")
39853991
self.expect_file("b/")
@@ -4170,6 +4176,24 @@ def testing_filter(member, path):
41704176
if sys.platform != 'win32':
41714177
self.assertFalse(path.stat().st_mode & stat.S_IWUSR)
41724178

4179+
def test_extract_filters_target_none(self):
4180+
# Test that when extract() falls back to extracting (rather than
4181+
# linking) a hardlink target, the member is skipped if the filter
4182+
# returns None.
4183+
with ArchiveMaker() as arc:
4184+
arc.add('a/b/s', symlink_to='../escape')
4185+
arc.add('q', hardlink_to='a/b/s')
4186+
def filter_unsafe_members(member, path):
4187+
try:
4188+
return tarfile.data_filter(member, path)
4189+
except tarfile.FilterError as error:
4190+
return None
4191+
with self.check_context(arc.open(), filter_unsafe_members):
4192+
if os_helper.can_symlink():
4193+
self.expect_file('a/b/s', symlink_to='../escape')
4194+
else:
4195+
self.expect_file('a/b/') # symlink is not extracted
4196+
41734197
def test_link_fallback_normalizes(self):
41744198
# Make sure hardlink fallbacks work for non-normalized paths for all
41754199
# 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)