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