From 6684b6d6953ad0987377f7cda4b1521a9f3d91e2 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 20 Aug 2026 14:27:27 +0300 Subject: [PATCH 1/2] gh-156099: Fix a crash when deleting SSLContext.keylog_filename The setter did not check the value for NULL and passed it to Py_fopen(). --- Lib/test/test_ssl.py | 4 ++++ .../Library/2026-08-20-12-00-00.gh-issue-156099.Kp4vRt.rst | 3 +++ Modules/_ssl/debughelpers.c | 6 ++++++ 3 files changed, 13 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-08-20-12-00-00.gh-issue-156099.Kp4vRt.rst diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index 2bba665d19343e..693064a7a81107 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -5518,6 +5518,10 @@ def test_keylog_defaults(self): with self.assertRaises(TypeError): ctx.keylog_filename = 1 + with self.assertRaisesRegex(AttributeError, 'cannot be deleted'): + del ctx.keylog_filename + self.assertEqual(ctx.keylog_filename, None) + def test_keylog_filename(self): self.addCleanup(os_helper.unlink, os_helper.TESTFN) client_context, server_context, hostname = testing_context() diff --git a/Misc/NEWS.d/next/Library/2026-08-20-12-00-00.gh-issue-156099.Kp4vRt.rst b/Misc/NEWS.d/next/Library/2026-08-20-12-00-00.gh-issue-156099.Kp4vRt.rst new file mode 100644 index 00000000000000..1092a5a5378563 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-08-20-12-00-00.gh-issue-156099.Kp4vRt.rst @@ -0,0 +1,3 @@ +Fix a crash when deleting the ``keylog_filename`` attribute of +:class:`ssl.SSLContext`. +It now raises :exc:`AttributeError`. diff --git a/Modules/_ssl/debughelpers.c b/Modules/_ssl/debughelpers.c index fb9043994f08f9..b2d552f97e5b0e 100644 --- a/Modules/_ssl/debughelpers.c +++ b/Modules/_ssl/debughelpers.c @@ -182,6 +182,12 @@ static int _PySSLContext_set_keylog_filename(PyObject *op, PyObject *arg, void *Py_UNUSED(closure)) { + if (arg == NULL) { + PyErr_Format(PyExc_AttributeError, + "attribute 'keylog_filename' of '%.100s' objects " + "cannot be deleted", Py_TYPE(op)->tp_name); + return -1; + } #if defined(MS_WINDOWS_APP) && !defined(MS_WINDOWS_DESKTOP) PyErr_SetString(PyExc_NotImplementedError, "set_keylog_filename: unavailable on UWP build"); From 628f25609622c93dfe1820168a4a18d7408f3c1e Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 20 Aug 2026 14:48:29 +0300 Subject: [PATCH 2/2] Check that a failed deletion does not change the value --- Lib/test/test_ssl.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index 693064a7a81107..14e4620669491f 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -5518,9 +5518,11 @@ def test_keylog_defaults(self): with self.assertRaises(TypeError): ctx.keylog_filename = 1 + ctx.keylog_filename = os_helper.TESTFN with self.assertRaisesRegex(AttributeError, 'cannot be deleted'): del ctx.keylog_filename - self.assertEqual(ctx.keylog_filename, None) + # a failed deletion does not change the value + self.assertEqual(ctx.keylog_filename, os_helper.TESTFN) def test_keylog_filename(self): self.addCleanup(os_helper.unlink, os_helper.TESTFN)