From bcb3b8cdfbb048677825e87a4a20aa02c7d16eae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Sun, 9 Apr 2017 00:02:02 +0200 Subject: [PATCH] Update validation * Allow strict validation * Add workaround to avoid all JSON dumps from MISP <=2.4.70 to fail --- pymisp/data/schema-lax.json | 4 ++-- pymisp/data/schema.json | 2 +- pymisp/mispevent.py | 17 ++++++++++------- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/pymisp/data/schema-lax.json b/pymisp/data/schema-lax.json index 35d38a7..3eeac9c 100644 --- a/pymisp/data/schema-lax.json +++ b/pymisp/data/schema-lax.json @@ -1,7 +1,7 @@ { "$schema": "http://json-schema.org/draft-04/schema#", "title": "Validator for misp events", - "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json", + "id": "https://github.com/MISP/MISP/blob/2.4/format/2.4/schema-lax.json", "defs": { "org": { "type": "object", @@ -32,7 +32,7 @@ } }, "sharing_group": { - "type": "object", + "type": ["object", "array"], "properties": { "id": { "type": "string" diff --git a/pymisp/data/schema.json b/pymisp/data/schema.json index 8b2460b..7b1bcd4 100644 --- a/pymisp/data/schema.json +++ b/pymisp/data/schema.json @@ -1,7 +1,7 @@ { "$schema": "http://json-schema.org/draft-04/schema#", "title": "Validator for misp events", - "id": "https://www.github.com/MISP/MISP/format/2.4/schema.json", + "id": "https://github.com/MISP/MISP/blob/2.4/format/2.4/schema.json", "defs": { "org": { "type": "object", diff --git a/pymisp/mispevent.py b/pymisp/mispevent.py index 9f0cfc6..c831984 100644 --- a/pymisp/mispevent.py +++ b/pymisp/mispevent.py @@ -277,14 +277,17 @@ def _int_to_str(d): class MISPEvent(object): - def __init__(self, describe_types=None): + def __init__(self, describe_types=None, strict_validation=False): self.ressources_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data') - with open(os.path.join(self.ressources_path, 'schema.json'), 'r') as f: - self.json_schema = json.load(f) - with open(os.path.join(self.ressources_path, 'schema-lax.json'), 'r') as f: - self.json_schema_lax = json.load(f) + if strict_validation: + with open(os.path.join(self.ressources_path, 'schema.json'), 'r') as f: + self.json_schema = json.load(f) + else: + with open(os.path.join(self.ressources_path, 'schema-lax.json'), 'r') as f: + self.json_schema = json.load(f) if not describe_types: - t = json.load(open(os.path.join(self.ressources_path, 'describeTypes.json'), 'r')) + with open(os.path.join(self.ressources_path, 'describeTypes.json'), 'r') as f: + t = json.load(f) describe_types = t['result'] self.describe_types = describe_types self.categories = describe_types['categories'] @@ -406,7 +409,7 @@ class MISPEvent(object): # Invalid event created by MISP up to 2.4.52 (attribute_count is none instead of '0') if event.get('Event') and event.get('Event').get('attribute_count') is None: event['Event']['attribute_count'] = '0' - jsonschema.validate(event, self.json_schema_lax) + jsonschema.validate(event, self.json_schema) e = event.get('Event') self._reinitialize_event() self.set_all_values(**e)