Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,8 @@ def _create_unverified_context(protocol=None, *, cert_reqs=CERT_NONE,
raise ValueError(purpose)

context = SSLContext(protocol)
# Setting verify_mode to CERT_NONE fails while check_hostname is
# enabled, so assign check_hostname first (gh-114905).
context.check_hostname = check_hostname
if cert_reqs is not None:
context.verify_mode = cert_reqs
Expand Down
29 changes: 29 additions & 0 deletions Lib/test/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1817,6 +1817,35 @@ def test__create_stdlib_context(self):
self.assertEqual(ctx.verify_mode, ssl.CERT_NONE)
self._assert_context_options(ctx)

def test__create_stdlib_context_check_hostname(self):
# gh-114905: check_hostname cannot be combined with CERT_NONE,
# the default for cert_reqs.
msg = "Cannot set verify_mode to CERT_NONE when check_hostname"
with self.assertRaisesRegex(ValueError, msg):
ssl._create_stdlib_context(check_hostname=True)
with self.assertRaisesRegex(ValueError, msg):
ssl._create_stdlib_context(cert_reqs=ssl.CERT_NONE,
check_hostname=True)

# Accepted before 3.10 with a legacy protocol.
if has_tls_protocol('PROTOCOL_TLSv1_2'):
with warnings_helper.check_warnings():
with self.assertRaisesRegex(ValueError, msg):
ssl._create_stdlib_context(ssl.PROTOCOL_TLSv1_2,
cert_reqs=ssl.CERT_NONE,
check_hostname=True)

# cert_reqs=None leaves PROTOCOL_TLS_CLIENT's CERT_REQUIRED.
ctx = ssl._create_stdlib_context(cert_reqs=None, check_hostname=True)
self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED)
self.assertTrue(ctx.check_hostname)

# CERT_REQUIRED is covered by test__create_stdlib_context().
ctx = ssl._create_stdlib_context(cert_reqs=ssl.CERT_OPTIONAL,
check_hostname=True)
self.assertEqual(ctx.verify_mode, ssl.CERT_OPTIONAL)
self.assertTrue(ctx.check_hostname)

def test_check_hostname(self):
with warnings_helper.check_warnings():
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS)
Expand Down
Loading