Core: Improve speed to JSONSchema validation

This commit is contained in:
Mike Auty
2025-01-09 17:12:27 +00:00
parent a98a23fa41
commit 0860441c2f
3 changed files with 15 additions and 6 deletions
+1 -2
View File
@@ -33,10 +33,9 @@ cloud = [
dev = [
"volatility3[full,cloud]",
"jsonschema>=4.23.0,<5",
"fastjsonschema>=2.21.1,<3",
"pyinstaller>=6.11.0,<7",
"pyinstaller-hooks-contrib>=2024.9",
"types-jsonschema>=4.23.0,<5",
]
test = [
+1 -1
View File
@@ -97,7 +97,7 @@ class IsfInfo(plugins.PluginInterface):
if filter_item in isf_file:
filtered_list.append(isf_file)
if find_spec("jsonschema") and self.config["validate"]:
if find_spec("fastjsonschema") and self.config["validate"]:
def check_valid(data):
return "True" if schemas.validate(data, True) else "False"
+13 -3
View File
@@ -14,6 +14,8 @@ vollog = logging.getLogger(__name__)
cached_validation_filepath = os.path.join(constants.CACHE_PATH, "valid_isf.hashcache")
validators = {}
def load_cached_validations() -> Set[str]:
"""Loads up the list of successfully cached json objects, so we don't need
@@ -92,7 +94,12 @@ def valid(
if input_hash in cached_validations and use_cache:
return True
try:
import jsonschema
import fastjsonschema
schema_key = json.dumps(schema, sort_keys=True)
if schema_key not in validators:
validator = fastjsonschema.compile(schema)
validators[schema_key] = validator
except ImportError:
vollog.info("Dependency for validation unavailable: jsonschema")
vollog.debug("All validations will report success, even with malformed input")
@@ -100,10 +107,13 @@ def valid(
try:
vollog.debug("Validating JSON against schema...")
jsonschema.validate(input, schema)
validators[schema_key](input)
import pdb
pdb.set_trace()
cached_validations.add(input_hash)
vollog.debug("JSON validated against schema (result cached)")
except jsonschema.exceptions.SchemaError:
except fastjsonschema.JsonSchemaValueException:
vollog.debug("Schema validation error", exc_info=True)
return False