Blame SOURCES/CVE-2018-18074.patch

75b7db
From ffbfdb53681207b23bcf67dd76368ad6185ade24 Mon Sep 17 00:00:00 2001
75b7db
From: Lumir Balhar <lbalhar@redhat.com>
75b7db
Date: Thu, 16 Jan 2020 07:06:09 +0100
75b7db
Subject: [PATCH] Fix for CVE-2018-18074
75b7db
75b7db
This patch contains the fix for CVE-2018-18074 and
75b7db
a subsequent regression fix combined in one.
75b7db
---
75b7db
 sessions.py | 36 +++++++++++++++++++++++++++++-------
75b7db
 utils.py    |  1 +
75b7db
 2 files changed, 30 insertions(+), 7 deletions(-)
75b7db
75b7db
diff --git a/sessions.py b/sessions.py
75b7db
index 6570e73..4038047 100644
75b7db
--- a/sessions.py
75b7db
+++ b/sessions.py
75b7db
@@ -29,7 +29,7 @@ from .adapters import HTTPAdapter
75b7db
 
75b7db
 from .utils import (
75b7db
     requote_uri, get_environ_proxies, get_netrc_auth, should_bypass_proxies,
75b7db
-    get_auth_from_url, rewind_body
75b7db
+    get_auth_from_url, rewind_body, DEFAULT_PORTS
75b7db
 )
75b7db
 
75b7db
 from .status_codes import codes
75b7db
@@ -116,6 +116,32 @@ class SessionRedirectMixin(object):
75b7db
             return to_native_string(location, 'utf8')
75b7db
         return None
75b7db
 
75b7db
+
75b7db
+    def should_strip_auth(self, old_url, new_url):
75b7db
+        """Decide whether Authorization header should be removed when redirecting"""
75b7db
+        old_parsed = urlparse(old_url)
75b7db
+        new_parsed = urlparse(new_url)
75b7db
+        if old_parsed.hostname != new_parsed.hostname:
75b7db
+            return True
75b7db
+        # Special case: allow http -> https redirect when using the standard
75b7db
+        # ports. This isn't specified by RFC 7235, but is kept to avoid
75b7db
+        # breaking backwards compatibility with older versions of requests
75b7db
+        # that allowed any redirects on the same host.
75b7db
+        if (old_parsed.scheme == 'http' and old_parsed.port in (80, None)
75b7db
+                and new_parsed.scheme == 'https' and new_parsed.port in (443, None)):
75b7db
+            return False
75b7db
+
75b7db
+        # Handle default port usage corresponding to scheme.
75b7db
+        changed_port = old_parsed.port != new_parsed.port
75b7db
+        changed_scheme = old_parsed.scheme != new_parsed.scheme
75b7db
+        default_port = (DEFAULT_PORTS.get(old_parsed.scheme, None), None)
75b7db
+        if (not changed_scheme and old_parsed.port in default_port
75b7db
+                and new_parsed.port in default_port):
75b7db
+            return False
75b7db
+
75b7db
+        # Standard case: root URI must match
75b7db
+        return changed_port or changed_scheme
75b7db
+
75b7db
     def resolve_redirects(self, resp, req, stream=False, timeout=None,
75b7db
                           verify=True, cert=None, proxies=None, yield_requests=False, **adapter_kwargs):
75b7db
         """Receives a Response. Returns a generator of Responses or Requests."""
75b7db
@@ -232,14 +258,10 @@ class SessionRedirectMixin(object):
75b7db
         headers = prepared_request.headers
75b7db
         url = prepared_request.url
75b7db
 
75b7db
-        if 'Authorization' in headers:
75b7db
+        if 'Authorization' in headers and self.should_strip_auth(response.request.url, url):
75b7db
             # If we get redirected to a new host, we should strip out any
75b7db
             # authentication headers.
75b7db
-            original_parsed = urlparse(response.request.url)
75b7db
-            redirect_parsed = urlparse(url)
75b7db
-
75b7db
-            if (original_parsed.hostname != redirect_parsed.hostname):
75b7db
-                del headers['Authorization']
75b7db
+            del headers['Authorization']
75b7db
 
75b7db
         # .netrc might have more auth for us on our new host.
75b7db
         new_auth = get_netrc_auth(url) if self.trust_env else None
75b7db
diff --git a/utils.py b/utils.py
75b7db
index 5c47de9..5695ab0 100644
75b7db
--- a/utils.py
75b7db
+++ b/utils.py
75b7db
@@ -38,6 +38,7 @@ NETRC_FILES = ('.netrc', '_netrc')
75b7db
 
75b7db
 DEFAULT_CA_BUNDLE_PATH = certs.where()
75b7db
 
75b7db
+DEFAULT_PORTS = {'http': 80, 'https': 443}
75b7db
 
75b7db
 if platform.system() == 'Windows':
75b7db
     # provide a proxy_bypass version on Windows without DNS lookups
75b7db
-- 
75b7db
2.24.1
75b7db