Skip to content

Commit c7979f3

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

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
@@ -1049,6 +1049,10 @@ reused in custom filters:
10491049
paths (in case the name is absolute
10501050
even after stripping slashes, e.g. ``C:/foo`` on Windows).
10511051
This raises :class:`~tarfile.AbsolutePathError`.
1052+
- Normalize filenames (:attr:`TarInfo.name`) that contain ``..`` components
1053+
using :func:`os.path.normpath`.
1054+
Note that this removes internal ``..`` components, which may change the
1055+
meaning of the name if it traverses symbolic links.
10521056
- :ref:`Refuse <tarfile-extraction-refuse>` to extract files whose absolute
10531057
path (after following symlinks) would end up outside the destination.
10541058
This raises :class:`~tarfile.OutsideDestinationError`.
@@ -1057,6 +1061,10 @@ reused in custom filters:
10571061

10581062
Return the modified ``TarInfo`` member.
10591063

1064+
.. versionchanged:: next
1065+
1066+
Filenames containing ``..`` components are now normalized.
1067+
10601068
.. function:: data_filter(member, path)
10611069

10621070
Implements the ``'data'`` filter.

Lib/tarfile.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,13 @@ def _get_filtered_attrs(member, dest_path, for_data=True):
808808
# For example, 'C:/foo' on Windows.
809809
raise AbsolutePathError(member)
810810
# Ensure we stay in the destination
811+
if '..' in name.replace(os.sep, '/').split('/'):
812+
# Directories are created from the name as given, so a name that
813+
# leaves the destination part-way through would create them
814+
# outside it even if the resolved path stays inside.
815+
normalized = os.path.normpath(name)
816+
if normalized != name:
817+
name = new_attrs['name'] = normalized
811818
target_path = os.path.realpath(os.path.join(dest_path, name),
812819
strict=os.path.ALLOW_MISSING)
813820
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
@@ -3948,6 +3948,20 @@ def test_absolute(self):
39483948
tarfile.AbsolutePathError,
39493949
"""['"].*escaped.evil['"] has an absolute path""")
39503950

3951+
def test_parent_dir_out_and_back(self):
3952+
# Test a member that leaves the destination and comes back.
3953+
# The containment check looks at the resolved path, which stays
3954+
# inside, but the intermediate directories are created from the
3955+
# name as given, which does not.
3956+
with ArchiveMaker() as arc:
3957+
arc.add(f'../escaped.evil/../{self.destdir.name}/sub/file',
3958+
content='content')
3959+
3960+
for filter in 'tar', 'data':
3961+
with self.subTest(filter):
3962+
with self.check_context(arc.open(), filter):
3963+
self.expect_file('sub/file', content='content')
3964+
39513965
@symlink_test
39523966
def test_parent_symlink(self):
39533967
# 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)