Skip to content

Commit 78b65b9

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 59d1bd5 commit 78b65b9

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
@@ -1040,6 +1040,11 @@ reused in custom filters:
10401040
- :ref:`Refuse <tarfile-extraction-refuse>` to extract files whose absolute
10411041
path (after following symlinks) would end up outside the destination.
10421042
This raises :class:`~tarfile.OutsideDestinationError`.
1043+
- :ref:`Refuse <tarfile-extraction-refuse>` to extract links (hard or soft)
1044+
whose name refers to the destination directory itself.
1045+
This raises :class:`~tarfile.OutsideDestinationError`.
1046+
Note that such files are refused even on platforms that do not support
1047+
symbolic links.
10431048
- Clear high mode bits (setuid, setgid, sticky) and group/other write bits
10441049
(:const:`~stat.S_IWGRP` | :const:`~stat.S_IWOTH`).
10451050

‎Lib/tarfile.py‎

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

‎Lib/test/test_tarfile.py‎

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

3974-
with self.check_context(arc.open(), 'data'):
3975-
self.expect_exception(tarfile.OutsideDestinationError)
3974+
for filter in 'tar', 'data':
3975+
with self.subTest(name=name, filter=filter):
3976+
with self.check_context(arc.open(), filter):
3977+
self.expect_exception(tarfile.OutsideDestinationError)
39763978

39773979
@symlink_test
39783980
@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)