Skip to content

: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.

fix(team): require admin to rename project or environment (#72744)

Tom Piccirellodb92fc4806f2
diff --git a/ee/api/test/test_project.py b/ee/api/test/test_project.py
index 2e0b0d9435a3..ae1d45f1b29a 100644
--- a/ee/api/test/test_project.py
+++ b/ee/api/test/test_project.py
@@ -122,15 +122,19 @@ class TestProjectEnterpriseAPI(team_enterprise_api_test_factory()): # type: ign
},
)
- def test_rename_project_as_org_member_allowed(self):
+ def test_rename_project_as_org_member_forbidden(self):
+ # Renaming is admin-only (mirrors the settings UI, which gates rename behind admin access).
self.organization_membership.level = OrganizationMembership.Level.MEMBER
self.organization_membership.save()
+ # In an access-control org, a member with no explicit project access defaults to effective admin;
+ # set default member access so the admin-only rename gate actually applies.
+ self._set_project_default_member_access(self.team)
response = self.client.patch(f"/api/projects/@current/", {"name": "Erinaceus europaeus"})
self.project.refresh_from_db()
- self.assertEqual(response.status_code, 200)
- self.assertEqual(self.project.name, "Erinaceus europaeus")
+ self.assertEqual(response.status_code, 403)
+ self.assertNotEqual(self.project.name, "Erinaceus europaeus")
def test_list_projects_restricted_ones_hidden(self):
self.organization_membership.level = OrganizationMembership.Level.MEMBER
diff --git a/ee/api/test/test_team.py b/ee/api/test/test_team.py
index 4d12fa398039..aeb04f8b4987 100644
--- a/ee/api/test/test_team.py
+++ b/ee/api/test/test_team.py
@@ -269,15 +269,19 @@ class TestTeamEnterpriseAPI(team_enterprise_api_test_factory()): # type: ignore
self.assertEqual(Team.objects.count(), 2)
self.assertEqual(Project.objects.count(), 2)
- def test_rename_team_as_org_member_allowed(self):
+ def test_rename_team_as_org_member_forbidden(self):
+ # Renaming is admin-only (mirrors the settings UI, which gates rename behind admin access).
self.organization_membership.level = OrganizationMembership.Level.MEMBER
self.organization_membership.save()
+ # In an access-control org, a member with no explicit project access defaults to effective admin;
+ # set default member access so the admin-only rename gate actually applies.
+ self._set_project_default_member_access(self.team)
response = self.client.patch(f"/api/environments/@current/", {"name": "Erinaceus europaeus"})
self.team.refresh_from_db()
- self.assertEqual(response.status_code, HTTP_200_OK)
- self.assertEqual(self.team.name, "Erinaceus europaeus")
+ self.assertEqual(response.status_code, HTTP_403_FORBIDDEN)
+ self.assertNotEqual(self.team.name, "Erinaceus europaeus")
def test_list_teams_restricted_ones_hidden(self):
self.organization_membership.level = OrganizationMembership.Level.MEMBER
diff --git a/posthog/api/team.py b/posthog/api/team.py
index 39949b2d674e..6916d23962b8 100644
--- a/posthog/api/team.py
+++ b/posthog/api/team.py
@@ -497,6 +497,10 @@ TEAM_CONFIG_ADMIN_FIELDS_SET: set[str] = (TEAM_CONFIG_FIELDS_SET - TEAM_CONFIG_M
"is_demo",
"app_urls",
"access_control",
+ # Renaming a project/environment is admin-only (the settings UI gates TeamDisplayName behind
+ # useRestrictedArea(Admin)). Excluded from the create-time gate below so members allowed to
+ # create projects can still name them.
+ "name",
}
# Fields that are not member-safe but carry their own `field_access_control` (enforced in
@@ -2540,8 +2544,9 @@ def validate_team_attrs(
# On create there's no team yet, so check the creator's org-level membership. Without this a
# non-admin member (allowed to create projects via members_can_create_projects) could set
# admin-only team fields like receive_org_level_activity_logs. `is_demo` is excluded — demo
- # project creation is intentionally open to members and gated separately.
- admin_fields_touched = (TEAM_CONFIG_ADMIN_FIELDS_SET - {"is_demo"}) & attrs.keys()
+ # project creation is intentionally open to members and gated separately. `name` is excluded
+ # too — naming a project you're allowed to create is not the same as renaming an existing one.
+ admin_fields_touched = (TEAM_CONFIG_ADMIN_FIELDS_SET - {"is_demo", "name"}) & attrs.keys()
if admin_fields_touched:
membership = OrganizationMembership.objects.filter(
user=cast(User, view.request.user), organization_id=view.organization_id
diff --git a/posthog/api/test/test_environments_rewrite.py b/posthog/api/test/test_environments_rewrite.py
index e5806a3390af..a82b311b3c51 100644
--- a/posthog/api/test/test_environments_rewrite.py
+++ b/posthog/api/test/test_environments_rewrite.py
@@ -6,6 +6,7 @@ from django.test import RequestFactory, SimpleTestCase, override_settings
from rest_framework import status
from posthog.middleware import EnvironmentsRewriteMiddleware
+from posthog.models.organization import OrganizationMembership
# EnvironmentsRewriteMiddleware serves /api/environments/* through the equivalent /api/projects/*
# viewset (same id — Project ↔ primary Team are 1:1 and share it) via an in-process path rewrite.
@@ -65,6 +66,9 @@ class TestEnvironmentsRewriteIntegration(APIBaseTest):
self.assertEqual(response["Link"], '</api/projects/@current/>; rel="successor-version"')
def test_write_round_trips_method_and_body(self):
+ # Renaming is admin-only, so run as an admin here — this test covers the rewrite, not the permission check.
+ self.organization_membership.level = OrganizationMembership.Level.ADMIN
+ self.organization_membership.save()
response = self.client.patch("/api/environments/@current/", {"name": "renamed via env alias"}, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json()["name"], "renamed via env alias")
diff --git a/posthog/api/test/test_team.py b/posthog/api/test/test_team.py
index 84065a321b81..66836497ff25 100644
--- a/posthog/api/test/test_team.py
+++ b/posthog/api/test/test_team.py
@@ -3187,9 +3187,13 @@ _MEMBER_SAFE_TEAM_CONFIG_FIELDS_FOR_PROJECTS: list[tuple[str, Any, str]] = [
# `@field_access_control`; with access controls enabled that governs it instead (see
# test_web_analytics_editor_can_write_app_urls_with_access_control), but without it the
# blanket project-admin gate still applies here.
+# `name` (the project/environment rename) is admin-only in the settings UI (TeamDisplayName
+# is gated behind useRestrictedArea(Admin)); a MEMBER must not rename via the API. It stays
+# member-settable at creation time — see test_member_can_create_project_when_org_allows.
_UNANNOTATED_SENSITIVE_FIELDS: list[tuple[str, Any, str]] = [
("is_demo", True, "is_demo"),
("app_urls", ["https://evil.example.com"], "app_urls"),
+ ("name", "Renamed by member", "name"),
]