: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
fix(oauth): handle `prompt=none` openid requests without 500 (#60996)
Rafael Audibertce861e9bbe7c
diff --git a/posthog/api/oauth/test_views.py b/posthog/api/oauth/test_views.pyindex d1ca97a89fb7..8752a2d67fde 100644--- a/posthog/api/oauth/test_views.py+++ b/posthog/api/oauth/test_views.py@@ -1,6 +1,7 @@import base64import hashlibfrom datetime import timedelta+from types import SimpleNamespacefrom typing import Optional, castfrom urllib.parse import parse_qs, quote, urlencode, urlparse, urlunparse@@ -21,6 +22,7 @@ from parameterized import parameterizedfrom rest_framework import statusfrom posthog.api.oauth import OAuthAuthorizationSerializer+from posthog.api.oauth.views import OAuthValidatorfrom posthog.models.oauth import (OAuthAccessToken,OAuthApplication,@@ -338,6 +340,32 @@ class TestOAuthAPI(APIBaseTest):redirect_to = response.json()["redirect_to"]self.assertEqual(redirect_to, "https://example.com/callback?error=access_denied")+ def test_authorize_with_prompt_none_openid_does_not_500(self):+ # Regression: a missing `validate_silent_login` override on OAuthValidator caused+ # oauthlib to raise NotImplementedError -> 500 whenever an OIDC client sent+ # `prompt=none` with the `openid` scope. Any well-formed response (consent page,+ # spec-compliant error redirect, or successful auth) is acceptable here — the+ # point is the request must not crash.+ url = f"{self.base_authorization_url}&scope=openid&prompt=none"++ response = self.client.get(url)++ self.assertLess(response.status_code, 500)++ def test_validate_silent_login_returns_true_for_authenticated_user(self):+ # The @login_required decorator on OAuthAuthorizationView intercepts unauthenticated+ # requests before validate_silent_login runs, so the validator's False branch can't+ # be exercised through the HTTP flow — call it directly instead.+ self.assertTrue(OAuthValidator().validate_silent_login(SimpleNamespace(user=self.user)))++ def test_validate_silent_login_returns_false_for_unauthenticated_request(self):+ unauthenticated = SimpleNamespace(user=SimpleNamespace(is_authenticated=False))+ self.assertFalse(OAuthValidator().validate_silent_login(unauthenticated))++ # request object with no `user` attribute at all+ self.assertFalse(OAuthValidator().validate_silent_login(SimpleNamespace()))+ self.assertFalse(OAuthValidator().validate_silent_login(SimpleNamespace(user=None)))+def test_cannot_get_token_with_invalid_code(self):data = {"grant_type": "authorization_code",diff --git a/posthog/api/oauth/views.py b/posthog/api/oauth/views.pyindex 8acacec3dc53..eea1b339bafe 100644--- a/posthog/api/oauth/views.py+++ b/posthog/api/oauth/views.py@@ -305,6 +305,14 @@ class OAuthValidator(OAuth2Validator):request.client = appreturn request.client+ def validate_silent_login(self, request) -> bool:+ # Called by oauthlib's OIDC validator for `prompt=none` openid requests. Neither+ # the base class nor django-oauth-toolkit implements this, so the default raises+ # NotImplementedError. Returning False here lets oauthlib emit `login_required`+ # to the client instead of crashing with a 500.+ user = getattr(request, "user", None)+ return bool(user and getattr(user, "is_authenticated", False))+def validate_client_id(self, client_id, request, *args, **kwargs):"""Validate client_id, supporting CIMD URL-form client_ids.