Skip to content

Commit aef7eda

Browse files
[3.13] gh-123726: Document caveats of zipfile.Path around name sanitization (GH-130537) (#130986)
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 fead51d commit aef7eda

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
@@ -543,6 +543,14 @@ Path Objects
543543
e.g. 'dir/file.txt', 'dir/', or ''. Defaults to the empty string,
544544
indicating the root.
545545

546+
.. note::
547+
The :class:`Path` class does not sanitize filenames within the ZIP archive. Unlike
548+
the :meth:`ZipFile.extract` and :meth:`ZipFile.extractall` methods, it is the
549+
caller's responsibility to validate or sanitize filenames to prevent path traversal
550+
vulnerabilities (e.g., filenames containing ".." or absolute paths). When handling
551+
untrusted archives, consider resolving filenames using :func:`os.path.abspath`
552+
and checking against the target directory with :func:`os.path.commonpath`.
553+
546554
Path objects expose the following features of :mod:`pathlib.Path`
547555
objects:
548556

0 commit comments

Comments
 (0)