Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Compare legacy deploy webhook tokens safely

Juan Denis752fb839191d
diff --git a/backend/app/services/git_service.py b/backend/app/services/git_service.py
index 77a948ce..807f0447 100644
--- a/backend/app/services/git_service.py
+++ b/backend/app/services/git_service.py
@@ -342,8 +342,11 @@ class GitService:
if not app_config:
return False
- # Simple token verification
- if token != app_config.get('webhook_token'):
+ # Constant-time, and never a match when either side is missing —
+ # `None != None` let a tokenless request through for a tokenless app.
+ expected = app_config.get('webhook_token')
+ if not (isinstance(token, str) and isinstance(expected, str) and token and expected
+ and hmac.compare_digest(token.encode(), expected.encode())):
return False
# If signature provided, verify based on provider
diff --git a/backend/tests/test_git_webhook_token.py b/backend/tests/test_git_webhook_token.py
new file mode 100644
index 00000000..bea487f0
--- /dev/null
+++ b/backend/tests/test_git_webhook_token.py
@@ -0,0 +1,20 @@
+"""The legacy deploy webhook token must match exactly, in constant time,
+and a missing token must never match a missing token."""
+import pytest
+
+from app.services.git_service import GitService
+
+
[email protected]('configured,sent,ok', [
+ ('tok-123', 'tok-123', True),
+ ('tok-123', 'tok-124', False),
+ ('tok-123', '', False),
+ ('tok-123', None, False),
+ (None, None, False),
+ ('', '', False),
+ (None, '', False),
+])
+def test_token_check(monkeypatch, configured, sent, ok):
+ monkeypatch.setattr(GitService, 'get_app_config',
+ classmethod(lambda cls, app_id: {'webhook_token': configured}))
+ assert GitService.verify_webhook(1, sent) is ok