Blame SOURCES/CVE-2018-18074.patch

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