Skip to content

Commit a5bfd96

Browse files
[3.12] gh-155999: tarfile: handle a member that leaves the destination but comes back (GH-156000) (#156043)
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 ead210d commit a5bfd96

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
@@ -1035,6 +1035,10 @@ reused in custom filters:
10351035
paths (in case the name is absolute
10361036
even after stripping slashes, e.g. ``C:/foo`` on Windows).
10371037
This raises :class:`~tarfile.AbsolutePathError`.
1038+
- Normalize filenames (:attr:`TarInfo.name`) that contain ``..`` components
1039+
using :func:`os.path.normpath`.
1040+
Note that this removes internal ``..`` components, which may change the
1041+
meaning of the name if it traverses symbolic links.
10381042
- :ref:`Refuse <tarfile-extraction-refuse>` to extract files whose absolute
10391043
path (after following symlinks) would end up outside the destination.
10401044
This raises :class:`~tarfile.OutsideDestinationError`.
@@ -1043,6 +1047,10 @@ reused in custom filters:
10431047

10441048
Return the modified ``TarInfo`` member.
10451049

1050+
.. versionchanged:: next
1051+
1052+
Filenames containing ``..`` components are now normalized.
1053+
10461054
.. function:: data_filter(member, path)
10471055

10481056
Implements the ``'data'`` filter.

Lib/tarfile.py

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

3719+
def test_parent_dir_out_and_back(self):
3720+
# Test a member that leaves the destination and comes back.
3721+
# The containment check looks at the resolved path, which stays
3722+
# inside, but the intermediate directories are created from the
3723+
# name as given, which does not.
3724+
with ArchiveMaker() as arc:
3725+
arc.add(f'../escaped.evil/../{self.destdir.name}/sub/file',
3726+
content='content')
3727+
3728+
for filter in 'tar', 'data':
3729+
with self.subTest(filter):
3730+
with self.check_context(arc.open(), filter):
3731+
self.expect_file('sub/file', content='content')
3732+
37193733
@symlink_test
37203734
def test_parent_symlink(self):
37213735
# 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)