From e2ddb48f18c37acca6de315e68f7262b5e8b381e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Tue, 7 Aug 2018 16:52:50 +0200 Subject: [PATCH] chg: Open all json files as bytes before loading in json --- pymisp/api.py | 16 ++++++++-------- pymisp/mispevent.py | 12 ++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/pymisp/api.py b/pymisp/api.py index 7fa9245..f902ef2 100644 --- a/pymisp/api.py +++ b/pymisp/api.py @@ -126,7 +126,7 @@ class PyMISP(object): return self._check_response(response) def get_local_describe_types(self): - with open(os.path.join(self.resources_path, 'describeTypes.json'), 'r') as f: + with open(os.path.join(self.resources_path, 'describeTypes.json'), 'rb') as f: describe_types = json.load(f) return describe_types['result'] @@ -1390,7 +1390,7 @@ class PyMISP(object): def sighting_per_json(self, json_file): """Push a sighting (JSON file)""" - with open(json_file, 'r') as f: + with open(json_file, 'rb') as f: jdata = json.load(f) return self.set_sightings(jdata) @@ -1460,7 +1460,7 @@ class PyMISP(object): return self._rest_add('admin/users', new_user) def add_user_json(self, json_file): - with open(json_file, 'r') as f: + with open(json_file, 'rb') as f: jdata = json.load(f) new_user = MISPUser() new_user.from_dict(**jdata) @@ -1475,7 +1475,7 @@ class PyMISP(object): return self._rest_edit('admin/users', edit_user, user_id) def edit_user_json(self, json_file, user_id): - with open(json_file, 'r') as f: + with open(json_file, 'rb') as f: jdata = json.load(f) new_user = MISPUser() new_user.from_dict(**jdata) @@ -1505,7 +1505,7 @@ class PyMISP(object): return self._rest_add('admin/organisations', new_org) def add_organisation_json(self, json_file): - with open(json_file, 'r') as f: + with open(json_file, 'rb') as f: jdata = json.load(f) new_org = MISPOrganisation() new_org.from_dict(**jdata) @@ -1520,7 +1520,7 @@ class PyMISP(object): return self._rest_edit('admin/organisations', edit_org, org_id) def edit_organisation_json(self, json_file, org_id): - with open(json_file, 'r') as f: + with open(json_file, 'rb') as f: jdata = json.load(f) edit_org = MISPOrganisation() edit_org.from_dict(**jdata) @@ -1594,7 +1594,7 @@ class PyMISP(object): return self._check_response(response) def add_server_json(self, json_file): - with open(json_file, 'r') as f: + with open(json_file, 'rb') as f: jdata = json.load(f) url = urljoin(self.root_url, 'servers/add') response = self.__prepare_request('POST', url, json.dumps(jdata)) @@ -1611,7 +1611,7 @@ class PyMISP(object): return self._check_response(response) def edit_server_json(self, json_file, server_id): - with open(json_file, 'r') as f: + with open(json_file, 'rb') as f: jdata = json.load(f) url = urljoin(self.root_url, 'servers/edit/{}'.format(server_id)) response = self.__prepare_request('POST', url, json.dumps(jdata)) diff --git a/pymisp/mispevent.py b/pymisp/mispevent.py index 1b665c8..3199613 100644 --- a/pymisp/mispevent.py +++ b/pymisp/mispevent.py @@ -93,7 +93,7 @@ class MISPAttribute(AbstractMISP): super(MISPAttribute, self).__init__() if not describe_types: ressources_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data') - with open(os.path.join(ressources_path, 'describeTypes.json'), 'r') as f: + with open(os.path.join(ressources_path, 'describeTypes.json'), 'rb') as f: t = json.load(f) describe_types = t['result'] self.__categories = describe_types['categories'] @@ -351,13 +351,13 @@ class MISPEvent(AbstractMISP): super(MISPEvent, self).__init__() ressources_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data') if strict_validation: - with open(os.path.join(ressources_path, 'schema.json'), 'r') as f: + with open(os.path.join(ressources_path, 'schema.json'), 'rb') as f: self.__json_schema = json.load(f) else: - with open(os.path.join(ressources_path, 'schema-lax.json'), 'r') as f: + with open(os.path.join(ressources_path, 'schema-lax.json'), 'rb') as f: self.__json_schema = json.load(f) if not describe_types: - with open(os.path.join(ressources_path, 'describeTypes.json'), 'r') as f: + with open(os.path.join(ressources_path, 'describeTypes.json'), 'rb') as f: t = json.load(f) describe_types = t['result'] @@ -427,7 +427,7 @@ class MISPEvent(AbstractMISP): """Load a JSON dump from a file on the disk""" if not os.path.exists(event_path): raise PyMISPError('Invalid path, unable to load the event.') - with open(event_path, 'r') as f: + with open(event_path, 'rb') as f: self.load(f) def load(self, json_event): @@ -897,7 +897,7 @@ class MISPObject(AbstractMISP): else: self._known_template = False if self._known_template: - with open(template_path, 'r') as f: + with open(template_path, 'rb') as f: self._definition = json.load(f) setattr(self, 'meta-category', self._definition['meta-category']) self.template_uuid = self._definition['uuid']