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()
11 changes: 7 additions & 4 deletions Lib/urllib/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

"""Return True if the host should bypass the proxy server.

The proxy override list is obtained from the Windows
Expand All @@ -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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is clearer. If the fix is to skip empty entries, just add ``if not test: continue`.


# Split and remove empty or whitespace-only entries
proxyOverride = [x.strip() for x in proxyOverride.split(';') if x.strip()]

for test in proxyOverride:
# "<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


Expand Down
2 changes: 2 additions & 0 deletions Misc/NEWS.d/next/32465.proxy_bypass_trailing_semicolon.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bpo-32465: _proxy_bypass_winreg_override strips empty entries from ProxyOverride on Windows.

Loading