Skip to content

Commit f6c5d3a

Browse files
committed
gh-65224: Improve the ssl error for check_hostname with server_side=True
1 parent ebd4a5d commit f6c5d3a

5 files changed

Lines changed: 30 additions & 0 deletions

File tree

‎Doc/library/ssl.rst‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2361,6 +2361,12 @@ to speed up repeated connections from the same clients.
23612361
:data:`PROTOCOL_TLS_CLIENT` protocol enables hostname checking by default.
23622362
With other protocols, hostname checking must be enabled explicitly.
23632363

2364+
Hostname checking only applies to client-side connections. A context
2365+
with hostname checking enabled cannot be used for server-side
2366+
connections: :meth:`~SSLContext.wrap_socket` and
2367+
:meth:`~SSLContext.wrap_bio` raise :exc:`ValueError` if *server_side*
2368+
is true. Use a separate context for servers.
2369+
23642370
Example::
23652371

23662372
import socket, ssl

‎Lib/ssl.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,10 @@ def _check_sslobject_params(server_side, context=None, server_hostname=None,
396396
if ech_config is not None:
397397
raise ValueError("ech_config can only be specified in "
398398
"client mode")
399+
if context.check_hostname:
400+
raise ValueError("check_hostname cannot be used with "
401+
"server_side=True; use a separate server "
402+
"context or disable check_hostname")
399403
if ech_config is not None and not HAS_ECH:
400404
raise NotImplementedError("ECH is not supported by the OpenSSL library")
401405
if context.check_hostname and not server_hostname:

‎Lib/test/test_ssl.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,6 +2725,22 @@ def test_check_hostname_requires_server_hostname(self):
27252725
self.assertFalse(context.check_hostname)
27262726
context.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO())
27272727

2728+
def test_check_hostname_rejects_server_side(self):
2729+
# gh-65224: check_hostname only applies to client connections, so
2730+
# server mode rejects it with an error that says so.
2731+
exp_str = ("check_hostname cannot be used with server_side=True; "
2732+
"use a separate server context or disable check_hostname")
2733+
2734+
_, server_context, _ = testing_context()
2735+
server_context.check_hostname = True
2736+
with self.assertRaisesRegex(ValueError, exp_str):
2737+
server_context.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO(),
2738+
server_side=True)
2739+
2740+
with self.assertRaisesRegex(ValueError, exp_str):
2741+
with socket.socket() as sock:
2742+
server_context.wrap_socket(sock, server_side=True)
2743+
27282744
def test_server_side_bad_params(self):
27292745
# A server neither sends a hostname nor resumes a client's session,
27302746
# so wrap_bio() rejects both in server mode like wrap_socket()

‎Misc/ACKS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,7 @@ Paul Kippes
10051005
Sergey B Kirpichev
10061006
Steve Kirsch
10071007
Sebastian Kirsche
1008+
Vasiliy Kiryanov
10081009
Kamil Kisiel
10091010
Akira Kitada
10101011
Ron Klatchko
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improve the error raised when a context with
2+
:attr:`ssl.SSLContext.check_hostname` enabled is used with
3+
``server_side=True``.

0 commit comments

Comments
 (0)