summaryrefslogtreecommitdiffstats
path: root/Lib/zipfile
diff options
context:
space:
mode:
authorJason R. Coombs <jaraco@jaraco.com>2024-08-27 21:10:30 (GMT)
committerGitHub <noreply@github.com>2024-08-27 21:10:30 (GMT)
commit2231286d78d328c2f575e0b05b16fe447d1656d6 (patch)
treeb52aa4507a99a2b79662700d8c181e0a914b956c /Lib/zipfile
parent7e38e6745d2f9ee235d934ab7f3c6b3085be2b70 (diff)
downloadcpython-2231286d78d328c2f575e0b05b16fe447d1656d6.zip
cpython-2231286d78d328c2f575e0b05b16fe447d1656d6.tar.gz
cpython-2231286d78d328c2f575e0b05b16fe447d1656d6.tar.bz2
gh-123270: Replaced SanitizedNames with a more surgical fix. (#123354)
Applies changes from zipp 3.20.1 and jaraco/zipp#124
Diffstat (limited to 'Lib/zipfile')
-rw-r--r--Lib/zipfile/_path/__init__.py82
1 files changed, 17 insertions, 65 deletions
diff --git a/Lib/zipfile/_path/__init__.py b/Lib/zipfile/_path/__init__.py
index 3c01659..5079db9 100644
--- a/Lib/zipfile/_path/__init__.py
+++ b/Lib/zipfile/_path/__init__.py
@@ -1,3 +1,12 @@
+"""
+A Path-like interface for zipfiles.
+
+This codebase is shared between zipfile.Path in the stdlib
+and zipp in PyPI. See
+https://github.com/python/importlib_metadata/wiki/Development-Methodology
+for more detail.
+"""
+
import io
import posixpath
import zipfile
@@ -36,7 +45,7 @@ def _parents(path):
def _ancestry(path):
"""
Given a path with elements separated by
- posixpath.sep, generate all elements of that path
+ posixpath.sep, generate all elements of that path.
>>> list(_ancestry('b/d'))
['b/d', 'b']
@@ -48,9 +57,14 @@ def _ancestry(path):
['b']
>>> list(_ancestry(''))
[]
+
+ Multiple separators are treated like a single.
+
+ >>> list(_ancestry('//b//d///f//'))
+ ['//b//d///f', '//b//d', '//b']
"""
path = path.rstrip(posixpath.sep)
- while path and path != posixpath.sep:
+ while path.rstrip(posixpath.sep):
yield path
path, tail = posixpath.split(path)
@@ -85,69 +99,7 @@ class InitializedState:
super().__init__(*args, **kwargs)
-class SanitizedNames:
- """
- ZipFile mix-in to ensure names are sanitized.
- """
-
- def namelist(self):
- return list(map(self._sanitize, super().namelist()))
-
- @staticmethod
- def _sanitize(name):
- r"""
- Ensure a relative path with posix separators and no dot names.
-
- Modeled after
- https://github.com/python/cpython/blob/bcc1be39cb1d04ad9fc0bd1b9193d3972835a57c/Lib/zipfile/__init__.py#L1799-L1813
- but provides consistent cross-platform behavior.
-
- >>> san = SanitizedNames._sanitize
- >>> san('/foo/bar')
- 'foo/bar'
- >>> san('//foo.txt')
- 'foo.txt'
- >>> san('foo/.././bar.txt')
- 'foo/bar.txt'
- >>> san('foo../.bar.txt')
- 'foo../.bar.txt'
- >>> san('\\foo\\bar.txt')
- 'foo/bar.txt'
- >>> san('D:\\foo.txt')
- 'D/foo.txt'
- >>> san('\\\\server\\share\\file.txt')
- 'server/share/file.txt'
- >>> san('\\\\?\\GLOBALROOT\\Volume3')
- '?/GLOBALROOT/Volume3'
- >>> san('\\\\.\\PhysicalDrive1\\root')
- 'PhysicalDrive1/root'
-
- Retain any trailing slash.
- >>> san('abc/')
- 'abc/'
-
- Raises a ValueError if the result is empty.
- >>> san('../..')
- Traceback (most recent call last):
- ...
- ValueError: Empty filename
- """
-
- def allowed(part):
- return part and part not in {'..', '.'}
-
- # Remove the drive letter.
- # Don't use ntpath.splitdrive, because that also strips UNC paths
- bare = re.sub('^([A-Z]):', r'\1', name, flags=re.IGNORECASE)
- clean = bare.replace('\\', '/')
- parts = clean.split('/')
- joined = '/'.join(filter(allowed, parts))
- if not joined:
- raise ValueError("Empty filename")
- return joined + '/' * name.endswith('/')
-
-
-class CompleteDirs(InitializedState, SanitizedNames, zipfile.ZipFile):
+class CompleteDirs(InitializedState, zipfile.ZipFile):
"""
A ZipFile subclass that ensures that implied directories
are always included in the namelist.