Skip to content
Myr

Merge pull request #3738 from Lukasa/revert-exclusion-patch

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

Revert "Restrict URL preparation to HTTP/HTTPS"

authored by Cory Benfield · committed by GitHub · parents ca15d48, 6f659a4 · Browse files · 2 files+69 added−15 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+69 added−15 removed

  • tests:+66 added−12 removed
  • other:+3 added−3 removed

2 files+69 added−15 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`, `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://')):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'):353 self.url = url354 return355356 # Support for unicode domain names and paths.
550 unmodified lines
modified:tests/test_requests.py+66 added-12 removed
2175 unmodified lines
2176 with pytest.raises(requests.exceptions.InvalidURL):2177 r.prepare()21782179 @pytest.mark.parametrize(2180 'protocol, url',2180 'input, expected',2181 (2182 ("http+unix://", b"http+unix://%2Fvar%2Frun%2Fsocket/path"),2183 ("http+unix://", u"http+unix://%2Fvar%2Frun%2Fsocket/path"),2184 ("mailto", b"mailto:user@example.org"),2185 ("mailto", u"mailto:user@example.org"),2186 ("data", b"data:SSDimaUgUHl0aG9uIQ=="),2182 (2183 b"http+unix://%2Fvar%2Frun%2Fsocket/path",2184 u"http+unix://%2fvar%2frun%2fsocket/path",2185 ),2186 (2187 u"http+unix://%2Fvar%2Frun%2Fsocket/path",2188 u"http+unix://%2fvar%2frun%2fsocket/path",2189 ),2190 (2191 b"mailto:user@example.org",2192 u"mailto:user@example.org",2193 ),2194 (2195 u"mailto:user@example.org",2196 u"mailto:user@example.org",2197 ),2198 (2199 b"data:SSDimaUgUHl0aG9uIQ==",2200 u"data:SSDimaUgUHl0aG9uIQ==",2201 )2202 )2203 )2189 def test_url_passthrough(self, protocol, url):2190 session = requests.Session()2191 session.mount(protocol, HTTPAdapter())2192 p = requests.Request('GET', url=url)2193 p.prepare()2194 assert p.url == url2204 def test_url_mutation(self, input, expected):2205 """2206 This test validates that we correctly exclude some URLs from2207 preparation, and that we handle others. Specifically, it tests that2208 any URL whose scheme doesn't begin with "http" is left alone, and2209 those whose scheme *does* begin with "http" are mutated.2210 """2211 r = requests.Request('GET', url=input)2212 p = r.prepare()2213 assert p.url == expected22142215 @pytest.mark.parametrize(2216 'input, params, expected',2217 (2218 (2219 b"http+unix://%2Fvar%2Frun%2Fsocket/path",2220 {"key": "value"},2221 u"http+unix://%2fvar%2frun%2fsocket/path?key=value",2222 ),2223 (2224 u"http+unix://%2Fvar%2Frun%2Fsocket/path",2225 {"key": "value"},2226 u"http+unix://%2fvar%2frun%2fsocket/path?key=value",2227 ),2228 (2229 b"mailto:user@example.org",2230 {"key": "value"},2231 u"mailto:user@example.org",2232 ),2233 (2234 u"mailto:user@example.org",2235 {"key": "value"},2236 u"mailto:user@example.org",2237 ),2238 )2239 )2240 def test_parameters_for_nonstandard_schemes(self, input, params, expected):2241 """2242 Setting paramters for nonstandard schemes is allowed if those schemes2243 begin with "http", and is forbidden otherwise.2244 """2245 r = requests.Request('GET', url=input, params=params)2246 p = r.prepare()2247 assert p.url == expected2248