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