Skip to content
Merged
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
15 changes: 15 additions & 0 deletions Lib/test/test_sqlite3/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ def test_collation_register_twice(self):
self.assertEqual(result[0][0], 'b')
self.assertEqual(result[1][0], 'a')

def test_collation_register_when_busy(self):
# See https://github.com/python/cpython/issues/146090.
con = self.con
con.create_collation("mycoll", lambda x, y: (x > y) - (x < y))
con.execute("CREATE TABLE t(x TEXT)")
con.execute("INSERT INTO t VALUES (?)", ("a",))
con.execute("INSERT INTO t VALUES (?)", ("b",))
con.commit()

cursor = self.con.execute("SELECT x FROM t ORDER BY x COLLATE mycoll")
next(cursor)
# Replace the collation while the statement is active -> SQLITE_BUSY.
with self.assertRaises(sqlite.OperationalError) as cm:
self.con.create_collation("mycoll", lambda a, b: 0)

def test_deregister_collation(self):
"""
Register a collation, then deregister it. Make sure an error is raised if we try
Expand Down
61 changes: 60 additions & 1 deletion Lib/test/test_ssl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Test the support for SSL and sockets

import contextlib
import sys
import unittest
import unittest.mock
Expand Down Expand Up @@ -47,12 +48,13 @@

PROTOCOLS = sorted(ssl._PROTOCOL_NAMES)
HOST = socket_helper.HOST
IS_AWS_LC = "AWS-LC" in ssl.OPENSSL_VERSION
IS_OPENSSL_3_0_0 = ssl.OPENSSL_VERSION_INFO >= (3, 0, 0)
CAN_GET_SELECTED_OPENSSL_GROUP = ssl.OPENSSL_VERSION_INFO >= (3, 2)
CAN_IGNORE_UNKNOWN_OPENSSL_GROUPS = ssl.OPENSSL_VERSION_INFO >= (3, 3)
CAN_GET_AVAILABLE_OPENSSL_GROUPS = ssl.OPENSSL_VERSION_INFO >= (3, 5)
CAN_GET_AVAILABLE_OPENSSL_SIGALGS = ssl.OPENSSL_VERSION_INFO >= (3, 4)
CAN_SET_CLIENT_SIGALGS = "AWS-LC" not in ssl.OPENSSL_VERSION
CAN_SET_CLIENT_SIGALGS = not IS_AWS_LC
CAN_IGNORE_UNKNOWN_OPENSSL_SIGALGS = ssl.OPENSSL_VERSION_INFO >= (3, 3)
CAN_GET_SELECTED_OPENSSL_SIGALG = ssl.OPENSSL_VERSION_INFO >= (3, 5)
PY_SSL_DEFAULT_CIPHERS = sysconfig.get_config_var('PY_SSL_DEFAULT_CIPHERS')
Expand Down Expand Up @@ -383,6 +385,20 @@ def testing_context(server_cert=SIGNED_CERTFILE, *, server_chain=True,
return client_context, server_context, hostname


def do_ssl_object_handshake(sslobject, outgoing, max_retry=25):
"""Call do_handshake() on the sslobject and return the sent data.

If do_handshake() fails more than *max_retry* times, return None.
"""
data, attempt = None, 0
while not data and attempt < max_retry:
with contextlib.suppress(ssl.SSLWantReadError):
sslobject.do_handshake()
data = outgoing.read()
attempt += 1
return data


class BasicSocketTests(unittest.TestCase):

def test_constants(self):
Expand Down Expand Up @@ -1535,6 +1551,49 @@ def dummycallback(sock, servername, ctx):
ctx.set_servername_callback(None)
ctx.set_servername_callback(dummycallback)

def test_sni_callback_on_dead_references(self):
# See https://github.com/python/cpython/issues/146080.
c_ctx = make_test_context()
c_inc, c_out = ssl.MemoryBIO(), ssl.MemoryBIO()
client = c_ctx.wrap_bio(c_inc, c_out, server_hostname=SIGNED_CERTFILE_HOSTNAME)

def sni_callback(sock, servername, ctx): pass
sni_callback = unittest.mock.Mock(wraps=sni_callback)
s_ctx = make_test_context(server_side=True, certfile=SIGNED_CERTFILE)
s_ctx.set_servername_callback(sni_callback)

s_inc, s_out = ssl.MemoryBIO(), ssl.MemoryBIO()
server = s_ctx.wrap_bio(s_inc, s_out, server_side=True)
server_impl = server._sslobj

# Perform the handshake on the client side first.
data = do_ssl_object_handshake(client, c_out)
sni_callback.assert_not_called()
if data is None:
self.skipTest("cannot establish a handshake from the client")
s_inc.write(data)
sni_callback.assert_not_called()
# Delete the server object before it starts doing its handshake
# and ensure that we did not call the SNI callback yet.
del server
gc.collect()
# Try to continue the server's handshake by directly using
# the internal SSL object. The latter is a weak reference
# stored in the server context and has now a dead owner.
with self.assertRaises(ssl.SSLError) as cm:
server_impl.do_handshake()
# The SNI C callback raised an exception before calling our callback.
sni_callback.assert_not_called()

# In AWS-LC, any handshake failures reports SSL_R_PARSE_TLSEXT,
# while OpenSSL uses SSL_R_CALLBACK_FAILED on SNI callback failures.
if IS_AWS_LC:
libssl_error_reason = "PARSE_TLSEXT"
else:
libssl_error_reason = "callback failed"
self.assertIn(libssl_error_reason, str(cm.exception))
self.assertEqual(cm.exception.errno, ssl.SSL_ERROR_SSL)

def test_sni_callback_refcycle(self):
# Reference cycles through the servername callback are detected
# and cleared.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update bundled `libexpat <https://libexpat.github.io/>`_ to version 2.7.5.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`sqlite3`: properly raise :exc:`MemoryError` instead of :exc:`SystemError`
when a context callback fails to be allocated. Patch by Bénédikt Tran.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:mod:`sqlite3`: fix a crash when :meth:`sqlite3.Connection.create_collation`
fails with `SQLITE_BUSY <https://sqlite.org/rescode.html#busy>`__. Patch by
Bénédikt Tran.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`ssl`: fix a crash when an SNI callback tries to use an SSL object that
has already been garbage-collected. Patch by Bénédikt Tran.
32 changes: 16 additions & 16 deletions Misc/sbom.spdx.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -1059,13 +1059,16 @@ static callback_context *
create_callback_context(PyTypeObject *cls, PyObject *callable)
{
callback_context *ctx = PyMem_Malloc(sizeof(callback_context));
if (ctx != NULL) {
PyObject *module = PyType_GetModule(cls);
ctx->refcount = 1;
ctx->callable = Py_NewRef(callable);
ctx->module = Py_NewRef(module);
ctx->state = pysqlite_get_state(module);
if (ctx == NULL) {
PyErr_NoMemory();
return NULL;
}

PyObject *module = PyType_GetModule(cls);
ctx->refcount = 1;
ctx->callable = Py_NewRef(callable);
ctx->module = Py_NewRef(module);
ctx->state = pysqlite_get_state(module);
return ctx;
}

Expand Down Expand Up @@ -2198,7 +2201,7 @@ pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
* the context before returning.
*/
if (callable != Py_None) {
free_callback_context(ctx);
decref_callback_context(ctx);
}
set_error_from_db(self->state, self->db);
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -5205,7 +5205,7 @@ _servername_callback(SSL *s, int *al, void *args)
return ret;

error:
Py_DECREF(ssl_socket);
Py_XDECREF(ssl_socket);
*al = SSL_AD_INTERNAL_ERROR;
ret = SSL_TLSEXT_ERR_ALERT_FATAL;
PyGILState_Release(gstate);
Expand Down
2 changes: 1 addition & 1 deletion Modules/expat/expat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ XML_SetReparseDeferralEnabled(XML_Parser parser, XML_Bool enabled);
*/
# define XML_MAJOR_VERSION 2
# define XML_MINOR_VERSION 7
# define XML_MICRO_VERSION 4
# define XML_MICRO_VERSION 5

# ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/expat/expat_external.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Copyright (c) 2001-2002 Greg Stein <gstein@users.sourceforge.net>
Copyright (c) 2002-2006 Karl Waclawek <karl@waclawek.net>
Copyright (c) 2016 Cristian Rodríguez <crrodriguez@opensuse.org>
Copyright (c) 2016-2025 Sebastian Pipping <sebastian@pipping.org>
Copyright (c) 2016-2026 Sebastian Pipping <sebastian@pipping.org>
Copyright (c) 2017 Rhodri James <rhodri@wildebeest.org.uk>
Copyright (c) 2018 Yury Gribov <tetra2005@gmail.com>
Licensed under the MIT license:
Expand Down
6 changes: 3 additions & 3 deletions Modules/expat/refresh.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ fi

# Update this when updating to a new version after verifying that the changes
# the update brings in are good. These values are used for verifying the SBOM, too.
expected_libexpat_tag="R_2_7_4"
expected_libexpat_version="2.7.4"
expected_libexpat_sha256="461ecc8aa98ab1a68c2db788175665d1a4db640dc05bf0e289b6ea17122144ec"
expected_libexpat_tag="R_2_7_5"
expected_libexpat_version="2.7.5"
expected_libexpat_sha256="9931f9860d18e6cf72d183eb8f309bfb96196c00e1d40caa978e95bc9aa978b6"

expat_dir="$(realpath "$(dirname -- "${BASH_SOURCE[0]}")")"
cd ${expat_dir}
Expand Down
Loading
Loading