Skip to content

Commit 18dfe06

Browse files
[3.10] gh-155999: tarfile: handle a member that leaves the destination but comes back (GH-156000)
(cherry picked from commit 9768834) Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent d9e8e8e commit 18dfe06

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
@@ -950,6 +950,10 @@ reused in custom filters:
950950
paths (in case the name is absolute
951951
even after stripping slashes, e.g. ``C:/foo`` on Windows).
952952
This raises :class:`~tarfile.AbsolutePathError`.
953+
- Normalize filenames (:attr:`TarInfo.name`) that contain ``..`` components
954+
using :func:`os.path.normpath`.
955+
Note that this removes internal ``..`` components, which may change the
956+
meaning of the name if it traverses symbolic links.
953957
- :ref:`Refuse <tarfile-extraction-refuse>` to extract files whose absolute
954958
path (after following symlinks) would end up outside the destination.
955959
This raises :class:`~tarfile.OutsideDestinationError`.
@@ -958,6 +962,10 @@ reused in custom filters:
958962

959963
Return the modified ``TarInfo`` member.
960964

965+
.. versionchanged:: next
966+
967+
Filenames containing ``..`` components are now normalized.
968+
961969
.. function:: data_filter(/, member, path)
962970

963971
Implements the ``'data'`` filter.

Lib/tarfile.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,13 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
777777
# For example, 'C:/foo' on Windows.
778778
raise AbsolutePathError(member)
779779
# Ensure we stay in the destination
780+
if '..' in name.replace(os.sep, '/').split('/'):
781+
# Directories are created from the name as given, so a name that
782+
# leaves the destination part-way through would create them
783+
# outside it even if the resolved path stays inside.
784+
normalized = os.path.normpath(name)
785+
if normalized != name:
786+
name = new_attrs['name'] = normalized
780787
target_path = os.path.realpath(os.path.join(dest_path, name),
781788
strict=os.path.ALLOW_MISSING)
782789
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
@@ -3535,6 +3535,20 @@ def test_absolute(self):
35353535
tarfile.AbsolutePathError,
35363536
"""['"].*escaped.evil['"] has an absolute path""")
35373537

3538+
def test_parent_dir_out_and_back(self):
3539+
# Test a member that leaves the destination and comes back.
3540+
# The containment check looks at the resolved path, which stays
3541+
# inside, but the intermediate directories are created from the
3542+
# name as given, which does not.
3543+
with ArchiveMaker() as arc:
3544+
arc.add(f'../escaped.evil/../{self.destdir.name}/sub/file',
3545+
content='content')
3546+
3547+
for filter in 'tar', 'data':
3548+
with self.subTest(filter):
3549+
with self.check_context(arc.open(), filter):
3550+
self.expect_file('sub/file', content='content')
3551+
35383552
def test_parent_symlink(self):
35393553
# Test interplaying symlinks
35403554
# Inspired by 'dirsymlink2a' in jwilk/traversal-archives
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)