: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 empty-scope refresh for first-party apps (#86115)
Nick Best09b5a328ba37
diff --git a/posthog/api/oauth/test_views.py b/posthog/api/oauth/test_views.pyindex a404db59e37a..eb8010df3230 100644--- a/posthog/api/oauth/test_views.py+++ b/posthog/api/oauth/test_views.py@@ -1698,6 +1698,26 @@ class TestOAuthAPI(APIBaseTest):self.assertEqual(response.status_code, status.HTTP_200_OK)self.assertEqual(response.json().get("scope", ""), "")+ def test_refresh_of_an_empty_grant_rejected_for_first_party_apps(self):+ self.confidential_application.is_first_party = True+ self.confidential_application.scopes = ["experiment:read"]+ self.confidential_application.save()++ refresh_token = self._create_refreshable_token_pair("")++ response = self.post(+ "/oauth/token/",+ {+ "grant_type": "refresh_token",+ "refresh_token": refresh_token.token,+ "client_id": self.confidential_application.client_id,+ "client_secret": "test_confidential_client_secret",+ },+ )++ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ self.assertEqual(response.json()["error"], "invalid_grant")+def test_refresh_rejected_when_token_scopes_outside_ceiling(self):self.confidential_application.scopes = ["experiment:read"]self.confidential_application.save()@@ -4610,6 +4630,7 @@ class TestOAuthAPI(APIBaseTest):code_challenge_method="S256",redirect_uri="https://example.com/callback",expires=timezone.now() + timedelta(minutes=5),+ scope="openid experiment:read",scoped_organizations=[str(self.organization.id)],scoped_teams=[],)diff --git a/posthog/api/oauth/views.py b/posthog/api/oauth/views.pyindex 0ee46b53f054..437fe4516e83 100644--- a/posthog/api/oauth/views.py+++ b/posthog/api/oauth/views.py@@ -694,6 +694,13 @@ class OAuthValidator(OAuth2Validator):- a token that never held any scope refreshes as an empty grant instead.Rejecting that one would loop, since re-authorizing returns the same emptygrant; its 403s by scope are where the client should find out.+ - except for first-party apps, where an empty grant is rejected+ (`invalid_grant`). `/authorize` auto-grants first-party requests their full+ clamped scope set with no consent screen, so re-authorization cannot return+ another empty grant and the loop above does not apply. A scope-less token+ only arises for them as a corruption artifact (e.g. a refresh racing the+ reuse-protection mass revoke), and letting it refresh forever leaves the+ client silently 403ing on every resource call instead of re-authorizing.An empty `ceiling_scopes` (no ceiling) is a no-op. Refresh never enforces therequired floor — a token consented below a later-declared required set keeps@@ -711,6 +718,16 @@ class OAuthValidator(OAuth2Validator):application = rt.application if rt else Nonenarrowed = narrow_scopes_to_ceiling(original_list, getattr(application, "ceiling_scopes", None) or [])+ if narrowed == [] and getattr(application, "is_first_party", False):+ logger.warning(+ "oauth_empty_scope_refresh_rejected",+ client_name=getattr(application, "name", "unknown"),+ app_id=str(getattr(application, "pk", "unknown")),+ )+ raise InvalidGrantError(+ description="Token carries no scopes; re-authorize to obtain a scoped token.",+ request=request,+ )if narrowed is None:# Raised inside oauthlib's validate_token_request, which create_token_response# wraps and turns into an RFC 6749 `invalid_grant` 400 — not a 500.