Skip to content
Myr

Preserve Authorization header on HTTPS redirect (#1850) (#2074)

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

* Preserve Authorization header on HTTPS redirect (#1850) * Update httpx/_client.py

authored by waterfountain1996 · committed by GitHub · parent 4f8068a · Browse files · 4 files+57 added−3 removed
This commit

State

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

Trailers

  • Co-authored-by: Tom Christie <tom@tomchristie.com>

Diff stats

4 files+57 added−3 removed

  • tests:+37 added−0 removed
  • other:+20 added−3 removed

4 files+57 added−3 removed

modified:httpx/_client.py+5 added-3 removed
49 unmodified lines
50 Timer,51 URLPattern,52 get_environment_proxies,53 get_logger,54 is_https_redirect,55 same_origin,56)5758# The type annotation for @classmethod and context managers here follows PEP 484
473 unmodified lines
532 """533 headers = Headers(request.headers)534535 if not same_origin(url, request.url):536 if not is_https_redirect(request.url, url):535 # Strip Authorization headers when responses are redirected away from536 # the origin.537 headers.pop("Authorization", None)537 # Strip Authorization headers when responses are redirected538 # away from the origin. (Except for direct HTTP to HTTPS redirects.)539 headers.pop("Authorization", None)540541 # Update the Host header.542 headers["Host"] = url.netloc.decode("ascii")543
1437 unmodified lines
modified:httpx/_utils.py+15 added
280 unmodified lines
281 and port_or_default(url) == port_or_default(other)282 )283284285def is_https_redirect(url: "URL", location: "URL") -> bool:286 """287 Return 'True' if 'location' is a HTTPS upgrade of 'url'288 """289 if url.host != location.host:290 return False291292 return (293 url.scheme == "http"294 and port_or_default(url) == 80295 and location.scheme == "https"296 and port_or_default(location) == 443297 )298299300def get_environment_proxies() -> typing.Dict[str, typing.Optional[str]]:301 """Gets proxy information from the environment"""302303 # urllib.request.getproxies() falls back on System
220 unmodified lines
modified:tests/client/test_redirects.py+18 added
268 unmodified lines
269 assert response.url == "https://example.org/cross_domain_target"270 assert "authorization" not in response.json()["headers"]271272273def test_cross_domain_https_redirect_with_auth_header():274 client = httpx.Client(transport=httpx.MockTransport(redirects))275 url = "http://example.com/cross_domain"276 headers = {"Authorization": "abc"}277 response = client.get(url, headers=headers, follow_redirects=True)278 assert response.url == "https://example.org/cross_domain_target"279 assert "authorization" not in response.json()["headers"]280281282def test_cross_domain_redirect_with_auth():283 client = httpx.Client(transport=httpx.MockTransport(redirects))284 url = "https://example.com/cross_domain"285 response = client.get(url, auth=("user", "pass"), follow_redirects=True)
9 unmodified lines
295 assert response.url == "https://example.org/cross_domain_target"296 assert response.json()["headers"]["authorization"] == "abc"297298299def test_same_domain_https_redirect_with_auth_header():300 client = httpx.Client(transport=httpx.MockTransport(redirects))301 url = "http://example.org/cross_domain"302 headers = {"Authorization": "abc"}303 response = client.get(url, headers=headers, follow_redirects=True)304 assert response.url == "https://example.org/cross_domain_target"305 assert response.json()["headers"]["authorization"] == "abc"306307308def test_body_redirect():309 """310 A 308 redirect should preserve the request body.311 """
134 unmodified lines
modified:tests/test_utils.py+19 added
8 unmodified lines
9 URLPattern,10 get_ca_bundle_from_env,11 get_environment_proxies,12 guess_json_utf,13 is_https_redirect,14 obfuscate_sensitive_headers,15 parse_header_links,16 same_origin,17)
203 unmodified lines
221 origin2 = httpx.URL("HTTP://EXAMPLE.COM")222 assert not same_origin(origin1, origin2)223224225def test_is_https_redirect():226 url = httpx.URL("http://example.com")227 location = httpx.URL("https://example.com")228 assert is_https_redirect(url, location)229230231def test_is_not_https_redirect():232 url = httpx.URL("http://example.com")233 location = httpx.URL("https://www.example.com")234 assert not is_https_redirect(url, location)235236237def test_is_not_https_redirect_if_not_default_ports():238 url = httpx.URL("http://example.com:9999")239 location = httpx.URL("https://example.com:1337")240 assert not is_https_redirect(url, location)241242243@pytest.mark.parametrize(244 ["pattern", "url", "expected"],245 [246 ("http://example.com", "http://example.com", True),
32 unmodified lines