Skip to content

Commit a3990df

Browse files
gh-123726: Document caveats of zipfile.Path around name sanitization (#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.
1 parent edd1eca commit a3990df

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
@@ -554,6 +554,14 @@ Path Objects
554554
e.g. 'dir/file.txt', 'dir/', or ''. Defaults to the empty string,
555555
indicating the root.
556556

557+
.. note::
558+
The :class:`Path` class does not sanitize filenames within the ZIP archive. Unlike
559+
the :meth:`ZipFile.extract` and :meth:`ZipFile.extractall` methods, it is the
560+
caller's responsibility to validate or sanitize filenames to prevent path traversal
561+
vulnerabilities (e.g., filenames containing ".." or absolute paths). When handling
562+
untrusted archives, consider resolving filenames using :func:`os.path.abspath`
563+
and checking against the target directory with :func:`os.path.commonpath`.
564+
557565
Path objects expose the following features of :mod:`pathlib.Path`
558566
objects:
559567

0 commit comments

Comments
 (0)