Skip to content

Commit 5ebd486

Browse files
gh-156099: Fix a crash when deleting SSLContext.keylog_filename (GH-156103)
The setter did not check the value for NULL and passed it to Py_fopen().
1 parent 04242c0 commit 5ebd486

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

Lib/test/test_ssl.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5518,6 +5518,12 @@ def test_keylog_defaults(self):
55185518
with self.assertRaises(TypeError):
55195519
ctx.keylog_filename = 1
55205520

5521+
ctx.keylog_filename = os_helper.TESTFN
5522+
with self.assertRaisesRegex(AttributeError, 'cannot be deleted'):
5523+
del ctx.keylog_filename
5524+
# a failed deletion does not change the value
5525+
self.assertEqual(ctx.keylog_filename, os_helper.TESTFN)
5526+
55215527
def test_keylog_filename(self):
55225528
self.addCleanup(os_helper.unlink, os_helper.TESTFN)
55235529
client_context, server_context, hostname = testing_context()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a crash when deleting the ``keylog_filename`` attribute of
2+
:class:`ssl.SSLContext`.
3+
It now raises :exc:`AttributeError`.

Modules/_ssl/debughelpers.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ static int
182182
_PySSLContext_set_keylog_filename(PyObject *op, PyObject *arg,
183183
void *Py_UNUSED(closure))
184184
{
185+
if (arg == NULL) {
186+
PyErr_Format(PyExc_AttributeError,
187+
"attribute 'keylog_filename' of '%.100s' objects "
188+
"cannot be deleted", Py_TYPE(op)->tp_name);
189+
return -1;
190+
}
185191
#if defined(MS_WINDOWS_APP) && !defined(MS_WINDOWS_DESKTOP)
186192
PyErr_SetString(PyExc_NotImplementedError,
187193
"set_keylog_filename: unavailable on UWP build");

0 commit comments

Comments
 (0)