: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(organizations): accept personal API keys on teams/data_freshness (#88368)
Eleftheria Trivyzaki792412aa3e13
diff --git a/posthog/api/organization.py b/posthog/api/organization.pyindex 37f412bae78b..d2ab80d96d3d 100644--- a/posthog/api/organization.py+++ b/posthog/api/organization.py@@ -725,7 +725,15 @@ class OrganizationViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):return Response({"success": True, "removed_members": removed})@extend_schema(request=None, responses={200: OrganizationDataFreshnessSerializer})- @action(detail=True, methods=["GET"], url_path="teams/data_freshness", pagination_class=None)+ @action(+ detail=True,+ methods=["GET"],+ url_path="teams/data_freshness",+ pagination_class=None,+ # A scope is only derived for `list` and `retrieve`, so without this the action reaches+ # APIScopePermission with no required scope and every personal API key is rejected.+ required_scopes=["organization:read"],+ )def data_freshness(self, request: Request, **kwargs) -> Response:"""When each project in the organization last received data, broken down by kind of data."""organization = self.organizationdiff --git a/posthog/api/test/test_personal_api_keys.py b/posthog/api/test/test_personal_api_keys.pyindex 06e775c921c5..85e757b1e91c 100644--- a/posthog/api/test/test_personal_api_keys.py+++ b/posthog/api/test/test_personal_api_keys.py@@ -888,6 +888,34 @@ class TestPersonalAPIKeysWithPersonScope(PersonalAPIKeysBaseTest):assert response.json()["detail"] == "API key missing required scope 'person:read'"+class TestPersonalAPIKeysWithOrganizationScope(PersonalAPIKeysBaseTest):+ # `teams/data_freshness` is a custom action, so it only accepts a personal API key while it+ # declares `required_scopes`. Drop that and APIScopePermission rejects every key, including+ # one scoped `*`, with "This action does not support personal API key access".++ @parameterized.expand(+ [+ ("organization:read", status.HTTP_200_OK, None),+ ("feature_flag:read", status.HTTP_403_FORBIDDEN, "API key missing required scope 'organization:read'"),+ ]+ )+ @patch("posthog.api.organization.get_organization_data_freshness")+ def test_data_freshness_requires_organization_read_scope(+ self, scope, expected_status, expected_detail, mock_freshness+ ):+ mock_freshness.return_value = []+ self.key.scopes = [scope]+ self.key.save()++ response = self._do_request(f"/api/organizations/{self.organization.id}/teams/data_freshness")++ assert response.status_code == expected_status, response.content+ # Asserting the reason, not just the 403: an unscoped action also rejects this key, but for+ # the wrong reason, and that is the regression being guarded.+ if expected_detail:+ assert response.json()["detail"] == expected_detail++class TestPersonalAPIKeysWithApprovalsScope(PersonalAPIKeysBaseTest):def setUp(self):super().setUp()