|
|
88b19a |
diff --git a/requests/sessions.py b/requests/sessions.py
|
|
|
88b19a |
index ef3f22b..a0a23ee 100644
|
|
|
88b19a |
--- a/requests/sessions.py
|
|
|
88b19a |
+++ b/requests/sessions.py
|
|
|
88b19a |
@@ -89,6 +89,23 @@ def merge_hooks(request_hooks, session_hooks, dict_class=OrderedDict):
|
|
|
88b19a |
|
|
|
88b19a |
|
|
|
88b19a |
class SessionRedirectMixin(object):
|
|
|
88b19a |
+
|
|
|
88b19a |
+ def should_strip_auth(self, old_url, new_url):
|
|
|
88b19a |
+ """Decide whether Authorization header should be removed when redirecting"""
|
|
|
88b19a |
+ old_parsed = urlparse(old_url)
|
|
|
88b19a |
+ new_parsed = urlparse(new_url)
|
|
|
88b19a |
+ if old_parsed.hostname != new_parsed.hostname:
|
|
|
88b19a |
+ return True
|
|
|
88b19a |
+ # Special case: allow http -> https redirect when using the standard
|
|
|
88b19a |
+ # ports. This isn't specified by RFC 7235, but is kept to avoid
|
|
|
88b19a |
+ # breaking backwards compatibility with older versions of requests
|
|
|
88b19a |
+ # that allowed any redirects on the same host.
|
|
|
88b19a |
+ if (old_parsed.scheme == 'http' and old_parsed.port in (80, None)
|
|
|
88b19a |
+ and new_parsed.scheme == 'https' and new_parsed.port in (443, None)):
|
|
|
88b19a |
+ return False
|
|
|
88b19a |
+ # Standard case: root URI must match
|
|
|
88b19a |
+ return old_parsed.port != new_parsed.port or old_parsed.scheme != new_parsed.scheme
|
|
|
88b19a |
+
|
|
|
88b19a |
def resolve_redirects(self, resp, req, stream=False, timeout=None,
|
|
|
88b19a |
verify=True, cert=None, proxies=None):
|
|
|
88b19a |
"""Receives a Response. Returns a generator of Responses."""
|
|
|
88b19a |
@@ -209,14 +226,10 @@ class SessionRedirectMixin(object):
|
|
|
88b19a |
headers = prepared_request.headers
|
|
|
88b19a |
url = prepared_request.url
|
|
|
88b19a |
|
|
|
88b19a |
- if 'Authorization' in headers:
|
|
|
88b19a |
+ if 'Authorization' in headers and self.should_strip_auth(response.request.url, url):
|
|
|
88b19a |
# If we get redirected to a new host, we should strip out any
|
|
|
88b19a |
# authentication headers.
|
|
|
88b19a |
- original_parsed = urlparse(response.request.url)
|
|
|
88b19a |
- redirect_parsed = urlparse(url)
|
|
|
88b19a |
-
|
|
|
88b19a |
- if (original_parsed.hostname != redirect_parsed.hostname):
|
|
|
88b19a |
- del headers['Authorization']
|
|
|
88b19a |
+ del headers['Authorization']
|
|
|
88b19a |
|
|
|
88b19a |
# .netrc might have more auth for us on our new host.
|
|
|
88b19a |
new_auth = get_netrc_auth(url) if self.trust_env else None
|
|
|
88b19a |
diff --git a/test_requests.py b/test_requests.py
|
|
|
88b19a |
index 15406a2..e19b436 100755
|
|
|
88b19a |
--- a/test_requests.py
|
|
|
88b19a |
+++ b/test_requests.py
|
|
|
88b19a |
@@ -991,6 +991,27 @@ class RequestsTestCase(unittest.TestCase):
|
|
|
88b19a |
|
|
|
88b19a |
assert h1 == h2
|
|
|
88b19a |
|
|
|
88b19a |
+ def test_should_strip_auth_host_change(self):
|
|
|
88b19a |
+ s = requests.Session()
|
|
|
88b19a |
+ assert s.should_strip_auth('http://example.com/foo', 'http://another.example.com/')
|
|
|
88b19a |
+
|
|
|
88b19a |
+ def test_should_strip_auth_http_downgrade(self):
|
|
|
88b19a |
+ s = requests.Session()
|
|
|
88b19a |
+ assert s.should_strip_auth('https://example.com/foo', 'http://example.com/bar')
|
|
|
88b19a |
+
|
|
|
88b19a |
+ def test_should_strip_auth_https_upgrade(self):
|
|
|
88b19a |
+ s = requests.Session()
|
|
|
88b19a |
+ assert not s.should_strip_auth('http://example.com/foo', 'https://example.com/bar')
|
|
|
88b19a |
+ assert not s.should_strip_auth('http://example.com:80/foo', 'https://example.com/bar')
|
|
|
88b19a |
+ assert not s.should_strip_auth('http://example.com/foo', 'https://example.com:443/bar')
|
|
|
88b19a |
+ # Non-standard ports should trigger stripping
|
|
|
88b19a |
+ assert s.should_strip_auth('http://example.com:8080/foo', 'https://example.com/bar')
|
|
|
88b19a |
+ assert s.should_strip_auth('http://example.com/foo', 'https://example.com:8443/bar')
|
|
|
88b19a |
+
|
|
|
88b19a |
+ def test_should_strip_auth_port_change(self):
|
|
|
88b19a |
+ s = requests.Session()
|
|
|
88b19a |
+ assert s.should_strip_auth('http://example.com:1234/foo', 'https://example.com:4321/bar')
|
|
|
88b19a |
+
|
|
|
88b19a |
def test_manual_redirect_with_partial_body_read(self):
|
|
|
88b19a |
s = requests.Session()
|
|
|
88b19a |
r1 = s.get(httpbin('redirect/2'), allow_redirects=False, stream=True)
|