Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify signature based on header #2

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions gitea_matrix/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@

from pprint import pprint

import json
import hmac


class GiteaBot(Plugin):
task_list: List[Task]
Expand Down Expand Up @@ -113,10 +116,16 @@ async def process_hook_01(self, req: Request) -> None:

try:
msg = None
body = await req.json()

if body["secret"] != self.config["webhook-secret"]:
self.log.error("Failed to handle Gitea event: secret doesnt match.")
raw = await req.content.read() # We need the raw bytes
charset = req.charset or 'utf-8'
body = json.loads(raw.decode(encoding=charset))

signature = req.headers['X-Gitea-Signature']
payload_signature = hmac.new(self.config["webhook-secret"].encode('utf-8'), raw, digestmod="sha256")

if not hmac.compare_digest(signature, payload_signature.hexdigest()):
self.log.error("Failed to handle Gitea event: Could not verify signature.")
else:
event = req.headers["X-Gitea-Event"]
if event == 'push':
Expand Down