: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(customer-analytics): bypass warehouse view access control in userless sync (#67846)
Arthur Moreira de Deusb3d7ed0c7620
diff --git a/products/customer_analytics/backend/logic/custom_property_sync.py b/products/customer_analytics/backend/logic/custom_property_sync.pyindex 5eabc45e4081..49f8df2a1839 100644--- a/products/customer_analytics/backend/logic/custom_property_sync.py+++ b/products/customer_analytics/backend/logic/custom_property_sync.py@@ -129,7 +129,9 @@ def _read_view(team: Team, view_name: str, columns: list[str]) -> list:select_from=ast.JoinExpr(table=ast.Field(chain=[view_name])),)with tags_context(product=Product.CUSTOMER_ANALYTICS, feature=Feature.ACCOUNTS, team_id=team.pk):- return execute_hogql_query(query, team=team).results or []+ # Runs as a userless system sync, so bypass user-scoped warehouse-view access control+ # (it fails closed without a user); tenant isolation still holds via team.+ return execute_hogql_query(query, team=team, bypass_warehouse_access_control=True).results or []def record_sync_outcome(diff --git a/products/customer_analytics/backend/logic/custom_property_sync_test.py b/products/customer_analytics/backend/logic/custom_property_sync_test.pyindex e281f714015f..e3c932d5099b 100644--- a/products/customer_analytics/backend/logic/custom_property_sync_test.py+++ b/products/customer_analytics/backend/logic/custom_property_sync_test.py@@ -1,6 +1,6 @@import pytest-from posthog.test.base import BaseTest-from unittest.mock import patch+from posthog.test.base import BaseTest, ClickhouseTestMixin+from unittest.mock import Mock, patchfrom django.apps import apps@@ -8,6 +8,7 @@ from parameterized import parameterizedfrom products.customer_analytics.backend.logic.custom_property_sync import (MAX_CONSECUTIVE_SYNC_FAILURES,+ _read_view,record_sync_outcome,run_custom_property_sync,sync_custom_property_values,@@ -137,6 +138,23 @@ class CustomPropertySyncTest(TeamScopedTestMixin, BaseTest):mock_capture.assert_called_once()+@patch("posthoganalytics.feature_enabled", new=Mock(return_value=True))+class ReadViewAccessControlTest(ClickhouseTestMixin, TeamScopedTestMixin, BaseTest):+ def test_userless_sync_reads_view_despite_warehouse_access_control(self):+ # The Celery sync runs with no user, so HogQL warehouse-view access control (flag on)+ # fails closed and denies the view unless the sync bypasses it.+ view = DataWarehouseSavedQuery.objects.create(+ team=self.team,+ name="account_health_scores",+ query={"kind": "HogQLQuery", "query": "SELECT 'acme' AS org_id, 100 AS health_score"},+ columns={"org_id": "String", "health_score": "Int64"},+ )++ rows = _read_view(self.team, view.name, ["health_score", "org_id"])++ assert rows == [(100, "acme")]++class RecordSyncOutcomeTest(TeamScopedTestMixin, BaseTest):def setUp(self):super().setUp()