: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
feat(oauth): capture invalid_scope rejections at /authorize (#61715)
Matt Brooker36e2465a8a5f
diff --git a/posthog/api/oauth/test_views.py b/posthog/api/oauth/test_views.pyindex 8752a2d67fde..ee4a34cbacc6 100644--- a/posthog/api/oauth/test_views.py+++ b/posthog/api/oauth/test_views.py@@ -2360,6 +2360,35 @@ class TestOAuthAPI(APIBaseTest):self.assertNotIn("error=invalid_scope", redirect_to)self.assertIn("code=", redirect_to)+ @parameterized.expand(+ [+ ("ceiling_excludes_requested", ["experiment:read"], "experiment:write"),+ ("empty_ceiling_rejects_privileged", [], "llm_gateway:read"),+ ]+ )+ def test_authorize_rejection_captures_invalid_scope_event(self, _name, ceiling, requested_scope):+ self._set_ceiling(*ceiling)+ with patch("posthog.api.oauth.views.posthoganalytics.capture") as mock_capture:+ response = self.client.get(f"{self.base_authorization_url}&scope={requested_scope}")+ self.assertEqual(response.status_code, status.HTTP_302_FOUND)+ self.assertIn("error=invalid_scope", response.get("Location") or "")+ rejected = [c for c in mock_capture.call_args_list if c.kwargs.get("event") == "oauth_authorization_rejected"]+ self.assertEqual(len(rejected), 1)+ props = rejected[0].kwargs["properties"]+ self.assertEqual(props["reason"], "invalid_scope")+ self.assertEqual(props["app_id"], str(self.confidential_application.pk))+ self.assertEqual(props["client_name"], self.confidential_application.name)+ self.assertEqual(props["registration_type"], "manual")+ self.assertEqual(props["is_verified"], self.confidential_application.is_verified)+ self.assertEqual(props["is_first_party"], self.confidential_application.is_first_party)++ def test_authorize_success_does_not_capture_invalid_scope_event(self):+ self._set_ceiling("experiment:read")+ with patch("posthog.api.oauth.views.posthoganalytics.capture") as mock_capture:+ self._authorize_post("experiment:read")+ rejected = [c for c in mock_capture.call_args_list if c.kwargs.get("event") == "oauth_authorization_rejected"]+ self.assertEqual(rejected, [])+@freeze_time("2025-01-01 00:00:00")def test_token_endpoint_with_json_payload(self):grant = OAuthGrant.objects.create(diff --git a/posthog/api/oauth/views.py b/posthog/api/oauth/views.pyindex eea1b339bafe..5263957b6ce4 100644--- a/posthog/api/oauth/views.py+++ b/posthog/api/oauth/views.py@@ -673,6 +673,12 @@ class OAuthAuthorizationView(OAuthLibMixin, APIView):return [IsAuthenticated()]return []+ @staticmethod+ def _registration_type(application: OAuthApplication) -> str:+ if application.is_cimd_client:+ return "cimd"+ return "dcr" if application.is_dcr_client else "manual"+@method_decorator(login_required)def get(self, request, *args, **kwargs):# Rate-limit new CIMD application creation by IP.@@ -717,7 +723,7 @@ class OAuthAuthorizationView(OAuthLibMixin, APIView):return Response({"error": "Invalid client_id"}, status=status.HTTP_400_BAD_REQUEST)# Track OAuth authorization attempts with the authenticated user- registration_type = "cimd" if application.is_cimd_client else ("dcr" if application.is_dcr_client else "manual")+ registration_type = self._registration_type(application)posthoganalytics.capture(distinct_id=str(request.user.distinct_id),event="oauth_authorization_requested",@@ -881,6 +887,22 @@ class OAuthAuthorizationView(OAuthLibMixin, APIView):"""redirect, error_response = super().error_response(error, **kwargs)+ # Surface scope-ceiling rejections so on-call can alert on /authorize failing with invalid_scope.+ if getattr(error_response["error"], "error", None) == "invalid_scope" and application is not None:+ distinct_id = getattr(getattr(self.request, "user", None), "distinct_id", None) or application.client_id+ posthoganalytics.capture(+ distinct_id=str(distinct_id),+ event="oauth_authorization_rejected",+ properties={+ "reason": "invalid_scope",+ "client_name": application.name,+ "app_id": str(application.pk),+ "registration_type": self._registration_type(application),+ "is_verified": application.is_verified,+ "is_first_party": application.is_first_party,+ },+ )+if redirect:if no_redirect:return Response(