Skip to content

Commit 98d9fff

Browse files
StanFromIrelandmiss-islington
authored andcommitted
gh-160097: Reject links replacing the destination in tarfile.tar_filter (GH-160098)
(cherry picked from commit e7e0b35) Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent bb130d8 commit 98d9fff

4 files changed

Lines changed: 20 additions & 7 deletions

File tree

‎Doc/library/tarfile.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,11 @@ reused in custom filters:
10421042
- :ref:`Refuse <tarfile-extraction-refuse>` to extract files whose absolute
10431043
path (after following symlinks) would end up outside the destination.
10441044
This raises :class:`~tarfile.OutsideDestinationError`.
1045+
- :ref:`Refuse <tarfile-extraction-refuse>` to extract links (hard or soft)
1046+
whose name refers to the destination directory itself.
1047+
This raises :class:`~tarfile.OutsideDestinationError`.
1048+
Note that such files are refused even on platforms that do not support
1049+
symbolic links.
10451050
- Clear high mode bits (setuid, setgid, sticky) and group/other write bits
10461051
(:const:`~stat.S_IWGRP` | :const:`~stat.S_IWOTH`).
10471052

‎Lib/tarfile.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,12 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
790790
strict=os.path.ALLOW_MISSING)
791791
if os.path.commonpath([target_path, dest_path]) != dest_path:
792792
raise OutsideDestinationError(member, target_path)
793+
# A link member named after the destination directory itself
794+
# would replace it with a (sym)link, redirecting the destination
795+
# for all subsequent members.
796+
if ((member.islnk() or member.issym())
797+
and os.path.normpath(name) == os.curdir):
798+
raise OutsideDestinationError(member, target_path)
793799
# Limit permissions (no high bits, and go-w)
794800
mode = member.mode
795801
if mode is not None:
@@ -825,11 +831,6 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
825831
if member.islnk() or member.issym():
826832
if os.path.isabs(member.linkname):
827833
raise AbsoluteLinkError(member)
828-
# A link member that resolves to the destination directory itself
829-
# would replace it with a (sym)link, redirecting the destination
830-
# for all subsequent members.
831-
if target_path == dest_path:
832-
raise OutsideDestinationError(member, target_path)
833834
normalized = os.path.normpath(member.linkname)
834835
if normalized != member.linkname:
835836
new_attrs['linkname'] = normalized

‎Lib/test/test_tarfile.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4072,8 +4072,10 @@ def test_link_at_destination(self):
40724072
t.linkname = '.'
40734073
arc.tar_w.addfile(t)
40744074

4075-
with self.check_context(arc.open(), 'data'):
4076-
self.expect_exception(tarfile.OutsideDestinationError)
4075+
for filter in 'tar', 'data':
4076+
with self.subTest(name=name, filter=filter):
4077+
with self.check_context(arc.open(), filter):
4078+
self.expect_exception(tarfile.OutsideDestinationError)
40774079

40784080
@symlink_test
40794081
@os_helper.skip_unless_symlink
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.. cve: CVE-2026-114583
2+
3+
:func:`tarfile.tar_filter` now rejects link members whose path resolves to
4+
the destination directory itself, as :func:`tarfile.data_filter` already
5+
did.

0 commit comments

Comments
 (0)