Blame SOURCES/00242-CVE-2016-1000110-httpoxy.patch

159b10
159b10
# HG changeset patch
159b10
# User Senthil Kumaran <senthil@uthcode.com>
159b10
# Date 1469947146 25200
159b10
# Node ID a0ac52ed8f7918222603b584ec8fc93d9b7bc0a5
159b10
# Parent  4cb94e561e2db9865fb4d752f2bceefca4c6819a# Parent  3c19023c9fec5a615c25598468b44fade89049ce
159b10
[merge from 3.4] - Prevent HTTPoxy attack (CVE-2016-1000110)
159b10
159b10
Ignore the HTTP_PROXY variable when REQUEST_METHOD environment is set, which
159b10
indicates that the script is in CGI mode.
159b10
159b10
Issue #27568 Reported and patch contributed by RĂ©mi Rampin.
159b10
159b10
diff --git a/Doc/howto/urllib2.rst b/Doc/howto/urllib2.rst
159b10
--- a/Doc/howto/urllib2.rst
159b10
+++ b/Doc/howto/urllib2.rst
159b10
@@ -538,6 +538,11 @@ setting up a `Basic Authentication`_ han
159b10
     through a proxy.  However, this can be enabled by extending urllib.request as
159b10
     shown in the recipe [#]_.
159b10
 
159b10
+.. note::
159b10
+
159b10
+    ``HTTP_PROXY`` will be ignored if a variable ``REQUEST_METHOD`` is set; see
159b10
+    the documentation on :func:`~urllib.request.getproxies`.
159b10
+
159b10
 
159b10
 Sockets and Layers
159b10
 ==================
159b10
diff --git a/Doc/library/urllib.request.rst b/Doc/library/urllib.request.rst
159b10
--- a/Doc/library/urllib.request.rst
159b10
+++ b/Doc/library/urllib.request.rst
159b10
@@ -166,6 +166,16 @@ The :mod:`urllib.request` module defines the following functions:
159b10
    cannot find it, looks for proxy information from Mac OSX System
159b10
    Configuration for Mac OS X and Windows Systems Registry for Windows.
159b10
 
159b10
+    .. note::
159b10
+
159b10
+       If the environment variable ``REQUEST_METHOD`` is set, which usually
159b10
+       indicates your script is running in a CGI environment, the environment
159b10
+       variable ``HTTP_PROXY`` (uppercase ``_PROXY``) will be ignored. This is
159b10
+       because that variable can be injected by a client using the "Proxy:" HTTP
159b10
+       header. If you need to use an HTTP proxy in a CGI environment, either use
159b10
+       ``ProxyHandler`` explicitly, or make sure the variable name is in
159b10
+       lowercase (or at least the ``_proxy`` suffix).
159b10
+
159b10
 
159b10
 The following classes are provided:
159b10
159b10
 
159b10
@@ -275,6 +285,11 @@ The following classes are provided:
159b10
 
159b10
    To disable autodetected proxy pass an empty dictionary.
159b10
 
159b10
+    .. note::
159b10
+
159b10
+       ``HTTP_PROXY`` will be ignored if a variable ``REQUEST_METHOD`` is set;
159b10
+       see the documentation on :func:`~urllib.request.getproxies`.
159b10
+
159b10
 
159b10
 .. class:: HTTPPasswordMgr()
159b10
159b10
 
159b10
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
159b10
--- a/Lib/test/test_urllib.py
159b10
+++ b/Lib/test/test_urllib.py
159b10
@@ -225,6 +225,18 @@ class ProxyTests(unittest.TestCase):
159b10
         self.env.set('NO_PROXY', 'localhost, anotherdomain.com, newdomain.com')
159b10
         self.assertTrue(urllib.request.proxy_bypass_environment('anotherdomain.com'))
159b10
 
159b10
+    def test_proxy_cgi_ignore(self):
159b10
+        try:
159b10
+            self.env.set('HTTP_PROXY', 'http://somewhere:3128')
159b10
+            proxies = urllib.request.getproxies_environment()
159b10
+            self.assertEqual('http://somewhere:3128', proxies['http'])
159b10
+            self.env.set('REQUEST_METHOD', 'GET')
159b10
+            proxies = urllib.request.getproxies_environment()
159b10
+            self.assertNotIn('http', proxies)
159b10
+        finally:
159b10
+            self.env.unset('REQUEST_METHOD')
159b10
+            self.env.unset('HTTP_PROXY')
159b10
+
159b10
 class urlopen_HttpTests(unittest.TestCase, FakeHTTPMixin, FakeFTPMixin):
159b10
     """Test urlopen() opening a fake http connection."""
159b10
159b10
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
159b10
--- a/Lib/urllib/request.py
159b10
+++ b/Lib/urllib/request.py
159b10
@@ -2394,6 +2394,11 @@ def getproxies_environment():
159b10
         name = name.lower()
159b10
         if value and name[-6:] == '_proxy':
159b10
             proxies[name[:-6]] = value
159b10
+    # CVE-2016-1000110 - If we are running as CGI script, forget HTTP_PROXY
159b10
+    # (non-all-lowercase) as it may be set from the web server by a "Proxy:"
159b10
+    # header from the client
159b10
+    if 'REQUEST_METHOD' in os.environ:
159b10
+        proxies.pop('http', None)
159b10
     return proxies
159b10
 
159b10
 def proxy_bypass_environment(host):