Skip to content

Commit c6ada40

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

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
@@ -1114,6 +1114,11 @@ reused in custom filters:
11141114
- :ref:`Refuse <tarfile-extraction-refuse>` to extract files whose absolute
11151115
path (after following symlinks) would end up outside the destination.
11161116
This raises :class:`~tarfile.OutsideDestinationError`.
1117+
- :ref:`Refuse <tarfile-extraction-refuse>` to extract links (hard or soft)
1118+
whose name refers to the destination directory itself.
1119+
This raises :class:`~tarfile.OutsideDestinationError`.
1120+
Note that such files are refused even on platforms that do not support
1121+
symbolic links.
11171122
- Clear high mode bits (setuid, setgid, sticky) and group/other write bits
11181123
(:const:`~stat.S_IWGRP` | :const:`~stat.S_IWOTH`).
11191124

‎Lib/tarfile.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,12 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
848848
strict=os.path.ALLOW_MISSING)
849849
if os.path.commonpath([target_path, dest_path]) != dest_path:
850850
raise OutsideDestinationError(member, target_path)
851+
# A link member named after the destination directory itself
852+
# would replace it with a (sym)link, redirecting the destination
853+
# for all subsequent members.
854+
if ((member.islnk() or member.issym())
855+
and os.path.normpath(name) == os.curdir):
856+
raise OutsideDestinationError(member, target_path)
851857
# Limit permissions (no high bits, and go-w)
852858
mode = member.mode
853859
if mode is not None:
@@ -883,11 +889,6 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
883889
if member.islnk() or member.issym():
884890
if os.path.isabs(member.linkname):
885891
raise AbsoluteLinkError(member)
886-
# A link member that resolves to the destination directory itself
887-
# would replace it with a (sym)link, redirecting the destination
888-
# for all subsequent members.
889-
if target_path == dest_path:
890-
raise OutsideDestinationError(member, target_path)
891892
normalized = os.path.normpath(member.linkname)
892893
if normalized != member.linkname:
893894
new_attrs['linkname'] = normalized

‎Lib/test/test_tarfile.py‎

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

4434-
with self.check_context(arc.open(), 'data'):
4435-
self.expect_exception(tarfile.OutsideDestinationError)
4434+
for filter in 'tar', 'data':
4435+
with self.subTest(name=name, filter=filter):
4436+
with self.check_context(arc.open(), filter):
4437+
self.expect_exception(tarfile.OutsideDestinationError)
44364438

44374439
@symlink_test
44384440
@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)