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(auth): reject ambiguous conversation widget tokens (#108161)

Paul D'Ambraf3b2d2ec4055
diff --git a/posthog/api/test/test_authentication.py b/posthog/api/test/test_authentication.py
index 99322fba51fb..80fdcfd23ad3 100644
--- a/posthog/api/test/test_authentication.py
+++ b/posthog/api/test/test_authentication.py
@@ -47,6 +47,7 @@ from posthog.auth import (
SessionAuthentication,
TeamSecretTokenAuthentication,
TeamSecretTokenUser,
+ WidgetAuthentication,
_extract_phs_token,
)
from posthog.clickhouse.query_tagging import AccessMethod
@@ -2541,6 +2542,15 @@ class TestTeamSecretTokenAuthentication(APIBaseTest):
self.assertEqual(user.team, self.team)
+class TestWidgetAuthentication(SimpleTestCase):
+ @parameterized.expand([(Team.DoesNotExist,), (Team.MultipleObjectsReturned,)])
+ def test_invalid_token_fails_authentication(self, lookup_error: type[Exception]) -> None:
+ request = Request(APIRequestFactory().get("/", HTTP_X_CONVERSATIONS_TOKEN="test-widget-token"))
+ with patch("posthog.models.Team.objects.get", side_effect=lookup_error):
+ with self.assertRaises(AuthenticationFailed):
+ WidgetAuthentication().authenticate(request)
+
+
class TestSyntheticUser(SimpleTestCase):
def _team(self, team_id=42):
return type("FakeTeam", (), {"id": team_id})()
diff --git a/posthog/auth.py b/posthog/auth.py
index e0a07d991636..7dcdca9397a0 100644
--- a/posthog/auth.py
+++ b/posthog/auth.py
@@ -1201,7 +1201,7 @@ class WidgetAuthentication(ActivityCredentialMixin, authentication.BaseAuthentic
try:
Team = apps.get_model(app_label="posthog", model_name="Team")
team = Team.objects.get(conversations_settings__widget_public_token=token, conversations_enabled=True)
- except Team.DoesNotExist:
+ except (Team.DoesNotExist, Team.MultipleObjectsReturned):
raise AuthenticationFailed("Invalid token or conversations not enabled")
self.record_activity_actor(None)