FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.15] gh-156166: Fix setting and deleting SSLContext._msg_callback (GH-156167) by miss-islington · Pull Request #156171 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 12 additions & 0 deletions Lib/test/test_ssl.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -5566,6 +5566,18 @@ def msg_cb(conn, direction, version, content_type, msg_type, data):
with self.assertRaises(TypeError):
client_context._msg_callback = object()

# the attribute of the underlying C type accepts only a callable
# and cannot be deleted
descr = _ssl._SSLContext.__dict__['_msg_callback']
with self.assertRaises(TypeError):
descr.__set__(client_context, object())
# a failed assignment does not change the value
self.assertIs(client_context._msg_callback, msg_cb)
with self.assertRaisesRegex(AttributeError, 'cannot be deleted'):
descr.__delete__(client_context)
# a failed deletion does not change the value
self.assertIs(client_context._msg_callback, msg_cb)

def test_msg_callback_exception(self):
client_context, server_context, hostname = testing_context()

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:mod:`ssl`: A failed assignment or deletion of the ``_msg_callback``
attribute of :class:`ssl.SSLContext` no longer removes the current callback.
Deleting it now raises :exc:`AttributeError` instead of :exc:`TypeError`.
22 changes: 15 additions & 7 deletions Modules/_ssl/debughelpers.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,28 @@ _PySSLContext_set_msg_callback(PyObject *op, PyObject *arg,
void *Py_UNUSED(closure))
{
PySSLContext *self = PySSLContext_CAST(op);
Py_CLEAR(self->msg_cb);
if (arg == NULL) {
PyErr_Format(PyExc_AttributeError,
"attribute '_msg_callback' of '%.100s' objects "
"cannot be deleted", Py_TYPE(op)->tp_name);
return -1;
}
if (arg != Py_None && !PyCallable_Check(arg)) {
PyErr_SetString(PyExc_TypeError,
"not a callable object");
return -1;
}
/* Releasing the old callback can run arbitrary code. */
PyObject *old_cb = self->msg_cb;
if (arg == Py_None) {
self->msg_cb = NULL;
SSL_CTX_set_msg_callback(self->ctx, NULL);
}
else {
if (!PyCallable_Check(arg)) {
SSL_CTX_set_msg_callback(self->ctx, NULL);
PyErr_SetString(PyExc_TypeError,
"not a callable object");
return -1;
}
self->msg_cb = Py_NewRef(arg);
SSL_CTX_set_msg_callback(self->ctx, _PySSL_msg_callback);
}
Py_XDECREF(old_cb);
return 0;
}

Expand Down
Loading

Back | FazBrowse Home | New Git URL