From 692e5f5c01a87a1c4cd7a9813b5ccc5463c977cd Mon Sep 17 00:00:00 2001 From: Gilbert Gilb's Date: Mon, 1 Oct 2018 18:17:54 +0200 Subject: [PATCH 1/2] Fix no_proxy being too greedy. Fixes #4795 --- requests/utils.py | 8 ++++++-- tests/test_utils.py | 10 ++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/requests/utils.py b/requests/utils.py index 671973127f..c3058f5d87 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -727,12 +727,16 @@ def should_bypass_proxies(url, no_proxy): # matches the IP of the index return True else: - host_with_port = parsed.hostname + host_without_port = '.' + parsed.hostname + host_with_port = host_without_port if parsed.port: host_with_port += ':{0}'.format(parsed.port) for host in no_proxy: - if parsed.hostname.endswith(host) or host_with_port.endswith(host): + if not host.startswith('.'): + host = '.' + host + + if host_without_port.endswith(host) or host_with_port.endswith(host): # The URL does match something in no_proxy, so we don't want # to apply the proxies on this URL. return True diff --git a/tests/test_utils.py b/tests/test_utils.py index f34c630f07..d9e90745b7 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -6,6 +6,7 @@ from io import BytesIO import zipfile from collections import deque +import sys import pytest from requests import compat @@ -182,6 +183,7 @@ def test_not_bypass(self, url): 'http://192.168.1.1:5000/', 'http://192.168.1.1/', 'http://www.requests.com/', + 'http://requests.com/', )) def test_bypass_no_proxy_keyword(self, url): no_proxy = '192.168.1.1,requests.com' @@ -194,6 +196,13 @@ def test_bypass_no_proxy_keyword(self, url): 'http://172.16.1.1/', 'http://172.16.1.1:5000/', 'http://localhost.localdomain:5000/v1.0/', + pytest.param( + 'http://somerequests.com', + marks=pytest.mark.skipif( + (sys.version_info[0], sys.version_info[1]) == (3, 4), + reason='Python 3.4 implementation of no_proxy is greedy', + ), + ), )) def test_not_bypass_no_proxy_keyword(self, url, monkeypatch): # This is testing that the 'no_proxy' argument overrides the @@ -619,6 +628,7 @@ def test_urldefragauth(url, expected): ('http://172.16.1.1:5000/', True), ('http://localhost.localdomain:5000/v1.0/', True), ('http://google.com:6000/', True), + ('http://zgoogle.com:6000/', False), ('http://172.16.1.12/', False), ('http://172.16.1.12:5000/', False), ('http://google.com:5000/v1.0/', False), From 72c574c8a87f3027ca68cd2c326d59955588ebed Mon Sep 17 00:00:00 2001 From: Gilbert Gilb's Date: Mon, 1 Oct 2018 20:25:50 +0200 Subject: [PATCH 2/2] Update minimum required pytest version. --- Pipfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pipfile b/Pipfile index 3e0fd729eb..dc62929f33 100644 --- a/Pipfile +++ b/Pipfile @@ -4,7 +4,7 @@ verify_ssl = true name = "pypi" [dev-packages] -pytest = ">=2.8.0" +pytest = ">=3.1.0" codecov = "*" pytest-httpbin = ">=0.0.7" pytest-mock = "*"