Skip to content

Commit

Permalink
[3.12] gh-123726: Document caveats of zipfile.Path around name saniti…
Browse files Browse the repository at this point in the history
…zation (GH-130537) (#130987)

gh-123726: Document caveats of zipfile.Path around name sanitization (GH-130537)

Add a note to the `zipfile.Path` class documentation clarifying that it does not sanitize filenames. This emphasizes the caller's responsibility to validate or sanitize inputs, especially when handling untrusted ZIP archives, to prevent path traversal vulnerabilities. The note also references the `extract` and `extractall` methods for comparison and suggests using `os.path.abspath` and `os.path.commonpath` for safe filename resolution.
(cherry picked from commit a3990df)

Co-authored-by: Affan Shaikhsurab <[email protected]>
  • Loading branch information
miss-islington and AffanShaikhsurab authored Mar 8, 2025
1 parent a69107b commit 552dad1
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Doc/library/zipfile.rst
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,14 @@ Path Objects
e.g. 'dir/file.txt', 'dir/', or ''. Defaults to the empty string,
indicating the root.

.. note::
The :class:`Path` class does not sanitize filenames within the ZIP archive. Unlike
the :meth:`ZipFile.extract` and :meth:`ZipFile.extractall` methods, it is the
caller's responsibility to validate or sanitize filenames to prevent path traversal
vulnerabilities (e.g., filenames containing ".." or absolute paths). When handling
untrusted archives, consider resolving filenames using :func:`os.path.abspath`
and checking against the target directory with :func:`os.path.commonpath`.

Path objects expose the following features of :mod:`pathlib.Path`
objects:

Expand Down

0 comments on commit 552dad1

Please sign in to comment.