Skip to content

Commit 16dea1e

Browse files
[3.14] gh-155999: tarfile: handle a member that leaves the destination but comes back (GH-156000) (#156041)
(cherry picked from commit 9768834) Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent 2c816a8 commit 16dea1e

4 files changed

Lines changed: 34 additions & 0 deletions

File tree

Doc/library/tarfile.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,10 @@ reused in custom filters:
11001100
paths (in case the name is absolute
11011101
even after stripping slashes, e.g. ``C:/foo`` on Windows).
11021102
This raises :class:`~tarfile.AbsolutePathError`.
1103+
- Normalize filenames (:attr:`TarInfo.name`) that contain ``..`` components
1104+
using :func:`os.path.normpath`.
1105+
Note that this removes internal ``..`` components, which may change the
1106+
meaning of the name if it traverses symbolic links.
11031107
- :ref:`Refuse <tarfile-extraction-refuse>` to extract files whose absolute
11041108
path (after following symlinks) would end up outside the destination.
11051109
This raises :class:`~tarfile.OutsideDestinationError`.
@@ -1108,6 +1112,10 @@ reused in custom filters:
11081112

11091113
Return the modified ``TarInfo`` member.
11101114

1115+
.. versionchanged:: next
1116+
1117+
Filenames containing ``..`` components are now normalized.
1118+
11111119
.. function:: data_filter(member, path)
11121120

11131121
Implements the ``'data'`` filter.

Lib/tarfile.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,13 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
819819
# For example, 'C:/foo' on Windows.
820820
raise AbsolutePathError(member)
821821
# Ensure we stay in the destination
822+
if '..' in name.replace(os.sep, '/').split('/'):
823+
# Directories are created from the name as given, so a name that
824+
# leaves the destination part-way through would create them
825+
# outside it even if the resolved path stays inside.
826+
normalized = os.path.normpath(name)
827+
if normalized != name:
828+
name = new_attrs['name'] = normalized
822829
target_path = os.path.realpath(os.path.join(dest_path, name),
823830
strict=os.path.ALLOW_MISSING)
824831
if os.path.commonpath([target_path, dest_path]) != dest_path:

Lib/test/test_tarfile.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3981,6 +3981,20 @@ def test_absolute(self):
39813981
tarfile.AbsolutePathError,
39823982
"""['"].*escaped.evil['"] has an absolute path""")
39833983

3984+
def test_parent_dir_out_and_back(self):
3985+
# Test a member that leaves the destination and comes back.
3986+
# The containment check looks at the resolved path, which stays
3987+
# inside, but the intermediate directories are created from the
3988+
# name as given, which does not.
3989+
with ArchiveMaker() as arc:
3990+
arc.add(f'../escaped.evil/../{self.destdir.name}/sub/file',
3991+
content='content')
3992+
3993+
for filter in 'tar', 'data':
3994+
with self.subTest(filter):
3995+
with self.check_context(arc.open(), filter):
3996+
self.expect_file('sub/file', content='content')
3997+
39843998
@symlink_test
39853999
def test_parent_symlink(self):
39864000
# Test interplaying symlinks
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix the :mod:`tarfile` ``tar`` and ``data`` extraction filters creating
2+
directories outside the destination for members whose name leaves the
3+
destination and returns to it, such as ``../evil/../dest/sub/file``. The
4+
containment check used the resolved path, but intermediate directories were
5+
created from the name as given.

0 commit comments

Comments
 (0)