|
| 1 | +import os |
1 | 2 | from datetime import datetime |
2 | 3 | import json |
3 | 4 | import logging |
| 5 | +import hashlib |
| 6 | +import hmac |
| 7 | +import base64 |
4 | 8 | from flask import Flask, request |
5 | 9 |
|
6 | | -logging.basicConfig( |
7 | | - level=logging.INFO, |
8 | | - handlers=[logging.StreamHandler()] |
9 | | -) |
| 10 | +logging.basicConfig(level=logging.INFO, handlers=[logging.StreamHandler()]) |
10 | 11 |
|
11 | 12 | app = Flask(__name__) |
12 | 13 | app.logger.setLevel(logging.INFO) |
13 | 14 |
|
| 15 | +HOOKDECK_WEBHOOK_SECRET = os.getenv("HOOKDECK_WEBHOOK_SECRET") |
| 16 | + |
| 17 | + |
| 18 | +def verify_webhook(request): |
| 19 | + if HOOKDECK_WEBHOOK_SECRET is None: |
| 20 | + app.logger.warn( |
| 21 | + "No HOOKDECK_WEBHOOK_SECRET found in environment variables. Skipping verification." |
| 22 | + ) |
| 23 | + return False |
| 24 | + |
| 25 | + # Extract x-hookdeck-signature and x-hookdeck-signature-2 headers from the request |
| 26 | + hmac_header = request.headers.get("x-hookdeck-signature") |
| 27 | + hmac_header2 = request.headers.get("x-hookdeck-signature-2") |
| 28 | + |
| 29 | + # Create a hash based on the raw body |
| 30 | + hash = base64.b64encode( |
| 31 | + hmac.new( |
| 32 | + HOOKDECK_WEBHOOK_SECRET.encode(), request.data, hashlib.sha256 |
| 33 | + ).digest() |
| 34 | + ).decode() |
| 35 | + |
| 36 | + # Compare the created hash with the value of the x-hookdeck-signature |
| 37 | + # Also check x-hookdeck-signature-2 header in case the secret was rolled |
| 38 | + return hash == hmac_header or (hmac_header2 and hash == hmac_header2) |
| 39 | + |
14 | 40 |
|
15 | 41 | @app.route("/<path:path>", methods=["POST"]) |
16 | 42 | def handle(path): |
17 | | - app.logger.info("webhook_received %s %s", |
18 | | - datetime.now().isoformat(), |
19 | | - json.dumps(request.json, indent=2)) |
| 43 | + app.logger.info( |
| 44 | + "webhook_received %s %s", |
| 45 | + datetime.now().isoformat(), |
| 46 | + json.dumps(request.json, indent=2), |
| 47 | + ) |
20 | 48 |
|
21 | | - return { |
22 | | - "status": "ACCEPTED" |
23 | | - } |
| 49 | + if not verify_webhook(request): |
| 50 | + return {"status": "UNAUTHORIZED"}, 403 |
| 51 | + else: |
| 52 | + return {"status": "ACCEPTED"}, 200 |
24 | 53 |
|
25 | 54 |
|
26 | 55 | if __name__ == "__main__": |
27 | | - app.run(debug=True, port=3030) |
| 56 | + app.run(debug=True, port=3031) |
0 commit comments