Skip to content

Commit eac1495

Browse files
[3.10] gh-115197: Stop resolving host in urllib.request proxy bypass (GH-115210) (GH-116070)
Use of a proxy is intended to defer DNS for the hosts to the proxy itself, rather than a potential for information leak of the host doing DNS resolution itself for any reason. Proxy bypass lists are strictly name based. Most implementations of proxy support agree. (cherry picked from commit c43b26d) Co-authored-by: Weii Wang <[email protected]>
1 parent 516a6d4 commit eac1495

File tree

3 files changed

+64
-44
lines changed

3 files changed

+64
-44
lines changed

Lib/test/test_urllib2.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
import subprocess
1515

1616
import urllib.request
17-
# The proxy bypass method imported below has logic specific to the OSX
18-
# proxy config data structure but is testable on all platforms.
17+
# The proxy bypass method imported below has logic specific to the
18+
# corresponding system but is testable on all platforms.
1919
from urllib.request import (Request, OpenerDirector, HTTPBasicAuthHandler,
2020
HTTPPasswordMgrWithPriorAuth, _parse_proxy,
21+
_proxy_bypass_winreg_override,
2122
_proxy_bypass_macosx_sysconf,
2223
AbstractDigestAuthHandler)
2324
from urllib.parse import urlparse
@@ -1443,6 +1444,30 @@ def test_proxy_https_proxy_authorization(self):
14431444
self.assertEqual(req.host, "proxy.example.com:3128")
14441445
self.assertEqual(req.get_header("Proxy-authorization"), "FooBar")
14451446

1447+
@unittest.skipUnless(os.name == "nt", "only relevant for Windows")
1448+
def test_winreg_proxy_bypass(self):
1449+
proxy_override = "www.example.com;*.example.net; 192.168.0.1"
1450+
proxy_bypass = _proxy_bypass_winreg_override
1451+
for host in ("www.example.com", "www.example.net", "192.168.0.1"):
1452+
self.assertTrue(proxy_bypass(host, proxy_override),
1453+
"expected bypass of %s to be true" % host)
1454+
1455+
for host in ("example.com", "www.example.org", "example.net",
1456+
"192.168.0.2"):
1457+
self.assertFalse(proxy_bypass(host, proxy_override),
1458+
"expected bypass of %s to be False" % host)
1459+
1460+
# check intranet address bypass
1461+
proxy_override = "example.com; <local>"
1462+
self.assertTrue(proxy_bypass("example.com", proxy_override),
1463+
"expected bypass of %s to be true" % host)
1464+
self.assertFalse(proxy_bypass("example.net", proxy_override),
1465+
"expected bypass of %s to be False" % host)
1466+
for host in ("test", "localhost"):
1467+
self.assertTrue(proxy_bypass(host, proxy_override),
1468+
"expect <local> to bypass intranet address '%s'"
1469+
% host)
1470+
14461471
@unittest.skipUnless(sys.platform == 'darwin', "only relevant for OSX")
14471472
def test_osx_proxy_bypass(self):
14481473
bypass = {

Lib/urllib/request.py

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2571,6 +2571,7 @@ def _proxy_bypass_macosx_sysconf(host, proxy_settings):
25712571
}
25722572
"""
25732573
from fnmatch import fnmatch
2574+
from ipaddress import AddressValueError, IPv4Address
25742575

25752576
hostonly, port = _splitport(host)
25762577

@@ -2587,20 +2588,17 @@ def ip2num(ipAddr):
25872588
return True
25882589

25892590
hostIP = None
2591+
try:
2592+
hostIP = int(IPv4Address(hostonly))
2593+
except AddressValueError:
2594+
pass
25902595

25912596
for value in proxy_settings.get('exceptions', ()):
25922597
# Items in the list are strings like these: *.local, 169.254/16
25932598
if not value: continue
25942599

25952600
m = re.match(r"(\d+(?:\.\d+)*)(/\d+)?", value)
2596-
if m is not None:
2597-
if hostIP is None:
2598-
try:
2599-
hostIP = socket.gethostbyname(hostonly)
2600-
hostIP = ip2num(hostIP)
2601-
except OSError:
2602-
continue
2603-
2601+
if m is not None and hostIP is not None:
26042602
base = ip2num(m.group(1))
26052603
mask = m.group(2)
26062604
if mask is None:
@@ -2623,6 +2621,31 @@ def ip2num(ipAddr):
26232621
return False
26242622

26252623

2624+
# Same as _proxy_bypass_macosx_sysconf, testable on all platforms
2625+
def _proxy_bypass_winreg_override(host, override):
2626+
"""Return True if the host should bypass the proxy server.
2627+
2628+
The proxy override list is obtained from the Windows
2629+
Internet settings proxy override registry value.
2630+
2631+
An example of a proxy override value is:
2632+
"www.example.com;*.example.net; 192.168.0.1"
2633+
"""
2634+
from fnmatch import fnmatch
2635+
2636+
host, _ = _splitport(host)
2637+
proxy_override = override.split(';')
2638+
for test in proxy_override:
2639+
test = test.strip()
2640+
# "<local>" should bypass the proxy server for all intranet addresses
2641+
if test == '<local>':
2642+
if '.' not in host:
2643+
return True
2644+
elif fnmatch(host, test):
2645+
return True
2646+
return False
2647+
2648+
26262649
if sys.platform == 'darwin':
26272650
from _scproxy import _get_proxy_settings, _get_proxies
26282651

@@ -2721,7 +2744,7 @@ def proxy_bypass_registry(host):
27212744
import winreg
27222745
except ImportError:
27232746
# Std modules, so should be around - but you never know!
2724-
return 0
2747+
return False
27252748
try:
27262749
internetSettings = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
27272750
r'Software\Microsoft\Windows\CurrentVersion\Internet Settings')
@@ -2731,40 +2754,10 @@ def proxy_bypass_registry(host):
27312754
'ProxyOverride')[0])
27322755
# ^^^^ Returned as Unicode but problems if not converted to ASCII
27332756
except OSError:
2734-
return 0
2757+
return False
27352758
if not proxyEnable or not proxyOverride:
2736-
return 0
2737-
# try to make a host list from name and IP address.
2738-
rawHost, port = _splitport(host)
2739-
host = [rawHost]
2740-
try:
2741-
addr = socket.gethostbyname(rawHost)
2742-
if addr != rawHost:
2743-
host.append(addr)
2744-
except OSError:
2745-
pass
2746-
try:
2747-
fqdn = socket.getfqdn(rawHost)
2748-
if fqdn != rawHost:
2749-
host.append(fqdn)
2750-
except OSError:
2751-
pass
2752-
# make a check value list from the registry entry: replace the
2753-
# '<local>' string by the localhost entry and the corresponding
2754-
# canonical entry.
2755-
proxyOverride = proxyOverride.split(';')
2756-
# now check if we match one of the registry values.
2757-
for test in proxyOverride:
2758-
if test == '<local>':
2759-
if '.' not in rawHost:
2760-
return 1
2761-
test = test.replace(".", r"\.") # mask dots
2762-
test = test.replace("*", r".*") # change glob sequence
2763-
test = test.replace("?", r".") # change glob char
2764-
for val in host:
2765-
if re.match(test, val, re.I):
2766-
return 1
2767-
return 0
2759+
return False
2760+
return _proxy_bypass_winreg_override(host, proxyOverride)
27682761

27692762
def proxy_bypass(host):
27702763
"""Return True, if host should be bypassed.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``urllib.request`` no longer resolves the hostname before checking it
2+
against the system's proxy bypass list on macOS and Windows.

0 commit comments

Comments
 (0)