Skip to content

Commit 4cbb23f

Browse files
committed
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 python#27568 Reported and patch contributed by Rémi Rampin.
1 parent d27a7c1 commit 4cbb23f

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

Doc/howto/urllib2.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,11 @@ setting up a `Basic Authentication`_ handler: ::
538538
through a proxy. However, this can be enabled by extending urllib.request as
539539
shown in the recipe [#]_.
540540

541+
.. note::
542+
543+
`HTTP_PROXY`` will be ignored if a variable ``REQUEST_METHOD`` is set; see
544+
the documentation on :func:`~urllib.request.getproxies`.
545+
541546

542547
Sockets and Layers
543548
==================

Doc/library/urllib.request.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ The :mod:`urllib.request` module defines the following functions:
161161
cannot find it, looks for proxy information from Mac OSX System
162162
Configuration for Mac OS X and Windows Systems Registry for Windows.
163163

164+
.. note::
165+
166+
If the environment variable ``REQUEST_METHOD`` is set, which usually
167+
indicates your script is running in a CGI environment, the environment
168+
variable ``HTTP_PROXY`` (uppercase ``_PROXY``) will be ignored. This is
169+
because that variable can be injected by a client using the "Proxy:" HTTP
170+
header. If you need to use an HTTP proxy in a CGI environment use
171+
``ProxyHandler`` explicitly.
164172

165173
The following classes are provided:
166174

@@ -265,6 +273,11 @@ The following classes are provided:
265273

266274
To disable autodetected proxy pass an empty dictionary.
267275

276+
.. note::
277+
278+
``HTTP_PROXY`` will be ignored if a variable ``REQUEST_METHOD`` is set;
279+
see the documentation on :func:`~urllib.request.getproxies`.
280+
268281

269282
.. class:: HTTPPasswordMgr()
270283

Lib/test/test_urllib.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,19 @@ def test_getproxies_environment_keep_no_proxies(self):
194194
self.env.set('NO_PROXY', 'localhost, anotherdomain.com, newdomain.com')
195195
self.assertTrue(urllib.request.proxy_bypass_environment('anotherdomain.com'))
196196

197+
def test_proxy_cgi_ignore(self):
198+
try:
199+
self.env.set('HTTP_PROXY', 'http://somewhere:3128')
200+
proxies = urllib.request.getproxies_environment()
201+
self.assertEqual('http://somewhere:3128', proxies['http'])
202+
self.env.set('REQUEST_METHOD', 'GET')
203+
proxies = urllib.request.getproxies_environment()
204+
self.assertNotIn('http', proxies)
205+
finally:
206+
self.env.unset('REQUEST_METHOD')
207+
self.env.unset('HTTP_PROXY')
208+
209+
197210
class urlopen_HttpTests(unittest.TestCase, FakeHTTPMixin):
198211
"""Test urlopen() opening a fake http connection."""
199212

Lib/urllib/request.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,6 +2366,13 @@ def getproxies_environment():
23662366
name = name.lower()
23672367
if value and name[-6:] == '_proxy':
23682368
proxies[name[:-6]] = value
2369+
2370+
# CVE-2016-1000110 - If we are running as CGI script, forget HTTP_PROXY
2371+
# (non-all-lowercase) as it may be set from the web server by a "Proxy:"
2372+
# header from the client
2373+
if 'REQUEST_METHOD' in os.environ:
2374+
proxies.pop('http', None)
2375+
23692376
return proxies
23702377

23712378
def proxy_bypass_environment(host):

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Core and Builtins
2929
Library
3030
-------
3131

32+
- Issue #27568: Prevent HTTPoxy attack (CVE-2016-1000110). Ignore the
33+
HTTP_PROXY variable when REQUEST_METHOD environment is set, which indicates
34+
that the script is in CGI mode.
35+
3236
- Issue #24521: Fix possible integer overflows in the pickle module.
3337

3438
- Issue #22931: Allow '[' and ']' in cookie values.

0 commit comments

Comments
 (0)