Skip to content

Commit c45d7c4

Browse files
authored
Merge pull request #4718 from bmerry/fix-4716
Strip Authorization header whenever root URL changes
2 parents dd754d1 + 857e9b7 commit c45d7c4

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

requests/sessions.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,22 @@ def get_redirect_target(self, resp):
115115
return to_native_string(location, 'utf8')
116116
return None
117117

118+
def should_strip_auth(self, old_url, new_url):
119+
"""Decide whether Authorization header should be removed when redirecting"""
120+
old_parsed = urlparse(old_url)
121+
new_parsed = urlparse(new_url)
122+
if old_parsed.hostname != new_parsed.hostname:
123+
return True
124+
# Special case: allow http -> https redirect when using the standard
125+
# ports. This isn't specified by RFC 7235, but is kept to avoid
126+
# breaking backwards compatibility with older versions of requests
127+
# that allowed any redirects on the same host.
128+
if (old_parsed.scheme == 'http' and old_parsed.port in (80, None)
129+
and new_parsed.scheme == 'https' and new_parsed.port in (443, None)):
130+
return False
131+
# Standard case: root URI must match
132+
return old_parsed.port != new_parsed.port or old_parsed.scheme != new_parsed.scheme
133+
118134
def resolve_redirects(self, resp, req, stream=False, timeout=None,
119135
verify=True, cert=None, proxies=None, yield_requests=False, **adapter_kwargs):
120136
"""Receives a Response. Returns a generator of Responses or Requests."""
@@ -236,14 +252,10 @@ def rebuild_auth(self, prepared_request, response):
236252
headers = prepared_request.headers
237253
url = prepared_request.url
238254

239-
if 'Authorization' in headers:
255+
if 'Authorization' in headers and self.should_strip_auth(response.request.url, url):
240256
# If we get redirected to a new host, we should strip out any
241257
# authentication headers.
242-
original_parsed = urlparse(response.request.url)
243-
redirect_parsed = urlparse(url)
244-
245-
if (original_parsed.hostname != redirect_parsed.hostname):
246-
del headers['Authorization']
258+
del headers['Authorization']
247259

248260
# .netrc might have more auth for us on our new host.
249261
new_auth = get_netrc_auth(url) if self.trust_env else None

tests/test_requests.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,15 +1573,15 @@ def test_nonhttp_schemes_dont_check_URLs(self):
15731573
preq = req.prepare()
15741574
assert test_url == preq.url
15751575

1576-
@pytest.mark.xfail(raises=ConnectionError)
1577-
def test_auth_is_stripped_on_redirect_off_host(self, httpbin):
1576+
def test_auth_is_stripped_on_http_downgrade(self, httpbin, httpbin_secure, httpbin_ca_bundle):
15781577
r = requests.get(
1579-
httpbin('redirect-to'),
1580-
params={'url': 'http://www.google.co.uk'},
1578+
httpbin_secure('redirect-to'),
1579+
params={'url': httpbin('get')},
15811580
auth=('user', 'pass'),
1581+
verify=httpbin_ca_bundle
15821582
)
15831583
assert r.history[0].request.headers['Authorization']
1584-
assert not r.request.headers.get('Authorization', '')
1584+
assert 'Authorization' not in r.request.headers
15851585

15861586
def test_auth_is_retained_for_redirect_on_host(self, httpbin):
15871587
r = requests.get(httpbin('redirect/1'), auth=('user', 'pass'))
@@ -1590,6 +1590,27 @@ def test_auth_is_retained_for_redirect_on_host(self, httpbin):
15901590

15911591
assert h1 == h2
15921592

1593+
def test_should_strip_auth_host_change(self):
1594+
s = requests.Session()
1595+
assert s.should_strip_auth('http://example.com/foo', 'http://another.example.com/')
1596+
1597+
def test_should_strip_auth_http_downgrade(self):
1598+
s = requests.Session()
1599+
assert s.should_strip_auth('https://example.com/foo', 'http://example.com/bar')
1600+
1601+
def test_should_strip_auth_https_upgrade(self):
1602+
s = requests.Session()
1603+
assert not s.should_strip_auth('http://example.com/foo', 'https://example.com/bar')
1604+
assert not s.should_strip_auth('http://example.com:80/foo', 'https://example.com/bar')
1605+
assert not s.should_strip_auth('http://example.com/foo', 'https://example.com:443/bar')
1606+
# Non-standard ports should trigger stripping
1607+
assert s.should_strip_auth('http://example.com:8080/foo', 'https://example.com/bar')
1608+
assert s.should_strip_auth('http://example.com/foo', 'https://example.com:8443/bar')
1609+
1610+
def test_should_strip_auth_port_change(self):
1611+
s = requests.Session()
1612+
assert s.should_strip_auth('http://example.com:1234/foo', 'https://example.com:4321/bar')
1613+
15931614
def test_manual_redirect_with_partial_body_read(self, httpbin):
15941615
s = requests.Session()
15951616
r1 = s.get(httpbin('redirect/2'), allow_redirects=False, stream=True)

0 commit comments

Comments
 (0)