: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
chore(product-analytics): document insight access rules (#104693)
Matt Pua365775a2760b
diff --git a/products/product_analytics/AGENTS.md b/products/product_analytics/AGENTS.mdindex ddd6f1987799..70f1d4b9d8ad 100644--- a/products/product_analytics/AGENTS.md+++ b/products/product_analytics/AGENTS.md@@ -1,5 +1,35 @@# Product analytics agent guide+## Insight access, sharing, and project scope++These rules apply when changing an insight endpoint, a bulk insight action, or a shared insight render.++Insights belong to a project, although the API serves them under an environment URL.+Read and write queries must use `team__project_id=self.team.project_id` where they operate on the insight set.+Do not restrict a project-level insight operation to only `team_id=self.team_id`.++### Authorization contract++- `InsightViewSet` filters normal `list` reads through `_filter_queryset_by_access_level`. Custom list-like actions need an access-aware queryset implementation because this helper only filters when `self.action == "list"`.+- On Enterprise installs, `EnterpriseInsightsViewSet` adds `CanEditInsight` for unsafe detail actions. A new unsafe detail action must keep object permission checks.+- Detail-false bulk actions do not receive DRF object permissions. Filter every target through `_bulk_filter_editable_insights`, or perform the equivalent per-insight editor check. Return skipped targets without changing them.+- Restore access to an insight does not grant edit access to every dashboard that contains it. Use `restore_tiles_for_insights` with `user_permissions`, so tiles remain hidden on dashboards the requester cannot edit.+- For an action that supports personal API keys, OAuth, or MCP, declare `required_scopes` or add it to the view set scope-action list. Otherwise, API-key callers receive a 403 response.++### Shared-link contract++Sharing authentication only permits insight `retrieve` and `list` for insights connected to its sharing configuration.+Do not add write, mutation, or project-wide actions to `sharing_enabled_actions`.+When an insight renders with `from_dashboard`, use the shared configuration's dashboard ID to validate the tile. Do not use the requester's normal dashboard access in that path.+Insight variables cannot use sharing authentication. Keep this rejection in `InsightVariableViewSet.initial` for every variable action.++### Where to make changes++- Read [the insight API](backend/presentation/insight.py) when changing access, sharing, bulk actions, or project scope.+- Read [the Enterprise permission wrapper](backend/presentation/insight_ee.py) when changing unsafe insight actions.+- Read [the insight-variable API](backend/presentation/insight_variable.py) when changing variable access.+- Extend [the existing insight API tests](backend/tests/api/test_insight.py) for object access, shared links, or bulk access. Cover inaccessible targets and dashboard tiles that must remain hidden.+## Insight deletion and related resourcesThese rules apply when changing insight deletion, restoration, or a resource attached to an insight.