Skip to content

Commit 4e4a4ee

Browse files
committed
Revert "Update macOS proxy bypass rules"
This reverts commit 95e393f.
1 parent 95e393f commit 4e4a4ee

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

Lib/test/test_urllib2.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,35 +1514,35 @@ def test_winreg_proxy_bypass(self):
15141514
def test_osx_proxy_bypass(self):
15151515
bypass = {
15161516
'exclude_simple': False,
1517-
'exceptions': ['foo.bar', '*.bar.com', '127.0.0.1', '10.0/16',
1518-
'11.0./16', '12.13.14.0/24', '19.*',
1519-
'2001:0db8:0123:4567:89ab:cdef:1234:5678']
1517+
'exceptions': ['foo.bar', '*.bar.com', '127.0.0.1', '10.10',
1518+
'10.0/16']
15201519
}
15211520
# Check hosts that should trigger the proxy bypass
1522-
for host in ('foo.bar', 'www.bar.com', '127.0.0.1', '10.0.0.1',
1523-
'11.0.0.1', '12.13.14.15', '19.0.0.1',
1524-
'[2001:0db8:0123:4567:89ab:cdef:1234:5678]'):
1521+
for host in ('foo.bar', 'www.bar.com', '127.0.0.1', '10.10.0.1',
1522+
'10.0.0.1'):
15251523
self.assertTrue(_proxy_bypass_macosx_sysconf(host, bypass),
15261524
'expected bypass of %s to be True' % host)
15271525
# Check hosts that should not trigger the proxy bypass
15281526
for host in ('abc.foo.bar', 'bar.com', '127.0.0.2', '10.11.0.1',
1529-
'11.1.0.1', '12.13.15.16', 'notinbypass',
1530-
'[2001:0db8:0123:4567:89ab:cdef:1234:0001]'):
1527+
'notinbypass'):
15311528
self.assertFalse(_proxy_bypass_macosx_sysconf(host, bypass),
15321529
'expected bypass of %s to be False' % host)
15331530

15341531
# Check the exclude_simple flag
15351532
bypass = {'exclude_simple': True, 'exceptions': []}
15361533
self.assertTrue(_proxy_bypass_macosx_sysconf('test', bypass))
15371534

1538-
# Check that invalid IPs are ignored
1535+
# Check that invalid prefix lengths are ignored
15391536
bypass = {
15401537
'exclude_simple': False,
1541-
'exceptions': ['10.0.0.0/40', '1.256/16', '192.168']
1538+
'exceptions': [ '10.0.0.0/40', '172.19.10.0/24' ]
15421539
}
1543-
for host in ('10.0.1.5', '1.256.0.1', '192.168.0.1'):
1544-
self.assertFalse(_proxy_bypass_macosx_sysconf(host, bypass),
1545-
'expected bypass of %s to be False' % host)
1540+
host = '172.19.10.5'
1541+
self.assertTrue(_proxy_bypass_macosx_sysconf(host, bypass),
1542+
'expected bypass of %s to be True' % host)
1543+
host = '10.0.1.5'
1544+
self.assertFalse(_proxy_bypass_macosx_sysconf(host, bypass),
1545+
'expected bypass of %s to be False' % host)
15461546

15471547
def check_basic_auth(self, headers, realm):
15481548
with self.subTest(realm=realm, headers=headers):

Lib/urllib/request.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,44 +2563,51 @@ def _proxy_bypass_macosx_sysconf(host, proxy_settings):
25632563
}
25642564
"""
25652565
from fnmatch import fnmatch
2566-
from ipaddress import IPv4Address, IPv4Network
2566+
from ipaddress import AddressValueError, IPv4Address
25672567

2568-
host, _ = _splitport(host)
2569-
# Strip brackets for IPv6 addresses
2570-
if host and host[0] == '[' and host[-1] == ']':
2571-
host = host[1:-1]
2568+
hostonly, port = _splitport(host)
2569+
2570+
def ip2num(ipAddr):
2571+
parts = ipAddr.split('.')
2572+
parts = list(map(int, parts))
2573+
if len(parts) != 4:
2574+
parts = (parts + [0, 0, 0, 0])[:4]
2575+
return (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3]
25722576

25732577
# Check for simple host names:
25742578
if '.' not in host:
25752579
if proxy_settings['exclude_simple']:
25762580
return True
25772581

2578-
host_ip = None
2582+
hostIP = None
25792583
try:
2580-
host_ip = IPv4Address(host)
2581-
except ValueError:
2584+
hostIP = int(IPv4Address(hostonly))
2585+
except AddressValueError:
25822586
pass
25832587

25842588
for value in proxy_settings.get('exceptions', ()):
25852589
# Items in the list are strings like these: *.local, 169.254/16
2586-
value = value.strip()
25872590
if not value: continue
25882591

2589-
m = re.match(r"(\d+(?:\.\d+)*)\.?/(\d+)", value)
2590-
if m is not None and host_ip is not None:
2591-
base = m.group(1)
2592+
m = re.match(r"(\d+(?:\.\d+)*)(/\d+)?", value)
2593+
if m is not None and hostIP is not None:
2594+
base = ip2num(m.group(1))
25922595
mask = m.group(2)
2593-
parts = base.split(".")
2594-
if len(parts) < 4:
2595-
base = ".".join((parts + ["0", "0", "0"])[:4])
2596-
try:
2597-
network = IPv4Network("{}/{}".format(base, mask))
2598-
if host_ip in network:
2599-
return True
2600-
except ValueError:
2601-
pass
2596+
if mask is None:
2597+
mask = 8 * (m.group(1).count('.') + 1)
2598+
else:
2599+
mask = int(mask[1:])
2600+
2601+
if mask < 0 or mask > 32:
2602+
# System libraries ignore invalid prefix lengths
2603+
continue
2604+
2605+
mask = 32 - mask
2606+
2607+
if (hostIP >> mask) == (base >> mask):
2608+
return True
26022609

2603-
if fnmatch(host, value):
2610+
elif fnmatch(host, value):
26042611
return True
26052612

26062613
return False

0 commit comments

Comments
 (0)