From 2541de74de69c88d1414c65587d7097a26fc81d2 Mon Sep 17 00:00:00 2001 From: Nikolai Kondrashov Date: Fri, 31 May 2024 12:51:52 +0300 Subject: [PATCH] mq: Discard messages with strings containing NUL Discard messages with strings containing NUL characters in the kcidb.mq.IOSubscriber. This will help us clear a submission queue containing a number of such messages at the moment, but should be done inside the I/O JSON schema instead. See https://github.com/kernelci/kcidb-io/issues/79 --- kcidb/mq/__init__.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/kcidb/mq/__init__.py b/kcidb/mq/__init__.py index 2e76b13c..b102c82a 100644 --- a/kcidb/mq/__init__.py +++ b/kcidb/mq/__init__.py @@ -19,6 +19,10 @@ from kcidb.misc import LIGHT_ASSERTS +# TODO: Break down the module along data type lines, but meanwhile: +# Dinna fash yersel', pylint: disable=too-many-lines + + # Module's logger LOGGER = logging.getLogger(__name__) @@ -440,9 +444,26 @@ def decode_data(self, message_data): Raises: An exception in case data decoding failed. """ + # TODO move the check to the I/O schema + def reject_nul_chars(data): + """Raise an Exception if a string in data contains NUL""" + if isinstance(data, dict): + for key, value in data.items(): + reject_nul_chars(key) + reject_nul_chars(value) + elif isinstance(data, list): + for value in data: + reject_nul_chars(value) + elif isinstance(data, str): + if '\0' in data: + raise Exception(f"NUL character in string {data!r}") + return data + return self.schema.upgrade( self.schema.validate( - super().decode_data(message_data) + reject_nul_chars( + super().decode_data(message_data) + ) ) )