Skip to content
This repository was archived by the owner on Mar 13, 2026. It is now read-only.

Commit 7a29efa

Browse files
committed
Add validate request as flask before_request func
1 parent 919454b commit 7a29efa

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

app.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,25 @@
1212

1313
jobs = dict()
1414

15-
def validate_origin_github() -> bool:
15+
# check all calls are valid
16+
@app.before_request
17+
def validate_origin_github():
18+
invalid = False
1619
userAgent = request.headers.get("User-Agent")
1720
if not userAgent.startswith("GitHub-Hookshot"):
1821
app.logger.warning("User-Agent is {userAgent}")
19-
return False
22+
invalid = True
2023

2124
if request.headers.get("Content-Type") != "application/json":
2225
app.logger.warning("Content is not JSON")
23-
return False
26+
invalid = True
2427

2528
if not request.headers.get(GithubHeaders.EVENT.value):
2629
app.logger.warning("No GitHub Event received!")
27-
return False
30+
invalid = True
2831

29-
return True
32+
if invalid:
33+
return abort(401)
3034

3135

3236
def process_workflow_job():
@@ -75,9 +79,6 @@ def process_workflow_job():
7579

7680
@app.route("/github-webhook", methods=["POST"])
7781
def github_webhook_process():
78-
if not validate_origin_github():
79-
return abort(401)
80-
8182
event = request.headers.get(GithubHeaders.EVENT.value)
8283
command = f"process_{event}"
8384

0 commit comments

Comments
 (0)