Skip to content

Commit 552dad1

Browse files
[3.12] gh-123726: Document caveats of zipfile.Path around name sanitization (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]>
1 parent a69107b commit 552dad1

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

Doc/library/zipfile.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,14 @@ Path Objects
528528
e.g. 'dir/file.txt', 'dir/', or ''. Defaults to the empty string,
529529
indicating the root.
530530

531+
.. note::
532+
The :class:`Path` class does not sanitize filenames within the ZIP archive. Unlike
533+
the :meth:`ZipFile.extract` and :meth:`ZipFile.extractall` methods, it is the
534+
caller's responsibility to validate or sanitize filenames to prevent path traversal
535+
vulnerabilities (e.g., filenames containing ".." or absolute paths). When handling
536+
untrusted archives, consider resolving filenames using :func:`os.path.abspath`
537+
and checking against the target directory with :func:`os.path.commonpath`.
538+
531539
Path objects expose the following features of :mod:`pathlib.Path`
532540
objects:
533541

0 commit comments

Comments
 (0)