Blame SOURCES/CVE-2018-18074.patch

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