Check if host is invalid for proxy
Before adoption: this commit came in with the history the project adopted, so no landing records it.
According to RFC3986, the authority section can be empty for a given URL, however, for a proxy URL, it shouldn't be. This patch adds a check to verify that the parsed URL will have a valid host before creating the proxy manager. Fixes #4353
authored by Nehal J Wani · committed by Nehal J Wani · parent 234f80a · Browse files · 5 files·+26 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+26 added−2 removed
- tests:+14 added−1 removed
- other:+12 added−1 removed
Provenance
- Commit
9a8a826- Tree
4b0df2b- Author
Nehal J Wani <nehaljw.kkd1@gmail.com>- Committer
Nehal J Wani <nehaljw.kkd1@gmail.com>- Parent
234f80a
5 files changed+26 added−2 removed
modified:requests/adapters.py+6 added-1 removed
11 unmodified lines12 import socket13 14 from urllib3.poolmanager import PoolManager, proxy_from_url15 from urllib3.response import HTTPResponse16+from urllib3.util import parse_url17 from urllib3.util import Timeout as TimeoutSauce18 from urllib3.util.retry import Retry19 from urllib3.exceptions import ClosedPoolError20 from urllib3.exceptions import ConnectTimeoutError13 unmodified lines34 get_auth_from_url, urldefragauth, select_proxy)35 from .structures import CaseInsensitiveDict36 from .cookies import extract_cookies_to_jar37 from .exceptions import (ConnectionError, ConnectTimeout, ReadTimeout, SSLError,37- ProxyError, RetryError, InvalidSchema)38+ ProxyError, RetryError, InvalidSchema, InvalidProxyURL)39 from .auth import _basic_auth_str40 41 try:42 from urllib3.contrib.socks import SOCKSProxyManager257 unmodified lines300 proxy = select_proxy(url, proxies)301 302 if proxy:303 proxy = prepend_scheme_if_needed(proxy, 'http')304+ proxy_url = parse_url(proxy)305+ if not proxy_url.host:306+ raise InvalidProxyURL("Please check proxy URL. It is malformed"307+ " and could be missing the host.")308 proxy_manager = self.proxy_manager_for(proxy)309 conn = proxy_manager.connection_from_url(url)310 else:311 # Only scheme should be lower case219 unmodified linesmodified:requests/exceptions.py+4 added
83 unmodified lines84 class InvalidHeader(RequestException, ValueError):85 """The header value provided was somehow invalid."""86 87 88+class InvalidProxyURL(InvalidURL):89+ """The proxy URL provided is invalid."""90+91+92 class ChunkedEncodingError(RequestException):93 """The server declared chunked encoding but sent an invalid chunk."""94 95 31 unmodified linesmodified:tests/test_requests.py+14 added-1 removed
21 unmodified lines22 cookiejar_from_dict, morsel_to_cookie)23 from requests.exceptions import (24 ConnectionError, ConnectTimeout, InvalidSchema, InvalidURL,25 MissingSchema, ReadTimeout, Timeout, RetryError, TooManyRedirects,26- ProxyError, InvalidHeader, UnrewindableBodyError, SSLError)26+ ProxyError, InvalidHeader, UnrewindableBodyError, SSLError, InvalidProxyURL)27 from requests.models import PreparedRequest28 from requests.structures import CaseInsensitiveDict29 from requests.sessions import SessionRedirectMixin30 from requests.models import urlencode494 unmodified lines525 # any proxy related error (address resolution, no route to host, etc) should result in a ProxyError526 with pytest.raises(ProxyError):527 requests.get('http://localhost:1', proxies={'http': 'non-resolvable-address'})528 529+ def test_proxy_error_on_bad_url(self, httpbin, httpbin_secure):530+ with pytest.raises(InvalidProxyURL):531+ requests.get(httpbin_secure(), proxies={'https': 'http:/badproxyurl:3128'})532+533+ with pytest.raises(InvalidProxyURL):534+ requests.get(httpbin(), proxies={'http': 'http://:8080'})535+536+ with pytest.raises(InvalidProxyURL):537+ requests.get(httpbin_secure(), proxies={'https': 'https://'})538+539+ with pytest.raises(InvalidProxyURL):540+ requests.get(httpbin(), proxies={'http': 'http:///example.com:8080'})541+542 def test_basicauth_with_netrc(self, httpbin):543 auth = ('user', 'pass')544 wrong_auth = ('wronguser', 'wrongpass')545 url = httpbin('basic-auth', 'user', 'pass')1895 unmodified linesmodified:AUTHORS.rst+1 added
178 unmodified lines179 - Ed Morley (`@edmorley <https://github.com/edmorley>`_)180 - Matt Liu <liumatt@gmail.com> (`@mlcrazy <https://github.com/mlcrazy>`_)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>`_)modified:HISTORY.rst+1 added
7 unmodified lines8 9 **Improvements**10 11 - Warn user about possible slowdown when using cryptography version < 1.3.412+- Check for invalid host in proxy URL, before forwarding request to adapter.13 14 **Bugfixes**15 16 - Parsing empty ``Link`` headers with ``parse_header_links()`` no longer return one bogus entry1526 unmodified lines