Skip to content
Myr

Fix DNS resolution by using hostname instead of netloc and strip username and password when comparing against proxy bypass items.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Darren Dormer · committed by Darren Dormer · parent 020f965 · Browse files · 4 files+37 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+37 added−11 removed

  • tests:+23 added−2 removed
  • other:+14 added−9 removed

4 files+37 added−11 removed

modified:requests/utils.py+11 added-8 removed
701 unmodified lines
702 # we're getting isn't in the no_proxy list.703 no_proxy_arg = no_proxy704 if no_proxy is None:705 no_proxy = get_proxy('no_proxy')706 netloc = urlparse(url).netloc706 parsed = urlparse(url)707708 if no_proxy:709 # We need to check whether we match here. We need to see if we match710 # the end of the netloc, both with and without the port.710 # the end of the hostname, both with and without the port.711 no_proxy = (712 host for host in no_proxy.replace(' ', '').split(',') if host713 )714715 ip = netloc.split(':')[0]716 if is_ipv4_address(ip):715 if is_ipv4_address(parsed.hostname):716 for proxy_ip in no_proxy:717 if is_valid_cidr(proxy_ip):719 if address_in_network(ip, proxy_ip):718 if address_in_network(parsed.hostname, proxy_ip):719 return True721 elif ip == proxy_ip:720 elif parsed.hostname == proxy_ip:721 # If no_proxy ip was defined in plain IP notation instead of cidr notation &722 # matches the IP of the index723 return True724 else:725 host_with_port = parsed.hostname726 if parsed.port:727 host_with_port += ':{0}'.format(parsed.port)728729 for host in no_proxy:727 if netloc.endswith(host) or netloc.split(':')[0].endswith(host):730 if parsed.hostname.endswith(host) or host_with_port.endswith(host):731 # The URL does match something in no_proxy, so we don't want732 # to apply the proxies on this URL.733 return True734
4 unmodified lines
739 # exceptions we've seen, though: this call failing in other ways can reveal740 # legitimate problems.741 with set_environ('no_proxy', no_proxy_arg):742 try:740 bypass = proxy_bypass(netloc)743 bypass = proxy_bypass(parsed.hostname)744 except (TypeError, socket.gaierror):745 bypass = False746747 if bypass:
228 unmodified lines
modified:tests/test_utils.py+23 added-2 removed
612 unmodified lines
613 ('http://192.168.0.1/', True),614 ('http://172.16.1.1/', True),615 ('http://172.16.1.1:5000/', True),616 ('http://localhost.localdomain:5000/v1.0/', True),617 ('http://google.com:6000/', True),618 ('http://172.16.1.12/', False),619 ('http://172.16.1.12:5000/', False),620 ('http://google.com:5000/v1.0/', False),621 ))622def test_should_bypass_proxies(url, expected, monkeypatch):623 """Tests for function should_bypass_proxies to check if proxy624 can be bypassed or not625 """625 monkeypatch.setenv('no_proxy', '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1')626 monkeypatch.setenv('NO_PROXY', '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1')626 monkeypatch.setenv('no_proxy', '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1, google.com:6000')627 monkeypatch.setenv('NO_PROXY', '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1, google.com:6000')628 assert should_bypass_proxies(url, no_proxy=None) == expected629630631@pytest.mark.parametrize(632 'url, expected', (633 ('http://172.16.1.1/', '172.16.1.1'),634 ('http://172.16.1.1:5000/', '172.16.1.1'),635 ('http://user:pass@172.16.1.1', '172.16.1.1'),636 ('http://user:pass@172.16.1.1:5000', '172.16.1.1'),637 ('http://hostname/', 'hostname'),638 ('http://hostname:5000/', 'hostname'),639 ('http://user:pass@hostname', 'hostname'),640 ('http://user:pass@hostname:5000', 'hostname'),641 ))642def test_should_bypass_proxies_pass_only_hostname(url, expected, mocker):643 """The proxy_bypass function should be called with a hostname or IP without644 a port number or auth credentials.645 """646 proxy_bypass = mocker.patch('requests.utils.proxy_bypass')647 should_bypass_proxies(url, no_proxy=None)648 proxy_bypass.assert_called_once_with(expected)649650651@pytest.mark.parametrize(652 'cookiejar', (653 compat.cookielib.CookieJar(),654 RequestsCookieJar()655 ))
122 unmodified lines
modified:AUTHORS.rst+1 added
180 unmodified lines
181- Taylor Hoff <primdevs@protonmail.com> (`@PrimordialHelios <https://github.com/PrimordialHelios>`_)182- Arthur Vigil (`@ahvigil <https://github.com/ahvigil>`_)183- Nehal J Wani (`@nehaljwani <https://github.com/nehaljwani>`_)184- Demetrios Bairaktaris (`@DemetriosBairaktaris <https://github.com/demetriosbairaktaris>`_)185- Darren Dormer (`@ddormer <https://github.com/ddormer>`_)
modified:HISTORY.rst+2 added-1 removed
15 unmodified lines
16- Parsing empty ``Link`` headers with ``parse_header_links()`` no longer return one bogus entry17- Fixed issue where loading the default certificate bundle from a zip archive18 would raise an ``IOError``19- Fixed issue with unexpected ``ImportError`` on windows system which do not support ``winreg`` module20- DNS resolution in proxy bypass no longer includes the username and password in21 the request. This also fixes the issue of DNS queries failing on macOS.2221232.18.4 (2017-08-15)24+++++++++++++++++++2526**Improvements**
1518 unmodified lines