978cfa
From 82224a0b35a9b381708cd6dee990aa9c4b4db7cd Mon Sep 17 00:00:00 2001
978cfa
From: Lumir Balhar <lbalhar@redhat.com>
978cfa
Date: Thu, 16 Jan 2020 10:08:55 +0100
978cfa
Subject: [PATCH] Fix for CVE-2018-18074
978cfa
978cfa
This patch contains the fix for CVE-2018-18074 and
978cfa
a subsequent regression fix combined in one.
978cfa
---
978cfa
 sessions.py | 36 +++++++++++++++++++++++++++++-------
978cfa
 utils.py    |  1 +
978cfa
 2 files changed, 30 insertions(+), 7 deletions(-)
978cfa
978cfa
diff --git a/sessions.py b/sessions.py
978cfa
index bcbcc88..c65c908 100644
978cfa
--- a/sessions.py
978cfa
+++ b/sessions.py
978cfa
@@ -17,7 +17,7 @@ from .cookies import (
978cfa
     cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar, merge_cookies)
978cfa
 from .models import Request, PreparedRequest, DEFAULT_REDIRECT_LIMIT
978cfa
 from .hooks import default_hooks, dispatch_hook
978cfa
-from .utils import to_key_val_list, default_headers, to_native_string
978cfa
+from .utils import to_key_val_list, default_headers, to_native_string, DEFAULT_PORTS
978cfa
 from .exceptions import (
978cfa
     TooManyRedirects, InvalidSchema, ChunkedEncodingError, ContentDecodingError)
978cfa
 from .packages.urllib3._collections import RecentlyUsedContainer
978cfa
@@ -85,6 +85,32 @@ def merge_hooks(request_hooks, session_hooks, dict_class=OrderedDict):
978cfa
 
978cfa
 
978cfa
 class SessionRedirectMixin(object):
978cfa
+
978cfa
+    def should_strip_auth(self, old_url, new_url):
978cfa
+        """Decide whether Authorization header should be removed when redirecting"""
978cfa
+        old_parsed = urlparse(old_url)
978cfa
+        new_parsed = urlparse(new_url)
978cfa
+        if old_parsed.hostname != new_parsed.hostname:
978cfa
+            return True
978cfa
+        # Special case: allow http -> https redirect when using the standard
978cfa
+        # ports. This isn't specified by RFC 7235, but is kept to avoid
978cfa
+        # breaking backwards compatibility with older versions of requests
978cfa
+        # that allowed any redirects on the same host.
978cfa
+        if (old_parsed.scheme == 'http' and old_parsed.port in (80, None)
978cfa
+                and new_parsed.scheme == 'https' and new_parsed.port in (443, None)):
978cfa
+            return False
978cfa
+
978cfa
+        # Handle default port usage corresponding to scheme.
978cfa
+        changed_port = old_parsed.port != new_parsed.port
978cfa
+        changed_scheme = old_parsed.scheme != new_parsed.scheme
978cfa
+        default_port = (DEFAULT_PORTS.get(old_parsed.scheme, None), None)
978cfa
+        if (not changed_scheme and old_parsed.port in default_port
978cfa
+                and new_parsed.port in default_port):
978cfa
+            return False
978cfa
+
978cfa
+        # Standard case: root URI must match
978cfa
+        return changed_port or changed_scheme
978cfa
+
978cfa
     def resolve_redirects(self, resp, req, stream=False, timeout=None,
978cfa
                           verify=True, cert=None, proxies=None, **adapter_kwargs):
978cfa
         """Receives a Response. Returns a generator of Responses."""
978cfa
@@ -190,14 +216,10 @@ class SessionRedirectMixin(object):
978cfa
         headers = prepared_request.headers
978cfa
         url = prepared_request.url
978cfa
 
978cfa
-        if 'Authorization' in headers:
978cfa
+        if 'Authorization' in headers and self.should_strip_auth(response.request.url, url):
978cfa
             # If we get redirected to a new host, we should strip out any
978cfa
             # authentication headers.
978cfa
-            original_parsed = urlparse(response.request.url)
978cfa
-            redirect_parsed = urlparse(url)
978cfa
-
978cfa
-            if (original_parsed.hostname != redirect_parsed.hostname):
978cfa
-                del headers['Authorization']
978cfa
+            del headers['Authorization']
978cfa
 
978cfa
         # .netrc might have more auth for us on our new host.
978cfa
         new_auth = get_netrc_auth(url) if self.trust_env else None
978cfa
diff --git a/utils.py b/utils.py
978cfa
index 30a03ca..9080923 100644
978cfa
--- a/utils.py
978cfa
+++ b/utils.py
978cfa
@@ -34,6 +34,7 @@ NETRC_FILES = ('.netrc', '_netrc')
978cfa
 
978cfa
 DEFAULT_CA_BUNDLE_PATH = certs.where()
978cfa
 
978cfa
+DEFAULT_PORTS = {'http': 80, 'https': 443}
978cfa
 
978cfa
 def dict_to_sequence(d):
978cfa
     """Returns an internal sequence dictionary update."""
978cfa
-- 
978cfa
2.24.1
978cfa