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(devex): refuse a webhook body that is not utf-8 with 401 (#104957)

Julian Bezb99bbeb3da96
diff --git a/posthog/auth.py b/posthog/auth.py
index c441e3fa1033..4dfb5a3565bb 100644
--- a/posthog/auth.py
+++ b/posthog/auth.py
@@ -1576,7 +1576,11 @@ class WebhookSignatureAuthentication(authentication.BaseAuthentication):
raise AuthenticationFailed("Webhook integration not found or disabled.")
django_request = getattr(request, "_request", request)
- raw_body = django_request.body.decode()
+ try:
+ raw_body = django_request.body.decode()
+ except UnicodeDecodeError:
+ # The signed input is text, so a body that is not UTF-8 cannot carry a valid signature.
+ raise AuthenticationFailed("Invalid webhook signature.")
hmac_input = self.build_hmac_input(timestamp, raw_body)
expected = hmac_sha256_signature(signing_secret, hmac_input.encode())
diff --git a/products/messaging/backend/api/test/test_customerio_webhook.py b/products/messaging/backend/api/test/test_customerio_webhook.py
index 56ea13f1c34c..0844a4dbd30c 100644
--- a/products/messaging/backend/api/test/test_customerio_webhook.py
+++ b/products/messaging/backend/api/test/test_customerio_webhook.py
@@ -77,6 +77,16 @@ class TestCustomerIOWebhook(APIBaseTest):
MessageRecipientPreference.objects.filter(team=self.team, identifier="[email protected]").exists()
)
+ def test_body_that_is_not_utf8_rejected(self):
+ sig, ts = self._sign("{}")
+ response = self.client.post(
+ self.url,
+ data=b"\xff\xfe not utf-8",
+ content_type="application/json",
+ headers={"x-cio-signature": sig, "x-cio-timestamp": ts},
+ )
+ self.assertEqual(response.status_code, 401)
+
def test_missing_both_headers_rejected(self):
body = {"metric": "unsubscribed", "data": {"email_address": "[email protected]"}}
response = self.client.post(