Skip to content

Commit

Permalink
new: [internal] Avoid double JSON decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubOnderka committed Jan 6, 2024
1 parent 92d7076 commit 9d6eb9c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
14 changes: 10 additions & 4 deletions misp_modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,14 @@ class QueryModule(tornado.web.RequestHandler):
executor = ThreadPoolExecutor(nb_threads)

@run_on_executor
def run_request(self, module_name, json_payload):
log.debug('MISP QueryModule request %s', json_payload)
response = mhandlers[module_name].handler(q=json_payload)
def run_request(self, module_name, json_payload, dict_payload):
log.debug('MISP QueryModule %s request %s', module_name, json_payload)
module = mhandlers[module_name]
if getattr(module, "dict_handler", None):
# New method that avoids double JSON decoding, new modules should define dict_handler
response = module.dict_handler(q=dict_payload)
else:
response = module.handler(q=json_payload)
return json.dumps(response)

@tornado.gen.coroutine
Expand All @@ -201,7 +206,8 @@ def post(self):
timeout = datetime.timedelta(seconds=int(dict_payload.get('timeout')))
else:
timeout = datetime.timedelta(seconds=300)
response = yield tornado.gen.with_timeout(timeout, self.run_request(dict_payload['module'], json_payload))
future = self.run_request(dict_payload['module'], json_payload, dict_payload)
response = yield tornado.gen.with_timeout(timeout, future)
self.write(response)
except tornado.gen.TimeoutError:
log.warning('Timeout on {}'.format(dict_payload['module']))
Expand Down
8 changes: 1 addition & 7 deletions misp_modules/modules/expansion/clamav.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import base64
import io
import json
import logging
import sys
import zipfile
Expand Down Expand Up @@ -58,12 +57,7 @@ def connect_to_clamav(connection_string: str) -> clamd.ClamdNetworkSocket:
raise Exception("ClamAV connection string is invalid. It must be unix socket path with 'unix://' prefix or IP:PORT.")


def handler(q=False):
if q is False:
return False

request = json.loads(q)

def dict_handler(request: dict):
connection_string: str = request["config"].get("connection")
if not connection_string:
return {"error": "No ClamAV connection string provided"}
Expand Down

0 comments on commit 9d6eb9c

Please sign in to comment.