Skip to content

Commit cc5fa3d

Browse files
authored
Merge pull request #242 from gm3dmo/hk1
Add status and optional to hook test"
2 parents a61ae1c + 4319661 commit cc5fa3d

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

hooky-secret-validation.py

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
"""
32
A simple Flask app that will act as a endpoint for a hook.
43
@@ -11,16 +10,18 @@
1110
1211
"""
1312

13+
1414
import os
1515
import argparse
1616
import sys
1717
import json
1818
import string
1919
import time
20-
from flask import Flask, request
20+
from flask import Flask, request, abort
2121
import hashlib
2222
import hmac
23-
#from werkzeug.exceptions import HTTPException # Import HTTPException
23+
from werkzeug.exceptions import HTTPException # Add this import
24+
2425

2526
def verify_signature(payload_body, secret_token, signature_header):
2627
"""
@@ -36,15 +37,15 @@ def verify_signature(payload_body, secret_token, signature_header):
3637
signature_header: header received from GitHub (x-hub-signature-256)
3738
"""
3839
if not signature_header:
39-
raise HTTPException(status_code=403, detail="x-hub-signature-256 header is missing!")
40+
abort(403, description="x-hub-signature-256 header is missing!")
4041
hash_object = hmac.new(secret_token.encode('utf-8'), msg=payload_body, digestmod=hashlib.sha256)
4142
expected_signature = "sha256=" + hash_object.hexdigest()
4243
if not hmac.compare_digest(expected_signature, signature_header):
43-
raise HTTPException(status_code=403, detail="Request signature didn't match signature on record")
44+
abort(403, description="Request signature didn't match signature on record")
4445
else:
45-
print("---------------------")
46-
print("the webhook signature matches" )
47-
print("---------------------")
46+
app.logger.debug("-" * 21)
47+
app.logger.debug("the webhook signature matches")
48+
app.logger.debug("-" * 21)
4849

4950

5051
app = Flask(__name__)
@@ -53,18 +54,22 @@ def verify_signature(payload_body, secret_token, signature_header):
5354
@app.route('/webhook', methods=['POST'])
5455
def slurphook():
5556
if request.method == 'POST':
56-
print("hook triggered")
57-
print("---------------------")
58-
print("X-Hub-Signature-256:", request.headers.get('X-Hub-Signature-256'))
57+
app.logger.debug("hook triggered")
58+
app.logger.debug("-" * 21)
59+
5960
signature_header = request.headers.get('X-Hub-Signature-256')
60-
print("---------------------")
61-
print("Headers:", request.headers) # Print the headers
62-
print("---------------------")
63-
#print("Payload body:", request.data.decode('utf-8')) # Print the payload body
64-
print("JSON payload:\n\n", json.dumps(request.json, indent=4)) # Print the JSON payload if available
65-
verify_signature(request.data, args.hook_secret, signature_header)
66-
#verify_signature(request.data.decode('utf-8'), "bangersandmash", signature_header)
67-
return ('status',200)
61+
app.logger.debug(f"X-Hub-Signature-256: {signature_header}")
62+
app.logger.debug("-" * 21)
63+
app.logger.debug(f"Headers: {request.headers}")
64+
app.logger.debug("-" * 21)
65+
app.logger.debug(f"JSON payload:\n\n{json.dumps(request.json, indent=4)}")
66+
67+
if signature_header and args.hook_secret:
68+
verify_signature(request.data, args.hook_secret, signature_header)
69+
else:
70+
app.logger.debug("Skipping signature verification - no signature header or secret provided")
71+
72+
return ('status', args.status_code)
6873

6974

7075
if __name__ == '__main__':
@@ -75,10 +80,18 @@ def slurphook():
7580
"--secret",
7681
action="store",
7782
dest="hook_secret",
78-
default=False,
83+
default=None,
7984
help="The secret for the webhook",
8085
)
8186

87+
parser.add_argument(
88+
"--status-code",
89+
action="store",
90+
dest="status_code",
91+
default=200,
92+
help="The response code the webhook will return",
93+
)
94+
8295
args = parser.parse_args()
8396

8497
app.config['DEBUG'] = True

0 commit comments

Comments
 (0)