From 9a316fe84a6e1a29f02a7c8afc0555def131c817 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Sun, 13 Jun 2021 08:18:12 -0400 Subject: [PATCH] database: check for invalid keys It is invalid for a namespace to contain a top level key represented as an empty bytestring. If this is detected, log the result and drop the invalid key. Signed-off-by: Eric Callahan --- moonraker/components/database.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/moonraker/components/database.py b/moonraker/components/database.py index 24428a1..53893e2 100644 --- a/moonraker/components/database.py +++ b/moonraker/components/database.py @@ -316,12 +316,21 @@ class MoonrakerDatabase: f"Invalid database namespace '{namespace}'") db = self.namespaces[namespace] result = {} + invalid_key_result = None with self.lmdb_env.begin(buffers=True, db=db) as txn: cursor = txn.cursor() cursor.first() for db_key, value in cursor: k = bytes(db_key).decode() + if not k: + invalid_key_result = self._decode_value(value) + continue result[k] = self._decode_value(value) + if invalid_key_result: + logging.info(f"Invalid Key found in namespace '{namespace}', " + f"dropping value: {repr(invalid_key_result)}") + with self.lmdb_env.begin(write=True, buffers=True, db=db) as txn: + txn.delete(b"") return result def _encode_value(self, value: DBRecord) -> bytes: