|
| 1 | +from logging import getLogger |
| 2 | +import hmac |
| 3 | + |
| 4 | +from flask import request |
| 5 | +from werkzeug.exceptions import Forbidden, BadRequest |
| 6 | + |
| 7 | +from .worker import event_queue |
| 8 | + |
| 9 | +logger = getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +def _schedule_data_from_request(): |
| 13 | + """Given an ongoing request, schedule it to the worker. |
| 14 | +
|
| 15 | + This can be called from an ongoing request, and it will get the data (the |
| 16 | + JSON payload) and put it in the worker event_queue for later processing. |
| 17 | + """ |
| 18 | + data = request.get_json(force=True, cache=False) |
| 19 | + if not data: |
| 20 | + raise BadRequest("Unconsistent JSON received") |
| 21 | + event_queue.put(data) |
| 22 | + |
| 23 | + |
| 24 | +def webhook_github(secret): |
| 25 | + """GitHub implementation of the webhook (requires secret).""" |
| 26 | + logger.debug("Received a POST from: %s", |
| 27 | + request.remote_addr) |
| 28 | + |
| 29 | + agent = request.headers.get("User-Agent") |
| 30 | + |
| 31 | + if not agent.startswith("GitHub-Hookshot"): |
| 32 | + raise Forbidden("Only GitHub is allowed to POST to us") |
| 33 | + |
| 34 | + signature = request.headers.get("X-Hub-Signature") |
| 35 | + event_type = request.headers.get("X-GitHub-Event") |
| 36 | + |
| 37 | + if not signature or not event_type: |
| 38 | + raise Forbidden("Webhooks must include the signature " |
| 39 | + "(add `secret` to the webhook settings in GitHub)") |
| 40 | + |
| 41 | + if not secret: |
| 42 | + raise SystemError("No `secret` configured, refusing to accept petition") |
| 43 | + |
| 44 | + try: |
| 45 | + digest = hmac.digest(secret, request.get_data(), "sha1") |
| 46 | + except AttributeError: # Python < 3.7, using older & slower approach |
| 47 | + h = hmac.new(secret, request.get_data(), "sha1") |
| 48 | + digest = h.digest() |
| 49 | + |
| 50 | + # Strip the first five characters: 'sha1=xxxxxxx' and get it in binary form |
| 51 | + sent_digest = bytes.fromhex(signature[5:]) |
| 52 | + if digest != sent_digest: |
| 53 | + logger.debug("Expected signature %s, received %s" % (digest, sent_digest)) |
| 54 | + raise Forbidden("The HMAC digest do not match. You are not allowed.") |
| 55 | + |
| 56 | + if event_type == "ping": |
| 57 | + logger.info("Ignoring ping event") |
| 58 | + else: |
| 59 | + _schedule_data_from_request() |
| 60 | + |
| 61 | + return "" |
| 62 | + |
| 63 | + |
| 64 | +def webhook_gogs(secret): |
| 65 | + """Gogs implementation of the webhook (requires secret).""" |
| 66 | + logger.debug("Received a POST from: %s", |
| 67 | + request.remote_addr) |
| 68 | + |
| 69 | + data = request.get_json(force=True, cache=False) |
| 70 | + if not data: |
| 71 | + raise BadRequest("Unconsistent JSON received") |
| 72 | + |
| 73 | + try: |
| 74 | + if data["secret"] != secret: |
| 75 | + logger.debug("Received secret: %s, which did not match the expected", data["secret"]) |
| 76 | + raise Forbidden("Invalid `secret` provided in HTTP method payload") |
| 77 | + except KeyError: |
| 78 | + raise Forbidden("The request should include the `secret`. " |
| 79 | + "Have you configured correctly the gogs sever webhook?") |
| 80 | + |
| 81 | + event_queue.put(data) |
| 82 | + return "" |
| 83 | + |
| 84 | + |
| 85 | +def webhook_plain(): |
| 86 | + """Trivial implementation.""" |
| 87 | + logger.debug("Received a POST from: %s", |
| 88 | + request.remote_addr) |
| 89 | + |
| 90 | + _schedule_data_from_request() |
| 91 | + |
| 92 | + return "" |
0 commit comments