Skip to content

Commit 155d407

Browse files
committed
00397: PEP 706, CVE-2007-4559: Filter API for tarfile.extractall
Add API for allowing checks on the content of tar files, allowing callers to mitigate directory traversal (CVE-2007-4559) and related issues. Python 3.12 will warn if this API is not used. Python 3.14 will fail if it's not used. RHEL adds configuration options, by default it will warn and fail like 3.14 upstream. Backport from python#102950 Change document: https://peps.python.org/pep-0706/
1 parent bb97e80 commit 155d407

File tree

9 files changed

+1790
-78
lines changed

9 files changed

+1790
-78
lines changed

Doc/library/shutil.rst

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
537537
Remove the archive format *name* from the list of supported formats.
538538

539539

540-
.. function:: unpack_archive(filename[, extract_dir[, format]])
540+
.. function:: unpack_archive(filename[, extract_dir[, format[, filter]]])
541541

542542
Unpack an archive. *filename* is the full path of the archive.
543543

@@ -551,6 +551,24 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
551551
registered for that extension. In case none is found,
552552
a :exc:`ValueError` is raised.
553553

554+
The keyword-only *filter* argument, which was added in Python 3.6.16,
555+
is passed to the underlying unpacking function.
556+
For zip files, *filter* is not accepted.
557+
For tar files, it is recommended to set it to ``'data'``,
558+
unless using features specific to tar and UNIX-like filesystems.
559+
(See :ref:`tarfile-extraction-filter` for details.)
560+
The ``'data'`` filter will become the default for tar files
561+
in Python 3.14.
562+
563+
.. warning::
564+
565+
Never extract archives from untrusted sources without prior inspection.
566+
It is possible that files are created outside of the path specified in
567+
the *extract_dir* argument, e.g. members that have absolute filenames
568+
starting with "/" or filenames with two dots "..".
569+
570+
.. versionchanged:: 3.6.16
571+
Added the *filter* argument.
554572

555573
.. function:: register_unpack_format(name, extensions, function[, extra_args[, description]])
556574

@@ -559,11 +577,14 @@ provided. They rely on the :mod:`zipfile` and :mod:`tarfile` modules.
559577
``.zip`` for Zip files.
560578

561579
*function* is the callable that will be used to unpack archives. The
562-
callable will receive the path of the archive, followed by the directory
563-
the archive must be extracted to.
564-
565-
When provided, *extra_args* is a sequence of ``(name, value)`` tuples that
566-
will be passed as keywords arguments to the callable.
580+
callable will receive:
581+
582+
- the path of the archive, as a positional argument;
583+
- the directory the archive must be extracted to, as a positional argument;
584+
- possibly a *filter* keyword argument, if it was given to
585+
:func:`unpack_archive`;
586+
- additional keyword arguments, specified by *extra_args* as a sequence
587+
of ``(name, value)`` tuples.
567588

568589
*description* can be provided to describe the format, and will be returned
569590
by the :func:`get_unpack_formats` function.

0 commit comments

Comments
 (0)