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