Skip to content

Commit e4c751b

Browse files
gpsheadencukou
andauthored
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 Co-authored-by: Petr Viktorin <encukou@gmail.com>
1 parent f8e65ee commit e4c751b

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

11221122
- Strip leading slashes (``/`` and :data:`os.sep`) from filenames.
1123-
- :ref:`Refuse <tarfile-extraction-refuse>` to extract files with absolute
1124-
paths (in case the name is absolute
1125-
even after stripping slashes, e.g. ``C:/foo`` on Windows).
1126-
This raises :class:`~tarfile.AbsolutePathError`.
11271123
- Normalize filenames (:attr:`TarInfo.name`) that contain ``..`` components
11281124
using :func:`os.path.normpath`.
11291125
Note that this removes internal ``..`` components, which may change the
11301126
meaning of the name if it traverses symbolic links.
1127+
- :ref:`Refuse <tarfile-extraction-refuse>` to extract files with absolute
1128+
paths (in case the name is absolute
1129+
even after stripping slashes, e.g. ``C:/foo`` on Windows)
1130+
or with a drive (e.g. ``C:foo`` on Windows).
1131+
This raises :class:`~tarfile.AbsolutePathError`.
11311132
- :ref:`Refuse <tarfile-extraction-refuse>` to extract files whose absolute
11321133
path (after following symlinks) would end up outside the destination.
11331134
This raises :class:`~tarfile.OutsideDestinationError`.
@@ -1152,6 +1153,10 @@ reused in custom filters:
11521153

11531154
Filenames containing ``..`` components are now normalized.
11541155

1156+
.. versionchanged:: next
1157+
1158+
Filenames with a drive that are not absolute are now refused.
1159+
11551160
.. function:: data_filter(member, path)
11561161

11571162
Implements the ``'data'`` filter.
@@ -1164,7 +1169,8 @@ reused in custom filters:
11641169
symbolic links.
11651170

11661171
- :ref:`Refuse <tarfile-extraction-refuse>` to extract links (hard or soft)
1167-
that link to absolute paths, or ones that link outside the destination.
1172+
that link to absolute paths or to paths with a drive,
1173+
or ones that link outside the destination.
11681174

11691175
This raises :class:`~tarfile.AbsoluteLinkError` or
11701176
:class:`~tarfile.LinkOutsideDestinationError`.
@@ -1198,6 +1204,10 @@ reused in custom filters:
11981204

11991205
Link targets are now normalized.
12001206

1207+
.. versionchanged:: next
1208+
1209+
Link targets with a drive that are not absolute are now refused.
1210+
12011211

12021212
.. _tarfile-extraction-refuse:
12031213

‎Lib/tarfile.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -839,10 +839,6 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
839839
# Include os.sep (target OS directory separator) as well.
840840
if name.startswith(('/', os.sep)):
841841
name = new_attrs['name'] = member.path.lstrip('/' + os.sep)
842-
if os.path.isabs(name):
843-
# Path is absolute even after stripping.
844-
# For example, 'C:/foo' on Windows.
845-
raise AbsolutePathError(member)
846842
# Ensure we stay in the destination
847843
if '..' in name.replace(os.sep, '/').split('/'):
848844
# Directories are created from the name as given, so a name that
@@ -851,6 +847,11 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
851847
normalized = os.path.normpath(name)
852848
if normalized != name:
853849
name = new_attrs['name'] = normalized
850+
if os.path.isabs(name) or os.path.splitdrive(name)[0]:
851+
# Path is absolute even after stripping, or has a drive.
852+
# For example, 'C:/foo' or 'C:foo' on Windows.
853+
# os.path.join() ignores a destination without a drive for 'C:foo'.
854+
raise AbsolutePathError(member)
854855
target_path = os.path.realpath(os.path.join(dest_path, name),
855856
strict=os.path.ALLOW_MISSING)
856857
if os.path.commonpath([target_path, dest_path]) != dest_path:
@@ -894,11 +895,11 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
894895
new_attrs['gname'] = None
895896
# Check link destination for 'data'
896897
if member.islnk() or member.issym():
897-
if os.path.isabs(member.linkname):
898-
raise AbsoluteLinkError(member)
899898
normalized = os.path.normpath(member.linkname)
900899
if normalized != member.linkname:
901900
new_attrs['linkname'] = normalized
901+
if os.path.isabs(normalized) or os.path.splitdrive(normalized)[0]:
902+
raise AbsoluteLinkError(member)
902903
if member.issym():
903904
# The symlink is created at `name` with trailing separators
904905
# 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
@@ -3,6 +3,7 @@
33
import sys
44
import os
55
import io
6+
import ntpath
67
from hashlib import sha256
78
from contextlib import contextmanager, ExitStack
89
from random import Random
@@ -4298,6 +4299,47 @@ def test_absolute(self):
42984299
tarfile.AbsolutePathError,
42994300
"""['"].*escaped.evil['"] has an absolute path""")
43004301

4302+
def test_drive_relative(self):
4303+
# Test names and link targets that have a drive but no root.
4304+
# They are not absolute, but os.path.join() does not put them
4305+
# below a destination that has no drive or another drive.
4306+
# Windows path handling is used on all platforms.
4307+
dest = r'C:\outerdir\dest'
4308+
with support.swap_attr(os, 'path', ntpath):
4309+
for name in 'C:escaped.evil', 'x/../C:escaped.evil':
4310+
for filter in tarfile.tar_filter, tarfile.data_filter:
4311+
with self.subTest(name=name, filter=filter.__name__):
4312+
with self.assertRaises(tarfile.AbsolutePathError):
4313+
filter(tarfile.TarInfo(name), dest)
4314+
4315+
for linkname in 'C:escaped.evil', 'x/../C:escaped.evil', './C:evil':
4316+
for type in tarfile.SYMTYPE, tarfile.LNKTYPE:
4317+
with self.subTest(linkname=linkname, type=type):
4318+
member = tarfile.TarInfo('link')
4319+
member.type = type
4320+
member.linkname = linkname
4321+
with self.assertRaises(tarfile.AbsoluteLinkError):
4322+
tarfile.data_filter(member, dest)
4323+
4324+
member = tarfile.TarInfo('x/../C/benign.txt')
4325+
member = tarfile.data_filter(member, dest)
4326+
self.assertEqual(member.name, r'C\benign.txt')
4327+
4328+
@unittest.skipUnless(os.name == 'nt', 'requires Windows drives')
4329+
def test_drive_relative_to_cwd(self):
4330+
# Test a name like 'C:escaped.evil' with a destination that has no
4331+
# drive. The file must not be created in the current directory.
4332+
with os_helper.temp_cwd() as cwd:
4333+
drive = os.path.splitdrive(cwd)[0]
4334+
with ArchiveMaker() as arc:
4335+
arc.add(f'{drive}escaped.evil')
4336+
4337+
for filter in 'tar', 'data':
4338+
with self.subTest(filter=filter), arc.open() as tar:
4339+
with self.assertRaises(tarfile.AbsolutePathError):
4340+
tar.extractall('dest', filter=filter)
4341+
self.assertFalse(os.path.lexists('escaped.evil'))
4342+
43014343
def test_parent_dir_out_and_back(self):
43024344
# Test a member that leaves the destination and comes back.
43034345
# 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)