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(api): limit token team override to team routes (#110262)

Raúl Negrón-Oteroe02bbd99805a
diff --git a/posthog/api/routing.py b/posthog/api/routing.py
index 757a59cecf2d..b0938c09b830 100644
--- a/posthog/api/routing.py
+++ b/posthog/api/routing.py
@@ -664,6 +664,11 @@ class TeamAndOrgViewSetMixin(_GenericViewSet):
@lru_cache(maxsize=1) # noqa: B019 - short-lived per-request router
def _get_team_from_request(self) -> Optional["Team"]:
+ # Permission classes treat `view.team` as the target of the request, so a route that
+ # identifies no team must not gain one from a request parameter.
+ if not (self._is_team_view or self._is_project_view):
+ return None
+
team_found = None
token = get_token(None, self.request)
diff --git a/posthog/api/test/test_personal_api_keys.py b/posthog/api/test/test_personal_api_keys.py
index 85e757b1e91c..7523bf975605 100644
--- a/posthog/api/test/test_personal_api_keys.py
+++ b/posthog/api/test/test_personal_api_keys.py
@@ -1103,8 +1103,21 @@ class TestPersonalAPIKeysWithTeamScopeAPIAuthentication(PersonalAPIKeysBaseTest)
assert response.status_code == status.HTTP_403_FORBIDDEN, response.json()
response = self._do_request(f"/api/projects/{self.other_team.id}/feature_flags")
assert response.status_code == status.HTTP_403_FORBIDDEN, response.json()
- assert response.status_code == status.HTTP_403_FORBIDDEN, response.json()
response = self._do_request(f"/api/projects/{self.other_team.id}")
+ assert response.status_code == status.HTTP_403_FORBIDDEN, response.json()
+
+ @parameterized.expand(
+ [
+ ("organization", "/api/organizations/{organization_id}/"),
+ ("organization_members", "/api/organizations/{organization_id}/members/"),
+ ]
+ )
+ def test_denies_access_to_org_resources_with_project_token_param(self, _name: str, url: str):
+ url = url.format(organization_id=self.organization.id)
+
+ response = self._do_request(f"{url}?token={self.team.api_token}")
+
+ assert response.status_code == status.HTTP_403_FORBIDDEN, response.json()
def test_allows_user_me_read_access(self):
# The /users/@me/ endpoint is not team-based, but it's useful as a way of checking whether the key works
diff --git a/products/feature_flags/backend/api/test/test_feature_flag.py b/products/feature_flags/backend/api/test/test_feature_flag.py
index 82d1c5fa3397..38336a770362 100644
--- a/products/feature_flags/backend/api/test/test_feature_flag.py
+++ b/products/feature_flags/backend/api/test/test_feature_flag.py
@@ -2292,6 +2292,19 @@ class TestFeatureFlag(APIBaseTest, ClickhouseTestMixin):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json(), '{"test": true}')
+ def test_get_remote_config_with_secret_api_token_of_second_environment(self):
+ environment = Team.objects.create(organization=self.organization, project=self.team.project)
+ environment.rotate_secret_token_and_save(user=self.user, is_impersonated_session=False)
+ self._create_remote_config_flag()
+ self.client.logout()
+
+ response = self.client.get(
+ f"/api/projects/{self.team.id}/feature_flags/my-remote-config-flag/remote_config?token={environment.api_token}",
+ headers={"authorization": f"Bearer {environment.secret_api_token}"},
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+
def _create_remote_config_flag(self, key: str = "my-remote-config-flag") -> None:
FeatureFlag.objects.create(
team=self.team,