From a4c415f3b4ac121d0e93abd8238ec93c9d64779c Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Wed, 27 Aug 2025 10:39:55 +0530 Subject: [PATCH 1/9] bpo-32465: Fix proxy_bypass_registry trailing semicolon on Windows - _proxy_bypass_winreg_override now strips empty values from ProxyOverride - Added unit test ProxyBypassRegistryTests to check trailing semicolon behavior --- Lib/test/test_urllib.py | 15 +++++++++++++++ Lib/urllib/request.py | 11 +++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index ae524c5ffba6b1..52a32738e67feb 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -1686,6 +1686,21 @@ def test_with_method_arg(self): request.method = 'HEAD' self.assertEqual(request.get_method(), 'HEAD') +class ProxyBypassRegistryTests(unittest.TestCase): + def test_proxy_bypass_registry_trailing_semicolon(self): + fake_proxy_override = "localhost;*.example.com;" + + # Monkeypatch registry reader + original_getproxies_registry = urllib.request.getproxies_registry + urllib.request.getproxies_registry = lambda: {"no": fake_proxy_override} + + try: + self.assertFalse(urllib.request.proxy_bypass("notmatching.com")) + self.assertTrue(urllib.request.proxy_bypass("localhost")) + self.assertTrue(urllib.request.proxy_bypass("sub.example.com")) + finally: + urllib.request.getproxies_registry = original_getproxies_registry + if __name__ == '__main__': unittest.main() diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index af93d4cd75dbef..22609f9ff2efde 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -1996,7 +1996,7 @@ def ip2num(ipAddr): # Same as _proxy_bypass_macosx_sysconf, testable on all platforms -def _proxy_bypass_winreg_override(host, override): +def _proxy_bypass_winreg_override(host, proxyOverride): """Return True if the host should bypass the proxy server. The proxy override list is obtained from the Windows @@ -2008,15 +2008,18 @@ def _proxy_bypass_winreg_override(host, override): from fnmatch import fnmatch host, _ = _splitport(host) - proxy_override = override.split(';') - for test in proxy_override: - test = test.strip() + + # Split and remove empty or whitespace-only entries + proxyOverride = [x.strip() for x in proxyOverride.split(';') if x.strip()] + + for test in proxyOverride: # "" should bypass the proxy server for all intranet addresses if test == '': if '.' not in host: return True elif fnmatch(host, test): return True + return False From b2f89f704c1eaa69ef2ba1a940e0237a5026dfd1 Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Wed, 27 Aug 2025 10:59:52 +0530 Subject: [PATCH 2/9] bpo-32465: Add NEWS entry for proxy_bypass_registry fix --- Misc/NEWS.d/next/32465.proxy_bypass_trailing_semicolon.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/32465.proxy_bypass_trailing_semicolon.rst diff --git a/Misc/NEWS.d/next/32465.proxy_bypass_trailing_semicolon.rst b/Misc/NEWS.d/next/32465.proxy_bypass_trailing_semicolon.rst new file mode 100644 index 00000000000000..f47d155548a1f7 --- /dev/null +++ b/Misc/NEWS.d/next/32465.proxy_bypass_trailing_semicolon.rst @@ -0,0 +1 @@ +bpo-32465: _proxy_bypass_winreg_override now strips empty entries from ProxyOverride on Windows. Added unit test ProxyBypassRegistryTests. From cf8e064ddcd37e397a9057422c1cdf877ef65f10 Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Wed, 27 Aug 2025 11:03:04 +0530 Subject: [PATCH 3/9] bpo-32465: Fix NEWS file formatting --- Misc/NEWS.d/next/32465.proxy_bypass_trailing_semicolon.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/32465.proxy_bypass_trailing_semicolon.rst b/Misc/NEWS.d/next/32465.proxy_bypass_trailing_semicolon.rst index f47d155548a1f7..d1b4ce55f6daec 100644 --- a/Misc/NEWS.d/next/32465.proxy_bypass_trailing_semicolon.rst +++ b/Misc/NEWS.d/next/32465.proxy_bypass_trailing_semicolon.rst @@ -1 +1,2 @@ -bpo-32465: _proxy_bypass_winreg_override now strips empty entries from ProxyOverride on Windows. Added unit test ProxyBypassRegistryTests. +bpo-32465: _proxy_bypass_winreg_override strips empty entries from ProxyOverride on Windows. + From 742ffde1d06718000c11bd23c538b6da33b0c873 Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Wed, 27 Aug 2025 11:14:47 +0530 Subject: [PATCH 4/9] Trigger CI recheck for NEWS file From 189feb60e30a1e901e8d8895aa4861f61f5fe3ff Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Wed, 27 Aug 2025 11:36:33 +0530 Subject: [PATCH 5/9] Trigger CI recheck for NEWS file --- Lib/test/test_urllib.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 52a32738e67feb..54a818b075ff66 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -1,7 +1,7 @@ """Regression tests for what was in Python 2's "urllib" module""" import urllib.parse -import urllib.request +import urllib.request import _proxy_bypass_winreg_override as proxy_bypass_winreg import urllib.error import http.client import email.message @@ -1690,17 +1690,10 @@ class ProxyBypassRegistryTests(unittest.TestCase): def test_proxy_bypass_registry_trailing_semicolon(self): fake_proxy_override = "localhost;*.example.com;" - # Monkeypatch registry reader - original_getproxies_registry = urllib.request.getproxies_registry - urllib.request.getproxies_registry = lambda: {"no": fake_proxy_override} - - try: - self.assertFalse(urllib.request.proxy_bypass("notmatching.com")) - self.assertTrue(urllib.request.proxy_bypass("localhost")) - self.assertTrue(urllib.request.proxy_bypass("sub.example.com")) - finally: - urllib.request.getproxies_registry = original_getproxies_registry - + # Directly test the internal function + self.assertFalse(proxy_bypass_winreg("notmatching.com", fake_proxy_override)) + self.assertTrue(proxy_bypass_winreg("localhost", fake_proxy_override)) + self.assertTrue(proxy_bypass_winreg("sub.example.com", fake_proxy_override)) if __name__ == '__main__': unittest.main() From b3d5cdc068de1f7297ab2eb65dce520f6f181312 Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Wed, 27 Aug 2025 12:03:59 +0530 Subject: [PATCH 6/9] Trigger CI recheck for NEWS file --- Lib/test/test_urllib.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index 54a818b075ff66..8340e1296b0713 100644 --- a/Lib/test/test_urllib.py +++ b/Lib/test/test_urllib.py @@ -1,7 +1,8 @@ """Regression tests for what was in Python 2's "urllib" module""" import urllib.parse -import urllib.request import _proxy_bypass_winreg_override as proxy_bypass_winreg +import urllib.request +import _proxy_bypass_winreg_override as proxy_bypass_winreg import urllib.error import http.client import email.message From fa4a8461dd23c0ad3ae0325e46297db15037bf09 Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Wed, 27 Aug 2025 16:01:25 +0530 Subject: [PATCH 7/9] bpo-32465: Add NEWS entry for proxy_bypass_registry fix --- Lib/urllib/request.py | 10 +++++----- .../next/32465.proxy_bypass_trailing_semicolon.rst | 2 -- .../2025-08-27-15-59-56.gh-issue-76646.88J2hZ.rst | 2 ++ 3 files changed, 7 insertions(+), 7 deletions(-) delete mode 100644 Misc/NEWS.d/next/32465.proxy_bypass_trailing_semicolon.rst create mode 100644 Misc/NEWS.d/next/Windows/2025-08-27-15-59-56.gh-issue-76646.88J2hZ.rst diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 22609f9ff2efde..79a3fe75434d53 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -1996,23 +1996,23 @@ def ip2num(ipAddr): # Same as _proxy_bypass_macosx_sysconf, testable on all platforms -def _proxy_bypass_winreg_override(host, proxyOverride): +def _proxy_bypass_winreg_override(host, proxy_override): """Return True if the host should bypass the proxy server. The proxy override list is obtained from the Windows Internet settings proxy override registry value. An example of a proxy override value is: - "www.example.com;*.example.net; 192.168.0.1" + "www.example.com;*.example.net;192.168.0.1" """ from fnmatch import fnmatch host, _ = _splitport(host) # Split and remove empty or whitespace-only entries - proxyOverride = [x.strip() for x in proxyOverride.split(';') if x.strip()] + proxy_override = [x.strip() for x in proxy_override.split(';') if x.strip()] - for test in proxyOverride: + for test in proxy_override: # "" should bypass the proxy server for all intranet addresses if test == '': if '.' not in host: @@ -2021,7 +2021,7 @@ def _proxy_bypass_winreg_override(host, proxyOverride): return True return False - + if sys.platform == 'darwin': from _scproxy import _get_proxy_settings, _get_proxies diff --git a/Misc/NEWS.d/next/32465.proxy_bypass_trailing_semicolon.rst b/Misc/NEWS.d/next/32465.proxy_bypass_trailing_semicolon.rst deleted file mode 100644 index d1b4ce55f6daec..00000000000000 --- a/Misc/NEWS.d/next/32465.proxy_bypass_trailing_semicolon.rst +++ /dev/null @@ -1,2 +0,0 @@ -bpo-32465: _proxy_bypass_winreg_override strips empty entries from ProxyOverride on Windows. - diff --git a/Misc/NEWS.d/next/Windows/2025-08-27-15-59-56.gh-issue-76646.88J2hZ.rst b/Misc/NEWS.d/next/Windows/2025-08-27-15-59-56.gh-issue-76646.88J2hZ.rst new file mode 100644 index 00000000000000..de73cb0ff7422a --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2025-08-27-15-59-56.gh-issue-76646.88J2hZ.rst @@ -0,0 +1,2 @@ +Fix proxy_bypass_registry on Windows when ProxyOverride ends with a trailing +semicolon. Empty entries are now ignored. From 751316e452051de66ce82875062abe846cae8803 Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Wed, 27 Aug 2025 16:25:08 +0530 Subject: [PATCH 8/9] Fix trailing whitespace --- Lib/urllib/request.py | 8 +++++--- .../Windows/2025-08-27-15-59-56.gh-issue-76646.88J2hZ.rst | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 79a3fe75434d53..3754c11db266ee 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -2009,10 +2009,12 @@ def _proxy_bypass_winreg_override(host, proxy_override): host, _ = _splitport(host) - # Split and remove empty or whitespace-only entries - proxy_override = [x.strip() for x in proxy_override.split(';') if x.strip()] - + proxy_override = proxy_override.split(';') for test in proxy_override: + test = test.strip() + if not test: + continue + # "" should bypass the proxy server for all intranet addresses if test == '': if '.' not in host: diff --git a/Misc/NEWS.d/next/Windows/2025-08-27-15-59-56.gh-issue-76646.88J2hZ.rst b/Misc/NEWS.d/next/Windows/2025-08-27-15-59-56.gh-issue-76646.88J2hZ.rst index de73cb0ff7422a..55a38782f86b4b 100644 --- a/Misc/NEWS.d/next/Windows/2025-08-27-15-59-56.gh-issue-76646.88J2hZ.rst +++ b/Misc/NEWS.d/next/Windows/2025-08-27-15-59-56.gh-issue-76646.88J2hZ.rst @@ -1,2 +1,2 @@ -Fix proxy_bypass_registry on Windows when ProxyOverride ends with a trailing -semicolon. Empty entries are now ignored. +.. gh-76646: Fix proxy_bypass_registry on Windows when ProxyOverride ends with a trailing semicolon. Empty entries are now ignored. + From ad9cac2122b0c1f509a92069b1a744d6cb716638 Mon Sep 17 00:00:00 2001 From: PrineNaroliya Date: Wed, 27 Aug 2025 16:28:05 +0530 Subject: [PATCH 9/9] Fix trailing whitespace --- .../next/Windows/2025-08-27-15-59-56.gh-issue-76646.88J2hZ.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Windows/2025-08-27-15-59-56.gh-issue-76646.88J2hZ.rst b/Misc/NEWS.d/next/Windows/2025-08-27-15-59-56.gh-issue-76646.88J2hZ.rst index 55a38782f86b4b..da041f3bc16668 100644 --- a/Misc/NEWS.d/next/Windows/2025-08-27-15-59-56.gh-issue-76646.88J2hZ.rst +++ b/Misc/NEWS.d/next/Windows/2025-08-27-15-59-56.gh-issue-76646.88J2hZ.rst @@ -1,2 +1 @@ -.. gh-76646: Fix proxy_bypass_registry on Windows when ProxyOverride ends with a trailing semicolon. Empty entries are now ignored. - +.. gh-76646: Fix proxy_bypass_registry on Windows when ProxyOverride ends with a trailing semicolon. Empty entries are now ignored. \ No newline at end of file