: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(customer-analytics): accept account:write psak on account updates (#113506)
Patricio220972a0578a
diff --git a/products/customer_analytics/backend/presentation/views/external.py b/products/customer_analytics/backend/presentation/views/external.pyindex 959f14e46e8b..82d5b594bfd4 100644--- a/products/customer_analytics/backend/presentation/views/external.py+++ b/products/customer_analytics/backend/presentation/views/external.py@@ -6,8 +6,8 @@ authenticate via the team secret API token passed as a Bearer token in theAuthorization header. The bulk account list instead authenticates via a projectsecret API key or personal API key carrying the ``account:read`` scope, because the team token isreadable by every project member and must not unlock a team-wide account export.-The single-account GET accepts either credential, and so does POST, where a project secret-API key needs the ``account:write`` scope. PATCH stays team-token only.+The single-account GET accepts either credential, and so do POST and PATCH, where a+project secret API key needs the ``account:write`` scope.The team token deliberately grants single-account writes (create, tags,relationships, custom property values) without per-user ``account`` scope checks:@@ -235,14 +235,17 @@ def _get_auth_method_label(request: Request) -> str:def _authenticate_team_for_update(request: Request) -> tuple[Team, None] | tuple[None, Response]:- """Updates stay team-token only: they change tags, relationships and churn state. A project- secret API key is rejected explicitly, because the team-token lookup treats it as an- unknown token and answers 401 instead of 403."""+ """Updates require ``account:write`` on a project secret API key; reads alone don't+ grant tag, relationship or churn changes. The team token keeps working unchanged."""if is_authenticated_via_project_secret_api_key(request):- return None, Response(- {"error": "Project secret API keys cannot update accounts on this route"},- status=status.HTTP_403_FORBIDDEN,- )+ authenticator = cast(ProjectSecretAPIKeyAuthentication, request.successful_authenticator)+ key_scopes = set(get_authenticator_scopes(authenticator) or [])+ if "*" not in key_scopes and EXTERNAL_ACCOUNT_WRITE_SCOPE not in key_scopes:+ return None, Response(+ {"error": f"API key missing required scope '{EXTERNAL_ACCOUNT_WRITE_SCOPE}'"},+ status=status.HTTP_403_FORBIDDEN,+ )+ return authenticator.project_secret_api_key.team, Nonereturn _authenticate_team(request)@@ -400,7 +403,7 @@ class ExternalAccountView(APIView):return errorassert team is not None- ACCOUNT_ACTION_AUTH_COUNTER.labels(auth_method="secret_api_token", http_method="patch").inc()+ ACCOUNT_ACTION_AUTH_COUNTER.labels(auth_method=_get_auth_method_label(request), http_method="patch").inc()return handle_account_update(request, team)diff --git a/products/customer_analytics/backend/test/test_external.py b/products/customer_analytics/backend/test/test_external.pyindex 639953e0b2c9..d3ccb578d085 100644--- a/products/customer_analytics/backend/test/test_external.py+++ b/products/customer_analytics/backend/test/test_external.py@@ -147,10 +147,9 @@ class TestExternalAccountAPI(APIBaseTest):("post_read_scope", "_post", ["account:read"], {"external_id": "acme-2"}),("post_unrelated_scope", "_post", ["endpoint:read"], {"external_id": "acme-2"}),("patch_read_scope", "_patch", ["account:read"], {"external_id": "acme-1", "churned_at": "2026-08-01"}),- ("patch_write_scope", "_patch", ["account:write"], {"external_id": "acme-1", "churned_at": "2026-08-01"}),])- def test_writes_reject_project_secret_api_key(+ def test_writes_reject_project_secret_api_key_without_write_scope(self, _name: str, request_method: str, scopes: list[str], payload: dict[str, str]) -> None:token = self._create_psak_token(scopes=scopes)@@ -160,6 +159,14 @@ class TestExternalAccountAPI(APIBaseTest):self.account.refresh_from_db()self.assertIsNone(self.account.churned_at)+ @parameterized.expand([("write", ["account:write"]), ("wildcard", ["*"])])+ def test_patch_accepts_project_secret_api_key_with_account_write_scope(self, _name: str, scopes: list[str]) -> None:+ token = self._create_psak_token(scopes=scopes)+ response = self._patch({"external_id": "acme-1", "churned_at": "2026-08-01"}, token=token)+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.account.refresh_from_db()+ self.assertIsNotNone(self.account.churned_at)+@parameterized.expand([("primary", "secret_api_token"), ("backup", "secret_api_token_backup")])def test_patch_accepts_legacy_token_that_has_a_migrated_psak_row(self, _name: str, token_field: str) -> None:# The #63111 backfill gives the legacy token a PSAK row with the same hash. The