churchyard / rpms / python2

Forked from rpms/python2 6 years ago
Clone
a759357
a759357
# HG changeset patch
a759357
# User Senthil Kumaran <senthil@uthcode.com>
a759357
# Date 1469882993 25200
a759357
# Node ID ba915d561667fa0584ad89f8d5a844fd43803c0d
a759357
# Parent  c8c1ea94379a7706638f1571988576d504d7fc98
a759357
Prevent HTTPoxy attack (CVE-2016-1000110)
a759357
a759357
Ignore the HTTP_PROXY variable when REQUEST_METHOD environment is set, which
a759357
indicates that the script is in CGI mode.
a759357
a759357
Issue reported and patch contributed by RĂ©mi Rampin.
a759357
a759357
diff --git a/Doc/howto/urllib2.rst b/Doc/howto/urllib2.rst
a759357
--- a/Doc/howto/urllib2.rst
a759357
+++ b/Doc/howto/urllib2.rst
a759357
@@ -525,6 +525,11 @@ setting up a `Basic Authentication`_ han
a759357
     through a proxy.  However, this can be enabled by extending urllib2 as
a759357
     shown in the recipe [#]_.
a759357
 
a759357
+.. note::
a759357
+
a759357
+    ``HTTP_PROXY`` will be ignored if a variable ``REQUEST_METHOD`` is set; see
a759357
+    the documentation on :func:`~urllib.getproxies`.
a759357
+
a759357
 
a759357
 Sockets and Layers
a759357
 ==================
a759357
diff --git a/Doc/library/urllib.rst b/Doc/library/urllib.rst
a759357
--- a/Doc/library/urllib.rst
a759357
+++ b/Doc/library/urllib.rst
a759357
@@ -295,6 +295,16 @@ Utility functions
a759357
    If both lowercase and uppercase environment variables exist (and disagree),
a759357
    lowercase is preferred.
a759357
 
a759357
+    .. note::
a759357
+
a759357
+        If the environment variable ``REQUEST_METHOD`` is set, which usually
a759357
+        indicates your script is running in a CGI environment, the environment
a759357
+        variable ``HTTP_PROXY`` (uppercase ``_PROXY``) will be ignored. This is
a759357
+        because that variable can be injected by a client using the "Proxy:"
a759357
+        HTTP header. If you need to use an HTTP proxy in a CGI environment,
a759357
+        either use ``ProxyHandler`` explicitly, or make sure the variable name
a759357
+        is in lowercase (or at least the ``_proxy`` suffix).
a759357
+
a759357
 .. note::
a759357
     urllib also exposes certain utility functions like splittype, splithost and
a759357
     others parsing URL into various components. But it is recommended to use
a759357
diff --git a/Doc/library/urllib2.rst b/Doc/library/urllib2.rst
a759357
--- a/Doc/library/urllib2.rst
a759357
+++ b/Doc/library/urllib2.rst
a759357
@@ -229,6 +229,11 @@ The following classes are provided:
a759357
 
a759357
    To disable autodetected proxy pass an empty dictionary.
a759357
 
a759357
+    .. note::
a759357
+
a759357
+       ``HTTP_PROXY`` will be ignored if a variable ``REQUEST_METHOD`` is set;
a759357
+       see the documentation on :func:`~urllib.getproxies`.
a759357
+
a759357
 
a759357
 .. class:: HTTPPasswordMgr()
a759357
 
a759357
diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py
a759357
--- a/Lib/test/test_urllib.py
a759357
+++ b/Lib/test/test_urllib.py
a759357
@@ -170,6 +170,18 @@ class ProxyTests(unittest.TestCase):
a759357
         self.assertTrue(urllib.proxy_bypass_environment('anotherdomain.com:8888'))
a759357
         self.assertTrue(urllib.proxy_bypass_environment('newdomain.com:1234'))
a759357
 
a759357
+    def test_proxy_cgi_ignore(self):
a759357
+        try:
a759357
+            self.env.set('HTTP_PROXY', 'http://somewhere:3128')
a759357
+            proxies = urllib.getproxies_environment()
a759357
+            self.assertEqual('http://somewhere:3128', proxies['http'])
a759357
+            self.env.set('REQUEST_METHOD', 'GET')
a759357
+            proxies = urllib.getproxies_environment()
a759357
+            self.assertNotIn('http', proxies)
a759357
+        finally:
a759357
+            self.env.unset('REQUEST_METHOD')
a759357
+            self.env.unset('HTTP_PROXY')
a759357
+
a759357
     def test_proxy_bypass_environment_host_match(self):
a759357
         bypass = urllib.proxy_bypass_environment
a759357
         self.env.set('NO_PROXY',
a759357
diff --git a/Lib/urllib.py b/Lib/urllib.py
a759357
--- a/Lib/urllib.py
a759357
+++ b/Lib/urllib.py
a759357
@@ -1380,12 +1380,21 @@ def getproxies_environment():
a759357
     If you need a different way, you can pass a proxies dictionary to the
a759357
     [Fancy]URLopener constructor.
a759357
     """
a759357
+    # Get all variables
a759357
     proxies = {}
a759357
     for name, value in os.environ.items():
a759357
         name = name.lower()
a759357
         if value and name[-6:] == '_proxy':
a759357
             proxies[name[:-6]] = value
a759357
 
a759357
+    # CVE-2016-1000110 - If we are running as CGI script, forget HTTP_PROXY
a759357
+    # (non-all-lowercase) as it may be set from the web server by a "Proxy:"
a759357
+    # header from the client
a759357
+    # If "proxy" is lowercase, it will still be used thanks to the next block
a759357
+    if 'REQUEST_METHOD' in os.environ:
a759357
+        proxies.pop('http', None)
a759357
+
a759357
+    # Get lowercase variables
a759357
     for name, value in os.environ.items():
a759357
         if name[-6:] == '_proxy':
a759357
             name = name.lower()
a759357