: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(cli): check project access before minting a cli key (#113791)
Tom Piccirello002f53207aeb
diff --git a/posthog/api/cli_auth.py b/posthog/api/cli_auth.pyindex 873b9f3e8d33..1476ca20cc06 100644--- a/posthog/api/cli_auth.py+++ b/posthog/api/cli_auth.py@@ -28,6 +28,7 @@ from posthog.auth import SessionAuthenticationfrom posthog.models import PersonalAPIKey, Team, Userfrom posthog.models.utils import generate_random_token_personal, hash_key_value, mask_key_valuefrom posthog.scopes import UNPRIVILEGED_SCOPES+from posthog.user_permissions import UserPermissions# Device code lives for 10 minutesDEVICE_CODE_EXPIRY_SECONDS = 600@@ -245,8 +246,7 @@ class CLIAuthViewSet(viewsets.ViewSet):# Verify user has access to the projecttry:team = Team.objects.get(id=project_id)- # Check if user has access to this team's organization- if not user.organization_memberships.filter(organization=team.organization).exists():+ if UserPermissions(user).team(team).effective_membership_level is None:return Response({"error": "access_denied", "error_description": "You do not have access to this project"},status=status.HTTP_403_FORBIDDEN,diff --git a/posthog/api/test/test_cli_auth.py b/posthog/api/test/test_cli_auth.pyindex 938b0a61f93d..4ca53be2752f 100644--- a/posthog/api/test/test_cli_auth.py+++ b/posthog/api/test/test_cli_auth.py@@ -11,10 +11,13 @@ from parameterized import parameterizedfrom rest_framework import statusfrom posthog.api.cli_auth import CLI_SCOPES, DEVICE_CODE_EXPIRY_SECONDS, get_device_cache_key, get_user_code_cache_key+from posthog.constants import AvailableFeaturefrom posthog.models import PersonalAPIKey, Team, User-from posthog.models.organization import Organization+from posthog.models.organization import Organization, OrganizationMembershipfrom posthog.models.utils import hash_key_value+from products.access_control.backend.models.access_control import AccessControl+class TestCLIAuthDeviceCodeEndpoint(APIBaseTest):"""@@ -242,6 +245,27 @@ class TestCLIAuthAuthorizeEndpoint(APIBaseTest):api_keys = PersonalAPIKey.objects.filter(user=self.user).count()self.assertEqual(api_keys, 0)+ def test_authorization_rejects_member_without_access_to_restricted_project(self):+ self.organization.available_product_features = [+ {"key": AvailableFeature.ACCESS_CONTROL, "name": AvailableFeature.ACCESS_CONTROL}+ ]+ self.organization.save()+ self.organization_membership.level = OrganizationMembership.Level.MEMBER+ self.organization_membership.save()+ restricted_team = Team.objects.create(organization=self.organization, name="Restricted Team")+ AccessControl.objects.create(+ team=restricted_team, resource="project", resource_id=str(restricted_team.id), access_level="none"+ )++ response = self.client.post(+ "/api/cli-auth/authorize/",+ {"user_code": self.user_code, "project_id": restricted_team.id},+ )++ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)+ self.assertEqual(response.json()["error"], "access_denied")+ self.assertFalse(PersonalAPIKey.objects.filter(user=self.user).exists())+def test_authorization_rejects_nonexistent_project(self):"""Test that authorization fails with non-existent project ID"""response = self.client.post(