From 887dc95e3d04338fd8ee21071ac745fe8adeb1b0 Mon Sep 17 00:00:00 2001 From: Guilhem Saurel Date: Mon, 8 Jun 2020 09:59:51 +0200 Subject: [PATCH] handle ill-formed JSON --- matrix_webhook.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/matrix_webhook.py b/matrix_webhook.py index eb4e06c..0374774 100755 --- a/matrix_webhook.py +++ b/matrix_webhook.py @@ -32,8 +32,12 @@ async def handler(request): This one handles a POST, checks its content, and forwards it to the matrix room. """ data = await request.read() - data = json.loads(data.decode()) - status, ret = HTTPStatus.BAD_REQUEST, 'I need a json dict with text & key' + try: + data = json.loads(data.decode()) + status, ret = HTTPStatus.BAD_REQUEST, 'I need a json dict with text & key' + except json.decoder.JSONDecodeError: + data = {} + status, ret = HTTPStatus.BAD_REQUEST, 'This was not valid JSON' if all(key in data for key in ['text', 'key']): status, ret = HTTPStatus.UNAUTHORIZED, 'I need the good "key"' if data['key'] == API_KEY: