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