diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py index ae524c5ffba6b1..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 urllib.request +import _proxy_bypass_winreg_override as proxy_bypass_winreg import urllib.error import http.client import email.message @@ -1686,6 +1687,14 @@ 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;" + + # 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() diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index af93d4cd75dbef..3754c11db266ee 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -1996,29 +1996,34 @@ 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, 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) - proxy_override = override.split(';') + + 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: return True elif fnmatch(host, test): return True - return False + return False + if sys.platform == 'darwin': from _scproxy import _get_proxy_settings, _get_proxies 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..da041f3bc16668 --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2025-08-27-15-59-56.gh-issue-76646.88J2hZ.rst @@ -0,0 +1 @@ +.. 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