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.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
Verify webhook signatures before deduplicating
Juan Denis7c6800a94697
diff --git a/backend/app/services/webhook_gateway_service.py b/backend/app/services/webhook_gateway_service.pyindex 58491f30..a2f421f3 100644--- a/backend/app/services/webhook_gateway_service.py+++ b/backend/app/services/webhook_gateway_service.py@@ -145,15 +145,23 @@ class WebhookGatewayService:if not endpoint:return {'success': False, 'error': 'Endpoint not found'}, 404- event_id = headers.get('X-Webhook-Event-ID') or headers.get('x-webhook-event-id') or str(uuid.uuid4())-- # Avoid duplicate deliveries- if WebhookDelivery.query.filter_by(event_id=event_id).first():- return {'success': True, 'message': 'Duplicate delivery ignored', 'event_id': event_id}, 200+ # Case-insensitive: Werkzeug hands headers over as X-Webhook-Event-Id,+ # which neither of the exact spellings once checked here matched.+ event_id = next((v for k, v in headers.items()+ if k.lower() == 'x-webhook-event-id' and v), None) or str(uuid.uuid4())signature_header, algorithm = cls._extract_signature(headers)signature_valid = endpoint.verify_signature(payload, signature_header, algorithm)+ if not signature_valid:+ # Logged under a fresh id: event_id is unique, so storing an+ # unauthenticated request under the id it claims would let anyone+ # pre-empt that event and get the real delivery dropped as a+ # duplicate. The claimed id stays visible in the saved headers.+ event_id = str(uuid.uuid4())+ elif WebhookDelivery.query.filter_by(event_id=event_id).first():+ return {'success': True, 'message': 'Duplicate delivery ignored', 'event_id': event_id}, 200+delivery = WebhookDelivery(endpoint_id=endpoint.id,event_id=event_id,diff --git a/backend/tests/test_secrets_webhooks_authz.py b/backend/tests/test_secrets_webhooks_authz.pyindex 9d8ba228..2a4a56d9 100644--- a/backend/tests/test_secrets_webhooks_authz.py+++ b/backend/tests/test_secrets_webhooks_authz.py@@ -301,3 +301,43 @@ def test_inbound_webhook_with_a_valid_signature_is_accepted(client, vault_rbac,headers={'X-Hub-Signature-256': _sign('whsec', payload)})assert resp.status_code == 200assert forwarded == [payload]+++def test_unsigned_request_cannot_preempt_an_event_id(client, vault_rbac, monkeypatch):+ """Dedupe ran before the signature check and event_id is unique, so an+ unsigned request claiming the next event's id got the real, signed+ delivery dropped as a 'duplicate'."""+ from app.services.webhook_gateway_service import WebhookGatewayService+ forwarded = []+ monkeypatch.setattr(WebhookGatewayService, '_forward',+ classmethod(lambda cls, e, d, p, h: forwarded.append(p) or+ {'success': True, 'status': 200, 'body': ''}))+ payload = b'{"a": 2}'++ spoof = client.post('/api/v1/webhooks/receive/scope-endpoint',+ data=payload, content_type='application/json',+ headers={'X-Webhook-Event-ID': 'evt-next'})+ assert spoof.status_code == 401++ real = client.post('/api/v1/webhooks/receive/scope-endpoint',+ data=payload, content_type='application/json',+ headers={'X-Webhook-Event-ID': 'evt-next',+ 'X-Hub-Signature-256': _sign('whsec', payload)})+ assert real.status_code == 200+ assert real.get_json()['event_id'] == 'evt-next'+ assert forwarded == [payload]+++def test_signed_redelivery_is_still_deduplicated(client, vault_rbac, monkeypatch):+ from app.services.webhook_gateway_service import WebhookGatewayService+ forwarded = []+ monkeypatch.setattr(WebhookGatewayService, '_forward',+ classmethod(lambda cls, e, d, p, h: forwarded.append(p) or+ {'success': True, 'status': 200, 'body': ''}))+ payload = b'{"a": 3}'+ headers = {'X-Webhook-Event-ID': 'evt-dup', 'X-Hub-Signature-256': _sign('whsec', payload)}+ for _ in range(2):+ resp = client.post('/api/v1/webhooks/receive/scope-endpoint', data=payload,+ content_type='application/json', headers=headers)+ assert resp.status_code == 200+ assert forwarded == [payload]