: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(sharing): scope the insight share lookup to the team (#91922)
Thomas Obermüller5829fa2f019e
diff --git a/posthog/api/sharing_publish_gate.py b/posthog/api/sharing_publish_gate.pyindex f2da7b0c92f4..9ffecd0c5f67 100644--- a/posthog/api/sharing_publish_gate.py+++ b/posthog/api/sharing_publish_gate.py@@ -121,23 +121,30 @@ def is_publicly_shared(artifact: "Dashboard | Notebook | Insight") -> bool:their own share; an insight also transitively - by a shared dashboard's tile or a sharednotebook embedding it."""if isinstance(artifact, Insight):- live_tile = Q(dashboard__tiles__insight=artifact) & (- Q(dashboard__tiles__deleted__isnull=True) | Q(dashboard__tiles__deleted=False)+ team_shares = SharingConfiguration.objects.filter(+ SharingConfiguration.tokens_active_q(), team_id=artifact.team_id)- if SharingConfiguration.objects.filter(- SharingConfiguration.tokens_active_q(), Q(insight=artifact) | live_tile+ # Each exposure route is a separate indexed EXISTS scoped to the team. Written as one+ # unscoped OR over the tile join, Postgres left-joins every active share across all teams+ # to every tile before it can apply the predicate.+ if team_shares.filter(insight=artifact).exists():+ return True+ # Both tile conditions stay in one filter() call so they match the same joined row.+ if team_shares.filter(+ Q(dashboard__tiles__deleted__isnull=True) | Q(dashboard__tiles__deleted=False),+ dashboard__tiles__insight=artifact,).exists():return True# Notebooks reference insights inside their content JSON, so the few active notebook# shares per team are scanned rather than joined.return any(artifact.short_id in extract_referenced_insight_short_ids(content)- for content in SharingConfiguration.objects.filter(- SharingConfiguration.tokens_active_q(), team_id=artifact.team_id, notebook__isnull=False- ).values_list("notebook__content", flat=True)+ for content in team_shares.filter(notebook__isnull=False).values_list("notebook__content", flat=True))field = "dashboard" if isinstance(artifact, Dashboard) else "notebook"- return SharingConfiguration.objects.filter(SharingConfiguration.tokens_active_q(), **{field: artifact}).exists()+ return SharingConfiguration.objects.filter(+ SharingConfiguration.tokens_active_q(), team_id=artifact.team_id, **{field: artifact}+ ).exists()def blocked_access_in_notebook_edit(user: User, notebook: Any, new_content: dict[str, Any] | None) -> list[str]:diff --git a/posthog/api/test/test_sharing.py b/posthog/api/test/test_sharing.pyindex c656eae1aebd..627d8fff55aa 100644--- a/posthog/api/test/test_sharing.py+++ b/posthog/api/test/test_sharing.py@@ -2319,8 +2319,22 @@ class TestSaveTimeAccessBlock(APIBaseTest):"source": {"kind": "HogQLQuery", "query": "SELECT 1 AS one"},}- def test_query_update_allowed_when_not_shared(self):+ @parameterized.expand([("no_share",), ("deleted_tile",)])+ def test_query_update_allowed_when_not_shared(self, coverage: str):self._deny_editor()+ if coverage == "deleted_tile":+ dashboard = Dashboard.objects.create(team=self.team, created_by=self.user)+ # The shared dashboard also carries a live tile for another insight. A share lookup+ # that matched the deleted tile and the live tile as two separate rows would report+ # this insight as shared.+ other_insight = Insight.objects.create(+ team=self.team,+ query={"kind": "DataTableNode", "source": {"kind": "HogQLQuery", "query": "SELECT 2 AS two"}},+ created_by=self.user,+ )+ DashboardTile.objects.create(dashboard=dashboard, insight=other_insight)+ DashboardTile.objects.create(dashboard=dashboard, insight=self.insight, deleted=True)+ SharingConfiguration.objects.create(team=self.team, dashboard=dashboard, enabled=True)response = self._patch_insight_query()