]> granicus.if.org Git - python/commitdiff
Prevent HTTPoxy attack (CVE-2016-1000110)
authorSenthil Kumaran <senthil@uthcode.com>
Sat, 30 Jul 2016 12:49:53 +0000 (05:49 -0700)
committerSenthil Kumaran <senthil@uthcode.com>
Sat, 30 Jul 2016 12:49:53 +0000 (05:49 -0700)
Ignore the HTTP_PROXY variable when REQUEST_METHOD environment is set, which
indicates that the script is in CGI mode.

Issue reported and patch contributed by Rémi Rampin.

Doc/howto/urllib2.rst
Doc/library/urllib.rst
Doc/library/urllib2.rst
Lib/test/test_urllib.py
Lib/urllib.py
Misc/ACKS
Misc/NEWS

index 6bb06d461a2e0359855583603e915ad9ecab7e81..5cf2c0ca2e4777896196d8a054403566b9d333b0 100644 (file)
@@ -525,6 +525,11 @@ setting up a `Basic Authentication`_ handler: ::
     through a proxy.  However, this can be enabled by extending urllib2 as
     shown in the recipe [#]_.
 
+.. note::
+
+    ``HTTP_PROXY`` will be ignored if a variable ``REQUEST_METHOD`` is set; see
+    the documentation on :func:`~urllib.getproxies`.
+
 
 Sockets and Layers
 ==================
index 3b5dc16c0297915c5487c9fa6cdc8572b7f80dbe..bddcba9e38c1f20e89838b93a2444c76859a9ba5 100644 (file)
@@ -295,6 +295,16 @@ Utility functions
    If both lowercase and uppercase environment variables exist (and disagree),
    lowercase is preferred.
 
+    .. note::
+
+        If the environment variable ``REQUEST_METHOD`` is set, which usually
+        indicates your script is running in a CGI environment, the environment
+        variable ``HTTP_PROXY`` (uppercase ``_PROXY``) will be ignored. This is
+        because that variable can be injected by a client using the "Proxy:"
+        HTTP header. If you need to use an HTTP proxy in a CGI environment,
+        either use ``ProxyHandler`` explicitly, or make sure the variable name
+        is in lowercase (or at least the ``_proxy`` suffix).
+
 .. note::
     urllib also exposes certain utility functions like splittype, splithost and
     others parsing URL into various components. But it is recommended to use
index 8a4c80e9adad1026149b714d53403c298ae54f8e..b808b98139d666ad3f37ccd7582e5983b445548c 100644 (file)
@@ -229,6 +229,11 @@ The following classes are provided:
 
    To disable autodetected proxy pass an empty dictionary.
 
+    .. note::
+
+       ``HTTP_PROXY`` will be ignored if a variable ``REQUEST_METHOD`` is set;
+       see the documentation on :func:`~urllib.getproxies`.
+
 
 .. class:: HTTPPasswordMgr()
 
index 434d533e0561494e35f6e0d2697115b98a3e4925..27a1d388458909f07d9f9f1696d4161279d810fe 100644 (file)
@@ -170,6 +170,18 @@ class ProxyTests(unittest.TestCase):
         self.assertTrue(urllib.proxy_bypass_environment('anotherdomain.com:8888'))
         self.assertTrue(urllib.proxy_bypass_environment('newdomain.com:1234'))
 
+    def test_proxy_cgi_ignore(self):
+        try:
+            self.env.set('HTTP_PROXY', 'http://somewhere:3128')
+            proxies = urllib.getproxies_environment()
+            self.assertEqual('http://somewhere:3128', proxies['http'])
+            self.env.set('REQUEST_METHOD', 'GET')
+            proxies = urllib.getproxies_environment()
+            self.assertNotIn('http', proxies)
+        finally:
+            self.env.unset('REQUEST_METHOD')
+            self.env.unset('HTTP_PROXY')
+
     def test_proxy_bypass_environment_host_match(self):
         bypass = urllib.proxy_bypass_environment
         self.env.set('NO_PROXY',
index 139fab907ef056dd7040ec84c221d55ac60ef62c..c3ba2c94cbfacf1bf3e6365c37fdd046d45ec183 100644 (file)
@@ -1380,12 +1380,21 @@ def getproxies_environment():
     If you need a different way, you can pass a proxies dictionary to the
     [Fancy]URLopener constructor.
     """
+    # Get all variables
     proxies = {}
     for name, value in os.environ.items():
         name = name.lower()
         if value and name[-6:] == '_proxy':
             proxies[name[:-6]] = value
 
+    # CVE-2016-1000110 - If we are running as CGI script, forget HTTP_PROXY
+    # (non-all-lowercase) as it may be set from the web server by a "Proxy:"
+    # header from the client
+    # If "proxy" is lowercase, it will still be used thanks to the next block
+    if 'REQUEST_METHOD' in os.environ:
+        proxies.pop('http', None)
+
+    # Get lowercase variables
     for name, value in os.environ.items():
         if name[-6:] == '_proxy':
             name = name.lower()
index 7aa8fc8af825a3da032e5805b1a080d5ca4c7493..0210a4cd3650e9a4cc6b783d9e0e151084a155cb 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1123,6 +1123,7 @@ Burton Radons
 Jeff Ramnani
 Varpu Rantala
 Brodie Rao
+Rémi Rampin
 Senko Rasic
 Antti Rasinen
 Nikolaus Rath
index 06f10c91786918445ad16183cdb065225a965bef..a911bef5b2999ef799690b9afe35fb2c458592c7 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #27568: Prevent HTTPoxy attack (CVE-2016-1000110). Ignore the
+  HTTP_PROXY variable when REQUEST_METHOD environment is set, which indicates
+  that the script is in CGI mode.
+
 - Issue #27130: In the "zlib" module, fix handling of large buffers
   (typically 2 or 4 GiB).  Previously, inputs were limited to 2 GiB, and
   compression and decompression operations did not properly handle results of