Compare commits

...

2 commits

Author SHA1 Message Date
Théophile Bastian 11e5507ec9 Fifo: write line with \n
This is needed for eg. `read` to trigger
2022-04-17 18:39:23 +02:00
Théophile Bastian ff8a91599a Don't use abort for cleaner errors 2022-04-17 18:30:38 +02:00

View file

@ -3,7 +3,7 @@ import hmac
import hashlib import hashlib
import json import json
from collections import defaultdict from collections import defaultdict
from flask import Flask, request, abort from flask import Flask, request
from .conf import Configuration from .conf import Configuration
config = Configuration() config = Configuration()
@ -19,12 +19,12 @@ def webhook_receiver(hook_type):
def inner(func): def inner(func):
def wrapped(hook_name: str): def wrapped(hook_name: str):
if request.content_length is None or request.content_length > 32000: if request.content_length is None or request.content_length > 32000:
abort(400) return "Too much content", 400
if request.content_type != "application/json": if request.content_type != "application/json":
return "Expected json", 415 return "Expected json", 415
if hook_name not in relevant_hooks: if hook_name not in relevant_hooks:
abort(404) return "No such hook", 404
hook_conf = relevant_hooks[hook_name] hook_conf = relevant_hooks[hook_name]
raw_payload: bytes = request.get_data(cache=False) raw_payload: bytes = request.get_data(cache=False)
@ -33,7 +33,7 @@ def webhook_receiver(hook_type):
hook_conf["secret"].encode("utf-8"), raw_payload, hashlib.sha256 hook_conf["secret"].encode("utf-8"), raw_payload, hashlib.sha256
).hexdigest() ).hexdigest()
if not hmac.compare_digest(provided_sig, computed_sig): if not hmac.compare_digest(provided_sig, computed_sig):
abort(403) return "Bad credentials", 403
try: try:
payload = json.loads(raw_payload) payload = json.loads(raw_payload)
@ -59,7 +59,7 @@ def fifo_hooks(payload, hook_name, hook_conf):
"""Fifo web handler -- write 1 to a unix fifo""" """Fifo web handler -- write 1 to a unix fifo"""
try: try:
with open(hook_conf["fifo_path"], "w") as fifo: with open(hook_conf["fifo_path"], "w") as fifo:
fifo.write("1") fifo.write("1\n")
except FileNotFoundError: except FileNotFoundError:
return "No such fifo", 500 return "No such fifo", 500
except PermissionError: except PermissionError: