Skip to content

Commit e7d40ca

Browse files
committed
Add auth to release hook.
1 parent b8a7e96 commit e7d40ca

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

mystbin/backend/routers/admin.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import datetime
2222
import subprocess
23+
from hmac import HMAC, compare_digest
24+
from hashlib import sha256
2325
from typing import Dict, Optional, Union
2426

2527
import psutil
@@ -245,7 +247,19 @@ async def get_server_stats(request: MystbinRequest):
245247
@router.get("/admin/release_hook", tags=["admin"], include_in_schema=False)
246248
@limit("admin")
247249
async def release_hook(request: MystbinRequest):
248-
if not request.state.user or not request.state.user["admin"]:
250+
251+
config = pathlib.Path("config.json")
252+
if not config.exists():
253+
config = pathlib.Path("../../config.json")
254+
255+
with open(config) as f:
256+
config: Dict[str, Dict[str, Any]] = ujson.load(f)
257+
258+
SECRET = config['github_secret'].encode()
259+
260+
received_sign = request.headers.get('X-Hub-Signature-256').split('sha256=')[-1].strip()
261+
expected_sign = HMAC(key=SECRET, msg=(await request.data), digestmod=sha256).hexdigest()
262+
if not compare_digest(received_sign, expected_sign):
249263
return UJSONResponse({"error": "Unauthorized"}, status_code=401)
250264

251265
command = 'cd /root/MystBin/; git pull;'

0 commit comments

Comments
 (0)