Skip to content

Commit b2e408c

Browse files
Merge remote-tracking branch 'upstream/3.15' into mi-156593
2 parents bede6e0 + c6465bc commit b2e408c

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

‎Lib/ssl.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,8 @@ def _create_unverified_context(protocol=None, *, cert_reqs=CERT_NONE,
769769
raise ValueError(purpose)
770770

771771
context = SSLContext(protocol)
772+
# Setting verify_mode to CERT_NONE fails while check_hostname is
773+
# enabled, so assign check_hostname first (gh-114905).
772774
context.check_hostname = check_hostname
773775
if cert_reqs is not None:
774776
context.verify_mode = cert_reqs

‎Lib/test/test_ssl.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,35 @@ def test__create_stdlib_context(self):
18471847
self.assertEqual(ctx.verify_mode, ssl.CERT_NONE)
18481848
self._assert_context_options(ctx)
18491849

1850+
def test__create_stdlib_context_check_hostname(self):
1851+
# gh-114905: check_hostname cannot be combined with CERT_NONE,
1852+
# the default for cert_reqs.
1853+
msg = "Cannot set verify_mode to CERT_NONE when check_hostname"
1854+
with self.assertRaisesRegex(ValueError, msg):
1855+
ssl._create_stdlib_context(check_hostname=True)
1856+
with self.assertRaisesRegex(ValueError, msg):
1857+
ssl._create_stdlib_context(cert_reqs=ssl.CERT_NONE,
1858+
check_hostname=True)
1859+
1860+
# Accepted before 3.10 with a legacy protocol.
1861+
if has_tls_protocol('PROTOCOL_TLSv1_2'):
1862+
with warnings_helper.check_warnings():
1863+
with self.assertRaisesRegex(ValueError, msg):
1864+
ssl._create_stdlib_context(ssl.PROTOCOL_TLSv1_2,
1865+
cert_reqs=ssl.CERT_NONE,
1866+
check_hostname=True)
1867+
1868+
# cert_reqs=None leaves PROTOCOL_TLS_CLIENT's CERT_REQUIRED.
1869+
ctx = ssl._create_stdlib_context(cert_reqs=None, check_hostname=True)
1870+
self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED)
1871+
self.assertTrue(ctx.check_hostname)
1872+
1873+
# CERT_REQUIRED is covered by test__create_stdlib_context().
1874+
ctx = ssl._create_stdlib_context(cert_reqs=ssl.CERT_OPTIONAL,
1875+
check_hostname=True)
1876+
self.assertEqual(ctx.verify_mode, ssl.CERT_OPTIONAL)
1877+
self.assertTrue(ctx.check_hostname)
1878+
18501879
def test_delete_sslobject_attributes(self):
18511880
# None of the attributes of _ssl._SSLSocket can be deleted.
18521881
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)

0 commit comments

Comments
 (0)