Skip to content

Commit 72ba1c0

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 e4c751b) Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com> Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent ffbb750 commit 72ba1c0

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
@@ -1029,14 +1029,15 @@ reused in custom filters:
10291029
Implements the ``'tar'`` filter.
10301030

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

10551056
Filenames containing ``..`` components are now normalized.
10561057

1058+
.. versionchanged:: next
1059+
1060+
Filenames with a drive that are not absolute are now refused.
1061+
10571062
.. function:: data_filter(member, path)
10581063

10591064
Implements the ``'data'`` filter.
@@ -1066,7 +1071,8 @@ reused in custom filters:
10661071
symbolic links.
10671072

10681073
- :ref:`Refuse <tarfile-extraction-refuse>` to extract links (hard or soft)
1069-
that link to absolute paths, or ones that link outside the destination.
1074+
that link to absolute paths or to paths with a drive,
1075+
or ones that link outside the destination.
10701076

10711077
This raises :class:`~tarfile.AbsoluteLinkError` or
10721078
:class:`~tarfile.LinkOutsideDestinationError`.
@@ -1097,6 +1103,10 @@ reused in custom filters:
10971103

10981104
Link targets are now normalized.
10991105

1106+
.. versionchanged:: next
1107+
1108+
Link targets with a drive that are not absolute are now refused.
1109+
11001110

11011111
.. _tarfile-extraction-refuse:
11021112

‎Lib/tarfile.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -773,10 +773,6 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
773773
# Include os.sep (target OS directory separator) as well.
774774
if name.startswith(('/', os.sep)):
775775
name = new_attrs['name'] = member.path.lstrip('/' + os.sep)
776-
if os.path.isabs(name):
777-
# Path is absolute even after stripping.
778-
# For example, 'C:/foo' on Windows.
779-
raise AbsolutePathError(member)
780776
# Ensure we stay in the destination
781777
if '..' in name.replace(os.sep, '/').split('/'):
782778
# Directories are created from the name as given, so a name that
@@ -785,6 +781,11 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
785781
normalized = os.path.normpath(name)
786782
if normalized != name:
787783
name = new_attrs['name'] = normalized
784+
if os.path.isabs(name) or os.path.splitdrive(name)[0]:
785+
# Path is absolute even after stripping, or has a drive.
786+
# For example, 'C:/foo' or 'C:foo' on Windows.
787+
# os.path.join() ignores a destination without a drive for 'C:foo'.
788+
raise AbsolutePathError(member)
788789
target_path = os.path.realpath(os.path.join(dest_path, name),
789790
strict=os.path.ALLOW_MISSING)
790791
if os.path.commonpath([target_path, dest_path]) != dest_path:
@@ -828,11 +829,11 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
828829
new_attrs['gname'] = None
829830
# Check link destination for 'data'
830831
if member.islnk() or member.issym():
831-
if os.path.isabs(member.linkname):
832-
raise AbsoluteLinkError(member)
833832
normalized = os.path.normpath(member.linkname)
834833
if normalized != member.linkname:
835834
new_attrs['linkname'] = normalized
835+
if os.path.isabs(normalized) or os.path.splitdrive(normalized)[0]:
836+
raise AbsoluteLinkError(member)
836837
if member.issym():
837838
# The symlink is created at `name` with trailing separators
838839
# 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
78
from random import Random
@@ -3615,6 +3616,47 @@ def test_absolute(self):
36153616
tarfile.AbsolutePathError,
36163617
"""['"].*escaped.evil['"] has an absolute path""")
36173618

3619+
def test_drive_relative(self):
3620+
# Test names and link targets that have a drive but no root.
3621+
# They are not absolute, but os.path.join() does not put them
3622+
# below a destination that has no drive or another drive.
3623+
# Windows path handling is used on all platforms.
3624+
dest = r'C:\outerdir\dest'
3625+
with support.swap_attr(os, 'path', ntpath):
3626+
for name in 'C:escaped.evil', 'x/../C:escaped.evil':
3627+
for filter in tarfile.tar_filter, tarfile.data_filter:
3628+
with self.subTest(name=name, filter=filter.__name__):
3629+
with self.assertRaises(tarfile.AbsolutePathError):
3630+
filter(tarfile.TarInfo(name), dest)
3631+
3632+
for linkname in 'C:escaped.evil', 'x/../C:escaped.evil', './C:evil':
3633+
for type in tarfile.SYMTYPE, tarfile.LNKTYPE:
3634+
with self.subTest(linkname=linkname, type=type):
3635+
member = tarfile.TarInfo('link')
3636+
member.type = type
3637+
member.linkname = linkname
3638+
with self.assertRaises(tarfile.AbsoluteLinkError):
3639+
tarfile.data_filter(member, dest)
3640+
3641+
member = tarfile.TarInfo('x/../C/benign.txt')
3642+
member = tarfile.data_filter(member, dest)
3643+
self.assertEqual(member.name, r'C\benign.txt')
3644+
3645+
@unittest.skipUnless(os.name == 'nt', 'requires Windows drives')
3646+
def test_drive_relative_to_cwd(self):
3647+
# Test a name like 'C:escaped.evil' with a destination that has no
3648+
# drive. The file must not be created in the current directory.
3649+
with os_helper.temp_cwd() as cwd:
3650+
drive = os.path.splitdrive(cwd)[0]
3651+
with ArchiveMaker() as arc:
3652+
arc.add(f'{drive}escaped.evil')
3653+
3654+
for filter in 'tar', 'data':
3655+
with self.subTest(filter=filter), arc.open() as tar:
3656+
with self.assertRaises(tarfile.AbsolutePathError):
3657+
tar.extractall('dest', filter=filter)
3658+
self.assertFalse(os.path.lexists('escaped.evil'))
3659+
36183660
def test_parent_dir_out_and_back(self):
36193661
# Test a member that leaves the destination and comes back.
36203662
# 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)