From f6c5d3adab6ec9fa51db88bc9b75f753df9de52b Mon Sep 17 00:00:00 2001 From: Vasiliy Kiryanov Date: Fri, 27 Nov 2026 18:23:10 -0500 Subject: [PATCH] gh-65224: Improve the ssl error for check_hostname with server_side=True --- Doc/library/ssl.rst | 6 ++++++ Lib/ssl.py | 4 ++++ Lib/test/test_ssl.py | 16 ++++++++++++++++ Misc/ACKS | 1 + ...2026-11-27-18-19-31.gh-issue-65224.vav0iw.rst | 3 +++ 5 files changed, 30 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-27-18-19-31.gh-issue-65224.vav0iw.rst diff --git a/Doc/library/ssl.rst b/Doc/library/ssl.rst index a1ed46e2b6e610a..f7b831c6518f69a 100644 --- a/Doc/library/ssl.rst +++ b/Doc/library/ssl.rst @@ -2361,6 +2361,12 @@ to speed up repeated connections from the same clients. :data:`PROTOCOL_TLS_CLIENT` protocol enables hostname checking by default. With other protocols, hostname checking must be enabled explicitly. + Hostname checking only applies to client-side connections. A context + with hostname checking enabled cannot be used for server-side + connections: :meth:`~SSLContext.wrap_socket` and + :meth:`~SSLContext.wrap_bio` raise :exc:`ValueError` if *server_side* + is true. Use a separate context for servers. + Example:: import socket, ssl diff --git a/Lib/ssl.py b/Lib/ssl.py index 49eec9cde0b6317..6fcec497308986d 100644 --- a/Lib/ssl.py +++ b/Lib/ssl.py @@ -396,6 +396,10 @@ def _check_sslobject_params(server_side, context=None, server_hostname=None, if ech_config is not None: raise ValueError("ech_config can only be specified in " "client mode") + if context.check_hostname: + raise ValueError("check_hostname cannot be used with " + "server_side=True; use a separate server " + "context or disable check_hostname") if ech_config is not None and not HAS_ECH: raise NotImplementedError("ECH is not supported by the OpenSSL library") if context.check_hostname and not server_hostname: diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index 837445dc06d67b9..d42bfdd6e07c82b 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -2725,6 +2725,22 @@ def test_check_hostname_requires_server_hostname(self): self.assertFalse(context.check_hostname) context.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO()) + def test_check_hostname_rejects_server_side(self): + # gh-65224: check_hostname only applies to client connections, so + # server mode rejects it with an error that says so. + exp_str = ("check_hostname cannot be used with server_side=True; " + "use a separate server context or disable check_hostname") + + _, server_context, _ = testing_context() + server_context.check_hostname = True + with self.assertRaisesRegex(ValueError, exp_str): + server_context.wrap_bio(ssl.MemoryBIO(), ssl.MemoryBIO(), + server_side=True) + + with self.assertRaisesRegex(ValueError, exp_str): + with socket.socket() as sock: + server_context.wrap_socket(sock, server_side=True) + def test_server_side_bad_params(self): # A server neither sends a hostname nor resumes a client's session, # so wrap_bio() rejects both in server mode like wrap_socket() diff --git a/Misc/ACKS b/Misc/ACKS index c3ecfc1e315a595..1da2dd9dabca44b 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1005,6 +1005,7 @@ Paul Kippes Sergey B Kirpichev Steve Kirsch Sebastian Kirsche +Vasiliy Kiryanov Kamil Kisiel Akira Kitada Ron Klatchko diff --git a/Misc/NEWS.d/next/Library/2026-11-27-18-19-31.gh-issue-65224.vav0iw.rst b/Misc/NEWS.d/next/Library/2026-11-27-18-19-31.gh-issue-65224.vav0iw.rst new file mode 100644 index 000000000000000..f0a63cbd6454d5c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-27-18-19-31.gh-issue-65224.vav0iw.rst @@ -0,0 +1,3 @@ +Improve the error raised when a context with +:attr:`ssl.SSLContext.check_hostname` enabled is used with +``server_side=True``.