From 443985f3f765cdbff76d389541a485feea34e667 Mon Sep 17 00:00:00 2001 From: Quentin Kaiser Date: Tue, 25 Apr 2023 12:25:51 +0200 Subject: [PATCH] fix(metadata): add metadata validator. --- unblob/models.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/unblob/models.py b/unblob/models.py index c66c507229..011e5ac608 100644 --- a/unblob/models.py +++ b/unblob/models.py @@ -33,6 +33,13 @@ def metadata_converter(obj: Union[Dict, Instance]) -> dict: raise ValueError("Can only convert dict or Instance") +def metadata_validator(instance, attribute, value): + if attribute.name == "metadata" and isinstance(instance, Chunk): + for k, _ in value.items(): + if not isinstance(k, str): + raise TypeError("metadata keys must be string") + + @attr.define(frozen=True) class Task: path: Path @@ -100,7 +107,9 @@ class ValidChunk(Chunk): handler: "Handler" = attr.ib(init=False, eq=False) is_encrypted: bool = attr.ib(default=False) - metadata: dict = attr.ib(factory=dict, converter=metadata_converter) + metadata: dict = attr.ib( + factory=dict, converter=metadata_converter, validator=metadata_validator + ) def extract(self, inpath: Path, outdir: Path): if self.is_encrypted: