: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
revert(user-interviews): revert X-Vapi-Secret webhook auth (#58733)
Paul D'Ambraae7cbae6bab2
diff --git a/products/user_interviews/backend/test_interview_sharing.py b/products/user_interviews/backend/test_interview_sharing.pyindex 502420e6cf1f..7a1475c10257 100644--- a/products/user_interviews/backend/test_interview_sharing.py+++ b/products/user_interviews/backend/test_interview_sharing.py@@ -1,4 +1,6 @@+import hmacimport json+import hashlibimport datetimefrom typing import Any@@ -571,11 +573,13 @@ 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=json.dumps(payload),+ data=body,content_type="application/json",- HTTP_X_VAPI_SECRET=secret,+ HTTP_X_VAPI_SIGNATURE=signature,)@override_settings(VAPI_WEBHOOK_SECRET="topsecret")@@ -609,25 +613,16 @@ 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_rejects_bad_or_missing_secret(self, _label: str, header_value: str | None):+ def test_webhook_requires_valid_signature(self):share = self._create_share()self.client.logout()- 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- )+ 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",+ )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.pyindex e33855dec154..96ec097800b8 100644--- a/products/user_interviews/backend/webhooks.py+++ b/products/user_interviews/backend/webhooks.py@@ -12,6 +12,7 @@ Two surfaces live here, both keyed on a SharingConfiguration access token:import hmacimport jsonimport string+import hashlibfrom typing import Anyfrom django.conf import settings@@ -91,14 +92,6 @@ def _emit_interview_embeddings(interview: UserInterview, topic: UserInterviewTop)-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- return hmac.compare_digest(provided_secret, expected_secret)--def _resolve_share(access_token: str) -> SharingConfiguration | None:"""Resolve a share token to its `SharingConfiguration`, mirroring the filtersused by `SharingViewerPageViewSet.get_object()`:@@ -286,9 +279,7 @@ def vapi_webhook(request: Request) -> Response:Fail-closed: if ``VAPI_WEBHOOK_SECRET`` is not configured we refuse to accept anyrequest — treating an unconfigured deployment as inert rather than insecure.- 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).+ With the secret set, the request body is HMAC-SHA256 verified.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@@ -300,19 +291,28 @@ 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-Secret")+ provided = request.headers.get("x-vapi-signature") or request.headers.get("X-Vapi-Signature")+ expected = (+ hmac.new(settings.VAPI_WEBHOOK_SECRET.encode(), request.body, hashlib.sha256).hexdigest() if provided else None+ )logger.info("user_interviews_vapi_webhook_received",header_keys=sorted(request.headers.keys()),- has_provided_secret=bool(provided),+ has_provided_signature=bool(provided),body_bytes=len(request.body),)- if not _verify_secret(settings.VAPI_WEBHOOK_SECRET, provided):+ if not (provided and expected and hmac.compare_digest(provided, expected)):+ # Log prefixes (first 8 chars of one-way hashes — safe to log; do not leak secrets) to diagnose+ # whether the failure is wrong-secret vs different-body vs case-mismatch.logger.warning(- "user_interviews_vapi_webhook_auth_failed",- has_provided_secret=bool(provided),+ "user_interviews_vapi_webhook_signature_failed",+ has_provided_signature=bool(provided),+ expected_prefix=expected[:8] if expected else None,+ provided_prefix=provided[:8] if provided else None,+ provided_length=len(provided) if provided else 0,+ body_bytes=len(request.body),)- return Response({"error": "invalid secret"}, status=status.HTTP_401_UNAUTHORIZED)+ return Response({"error": "invalid signature"}, status=status.HTTP_401_UNAUTHORIZED)payload = request.data if isinstance(request.data, dict) else {}message: dict[str, Any] = payload.get("message", {})