Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(user-interviews): authenticate Vapi webhook via X-Vapi-Secret (#58724)

Paul D'Ambraa9e7324b63b1
diff --git a/products/user_interviews/backend/test_interview_sharing.py b/products/user_interviews/backend/test_interview_sharing.py
index 7a1475c10257..502420e6cf1f 100644
--- a/products/user_interviews/backend/test_interview_sharing.py
+++ b/products/user_interviews/backend/test_interview_sharing.py
@@ -1,6 +1,4 @@
-import hmac
import json
-import hashlib
import datetime
from typing import Any
@@ -573,13 +571,11 @@ class TestVapiWebhook(APIBaseTest):
}
def _signed_post(self, secret: str, payload: dict) -> Any:
- body = json.dumps(payload)
- signature = hmac.new(secret.encode(), body.encode(), hashlib.sha256).hexdigest()
return self.client.post(
"/api/user_interviews/vapi_webhook/",
- data=body,
+ data=json.dumps(payload),
content_type="application/json",
- HTTP_X_VAPI_SIGNATURE=signature,
+ HTTP_X_VAPI_SECRET=secret,
)
@override_settings(VAPI_WEBHOOK_SECRET="topsecret")
@@ -613,16 +609,25 @@ class TestVapiWebhook(APIBaseTest):
response = self._signed_post("topsecret", self._end_of_call_payload("does-not-exist"))
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
+ @parameterized.expand(
+ [
+ ("wrong_secret", "wrong"),
+ ("empty_secret", ""),
+ ("missing_header", None),
+ ]
+ )
@override_settings(VAPI_WEBHOOK_SECRET="topsecret")
- def test_webhook_requires_valid_signature(self):
+ def test_webhook_rejects_bad_or_missing_secret(self, _label: str, header_value: str | None):
share = self._create_share()
self.client.logout()
- response = self.client.post(
- "/api/user_interviews/vapi_webhook/",
- data=self._end_of_call_payload(share.access_token),
- content_type="application/json",
- HTTP_X_VAPI_SIGNATURE="wrong",
- )
+ body = json.dumps(self._end_of_call_payload(share.access_token))
+ url = "/api/user_interviews/vapi_webhook/"
+ if header_value is None:
+ response = self.client.post(url, data=body, content_type="application/json")
+ else:
+ response = self.client.post(
+ url, data=body, content_type="application/json", HTTP_X_VAPI_SECRET=header_value
+ )
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
@override_settings(VAPI_WEBHOOK_SECRET="topsecret")
diff --git a/products/user_interviews/backend/webhooks.py b/products/user_interviews/backend/webhooks.py
index 8d857b7e34de..e33855dec154 100644
--- a/products/user_interviews/backend/webhooks.py
+++ b/products/user_interviews/backend/webhooks.py
@@ -12,7 +12,6 @@ Two surfaces live here, both keyed on a SharingConfiguration access token:
import hmac
import json
import string
-import hashlib
from typing import Any
from django.conf import settings
@@ -92,11 +91,12 @@ def _emit_interview_embeddings(interview: UserInterview, topic: UserInterviewTop
)
-def _verify_signature(secret: str, raw_body: bytes, provided_signature: str | None) -> bool:
- if not secret or not provided_signature:
+def _verify_secret(expected_secret: str, provided_secret: str | None) -> bool:
+ """Vapi sends the configured webhook secret verbatim in ``X-Vapi-Secret`` —
+ constant-time compare it against ``VAPI_WEBHOOK_SECRET`` to authenticate."""
+ if not expected_secret or not provided_secret:
return False
- expected = hmac.new(secret.encode(), raw_body, hashlib.sha256).hexdigest()
- return hmac.compare_digest(provided_signature, expected)
+ return hmac.compare_digest(provided_secret, expected_secret)
def _resolve_share(access_token: str) -> SharingConfiguration | None:
@@ -286,7 +286,9 @@ def vapi_webhook(request: Request) -> Response:
Fail-closed: if ``VAPI_WEBHOOK_SECRET`` is not configured we refuse to accept any
request — treating an unconfigured deployment as inert rather than insecure.
- With the secret set, the request body is HMAC-SHA256 verified.
+ With the secret set, the ``X-Vapi-Secret`` header is constant-time compared
+ against it (Vapi's documented webhook auth — the configured token is sent
+ verbatim in that header).
Idempotent: Vapi retries on 5xx and transient errors, so we de-duplicate by
``call.id`` (stored in ``call_metadata.id``). A repeat delivery returns the
@@ -298,19 +300,19 @@ def vapi_webhook(request: Request) -> Response:
{"error": "Vapi webhook secret is not configured on this PostHog instance."},
status=status.HTTP_503_SERVICE_UNAVAILABLE,
)
- provided = request.headers.get("x-vapi-signature") or request.headers.get("X-Vapi-Signature")
+ provided = request.headers.get("X-Vapi-Secret")
logger.info(
"user_interviews_vapi_webhook_received",
header_keys=sorted(request.headers.keys()),
- has_provided_signature=bool(provided),
+ has_provided_secret=bool(provided),
body_bytes=len(request.body),
)
- if not _verify_signature(settings.VAPI_WEBHOOK_SECRET, request.body, provided):
+ if not _verify_secret(settings.VAPI_WEBHOOK_SECRET, provided):
logger.warning(
- "user_interviews_vapi_webhook_signature_failed",
- has_provided_signature=bool(provided),
+ "user_interviews_vapi_webhook_auth_failed",
+ has_provided_secret=bool(provided),
)
- return Response({"error": "invalid signature"}, status=status.HTTP_401_UNAUTHORIZED)
+ return Response({"error": "invalid secret"}, status=status.HTTP_401_UNAUTHORIZED)
payload = request.data if isinstance(request.data, dict) else {}
message: dict[str, Any] = payload.get("message", {})