Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Doc/library/tarfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,11 @@ reused in custom filters:
- :ref:`Refuse <tarfile-extraction-refuse>` to extract files whose absolute
path (after following symlinks) would end up outside the destination.
This raises :class:`~tarfile.OutsideDestinationError`.
- :ref:`Refuse <tarfile-extraction-refuse>` to extract links (hard or soft)
whose name refers to the destination directory itself.
This raises :class:`~tarfile.OutsideDestinationError`.
Note that such files are refused even on platforms that do not support
symbolic links.
- Clear high mode bits (setuid, setgid, sticky) and group/other write bits
(:const:`~stat.S_IWGRP` | :const:`~stat.S_IWOTH`).

Expand Down
11 changes: 6 additions & 5 deletions Lib/tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,12 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
strict=os.path.ALLOW_MISSING)
if os.path.commonpath([target_path, dest_path]) != dest_path:
raise OutsideDestinationError(member, target_path)
# A link member named after the destination directory itself
# would replace it with a (sym)link, redirecting the destination
# for all subsequent members.
if ((member.islnk() or member.issym())
and os.path.normpath(name) == os.curdir):
raise OutsideDestinationError(member, target_path)
# Limit permissions (no high bits, and go-w)
mode = member.mode
if mode is not None:
Expand Down Expand Up @@ -824,11 +830,6 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
if member.islnk() or member.issym():
if os.path.isabs(member.linkname):
raise AbsoluteLinkError(member)
# A link member that resolves to the destination directory itself
# would replace it with a (sym)link, redirecting the destination
# for all subsequent members.
if target_path == dest_path:
raise OutsideDestinationError(member, target_path)
normalized = os.path.normpath(member.linkname)
if normalized != member.linkname:
new_attrs['linkname'] = normalized
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_tarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3971,8 +3971,10 @@ def test_link_at_destination(self):
t.linkname = '.'
arc.tar_w.addfile(t)

with self.check_context(arc.open(), 'data'):
self.expect_exception(tarfile.OutsideDestinationError)
for filter in 'tar', 'data':
with self.subTest(name=name, filter=filter):
with self.check_context(arc.open(), filter):
self.expect_exception(tarfile.OutsideDestinationError)

@symlink_test
@os_helper.skip_unless_symlink
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. cve: CVE-2026-114583
:func:`tarfile.tar_filter` now rejects link members whose path resolves to
the destination directory itself, as :func:`tarfile.data_filter` already
did.
Loading