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

Skip to content

Commit b46247b

Browse files
committed
merge 3.4 (#22959)
2 parents 5db1bb8 + a090f01 commit b46247b

4 files changed

Lines changed: 22 additions & 10 deletions

File tree

Doc/library/http.client.rst

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,12 @@ The module provides the following classes:
6969
*key_file* and *cert_file* are deprecated, please use
7070
:meth:`ssl.SSLContext.load_cert_chain` instead, or let
7171
:func:`ssl.create_default_context` select the system's trusted CA
72-
certificates for you.
72+
certificates for you. The *check_hostname* parameter is also deprecated; the
73+
:attr:`SSLContext.check_hostname` attribute of *context* should be used
74+
instead.
7375

7476
Please read :ref:`ssl-security` for more information on best practices.
7577

76-
.. note::
77-
If *context* is specified and has a :attr:`~ssl.SSLContext.verify_mode`
78-
of either :data:`~ssl.CERT_OPTIONAL` or :data:`~ssl.CERT_REQUIRED`, then
79-
by default *host* is matched against the host name(s) allowed by the
80-
server's certificate. If you want to change that behaviour, you can
81-
explicitly set *check_hostname* to False.
82-
8378
.. versionchanged:: 3.2
8479
*source_address*, *context* and *check_hostname* were added.
8580

Lib/http/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,8 +1274,8 @@ def __init__(self, host, port=None, key_file=None, cert_file=None,
12741274
context = ssl._create_default_https_context()
12751275
will_verify = context.verify_mode != ssl.CERT_NONE
12761276
if check_hostname is None:
1277-
check_hostname = will_verify
1278-
elif check_hostname and not will_verify:
1277+
check_hostname = context.check_hostname
1278+
if check_hostname and not will_verify:
12791279
raise ValueError("check_hostname needs a SSL context with "
12801280
"either CERT_OPTIONAL or CERT_REQUIRED")
12811281
if key_file or cert_file:

Lib/test/test_httplib.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ def test_local_bad_hostname(self):
11131113
server = self.make_server(CERT_fakehostname)
11141114
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
11151115
context.verify_mode = ssl.CERT_REQUIRED
1116+
context.check_hostname = True
11161117
context.load_verify_locations(CERT_fakehostname)
11171118
h = client.HTTPSConnection('localhost', server.port, context=context)
11181119
with self.assertRaises(ssl.CertificateError):
@@ -1123,11 +1124,24 @@ def test_local_bad_hostname(self):
11231124
with self.assertRaises(ssl.CertificateError):
11241125
h.request('GET', '/')
11251126
# With check_hostname=False, the mismatching is ignored
1127+
context.check_hostname = False
11261128
h = client.HTTPSConnection('localhost', server.port, context=context,
11271129
check_hostname=False)
11281130
h.request('GET', '/nonexistent')
11291131
resp = h.getresponse()
11301132
self.assertEqual(resp.status, 404)
1133+
# The context's check_hostname setting is used if one isn't passed to
1134+
# HTTPSConnection.
1135+
context.check_hostname = False
1136+
h = client.HTTPSConnection('localhost', server.port, context=context)
1137+
h.request('GET', '/nonexistent')
1138+
self.assertEqual(h.getresponse().status, 404)
1139+
# Passing check_hostname to HTTPSConnection should override the
1140+
# context's setting.
1141+
h = client.HTTPSConnection('localhost', server.port, context=context,
1142+
check_hostname=True)
1143+
with self.assertRaises(ssl.CertificateError):
1144+
h.request('GET', '/')
11311145

11321146
@unittest.skipIf(not hasattr(client, 'HTTPSConnection'),
11331147
'http.client.HTTPSConnection not available')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ Core and Builtins
194194
Library
195195
-------
196196

197+
- Issue #22959: In the constructor of http.client.HTTPSConnection, prefer the
198+
context's check_hostname attribute over the *check_hostname* parameter.
199+
197200
- Issue #22696: Add function :func:`sys.is_finalizing` to know about
198201
interpreter shutdown.
199202

0 commit comments

Comments
 (0)