Skip to content
Myr

Strip Authorization header whenever root URL changes

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

Previously the header was stripped only if the hostname changed, but in an https -> http redirect that can leak the credentials on the wire (#4716). Based on with RFC 7235 section 2.2, the header is now stripped if the "canonical root URL" (scheme+authority) has changed, by checking scheme, hostname and port.

authored by Bruce Merry · committed by Bruce Merry · parent dd754d1 · Browse files · 2 files+14 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

2 files+14 added−2 removed

  • tests:+11 added−1 removed
  • other:+3 added−1 removed

2 files+14 added−2 removed

modified:requests/sessions.py+3 added-1 removed
240 unmodified lines
241 # authentication headers.242 original_parsed = urlparse(response.request.url)243 redirect_parsed = urlparse(url)244245 if (original_parsed.hostname != redirect_parsed.hostname):245 if (original_parsed.hostname != redirect_parsed.hostname246 or original_parsed.port != redirect_parsed.port247 or original_parsed.scheme != redirect_parsed.scheme):248 del headers['Authorization']249250 # .netrc might have more auth for us on our new host.251 new_auth = get_netrc_auth(url) if self.trust_env else None
500 unmodified lines
modified:tests/test_requests.py+11 added-1 removed
1579 unmodified lines
1580 params={'url': 'http://www.google.co.uk'},1581 auth=('user', 'pass'),1582 )1583 assert r.history[0].request.headers['Authorization']1584 assert not r.request.headers.get('Authorization', '')1584 assert 'Authorization' not in r.request.headers15851586 def test_auth_is_stripped_on_scheme_redirect(self, httpbin, httpbin_secure, httpbin_ca_bundle):1587 r = requests.get(1588 httpbin_secure('redirect-to'),1589 params={'url': httpbin('get')},1590 auth=('user', 'pass'),1591 verify=httpbin_ca_bundle1592 )1593 assert r.history[0].request.headers['Authorization']1594 assert 'Authorization' not in r.request.headers15951596 def test_auth_is_retained_for_redirect_on_host(self, httpbin):1597 r = requests.get(httpbin('redirect/1'), auth=('user', 'pass'))1598 h1 = r.history[0].request.headers['Authorization']1599 h2 = r.request.headers['Authorization']
887 unmodified lines