Skip to content

Commit a4c415f

Browse files
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
1 parent ffaec6e commit a4c415f

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

Lib/test/test_urllib.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,21 @@ def test_with_method_arg(self):
16861686
request.method = 'HEAD'
16871687
self.assertEqual(request.get_method(), 'HEAD')
16881688

1689+
class ProxyBypassRegistryTests(unittest.TestCase):
1690+
def test_proxy_bypass_registry_trailing_semicolon(self):
1691+
fake_proxy_override = "localhost;*.example.com;"
1692+
1693+
# Monkeypatch registry reader
1694+
original_getproxies_registry = urllib.request.getproxies_registry
1695+
urllib.request.getproxies_registry = lambda: {"no": fake_proxy_override}
1696+
1697+
try:
1698+
self.assertFalse(urllib.request.proxy_bypass("notmatching.com"))
1699+
self.assertTrue(urllib.request.proxy_bypass("localhost"))
1700+
self.assertTrue(urllib.request.proxy_bypass("sub.example.com"))
1701+
finally:
1702+
urllib.request.getproxies_registry = original_getproxies_registry
1703+
16891704

16901705
if __name__ == '__main__':
16911706
unittest.main()

Lib/urllib/request.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,7 +1996,7 @@ def ip2num(ipAddr):
19961996

19971997

19981998
# Same as _proxy_bypass_macosx_sysconf, testable on all platforms
1999-
def _proxy_bypass_winreg_override(host, override):
1999+
def _proxy_bypass_winreg_override(host, proxyOverride):
20002000
"""Return True if the host should bypass the proxy server.
20012001
20022002
The proxy override list is obtained from the Windows
@@ -2008,15 +2008,18 @@ def _proxy_bypass_winreg_override(host, override):
20082008
from fnmatch import fnmatch
20092009

20102010
host, _ = _splitport(host)
2011-
proxy_override = override.split(';')
2012-
for test in proxy_override:
2013-
test = test.strip()
2011+
2012+
# Split and remove empty or whitespace-only entries
2013+
proxyOverride = [x.strip() for x in proxyOverride.split(';') if x.strip()]
2014+
2015+
for test in proxyOverride:
20142016
# "<local>" should bypass the proxy server for all intranet addresses
20152017
if test == '<local>':
20162018
if '.' not in host:
20172019
return True
20182020
elif fnmatch(host, test):
20192021
return True
2022+
20202023
return False
20212024

20222025

0 commit comments

Comments
 (0)