Skip to content

Commit 0901ad4

Browse files
[3.14] gh-160097: Reject links replacing the destination in tarfile.tar_filter (GH-160098) (#160340)
(cherry picked from commit e7e0b35) Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent f7a7b4b commit 0901ad4

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
@@ -1107,6 +1107,11 @@ reused in custom filters:
11071107
- :ref:`Refuse <tarfile-extraction-refuse>` to extract files whose absolute
11081108
path (after following symlinks) would end up outside the destination.
11091109
This raises :class:`~tarfile.OutsideDestinationError`.
1110+
- :ref:`Refuse <tarfile-extraction-refuse>` to extract links (hard or soft)
1111+
whose name refers to the destination directory itself.
1112+
This raises :class:`~tarfile.OutsideDestinationError`.
1113+
Note that such files are refused even on platforms that do not support
1114+
symbolic links.
11101115
- Clear high mode bits (setuid, setgid, sticky) and group/other write bits
11111116
(:const:`~stat.S_IWGRP` | :const:`~stat.S_IWOTH`).
11121117

‎Lib/tarfile.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,12 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
835835
strict=os.path.ALLOW_MISSING)
836836
if os.path.commonpath([target_path, dest_path]) != dest_path:
837837
raise OutsideDestinationError(member, target_path)
838+
# A link member named after the destination directory itself
839+
# would replace it with a (sym)link, redirecting the destination
840+
# for all subsequent members.
841+
if ((member.islnk() or member.issym())
842+
and os.path.normpath(name) == os.curdir):
843+
raise OutsideDestinationError(member, target_path)
838844
# Limit permissions (no high bits, and go-w)
839845
mode = member.mode
840846
if mode is not None:
@@ -870,11 +876,6 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
870876
if member.islnk() or member.issym():
871877
if os.path.isabs(member.linkname):
872878
raise AbsoluteLinkError(member)
873-
# A link member that resolves to the destination directory itself
874-
# would replace it with a (sym)link, redirecting the destination
875-
# for all subsequent members.
876-
if target_path == dest_path:
877-
raise OutsideDestinationError(member, target_path)
878879
normalized = os.path.normpath(member.linkname)
879880
if normalized != member.linkname:
880881
new_attrs['linkname'] = normalized

‎Lib/test/test_tarfile.py‎

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

4360-
with self.check_context(arc.open(), 'data'):
4361-
self.expect_exception(tarfile.OutsideDestinationError)
4360+
for filter in 'tar', 'data':
4361+
with self.subTest(name=name, filter=filter):
4362+
with self.check_context(arc.open(), filter):
4363+
self.expect_exception(tarfile.OutsideDestinationError)
43624364

43634365
@symlink_test
43644366
@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)