: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(api): show the secret api token only to callers with its scopes (#127775)
Rafael Audibert7461fb8383f0
diff --git a/posthog/api/project.py b/posthog/api/project.pyindex 19014ebe0f3f..9e3e1f8ee596 100644--- a/posthog/api/project.py+++ b/posthog/api/project.py@@ -52,6 +52,7 @@ from posthog.api.team.marketing_config import TeamMarketingAnalyticsConfigSerialfrom posthog.api.team.settings_validation import (_format_serializer_errors,heatmaps_screenshot_secret_for_reader,+ hide_secret_api_tokens_from_unscoped_caller,validate_team_attrs,validate_team_workflows_config,)@@ -1017,6 +1018,7 @@ class ProjectBackwardCompatSerializer(# Mirror TeamSerializer: fall back to the global default data theme when the color feature isn't availableif not instance.organization.is_feature_available(AvailableFeature.DATA_COLOR_THEMES):representation["default_data_theme"] = _default_data_color_theme_id()+ hide_secret_api_tokens_from_unscoped_caller(representation, self.context.get("request"))return representationdef get_user_access_level(self, obj: Model | ObjectAccessRef) -> Optional[str]:diff --git a/posthog/api/project_secret_api_key.py b/posthog/api/project_secret_api_key.pyindex 780e4b635bc0..f33cc4de5a8d 100644--- a/posthog/api/project_secret_api_key.py+++ b/posthog/api/project_secret_api_key.py@@ -20,17 +20,12 @@ from posthog.llm.wizard_blocklist import WIZARD_BLOCKED_DETAILfrom posthog.models import Organization, Userfrom posthog.models.project_secret_api_key import ProjectSecretAPIKey, RevokedTeamSecretTokenfrom posthog.models.utils import ai_credential_value, generate_random_token_secret, hash_key_value, mask_key_value-from posthog.permissions import (- TeamMemberStrictManagementPermission,- TimeSensitiveActionPermission,- get_authenticator_scopes,-)+from posthog.permissions import TeamMemberStrictManagementPermission, TimeSensitiveActionPermission, scopes_caller_lacksfrom posthog.scopes import (API_SCOPE_ACTIONS,API_SCOPE_OBJECTS,INTERNAL_API_SCOPE_OBJECTS,PROJECT_SECRET_API_KEY_ALLOWED_API_SCOPE_ACTION,- scopes_not_covered,)from posthog.settings import EE_AVAILABLEfrom posthog.tasks.email import send_feature_flags_secure_api_key_exposed, send_project_secret_api_key_exposed@@ -50,11 +45,8 @@ MAX_PROJECT_SECRET_API_KEYS_PER_TEAM = 50def _enforce_caller_holds_scopes(request: Request, scopes: Iterable[str]) -> None:"""A project secret API key outlives the credential that issued it, so a scoped caller must not- issue one with scopes it lacks. Session auth and `*` keys carry the user's full authority."""- caller_scopes = get_authenticator_scopes(getattr(request, "successful_authenticator", None))- if caller_scopes is None or "*" in caller_scopes:- return- missing = sorted(scopes_not_covered(caller_scopes, scopes))+ issue one with scopes it lacks."""+ missing = scopes_caller_lacks(request, scopes)if missing:raise PermissionDenied("Your API key or OAuth token can only issue a project secret API key with scopes it has. "diff --git a/posthog/api/team/settings_validation.py b/posthog/api/team/settings_validation.pyindex 8278af8a31a1..8c3a127775f3 100644--- a/posthog/api/team/settings_validation.py+++ b/posthog/api/team/settings_validation.py@@ -3,6 +3,8 @@import jsonfrom typing import TYPE_CHECKING, Any, cast+from django.conf import settings+import re2from pydantic import TypeAdapterfrom pydantic_core import ValidationError as PydanticValidationError@@ -13,13 +15,16 @@ from posthog.api.routing import TeamAndOrgViewSetMixinfrom posthog.models import Team, Userfrom posthog.models.organization import Organization, OrganizationMembershipfrom posthog.models.project import Project+from posthog.models.project_secret_api_key import MIRRORED_LEGACY_TOKEN_SCOPESfrom posthog.models.team.logs_retention import LOGS_CUSTOM_RETENTION_FLAG-from posthog.permissions import posthog_feature_flag_enabled+from posthog.permissions import get_authenticator_scopes, posthog_feature_flag_enabled, scopes_caller_lacksfrom posthog.types import AnyPropertyFilterfrom . import team_configif TYPE_CHECKING:+ from rest_framework.request import Request+from posthog.user_permissions import UserPermissionstest_account_filters_adapter = TypeAdapter(list[AnyPropertyFilter])@@ -167,6 +172,26 @@ def heatmaps_screenshot_secret_for_reader(team: Team, user_permissions: "UserPerreturn team.heatmaps_screenshot_secret+def _caller_may_read_secret_api_tokens(request: "Request | None") -> bool:+ caller_scopes = get_authenticator_scopes(getattr(request, "successful_authenticator", None))+ if caller_scopes is None or "*" in caller_scopes:+ return True+ # While legacy team tokens are accepted, the raw token also authenticates routes that check no scope,+ # such as external ticket updates and customer analytics accounts, so no scope list covers it.+ if settings.ACCEPT_TEAM_SECRET_TOKENS:+ return False+ # Otherwise it authenticates only through its PSAK mirror row, whose scopes rotation also requires.+ return not scopes_caller_lacks(request, MIRRORED_LEGACY_TOKEN_SCOPES)+++def hide_secret_api_tokens_from_unscoped_caller(representation: dict[str, Any], request: "Request | None") -> None:+ if _caller_may_read_secret_api_tokens(request):+ return+ for field in ("secret_api_token", "secret_api_token_backup"):+ if field in representation:+ representation[field] = None++def _get_organization_for_logs_settings_check(serializer: serializers.BaseSerializer) -> Organization | None:if serializer.instance is not None:team = (diff --git a/posthog/api/team/team_serializer.py b/posthog/api/team/team_serializer.pyindex 20aa1894fad4..2009c3e479ca 100644--- a/posthog/api/team/team_serializer.py+++ b/posthog/api/team/team_serializer.py@@ -183,6 +183,7 @@ class TeamSerializer(serializers.ModelSerializer, UserPermissionsSerializerMixinif not instance.organization.is_feature_available(AvailableFeature.DATA_COLOR_THEMES):with tracer.start_as_current_span("team_serializer.default_data_theme_fallback"):representation["default_data_theme"] = live_events._default_data_color_theme_id()+ settings_validation.hide_secret_api_tokens_from_unscoped_caller(representation, self.context.get("request"))return representationdiff --git a/posthog/api/test/test_team.py b/posthog/api/test/test_team.pyindex ac7f301ac20b..f693e0a910b9 100644--- a/posthog/api/test/test_team.py+++ b/posthog/api/test/test_team.py@@ -4334,6 +4334,92 @@ class TestGetOrMintLiveEventsToken(APIBaseTest):assert token_old_key != token_new_key+class TestSecretApiTokenVisibility(APIBaseTest):+ def setUp(self) -> None:+ super().setUp()+ self.team.secret_api_token = "phs_JVRb8fNi0XyIKGgUCyi29ZJUOXEr6NF2dKBy5Ws8XVeF11C"+ self.team.secret_api_token_backup = "phs_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"+ self.team.save()++ def _oauth_token(self, scope: str) -> str:+ application = OAuthApplication.objects.create(+ name="Test OAuth App",+ client_type=OAuthApplication.CLIENT_CONFIDENTIAL,+ authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,+ redirect_uris="https://example.com/callback",+ algorithm="RS256",+ user=self.user,+ )+ return OAuthAccessToken.objects.create(+ application=application,+ user=self.user,+ token="pha_secret_token_visibility",+ scope=scope,+ expires=timezone.now() + timedelta(hours=1),+ ).token++ @parameterized.expand(+ [+ ("session", None, [], True, True),+ ("personal_api_key_project_read", "personal_api_key", ["project:read"], True, False),+ (+ "personal_api_key_mirrored_scopes_legacy_tokens_on",+ "personal_api_key",+ ["project:read", "feature_flag:read", "support_ticket:read"],+ True,+ False,+ ),+ (+ "personal_api_key_mirrored_scopes_legacy_tokens_off",+ "personal_api_key",+ ["project:read", "feature_flag:read", "support_ticket:read"],+ False,+ True,+ ),+ ("personal_api_key_all_scopes", "personal_api_key", ["*"], True, True),+ ("oauth_project_read", "oauth", ["project:read"], True, False),+ ]+ )+ @patch("posthog.event_usage.report_user_action")+ def test_secret_api_tokens_are_visible_only_to_callers_that_hold_their_authority(+ self,+ _name: str,+ credential: str | None,+ scopes: list[str],+ accept_team_secret_tokens: bool,+ visible: bool,+ _mock_report_user_action: MagicMock,+ ) -> None:+ headers: dict[str, str] = {}+ if credential is not None:+ token = (+ self.create_personal_api_key_with_scopes(scopes)+ if credential == "personal_api_key"+ else self._oauth_token(" ".join(scopes))+ )+ headers["authorization"] = f"Bearer {token}"+ self.client.logout()++ with override_settings(ACCEPT_TEAM_SECRET_TOKENS=accept_team_secret_tokens):+ responses = self._read_both_serializers(headers)++ expected = (self.team.secret_api_token, self.team.secret_api_token_backup) if visible else (None, None)+ for serializer_name, response in responses.items():+ assert response.status_code in (status.HTTP_200_OK, status.HTTP_201_CREATED), response.json()+ body = response.json()+ assert (body["secret_api_token"], body["secret_api_token_backup"]) == expected, serializer_name++ def _read_both_serializers(self, headers: dict[str, str]) -> dict[str, Any]:+ return {+ "ProjectBackwardCompatSerializer": self.client.get(f"/api/projects/{self.project.id}/", headers=headers),+ "TeamSerializer": self.client.patch(+ f"/api/projects/{self.project.id}/add_product_intent/",+ {"product_type": "product_analytics"},+ headers=headers,+ ),+ }++# Sensitive Team/Project settings the frontend gates behind# `TeamMembershipLevel.Admin` AND that ordinary members are not expected to flip# from any onboarding/dashboard surface. Each tuple isdiff --git a/posthog/permissions.py b/posthog/permissions.pyindex 9796aaee942a..008d13bd6b43 100644--- a/posthog/permissions.py+++ b/posthog/permissions.py@@ -1,6 +1,7 @@import osimport timeimport uuid+from collections.abc import Iterablefrom typing import Any, Optional, castfrom django.conf import settings@@ -753,6 +754,15 @@ def get_authenticator_scopes(authenticator) -> list[str] | None:return None+def scopes_caller_lacks(request: Request | None, scopes: Iterable[str]) -> list[str]:+ """The given scopes that the request's token does not cover, sorted. Empty for session auth and+ `*` keys, which carry the user's full authority."""+ caller_scopes = get_authenticator_scopes(getattr(request, "successful_authenticator", None))+ if caller_scopes is None or "*" in caller_scopes:+ return []+ return sorted(scopes_not_covered(caller_scopes, scopes))++CLIENT_ID_POSTHOG = "posthog"