Skip to content

Commit e7e0b35

Browse files
gh-160097: Reject links replacing the destination in tarfile.tar_filter (#160098)
1 parent d6b77c8 commit e7e0b35

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
@@ -1131,6 +1131,11 @@ reused in custom filters:
11311131
- :ref:`Refuse <tarfile-extraction-refuse>` to extract files whose absolute
11321132
path (after following symlinks) would end up outside the destination.
11331133
This raises :class:`~tarfile.OutsideDestinationError`.
1134+
- :ref:`Refuse <tarfile-extraction-refuse>` to extract links (hard or soft)
1135+
whose name refers to the destination directory itself.
1136+
This raises :class:`~tarfile.OutsideDestinationError`.
1137+
Note that such files are refused even on platforms that do not support
1138+
symbolic links.
11341139
- Clear high mode bits (setuid, setgid, sticky) and group/other write bits
11351140
(:const:`~stat.S_IWGRP` | :const:`~stat.S_IWOTH`).
11361141

‎Lib/tarfile.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,12 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
854854
strict=os.path.ALLOW_MISSING)
855855
if os.path.commonpath([target_path, dest_path]) != dest_path:
856856
raise OutsideDestinationError(member, target_path)
857+
# A link member named after the destination directory itself
858+
# would replace it with a (sym)link, redirecting the destination
859+
# for all subsequent members.
860+
if ((member.islnk() or member.issym())
861+
and os.path.normpath(name) == os.curdir):
862+
raise OutsideDestinationError(member, target_path)
857863
# Limit permissions (no high bits, and go-w)
858864
mode = member.mode
859865
if mode is not None:
@@ -889,11 +895,6 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
889895
if member.islnk() or member.issym():
890896
if os.path.isabs(member.linkname):
891897
raise AbsoluteLinkError(member)
892-
# A link member that resolves to the destination directory itself
893-
# would replace it with a (sym)link, redirecting the destination
894-
# for all subsequent members.
895-
if target_path == dest_path:
896-
raise OutsideDestinationError(member, target_path)
897898
normalized = os.path.normpath(member.linkname)
898899
if normalized != member.linkname:
899900
new_attrs['linkname'] = normalized

‎Lib/test/test_tarfile.py‎

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

4674-
with self.check_context(arc.open(), 'data'):
4675-
self.expect_exception(tarfile.OutsideDestinationError)
4674+
for filter in 'tar', 'data':
4675+
with self.subTest(name=name, filter=filter):
4676+
with self.check_context(arc.open(), filter):
4677+
self.expect_exception(tarfile.OutsideDestinationError)
46764678

46774679
@symlink_test
46784680
@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)