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