: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): reject `prompt=none` with spec-compliant errors instead of 500 (#61729)
Rafael Audibert186675413295
diff --git a/posthog/api/oauth/test_views.py b/posthog/api/oauth/test_views.pyindex ee4a34cbacc6..1d5a8f20c7c0 100644--- a/posthog/api/oauth/test_views.py+++ b/posthog/api/oauth/test_views.py@@ -340,31 +340,47 @@ 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.+ def test_authorize_with_prompt_none_openid_is_rejected_not_500(self):+ # PostHog does not support OIDC silent authentication: `prompt=none` is always rejected+ # with a spec-compliant error, never a token and never a 500. On the GET validate path+ # oauthlib hasn't attached the user yet, so it rejects at the first gate with+ # `login_required` (a missing validate_silent_login override used to crash here instead).url = f"{self.base_authorization_url}&scope=openid&prompt=none"response = self.client.get(url)self.assertLess(response.status_code, 500)+ self.assertIn("error=login_required", response.headers["Location"])- 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)))+ # 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.+ @parameterized.expand(+ [+ ("authenticated_user", lambda self: SimpleNamespace(user=self.user), True),+ ("unauthenticated_user", lambda self: SimpleNamespace(user=SimpleNamespace(is_authenticated=False)), False),+ ("no_user_attr", lambda self: SimpleNamespace(), False),+ ("none_user", lambda self: SimpleNamespace(user=None), False),+ ]+ )+ def test_validate_silent_login(self, _name, make_request, expected):+ self.assertEqual(OAuthValidator().validate_silent_login(make_request(self)), expected)- 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))+ def test_validate_silent_authorization_always_false(self):+ # PostHog never authorizes silently; prompt=none must fall back to interactive consent.+ self.assertFalse(OAuthValidator().validate_silent_authorization(SimpleNamespace(user=self.user)))- # 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_authorize_post_allow_with_prompt_none_returns_consent_required(self):+ # The authorization-completion path (unlike the GET validate path) attaches the+ # authenticated user, so oauthlib runs validate_silent_authorization here. We reject+ # silent authorization with a spec-compliant consent_required error rather than a 500.+ response = self.client.post(+ "/oauth/authorize/?scope=openid&prompt=none",+ {**self.base_authorization_post_body, "allow": True},+ )++ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertIn("error=consent_required", response.json()["redirect_to"])def test_cannot_get_token_with_invalid_code(self):data = {diff --git a/posthog/api/oauth/views.py b/posthog/api/oauth/views.pyindex 5263957b6ce4..abc16dc4a231 100644--- a/posthog/api/oauth/views.py+++ b/posthog/api/oauth/views.py@@ -305,14 +305,34 @@ class OAuthValidator(OAuth2Validator):request.client = appreturn request.client+ # PostHog deliberately does NOT support OIDC silent authentication (`prompt=none`). Every+ # authorization must go through the interactive login + consent prompt — we never issue a+ # token without showing UI. oauthlib gates `prompt=none` on two validators in sequence+ # (validate_silent_login then validate_silent_authorization); neither the base class nor+ # django-oauth-toolkit implements them, so both default to NotImplementedError -> 500. We+ # override both so that every `prompt=none` request is instead rejected with a+ # spec-compliant OIDC error, forcing the client into the normal interactive flow.+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.+ # First gate. We don't authorize silently regardless, but reporting real login state+ # here yields the correct rejection error: a logged-out user gets `login_required`,+ # while a logged-in user passes this gate and is rejected by validate_silent_authorization+ # below with `consent_required`.user = getattr(request, "user", None)return bool(user and getattr(user, "is_authenticated", False))+ def validate_silent_authorization(self, request) -> bool:+ # Second gate — the one that actually disables silent authentication. Always False, so+ # oauthlib raises `consent_required` for any authenticated `prompt=none` request instead+ # of completing the grant (or crashing with NotImplementedError -> 500).+ #+ # This gate is only reached when the user is authenticated, which is precisely the+ # silent-auth case `prompt=none` is meant to enable: oauthlib attaches request.user via+ # credentials only in create_authorization_response (POST allow, first-party auto-grant,+ # auto-approval), not in validate_authorization_request. Overriding validate_silent_login+ # alone would leave the 500 in place for exactly that case.+ return False+def validate_client_id(self, client_id, request, *args, **kwargs):"""Validate client_id, supporting CIMD URL-form client_ids.