Skip to content
Myr

Merge pull request #3713 from tiran/strict_http_protocol_check

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

Restrict URL preparation to HTTP/HTTPS

authored by Cory Benfield · committed by GitHub · parents 268672a, 34af72c · Browse files · 2 files+20 added−3 removed
This commit

State

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

Diff stats

2 files+20 added−3 removed

  • tests:+17 added−0 removed
  • other:+3 added−3 removed

2 files+20 added−3 removed

modified:requests/models.py+3 added-3 removed
345 unmodified lines
346 # Remove leading whitespaces from url347 url = url.lstrip()348349 # Don't do any URL preparation for non-HTTP schemes like `mailto`,350 # `data` etc to work around exceptions from `url_parse`, which351 # handles RFC 3986 only.352 if ':' in url and not url.lower().startswith('http'):350 # `data`, `http+unix` etc to work around exceptions from `url_parse`,351 # which handles RFC 3986 only.352 if ':' in url and not url.lower().startswith(('http://', 'https://')):353 self.url = url354 return355356 # Support for unicode domain names and paths.
544 unmodified lines
modified:tests/test_requests.py+17 added
2136 unmodified lines
2137 def test_preparing_bad_url(self, url):2138 r = requests.Request('GET', url=url)2139 with pytest.raises(requests.exceptions.InvalidURL):2140 r.prepare()21412142 @pytest.mark.parametrize(2143 'protocol, url',2144 (2145 ("http+unix://", b"http+unix://%2Fvar%2Frun%2Fsocket/path"),2146 ("http+unix://", u"http+unix://%2Fvar%2Frun%2Fsocket/path"),2147 ("mailto", b"mailto:user@example.org"),2148 ("mailto", u"mailto:user@example.org"),2149 ("data", b"data:SSDimaUgUHl0aG9uIQ=="),2150 )2151 )2152 def test_url_passthrough(self, protocol, url):2153 session = requests.Session()2154 session.mount(protocol, HTTPAdapter())2155 p = requests.Request('GET', url=url)2156 p.prepare()2157 assert p.url == url