diff -r caa8f9248ab8 Doc/library/http.client.rst --- a/Doc/library/http.client.rst Sun Nov 02 22:19:56 2014 +0200 +++ b/Doc/library/http.client.rst Mon Nov 03 07:28:18 2014 -0800 @@ -71,12 +71,6 @@ :func:`ssl.create_default_context` select the system's trusted CA certificates for you. - The recommended way to connect to HTTPS hosts on the Internet is as - follows:: - - context = ssl.create_default_context() - h = client.HTTPSConnection('www.python.org', 443, context=context) - Please read :ref:`ssl-security` for more information on best practices. .. note:: @@ -97,6 +91,12 @@ The *strict* parameter was removed. HTTP 0.9-style "Simple Responses" are no longer supported. + .. versionchanged:: 3.4.3 + This class now performs all the necessary certificate and hostname checks + by default. To revert to the previous, unverified, behavior + :func:`ssl._create_unverified_context` can be passed to the *context* + parameter. + .. class:: HTTPResponse(sock, debuglevel=0, method=None, url=None) diff -r caa8f9248ab8 Doc/library/urllib.request.rst --- a/Doc/library/urllib.request.rst Sun Nov 02 22:19:56 2014 +0200 +++ b/Doc/library/urllib.request.rst Mon Nov 03 07:28:18 2014 -0800 @@ -62,11 +62,6 @@ *cafile* and *capath* parameters are omitted. This will only work on some non-Windows platforms. - .. warning:: - If neither *cafile* nor *capath* is specified, and *cadefault* is ``False``, - an HTTPS request will not do any verification of the server's - certificate. - For http and https urls, this function returns a :class:`http.client.HTTPResponse` object which has the following :ref:`httpresponse-objects` methods. @@ -115,8 +110,8 @@ .. versionchanged:: 3.3 *cadefault* was added. - .. versionchanged:: 3.5 - *context* was added. + .. versionchanged:: 3.4.3 + *context* was added, and this function now performs all the necessary certificate and hostname checks by default for *https* URLS. .. function:: install_opener(opener) diff -r caa8f9248ab8 Doc/library/xmlrpc.client.rst --- a/Doc/library/xmlrpc.client.rst Sun Nov 02 22:19:56 2014 +0200 +++ b/Doc/library/xmlrpc.client.rst Mon Nov 03 07:28:18 2014 -0800 @@ -27,11 +27,10 @@ constructed data. If you need to parse untrusted or unauthenticated data see :ref:`xml-vulnerabilities`. -.. warning:: +.. versionchanged:: 3.4.3 - In the case of https URIs, :mod:`xmlrpc.client` does not do any verification - of the server's certificate. - + For https URIs, :mod:`xmlrpc.client` now performs all the necessary + certificate and hostname checks by default .. class:: ServerProxy(uri, transport=None, encoding=None, verbose=False, \ allow_none=False, use_datetime=False, \ diff -r caa8f9248ab8 Doc/whatsnew/3.4.rst --- a/Doc/whatsnew/3.4.rst Sun Nov 02 22:19:56 2014 +0200 +++ b/Doc/whatsnew/3.4.rst Mon Nov 03 07:28:18 2014 -0800 @@ -2504,3 +2504,32 @@ * The ``f_tstate`` (thread state) field of the :c:type:`PyFrameObject` structure has been removed to fix a bug: see :issue:`14432` for the rationale. + +Changed in 3.4.3 +================ + +.. _pep-476: + +PEP 476: Enabling certificate verification by default for stdlib http clients +----------------------------------------------------------------------------- + +:mod:`http.client` and modules which use it, such as :mod:`urllib.request` and +:mod:`xmlrpc.client`, will now verify that the server presents a certificate +which is signed by a CA in the platform trust store and whose hostname matches +the hostname being requested by default, significantly improving security for +many applications. + +For applications which require the old previous behavior, they can pass an +alternate context:: + + import urllib.request + import ssl + + # This disables all verification + context = ssl._create_unverified_context() + + # This allows using a specific certificate for the host, which doesn't need + # to be in the trust store + context = ssl.create_default_context(cafile="/path/to/file.crt") + + urllib.request.urlopen("https://invalid-cert", context=context) diff -r caa8f9248ab8 Lib/http/client.py --- a/Lib/http/client.py Sun Nov 02 22:19:56 2014 +0200 +++ b/Lib/http/client.py Mon Nov 03 07:28:18 2014 -0800 @@ -1267,7 +1267,7 @@ self.key_file = key_file self.cert_file = cert_file if context is None: - context = ssl._create_stdlib_context() + context = ssl._create_default_https_context() will_verify = context.verify_mode != ssl.CERT_NONE if check_hostname is None: check_hostname = will_verify diff -r caa8f9248ab8 Lib/ssl.py --- a/Lib/ssl.py Sun Nov 02 22:19:56 2014 +0200 +++ b/Lib/ssl.py Mon Nov 03 07:28:18 2014 -0800 @@ -436,8 +436,7 @@ context.load_default_certs(purpose) return context - -def _create_stdlib_context(protocol=PROTOCOL_SSLv23, *, cert_reqs=None, +def _create_unverified_context(protocol=PROTOCOL_SSLv23, *, cert_reqs=None, check_hostname=False, purpose=Purpose.SERVER_AUTH, certfile=None, keyfile=None, cafile=None, capath=None, cadata=None): @@ -601,6 +600,14 @@ return self._sslobj.version() +# Used by http.client if no context is explicitly passed. +_create_default_https_context = create_default_context + + +# Backwards compatibility alias, even though it's not a public name. +_create_stdlib_context = _create_unverified_context + + class SSLSocket(socket): """This class implements a subtype of socket.socket that wraps the underlying OS socket in an SSL context when necessary, and diff -r caa8f9248ab8 Lib/test/test_httplib.py --- a/Lib/test/test_httplib.py Sun Nov 02 22:19:56 2014 +0200 +++ b/Lib/test/test_httplib.py Mon Nov 03 07:28:18 2014 -0800 @@ -1012,13 +1012,36 @@ self.assertIn('Apache', server_string) def test_networked(self): - # Default settings: no cert verification is done + # Default settings: requires a valid cert from a trusted CA + import ssl support.requires('network') - with support.transient_internet('svn.python.org'): - h = client.HTTPSConnection('svn.python.org', 443) + with support.transient_internet('self-signed.pythontest.net'): + h = client.HTTPSConnection('self-signed.pythontest.net', 443) + with self.assertRaises(ssl.SSLError) as exc_info: + h.request('GET', '/') + self.assertEqual(exc_info.exception.reason, 'CERTIFICATE_VERIFY_FAILED') + + def test_networked_noverification(self): + # Switch off cert verification + import ssl + support.requires('network') + with support.transient_internet('self-signed.pythontest.net'): + context = ssl._create_unverified_context() + h = client.HTTPSConnection('self-signed.pythontest.net', 443, + context=context) h.request('GET', '/') resp = h.getresponse() - self._check_svn_python_org(resp) + self.assertIn('nginx', resp.getheader('server')) + + def test_networked_trusted_by_default_cert(self): + # Default settings: requires a valid cert from a trusted CA + support.requires('network') + with support.transient_internet('www.python.org'): + h = client.HTTPSConnection('www.python.org', 443) + h.request('GET', '/') + resp = h.getresponse() + content_type = resp.getheader('content-type') + self.assertIn('text/html', content_type) def test_networked_good_cert(self): # We feed a CA cert that validates the server's cert @@ -1037,13 +1060,23 @@ # We feed a "CA" cert that is unrelated to the server's cert import ssl support.requires('network') - with support.transient_internet('svn.python.org'): + with support.transient_internet('self-signed.pythontest.net'): context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) context.verify_mode = ssl.CERT_REQUIRED context.load_verify_locations(CERT_localhost) - h = client.HTTPSConnection('svn.python.org', 443, context=context) - with self.assertRaises(ssl.SSLError): + h = client.HTTPSConnection('self-signed.pythontest.net', 443, context=context) + with self.assertRaises(ssl.SSLError) as exc_info: h.request('GET', '/') + self.assertEqual(exc_info.exception.reason, 'CERTIFICATE_VERIFY_FAILED') + + def test_local_unknown_cert(self): + # The custom cert isn't known to the default trust bundle + import ssl + server = self.make_server(CERT_localhost) + h = client.HTTPSConnection('localhost', server.port) + with self.assertRaises(ssl.SSLError) as exc_info: + h.request('GET', '/') + self.assertEqual(exc_info.exception.reason, 'CERTIFICATE_VERIFY_FAILED') def test_local_good_hostname(self): # The (valid) cert validates the HTTP hostname @@ -1056,7 +1089,6 @@ h.request('GET', '/nonexistent') resp = h.getresponse() self.assertEqual(resp.status, 404) - del server def test_local_bad_hostname(self): # The (valid) cert doesn't validate the HTTP hostname @@ -1079,7 +1111,6 @@ h.request('GET', '/nonexistent') resp = h.getresponse() self.assertEqual(resp.status, 404) - del server @unittest.skipIf(not hasattr(client, 'HTTPSConnection'), 'http.client.HTTPSConnection not available') diff -r caa8f9248ab8 Lib/test/test_urllib2_localnet.py --- a/Lib/test/test_urllib2_localnet.py Sun Nov 02 22:19:56 2014 +0200 +++ b/Lib/test/test_urllib2_localnet.py Mon Nov 03 07:28:18 2014 -0800 @@ -545,7 +545,8 @@ def test_https(self): handler = self.start_https_server() - data = self.urlopen("https://localhost:%s/bizarre" % handler.port) + context = ssl.create_default_context(cafile=CERT_localhost) + data = self.urlopen("https://localhost:%s/bizarre" % handler.port, context=context) self.assertEqual(data, b"we care a bit") def test_https_with_cafile(self): @@ -584,7 +585,8 @@ context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) context.set_servername_callback(cb_sni) handler = self.start_https_server(context=context, certfile=CERT_localhost) - self.urlopen("https://localhost:%s" % handler.port) + context = ssl.create_default_context(cafile=CERT_localhost) + self.urlopen("https://localhost:%s" % handler.port, context=context) self.assertEqual(sni_name, "localhost") def test_sending_headers(self):