Update validation

* Allow strict validation
* Add workaround to avoid all JSON dumps from MISP <=2.4.70 to fail
pull/77/head
Raphaël Vinot 2017-04-09 00:02:02 +02:00
parent 7b60a7355c
commit bcb3b8cdfb
3 changed files with 13 additions and 10 deletions

View File

@ -1,7 +1,7 @@
{ {
"$schema": "http://json-schema.org/draft-04/schema#", "$schema": "http://json-schema.org/draft-04/schema#",
"title": "Validator for misp events", "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": { "defs": {
"org": { "org": {
"type": "object", "type": "object",
@ -32,7 +32,7 @@
} }
}, },
"sharing_group": { "sharing_group": {
"type": "object", "type": ["object", "array"],
"properties": { "properties": {
"id": { "id": {
"type": "string" "type": "string"

View File

@ -1,7 +1,7 @@
{ {
"$schema": "http://json-schema.org/draft-04/schema#", "$schema": "http://json-schema.org/draft-04/schema#",
"title": "Validator for misp events", "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": { "defs": {
"org": { "org": {
"type": "object", "type": "object",

View File

@ -277,14 +277,17 @@ def _int_to_str(d):
class MISPEvent(object): 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') 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: if strict_validation:
self.json_schema = json.load(f) with open(os.path.join(self.ressources_path, 'schema.json'), 'r') as f:
with open(os.path.join(self.ressources_path, 'schema-lax.json'), 'r') as f: self.json_schema = json.load(f)
self.json_schema_lax = 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: 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'] describe_types = t['result']
self.describe_types = describe_types self.describe_types = describe_types
self.categories = describe_types['categories'] 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') # 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: if event.get('Event') and event.get('Event').get('attribute_count') is None:
event['Event']['attribute_count'] = '0' event['Event']['attribute_count'] = '0'
jsonschema.validate(event, self.json_schema_lax) jsonschema.validate(event, self.json_schema)
e = event.get('Event') e = event.get('Event')
self._reinitialize_event() self._reinitialize_event()
self.set_all_values(**e) self.set_all_values(**e)