Skip to content
11 changes: 10 additions & 1 deletion Lib/test/test_urllib.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
13 changes: 9 additions & 4 deletions Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

# "<local>" should bypass the proxy server for all intranet addresses
if test == '<local>':
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. gh-76646: Fix proxy_bypass_registry on Windows when ProxyOverride ends with a trailing semicolon. Empty entries are now ignored.
Loading