From 14efa1951eca3cc8f8f7ec5b91c905d8a508231b Mon Sep 17 00:00:00 2001 From: Matan Perelman Date: Wed, 21 Oct 2020 23:18:49 +0300 Subject: [PATCH 1/5] Test for recreating the issue added --- Lib/test/test_zipfile/test_core.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Lib/test/test_zipfile/test_core.py b/Lib/test/test_zipfile/test_core.py index ffed328b171fda2..f51093a52eda16d 100644 --- a/Lib/test/test_zipfile/test_core.py +++ b/Lib/test/test_zipfile/test_core.py @@ -2711,6 +2711,16 @@ def test_overlap_with_archive_comment(self): with self.assertRaisesRegex(zipfile.BadZipFile, 'Overlapped entries'): zipf.read('a') + def test_reading_archive_member_starts_with_slash(self): + member_path = '/folder/file.txt' + member_data = b'hello world' + with open(TESTFN, "wb") as f: + f.write(member_data) + with zipfile.ZipFile(TESTFN2, 'w') as zf: + zf.write(TESTFN, member_path) + with zipfile.ZipFile(TESTFN2) as zf: + self.assertEqual(zf.read(member_path), member_data) + def tearDown(self): unlink(TESTFN) unlink(TESTFN2) From 9eff7863c37ca687b48a82b8155eceaa416723ca Mon Sep 17 00:00:00 2001 From: Matan Perelman Date: Wed, 21 Oct 2020 23:51:58 +0300 Subject: [PATCH 2/5] Scanning until the last slash and removing it only if needed --- Lib/zipfile/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/zipfile/__init__.py b/Lib/zipfile/__init__.py index 005b6f4eb840448..453d719a8f450de 100644 --- a/Lib/zipfile/__init__.py +++ b/Lib/zipfile/__init__.py @@ -644,7 +644,9 @@ def from_file(cls, filename, arcname=None, *, strict_timestamps=True): if arcname is None: arcname = filename arcname = os.path.normpath(os.path.splitdrive(arcname)[1]) - while arcname[0] in (os.sep, os.altsep): + while arcname[0] in (os.sep, os.altsep) and arcname[1] in (os.sep, os.altsep): + arcname = arcname[1:] + if arcname[0] in (os.sep, os.altsep) and os.sep not in arcname[1:]: arcname = arcname[1:] if isdir: arcname += '/' From 3c9604894e0c83f3adce9f281c84ac7681d7e813 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 22 Oct 2020 07:07:18 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2020-10-22-07-07-17.bpo-42112.Z_ED9N.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-10-22-07-07-17.bpo-42112.Z_ED9N.rst diff --git a/Misc/NEWS.d/next/Library/2020-10-22-07-07-17.bpo-42112.Z_ED9N.rst b/Misc/NEWS.d/next/Library/2020-10-22-07-07-17.bpo-42112.Z_ED9N.rst new file mode 100644 index 000000000000000..2e3c43499ab620d --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-10-22-07-07-17.bpo-42112.Z_ED9N.rst @@ -0,0 +1,2 @@ +Fix unnecessary removing of a leading slash in :func:`zipfile.ZipInfo.from_file` +when member's archive name is in the form of `/directory/filename`. \ No newline at end of file From bd659dfe9705145726f90527ae49adeee43b9535 Mon Sep 17 00:00:00 2001 From: Matan Perelman Date: Sun, 1 Nov 2020 20:22:28 +0200 Subject: [PATCH 4/5] Fixing line length in a while condition and fixing the BPO func to meth --- Lib/zipfile/__init__.py | 3 ++- .../next/Library/2020-10-22-07-07-17.bpo-42112.Z_ED9N.rst | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/zipfile/__init__.py b/Lib/zipfile/__init__.py index 453d719a8f450de..47b548a41d38a42 100644 --- a/Lib/zipfile/__init__.py +++ b/Lib/zipfile/__init__.py @@ -644,7 +644,8 @@ def from_file(cls, filename, arcname=None, *, strict_timestamps=True): if arcname is None: arcname = filename arcname = os.path.normpath(os.path.splitdrive(arcname)[1]) - while arcname[0] in (os.sep, os.altsep) and arcname[1] in (os.sep, os.altsep): + while (arcname[0] in (os.sep, os.altsep) and + arcname[1] in (os.sep, os.altsep)): arcname = arcname[1:] if arcname[0] in (os.sep, os.altsep) and os.sep not in arcname[1:]: arcname = arcname[1:] diff --git a/Misc/NEWS.d/next/Library/2020-10-22-07-07-17.bpo-42112.Z_ED9N.rst b/Misc/NEWS.d/next/Library/2020-10-22-07-07-17.bpo-42112.Z_ED9N.rst index 2e3c43499ab620d..4cf34cbed278c2e 100644 --- a/Misc/NEWS.d/next/Library/2020-10-22-07-07-17.bpo-42112.Z_ED9N.rst +++ b/Misc/NEWS.d/next/Library/2020-10-22-07-07-17.bpo-42112.Z_ED9N.rst @@ -1,2 +1,2 @@ -Fix unnecessary removing of a leading slash in :func:`zipfile.ZipInfo.from_file` +Fix unnecessary removing of a leading slash in :meth:`zipfile.ZipInfo.from_file` when member's archive name is in the form of `/directory/filename`. \ No newline at end of file From 9ab0ca5a04d7eb44c2faf1c2e9604f8f8b0d511a Mon Sep 17 00:00:00 2001 From: Matan Perelman Date: Fri, 12 Jun 2026 19:49:42 +0300 Subject: [PATCH 5/5] blurb_it: Fix linting issues --- .../next/Library/2020-10-22-07-07-17.bpo-42112.Z_ED9N.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2020-10-22-07-07-17.bpo-42112.Z_ED9N.rst b/Misc/NEWS.d/next/Library/2020-10-22-07-07-17.bpo-42112.Z_ED9N.rst index 4cf34cbed278c2e..fdda11afbbb884e 100644 --- a/Misc/NEWS.d/next/Library/2020-10-22-07-07-17.bpo-42112.Z_ED9N.rst +++ b/Misc/NEWS.d/next/Library/2020-10-22-07-07-17.bpo-42112.Z_ED9N.rst @@ -1,2 +1,2 @@ Fix unnecessary removing of a leading slash in :meth:`zipfile.ZipInfo.from_file` -when member's archive name is in the form of `/directory/filename`. \ No newline at end of file +when member's archive name is in the form of :code:`/directory/filename`.