]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-107774: Add missing audit event for PEP 669 (GH-107775)
authorMark Shannon <mark@hotpy.org>
Thu, 10 Aug 2023 11:29:06 +0000 (12:29 +0100)
committerGitHub <noreply@github.com>
Thu, 10 Aug 2023 11:29:06 +0000 (12:29 +0100)
Lib/test/audit-tests.py
Lib/test/test_audit.py
Misc/NEWS.d/next/Security/2023-08-05-03-51-05.gh-issue-107774.VPjaTR.rst [new file with mode: 0644]
Python/instrumentation.c

index 0edc9d9c472766c8357b4feca3af539083b79318..9504829e96f00e2bf5a1040a2d2b49a04852e255 100644 (file)
@@ -514,6 +514,17 @@ def test_not_in_gc():
             assert hook not in o
 
 
+def test_sys_monitoring_register_callback():
+    import sys
+
+    def hook(event, args):
+        if event.startswith("sys.monitoring"):
+            print(event, args)
+
+    sys.addaudithook(hook)
+    sys.monitoring.register_callback(1, 1, None)
+
+
 if __name__ == "__main__":
     from test.support import suppress_msvcrt_asserts
 
index 0b69864751d83d8281c27514cc94ea1ca9b3e543..b12ffa5d872e8334118ec3bf465d0d19507e8182 100644 (file)
@@ -257,5 +257,18 @@ class AuditTest(unittest.TestCase):
             self.fail(stderr)
 
 
+    def test_sys_monitoring_register_callback(self):
+        returncode, events, stderr = self.run_python("test_sys_monitoring_register_callback")
+        if returncode:
+            self.fail(stderr)
+
+        if support.verbose:
+            print(*events, sep='\n')
+        actual = [(ev[0], ev[2]) for ev in events]
+        expected = [("sys.monitoring.register_callback", "(None,)")]
+
+        self.assertEqual(actual, expected)
+
+
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Security/2023-08-05-03-51-05.gh-issue-107774.VPjaTR.rst b/Misc/NEWS.d/next/Security/2023-08-05-03-51-05.gh-issue-107774.VPjaTR.rst
new file mode 100644 (file)
index 0000000..b89b50c
--- /dev/null
@@ -0,0 +1,3 @@
+PEP 669 specifies that ``sys.monitoring.register_callback`` will generate an
+audit event. Pre-releases of Python 3.12 did not generate the audit event.
+This is now fixed.
index 65ea7902a80a60e9406d1a4e045f10498ef7ba70..64684ad522f687b4aecb96268763c9faac1ac549 100644 (file)
@@ -1851,6 +1851,9 @@ monitoring_register_callback_impl(PyObject *module, int tool_id, int event,
         PyErr_Format(PyExc_ValueError, "invalid event %d", event);
         return NULL;
     }
+    if (PySys_Audit("sys.monitoring.register_callback", "O", func) < 0) {
+        return NULL;
+    }
     if (func == Py_None) {
         func = NULL;
     }