|
| 1 | +From ba1d5fdf7bc8aa1b4dc157722161e79f3c290ab1 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Alex Gaynor <alex.gaynor@gmail.com> |
| 3 | +Date: Mon, 16 Feb 2026 21:04:37 -0500 |
| 4 | +Subject: [PATCH] Handle exceptions in set_tlsext_servername_callback callbacks |
| 5 | + (#1478) |
| 6 | + |
| 7 | +When the servername callback raises an exception, call sys.excepthook |
| 8 | +with the exception info and return SSL_TLSEXT_ERR_ALERT_FATAL to abort |
| 9 | +the handshake. Previously, exceptions would propagate uncaught through |
| 10 | +the CFFI callback boundary. |
| 11 | + |
| 12 | +https://claude.ai/code/session_01P7y1XmWkdtC5UcmZwGDvGi |
| 13 | + |
| 14 | +Co-authored-by: Claude <noreply@anthropic.com> |
| 15 | +Signed-off-by: Azure Linux Security Servicing Account <azurelinux-security@microsoft.com> |
| 16 | +Upstream-reference: https://github.com/pyca/pyopenssl/commit/d41a814759a9fb49584ca8ab3f7295de49a85aa0.patch |
| 17 | +--- |
| 18 | + src/OpenSSL/SSL.py | 7 ++++++- |
| 19 | + tests/test_ssl.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++ |
| 20 | + 2 files changed, 56 insertions(+), 1 deletion(-) |
| 21 | + |
| 22 | +diff --git a/src/OpenSSL/SSL.py b/src/OpenSSL/SSL.py |
| 23 | +index 4ca5950..8f9d21e 100644 |
| 24 | +--- a/src/OpenSSL/SSL.py |
| 25 | ++++ b/src/OpenSSL/SSL.py |
| 26 | +@@ -1,5 +1,6 @@ |
| 27 | + import os |
| 28 | + import socket |
| 29 | ++import sys |
| 30 | + import typing |
| 31 | + from errno import errorcode |
| 32 | + from functools import partial, wraps |
| 33 | +@@ -1621,7 +1622,11 @@ class Context: |
| 34 | + |
| 35 | + @wraps(callback) |
| 36 | + def wrapper(ssl, alert, arg): # type: ignore[no-untyped-def] |
| 37 | +- callback(Connection._reverse_mapping[ssl]) |
| 38 | ++ try: |
| 39 | ++ callback(Connection._reverse_mapping[ssl]) |
| 40 | ++ except Exception: |
| 41 | ++ sys.excepthook(*sys.exc_info()) |
| 42 | ++ return _lib.SSL_TLSEXT_ERR_ALERT_FATAL |
| 43 | + return 0 |
| 44 | + |
| 45 | + self._tlsext_servername_callback = _ffi.callback( |
| 46 | +diff --git a/tests/test_ssl.py b/tests/test_ssl.py |
| 47 | +index 3a0cddf..df142d6 100644 |
| 48 | +--- a/tests/test_ssl.py |
| 49 | ++++ b/tests/test_ssl.py |
| 50 | +@@ -1854,6 +1854,56 @@ class TestServerNameCallback: |
| 51 | + |
| 52 | + assert args == [(server, b"foo1.example.com")] |
| 53 | + |
| 54 | ++ def test_servername_callback_exception( |
| 55 | ++ self, monkeypatch: pytest.MonkeyPatch |
| 56 | ++ ) -> None: |
| 57 | ++ """ |
| 58 | ++ When the callback passed to `Context.set_tlsext_servername_callback` |
| 59 | ++ raises an exception, ``sys.excepthook`` is called with the exception |
| 60 | ++ and the handshake fails with an ``Error``. |
| 61 | ++ """ |
| 62 | ++ exc = TypeError("server name callback failed") |
| 63 | ++ |
| 64 | ++ def servername(conn: Connection) -> None: |
| 65 | ++ raise exc |
| 66 | ++ |
| 67 | ++ excepthook_calls: list[ |
| 68 | ++ tuple[type[BaseException], BaseException, object] |
| 69 | ++ ] = [] |
| 70 | ++ |
| 71 | ++ def custom_excepthook( |
| 72 | ++ exc_type: type[BaseException], |
| 73 | ++ exc_value: BaseException, |
| 74 | ++ exc_tb: object, |
| 75 | ++ ) -> None: |
| 76 | ++ excepthook_calls.append((exc_type, exc_value, exc_tb)) |
| 77 | ++ |
| 78 | ++ context = Context(SSLv23_METHOD) |
| 79 | ++ context.set_tlsext_servername_callback(servername) |
| 80 | ++ |
| 81 | ++ # Necessary to actually accept the connection |
| 82 | ++ context.use_privatekey(load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 83 | ++ context.use_certificate( |
| 84 | ++ load_certificate(FILETYPE_PEM, server_cert_pem) |
| 85 | ++ ) |
| 86 | ++ |
| 87 | ++ # Do a little connection to trigger the logic |
| 88 | ++ server = Connection(context, None) |
| 89 | ++ server.set_accept_state() |
| 90 | ++ |
| 91 | ++ client = Connection(Context(SSLv23_METHOD), None) |
| 92 | ++ client.set_connect_state() |
| 93 | ++ client.set_tlsext_host_name(b"foo1.example.com") |
| 94 | ++ |
| 95 | ++ monkeypatch.setattr(sys, "excepthook", custom_excepthook) |
| 96 | ++ with pytest.raises(Error): |
| 97 | ++ interact_in_memory(server, client) |
| 98 | ++ |
| 99 | ++ assert len(excepthook_calls) == 1 |
| 100 | ++ assert excepthook_calls[0][0] is TypeError |
| 101 | ++ assert excepthook_calls[0][1] is exc |
| 102 | ++ assert excepthook_calls[0][2] is not None |
| 103 | ++ |
| 104 | + |
| 105 | + class TestApplicationLayerProtoNegotiation: |
| 106 | + """ |
| 107 | +-- |
| 108 | +2.45.4 |
| 109 | + |
0 commit comments