From ad1747255880487be1dfd7d830ce7fd116e171e1 Mon Sep 17 00:00:00 2001 From: Thomas Lam <79589038+tl-hbk@users.noreply.github.com> Date: Wed, 30 Jun 2021 16:18:21 -0500 Subject: [PATCH] Fix extract_zipped_paths infinite loop when provided invalid unc path --- requests/utils.py | 3 +++ tests/test_utils.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/requests/utils.py b/requests/utils.py index db67938e67..b6d5c2ea0c 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -245,6 +245,9 @@ def extract_zipped_paths(path): archive, member = os.path.split(path) while archive and not os.path.exists(archive): archive, prefix = os.path.split(archive) + if not prefix: + # Needed to prevent infinite looping when provided invalid UNC path + break member = '/'.join([prefix, member]) if not zipfile.is_zipfile(archive): diff --git a/tests/test_utils.py b/tests/test_utils.py index 463516b2e5..98ffb25a6c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -285,6 +285,10 @@ def test_zipped_paths_extracted(self, tmpdir): assert os.path.exists(extracted_path) assert filecmp.cmp(extracted_path, __file__) + def test_invalid_unc_path(self): + path = r"\\localhost\invalid\location" + assert extract_zipped_paths(path) == path + class TestContentEncodingDetection: