Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 436fe5a

Browse files
committed
[merge from 3.3] 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 #27568 Reported and patch contributed by Rémi Rampin.
2 parents b7b5d35 + 4cbb23f commit 436fe5a

5 files changed

Lines changed: 42 additions & 0 deletions

File tree

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
@@ -166,6 +166,14 @@ The :mod:`urllib.request` module defines the following functions:
166166
cannot find it, looks for proxy information from Mac OSX System
167167
Configuration for Mac OS X and Windows Systems Registry for Windows.
168168

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

170178
The following classes are provided:
171179

@@ -268,6 +276,11 @@ The following classes are provided:
268276

269277
To disable autodetected proxy pass an empty dictionary.
270278

279+
.. note::
280+
281+
``HTTP_PROXY`` will be ignored if a variable ``REQUEST_METHOD`` is set;
282+
see the documentation on :func:`~urllib.request.getproxies`.
283+
271284

272285
.. class:: HTTPPasswordMgr()
273286

Lib/test/test_urllib.py

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

225+
def test_proxy_cgi_ignore(self):
226+
try:
227+
self.env.set('HTTP_PROXY', 'http://somewhere:3128')
228+
proxies = urllib.request.getproxies_environment()
229+
self.assertEqual('http://somewhere:3128', proxies['http'])
230+
self.env.set('REQUEST_METHOD', 'GET')
231+
proxies = urllib.request.getproxies_environment()
232+
self.assertNotIn('http', proxies)
233+
finally:
234+
self.env.unset('REQUEST_METHOD')
235+
self.env.unset('HTTP_PROXY')
236+
237+
225238
class urlopen_HttpTests(unittest.TestCase, FakeHTTPMixin, FakeFTPMixin):
226239
"""Test urlopen() opening a fake http connection."""
227240

Lib/urllib/request.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2337,6 +2337,13 @@ def getproxies_environment():
23372337
name = name.lower()
23382338
if value and name[-6:] == '_proxy':
23392339
proxies[name[:-6]] = value
2340+
2341+
# CVE-2016-1000110 - If we are running as CGI script, forget HTTP_PROXY
2342+
# (non-all-lowercase) as it may be set from the web server by a "Proxy:"
2343+
# header from the client
2344+
if 'REQUEST_METHOD' in os.environ:
2345+
proxies.pop('http', None)
2346+
23402347
return proxies
23412348

23422349
def proxy_bypass_environment(host):

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ Core and Builtins
1313
Library
1414
-------
1515

16+
- Issue #27568: Prevent HTTPoxy attack (CVE-2016-1000110). Ignore the
17+
HTTP_PROXY variable when REQUEST_METHOD environment is set, which indicates
18+
that the script is in CGI mode.
19+
1620
Tests
1721
-----
1822

0 commit comments

Comments
 (0)