Skip to content

Commit 57bbb00

Browse files
weiiwang01miss-islington
authored andcommitted
pythongh-115197: Stop resolving host in urllib.request proxy bypass (pythonGH-115210)
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 02bb367 commit 57bbb00

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
@@ -1445,6 +1446,30 @@ def test_proxy_https_proxy_authorization(self):
14451446
self.assertEqual(req.host, "proxy.example.com:3128")
14461447
self.assertEqual(req.get_header("Proxy-authorization"), "FooBar")
14471448

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

Lib/urllib/request.py

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2577,6 +2577,7 @@ def _proxy_bypass_macosx_sysconf(host, proxy_settings):
25772577
}
25782578
"""
25792579
from fnmatch import fnmatch
2580+
from ipaddress import AddressValueError, IPv4Address
25802581

25812582
hostonly, port = _splitport(host)
25822583

@@ -2593,20 +2594,17 @@ def ip2num(ipAddr):
25932594
return True
25942595

25952596
hostIP = None
2597+
try:
2598+
hostIP = int(IPv4Address(hostonly))
2599+
except AddressValueError:
2600+
pass
25962601

25972602
for value in proxy_settings.get('exceptions', ()):
25982603
# Items in the list are strings like these: *.local, 169.254/16
25992604
if not value: continue
26002605

26012606
m = re.match(r"(\d+(?:\.\d+)*)(/\d+)?", value)
2602-
if m is not None:
2603-
if hostIP is None:
2604-
try:
2605-
hostIP = socket.gethostbyname(hostonly)
2606-
hostIP = ip2num(hostIP)
2607-
except OSError:
2608-
continue
2609-
2607+
if m is not None and hostIP is not None:
26102608
base = ip2num(m.group(1))
26112609
mask = m.group(2)
26122610
if mask is None:
@@ -2629,6 +2627,31 @@ def ip2num(ipAddr):
26292627
return False
26302628

26312629

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

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

27752768
def proxy_bypass(host):
27762769
"""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)