Skip to content

Commit 72fbc12

Browse files
gpsheadencukou
authored andcommitted
gh-161323: Refuse drive-relative paths in tarfile extraction filters (GH-161334)
* gh-161323: Refuse drive-relative paths in tarfile extraction filters On Windows, os.path.join() does not put a name such as 'C:foo' below a destination that has no drive, so the 'tar' and 'data' filters now refuse names and link targets with a drive, after normalization. Fixes CVE-2026-107784. reported by Girish Konda ANT-2026-RSL1F4AT (cherry picked from commit e4c751ba63d41660332db162ff7836e933631858) Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com> Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent 3d6d2a6 commit 72fbc12

4 files changed

Lines changed: 69 additions & 11 deletions

File tree

‎Doc/library/tarfile.rst‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,14 +1031,15 @@ reused in custom filters:
10311031
Implements the ``'tar'`` filter.
10321032

10331033
- Strip leading slashes (``/`` and :data:`os.sep`) from filenames.
1034-
- :ref:`Refuse <tarfile-extraction-refuse>` to extract files with absolute
1035-
paths (in case the name is absolute
1036-
even after stripping slashes, e.g. ``C:/foo`` on Windows).
1037-
This raises :class:`~tarfile.AbsolutePathError`.
10381034
- Normalize filenames (:attr:`TarInfo.name`) that contain ``..`` components
10391035
using :func:`os.path.normpath`.
10401036
Note that this removes internal ``..`` components, which may change the
10411037
meaning of the name if it traverses symbolic links.
1038+
- :ref:`Refuse <tarfile-extraction-refuse>` to extract files with absolute
1039+
paths (in case the name is absolute
1040+
even after stripping slashes, e.g. ``C:/foo`` on Windows)
1041+
or with a drive (e.g. ``C:foo`` on Windows).
1042+
This raises :class:`~tarfile.AbsolutePathError`.
10421043
- :ref:`Refuse <tarfile-extraction-refuse>` to extract files whose absolute
10431044
path (after following symlinks) would end up outside the destination.
10441045
This raises :class:`~tarfile.OutsideDestinationError`.
@@ -1056,6 +1057,10 @@ reused in custom filters:
10561057

10571058
Filenames containing ``..`` components are now normalized.
10581059

1060+
.. versionchanged:: next
1061+
1062+
Filenames with a drive that are not absolute are now refused.
1063+
10591064
.. function:: data_filter(member, path)
10601065

10611066
Implements the ``'data'`` filter.
@@ -1068,7 +1073,8 @@ reused in custom filters:
10681073
symbolic links.
10691074

10701075
- :ref:`Refuse <tarfile-extraction-refuse>` to extract links (hard or soft)
1071-
that link to absolute paths, or ones that link outside the destination.
1076+
that link to absolute paths or to paths with a drive,
1077+
or ones that link outside the destination.
10721078

10731079
This raises :class:`~tarfile.AbsoluteLinkError` or
10741080
:class:`~tarfile.LinkOutsideDestinationError`.
@@ -1099,6 +1105,10 @@ reused in custom filters:
10991105

11001106
Link targets are now normalized.
11011107

1108+
.. versionchanged:: next
1109+
1110+
Link targets with a drive that are not absolute are now refused.
1111+
11021112

11031113
.. _tarfile-extraction-refuse:
11041114

‎Lib/tarfile.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -774,10 +774,6 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
774774
# Include os.sep (target OS directory separator) as well.
775775
if name.startswith(('/', os.sep)):
776776
name = new_attrs['name'] = member.path.lstrip('/' + os.sep)
777-
if os.path.isabs(name):
778-
# Path is absolute even after stripping.
779-
# For example, 'C:/foo' on Windows.
780-
raise AbsolutePathError(member)
781777
# Ensure we stay in the destination
782778
if '..' in name.replace(os.sep, '/').split('/'):
783779
# Directories are created from the name as given, so a name that
@@ -786,6 +782,11 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
786782
normalized = os.path.normpath(name)
787783
if normalized != name:
788784
name = new_attrs['name'] = normalized
785+
if os.path.isabs(name) or os.path.splitdrive(name)[0]:
786+
# Path is absolute even after stripping, or has a drive.
787+
# For example, 'C:/foo' or 'C:foo' on Windows.
788+
# os.path.join() ignores a destination without a drive for 'C:foo'.
789+
raise AbsolutePathError(member)
789790
target_path = os.path.realpath(os.path.join(dest_path, name),
790791
strict=os.path.ALLOW_MISSING)
791792
if os.path.commonpath([target_path, dest_path]) != dest_path:
@@ -829,11 +830,11 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
829830
new_attrs['gname'] = None
830831
# Check link destination for 'data'
831832
if member.islnk() or member.issym():
832-
if os.path.isabs(member.linkname):
833-
raise AbsoluteLinkError(member)
834833
normalized = os.path.normpath(member.linkname)
835834
if normalized != member.linkname:
836835
new_attrs['linkname'] = normalized
836+
if os.path.isabs(normalized) or os.path.splitdrive(normalized)[0]:
837+
raise AbsoluteLinkError(member)
837838
if member.issym():
838839
# The symlink is created at `name` with trailing separators
839840
# stripped, so its target is relative to the directory

‎Lib/test/test_tarfile.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
import os
44
import io
5+
import ntpath
56
from hashlib import sha256
67
from contextlib import contextmanager, ExitStack
78
from random import Random
@@ -3716,6 +3717,47 @@ def test_absolute(self):
37163717
tarfile.AbsolutePathError,
37173718
"""['"].*escaped.evil['"] has an absolute path""")
37183719

3720+
def test_drive_relative(self):
3721+
# Test names and link targets that have a drive but no root.
3722+
# They are not absolute, but os.path.join() does not put them
3723+
# below a destination that has no drive or another drive.
3724+
# Windows path handling is used on all platforms.
3725+
dest = r'C:\outerdir\dest'
3726+
with support.swap_attr(os, 'path', ntpath):
3727+
for name in 'C:escaped.evil', 'x/../C:escaped.evil':
3728+
for filter in tarfile.tar_filter, tarfile.data_filter:
3729+
with self.subTest(name=name, filter=filter.__name__):
3730+
with self.assertRaises(tarfile.AbsolutePathError):
3731+
filter(tarfile.TarInfo(name), dest)
3732+
3733+
for linkname in 'C:escaped.evil', 'x/../C:escaped.evil', './C:evil':
3734+
for type in tarfile.SYMTYPE, tarfile.LNKTYPE:
3735+
with self.subTest(linkname=linkname, type=type):
3736+
member = tarfile.TarInfo('link')
3737+
member.type = type
3738+
member.linkname = linkname
3739+
with self.assertRaises(tarfile.AbsoluteLinkError):
3740+
tarfile.data_filter(member, dest)
3741+
3742+
member = tarfile.TarInfo('x/../C/benign.txt')
3743+
member = tarfile.data_filter(member, dest)
3744+
self.assertEqual(member.name, r'C\benign.txt')
3745+
3746+
@unittest.skipUnless(os.name == 'nt', 'requires Windows drives')
3747+
def test_drive_relative_to_cwd(self):
3748+
# Test a name like 'C:escaped.evil' with a destination that has no
3749+
# drive. The file must not be created in the current directory.
3750+
with os_helper.temp_cwd() as cwd:
3751+
drive = os.path.splitdrive(cwd)[0]
3752+
with ArchiveMaker() as arc:
3753+
arc.add(f'{drive}escaped.evil')
3754+
3755+
for filter in 'tar', 'data':
3756+
with self.subTest(filter=filter), arc.open() as tar:
3757+
with self.assertRaises(tarfile.AbsolutePathError):
3758+
tar.extractall('dest', filter=filter)
3759+
self.assertFalse(os.path.lexists('escaped.evil'))
3760+
37193761
def test_parent_dir_out_and_back(self):
37203762
# Test a member that leaves the destination and comes back.
37213763
# The containment check looks at the resolved path, which stays
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
On Windows, the ``'tar'`` and ``'data'`` :mod:`tarfile` extraction filters
2+
now refuse member names and link targets that have a drive but are not
3+
absolute, such as ``C:foo``, with :exc:`~tarfile.AbsolutePathError` or
4+
:exc:`~tarfile.AbsoluteLinkError`. Such members could be extracted outside
5+
the destination directory. Fixes :cve:`2026-107784`.

0 commit comments

Comments
 (0)