854cc6
From 3a184cc875709d4324d234a4b939d614a2c9bb0f Mon Sep 17 00:00:00 2001
854cc6
From: Charalampos Stratakis <cstratak@redhat.com>
854cc6
Date: Mon, 1 Aug 2016 11:20:11 +0200
854cc6
Subject: [PATCH] =?UTF-8?q?Fix=20for=20CVE-2016-1000110=20http://bugs.pyth?=
854cc6
 =?UTF-8?q?on.org/issue27568=20Based=20on=20the=20patch=20by=20R=C3=A9mi?=
854cc6
 =?UTF-8?q?=20Rampin?=
854cc6
MIME-Version: 1.0
854cc6
Content-Type: text/plain; charset=UTF-8
854cc6
Content-Transfer-Encoding: 8bit
854cc6
854cc6
---
854cc6
 Doc/howto/urllib2.rst   |  5 +++++
854cc6
 Doc/library/urllib.rst  | 10 ++++++++++
854cc6
 Doc/library/urllib2.rst |  4 ++++
854cc6
 Lib/test/test_urllib.py | 12 ++++++++++++
854cc6
 Lib/urllib.py           |  9 +++++++++
854cc6
 5 files changed, 40 insertions(+)
854cc6
854cc6
diff --git a/Doc/howto/urllib2.rst b/Doc/howto/urllib2.rst
854cc6
index f84bf08..6542846 100644
854cc6
--- a/Doc/howto/urllib2.rst
854cc6
+++ b/Doc/howto/urllib2.rst
854cc6
@@ -523,6 +523,11 @@ setting up a `Basic Authentication`_ handler : ::
854cc6
     through a proxy.  However, this can be enabled by extending urllib2 as
854cc6
     shown in the recipe [#]_.
854cc6
 
854cc6
+.. note::
854cc6
+
854cc6
+    ``HTTP_PROXY`` will be ignored if a variable ``REQUEST_METHOD`` is set; see
854cc6
+    the documentation on :func:`~urllib.getproxies`.
854cc6
+
854cc6
 
854cc6
 Sockets and Layers
854cc6
 ==================
854cc6
diff --git a/Doc/library/urllib.rst b/Doc/library/urllib.rst
854cc6
index c7d200d..3b9876e 100644
854cc6
--- a/Doc/library/urllib.rst
854cc6
+++ b/Doc/library/urllib.rst
854cc6
@@ -280,6 +280,16 @@ Utility functions
854cc6
    find it, looks for proxy information from Mac OSX System Configuration for
854cc6
    Mac OS X and Windows Systems Registry for Windows.
854cc6
 
854cc6
+    .. note::
854cc6
+
854cc6
+        If the environment variable ``REQUEST_METHOD`` is set, which usually
854cc6
+        indicates your script is running in a CGI environment, the environment
854cc6
+        variable ``HTTP_PROXY`` (uppercase ``_PROXY``) will be ignored. This is
854cc6
+        because that variable can be injected by a client using the "Proxy:"
854cc6
+        HTTP header. If you need to use an HTTP proxy in a CGI environment,
854cc6
+        either use ``ProxyHandler`` explicitly, or make sure the variable name
854cc6
+        is in lowercase (or at least the ``_proxy`` suffix).
854cc6
+
854cc6
 .. note::
854cc6
     urllib also exposes certain utility functions like splittype, splithost and
854cc6
     others parsing url into various components. But it is recommended to use
854cc6
diff --git a/Doc/library/urllib2.rst b/Doc/library/urllib2.rst
854cc6
index 24deeb4..46fce59 100644
854cc6
--- a/Doc/library/urllib2.rst
854cc6
+++ b/Doc/library/urllib2.rst
854cc6
@@ -224,6 +224,10 @@ The following classes are provided:
854cc6
 
854cc6
    To disable autodetected proxy pass an empty dictionary.
854cc6
 
854cc6
+    .. note::
854cc6
+
854cc6
+       ``HTTP_PROXY`` will be ignored if a variable ``REQUEST_METHOD`` is set;
854cc6
+       see the documentation on :func:`~urllib.getproxies`.
854cc6
 
854cc6
 .. class:: HTTPPasswordMgr()
854cc6
 
854cc6
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
854cc6
index 3a273f8..3845012 100644
854cc6
--- a/Lib/test/test_urllib.py
854cc6
+++ b/Lib/test/test_urllib.py
854cc6
@@ -161,6 +161,18 @@ class ProxyTests(unittest.TestCase):
854cc6
         self.env.set('NO_PROXY', 'localhost, anotherdomain.com, newdomain.com')
854cc6
         self.assertTrue(urllib.proxy_bypass_environment('anotherdomain.com'))
854cc6
 
854cc6
+    def test_proxy_cgi_ignore(self):
854cc6
+        try:
854cc6
+            self.env.set('HTTP_PROXY', 'http://somewhere:3128')
854cc6
+            proxies = urllib.getproxies_environment()
854cc6
+            self.assertEqual('http://somewhere:3128', proxies['http'])
854cc6
+            self.env.set('REQUEST_METHOD', 'GET')
854cc6
+            proxies = urllib.getproxies_environment()
854cc6
+            self.assertNotIn('http', proxies)
854cc6
+        finally:
854cc6
+            self.env.unset('REQUEST_METHOD')
854cc6
+            self.env.unset('HTTP_PROXY')
854cc6
+
854cc6
 
854cc6
 class urlopen_HttpTests(unittest.TestCase, FakeHTTPMixin):
854cc6
     """Test urlopen() opening a fake http connection."""
854cc6
diff --git a/Lib/urllib.py b/Lib/urllib.py
854cc6
index f9655f9..9b31df1 100644
854cc6
--- a/Lib/urllib.py
854cc6
+++ b/Lib/urllib.py
854cc6
@@ -1361,11 +1361,20 @@ def getproxies_environment():
854cc6
     [Fancy]URLopener constructor.
854cc6
 
854cc6
     """
854cc6
+    # Get all variables
854cc6
     proxies = {}
854cc6
     for name, value in os.environ.items():
854cc6
         name = name.lower()
854cc6
         if value and name[-6:] == '_proxy':
854cc6
             proxies[name[:-6]] = value
854cc6
+
854cc6
+    # CVE-2016-1000110 - If we are running as CGI script, forget HTTP_PROXY
854cc6
+    # (non-all-lowercase) as it may be set from the web server by a "Proxy:"
854cc6
+    # header from the client
854cc6
+    # If "proxy" is lowercase, it will still be used thanks to the next block
854cc6
+    if 'REQUEST_METHOD' in os.environ:
854cc6
+        proxies.pop('http', None)
854cc6
+
854cc6
     return proxies
854cc6
 
854cc6
 def proxy_bypass_environment(host):
854cc6
-- 
854cc6
2.7.4
854cc6