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
Provenance
- Commit
4401d55- Tree
c018084- Author
waterfountain1996 <71171892+waterfountain1996@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
4f8068a
4 files changed+57 added−3 removed
modified:httpx/_client.py+5 added-3 removed
49 unmodified lines50 Timer,51 URLPattern,52 get_environment_proxies,53 get_logger,54+ is_https_redirect,55 same_origin,56 )57 58 # The type annotation for @classmethod and context managers here follows PEP 484473 unmodified lines532 """533 headers = Headers(request.headers)534 535 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)540 541 # Update the Host header.542 headers["Host"] = url.netloc.decode("ascii")543 1437 unmodified linesmodified:httpx/_utils.py+15 added
280 unmodified lines281 and port_or_default(url) == port_or_default(other)282 )283 284 285+def 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 False291+292+ return (293+ url.scheme == "http"294+ and port_or_default(url) == 80295+ and location.scheme == "https"296+ and port_or_default(location) == 443297+ )298+299+300 def get_environment_proxies() -> typing.Dict[str, typing.Optional[str]]:301 """Gets proxy information from the environment"""302 303 # urllib.request.getproxies() falls back on System220 unmodified linesmodified:tests/client/test_redirects.py+18 added
268 unmodified lines269 assert response.url == "https://example.org/cross_domain_target"270 assert "authorization" not in response.json()["headers"]271 272 273+def 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"]280+281+282 def 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 lines295 assert response.url == "https://example.org/cross_domain_target"296 assert response.json()["headers"]["authorization"] == "abc"297 298 299+def 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"306+307+308 def test_body_redirect():309 """310 A 308 redirect should preserve the request body.311 """134 unmodified linesmodified:tests/test_utils.py+19 added
8 unmodified lines9 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 lines221 origin2 = httpx.URL("HTTP://EXAMPLE.COM")222 assert not same_origin(origin1, origin2)223 224 225+def 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)229+230+231+def 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)235+236+237+def 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)241+242+243 @pytest.mark.parametrize(244 ["pattern", "url", "expected"],245 [246 ("http://example.com", "http://example.com", True),32 unmodified lines