f866d3
From c734f873270cf9ca414832423f7aad98443c379f Mon Sep 17 00:00:00 2001
f866d3
From: Lumir Balhar <lbalhar@redhat.com>
f866d3
Date: Thu, 9 Jan 2020 11:26:24 +0100
f866d3
Subject: [PATCH] CVE-2018-20060
f866d3
f866d3
---
f866d3
 poolmanager.py | 11 ++++++++++-
f866d3
 util/retry.py  | 12 +++++++++++-
f866d3
 2 files changed, 21 insertions(+), 2 deletions(-)
f866d3
f866d3
diff --git a/poolmanager.py b/poolmanager.py
f866d3
index 4ae9174..bfa5115 100644
f866d3
--- a/poolmanager.py
f866d3
+++ b/poolmanager.py
f866d3
@@ -312,8 +312,9 @@ class PoolManager(RequestMethods):
f866d3
 
f866d3
         kw['assert_same_host'] = False
f866d3
         kw['redirect'] = False
f866d3
+
f866d3
         if 'headers' not in kw:
f866d3
-            kw['headers'] = self.headers
f866d3
+            kw['headers'] = self.headers.copy()
f866d3
 
f866d3
         if self.proxy is not None and u.scheme == "http":
f866d3
             response = conn.urlopen(method, url, **kw)
f866d3
@@ -335,6 +336,14 @@ class PoolManager(RequestMethods):
f866d3
         if not isinstance(retries, Retry):
f866d3
             retries = Retry.from_int(retries, redirect=redirect)
f866d3
 
f866d3
+        # Strip headers marked as unsafe to forward to the redirected location.
f866d3
+        # Check remove_headers_on_redirect to avoid a potential network call within
f866d3
+        # conn.is_same_host() which may use socket.gethostbyname() in the future.
f866d3
+        if (retries.remove_headers_on_redirect
f866d3
+                and not conn.is_same_host(redirect_location)):
f866d3
+            for header in retries.remove_headers_on_redirect:
f866d3
+                kw['headers'].pop(header, None)
f866d3
+
f866d3
         try:
f866d3
             retries = retries.increment(method, url, response=response, _pool=conn)
f866d3
         except MaxRetryError:
f866d3
diff --git a/util/retry.py b/util/retry.py
f866d3
index c603cb4..0b83963 100644
f866d3
--- a/util/retry.py
f866d3
+++ b/util/retry.py
f866d3
@@ -126,6 +126,11 @@ class Retry(object):
f866d3
         exhausted, to raise a MaxRetryError, or to return a response with a
f866d3
         response code in the 3xx range.
f866d3
 
f866d3
+    :param iterable remove_headers_on_redirect:
f866d3
+        Sequence of headers to remove from the request when a response
f866d3
+        indicating a redirect is returned before firing off the redirected
f866d3
+        request
f866d3
+
f866d3
     :param bool raise_on_status: Similar meaning to ``raise_on_redirect``:
f866d3
         whether we should raise an exception, or return a response,
f866d3
         if status falls in ``status_forcelist`` range and retries have
f866d3
@@ -144,6 +149,8 @@ class Retry(object):
f866d3
     DEFAULT_METHOD_WHITELIST = frozenset([
f866d3
         'HEAD', 'GET', 'PUT', 'DELETE', 'OPTIONS', 'TRACE'])
f866d3
 
f866d3
+    DEFAULT_REDIRECT_HEADERS_BLACKLIST = frozenset(['Authorization'])
f866d3
+
f866d3
     RETRY_AFTER_STATUS_CODES = frozenset([413, 429, 503])
f866d3
 
f866d3
     #: Maximum backoff time.
f866d3
@@ -152,7 +159,8 @@ class Retry(object):
f866d3
     def __init__(self, total=10, connect=None, read=None, redirect=None, status=None,
f866d3
                  method_whitelist=DEFAULT_METHOD_WHITELIST, status_forcelist=None,
f866d3
                  backoff_factor=0, raise_on_redirect=True, raise_on_status=True,
f866d3
-                 history=None, respect_retry_after_header=True):
f866d3
+                 history=None, respect_retry_after_header=True,
f866d3
+                 remove_headers_on_redirect=DEFAULT_REDIRECT_HEADERS_BLACKLIST):
f866d3
 
f866d3
         self.total = total
f866d3
         self.connect = connect
f866d3
@@ -171,6 +179,7 @@ class Retry(object):
f866d3
         self.raise_on_status = raise_on_status
f866d3
         self.history = history or tuple()
f866d3
         self.respect_retry_after_header = respect_retry_after_header
f866d3
+        self.remove_headers_on_redirect = remove_headers_on_redirect
f866d3
 
f866d3
     def new(self, **kw):
f866d3
         params = dict(
f866d3
@@ -182,6 +191,7 @@ class Retry(object):
f866d3
             raise_on_redirect=self.raise_on_redirect,
f866d3
             raise_on_status=self.raise_on_status,
f866d3
             history=self.history,
f866d3
+            remove_headers_on_redirect=self.remove_headers_on_redirect,
f866d3
         )
f866d3
         params.update(kw)
f866d3
         return type(self)(**params)
f866d3
-- 
f866d3
2.24.1
f866d3