: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(insights): resolve warehouse access control in system queries (#67539)
Javier Bahamondes66e58047939a
diff --git a/posthog/hogql/printer/test/test_hogql_access_control.py b/posthog/hogql/printer/test/test_hogql_access_control.pyindex 4a9c36e26cc6..cfb02ab710cf 100644--- a/posthog/hogql/printer/test/test_hogql_access_control.py+++ b/posthog/hogql/printer/test/test_hogql_access_control.py@@ -498,6 +498,20 @@ class TestWarehouseTableAccessControl(BaseTest):assert "denied_table" not in database._denied_tablesassert "allowed_table" not in database._denied_tables+ def test_to_printed_hogql_bypass_prints_warehouse_table_userless(self):+ # Guards the fail-closed fix: query runners print the response HogQL userless right after the+ # user-scoped execute. Without the bypass, that print fails closed on a warehouse table; the param+ # must reach the database so warehouse-backed insights don't 500 on the print.+ from posthog.hogql.errors import QueryError+ from posthog.hogql.parser import parse_select+ from posthog.hogql.printer import to_printed_hogql++ query = parse_select("SELECT id FROM allowed_table")+ with self.assertRaises(QueryError):+ to_printed_hogql(query, self.team)+ printed = to_printed_hogql(query, self.team, bypass_warehouse_access_control=True)+ assert "allowed_table" in printed+def test_denied_table_lookup_raises_access_error(self):from posthog.hogql.errors import QueryErrordiff --git a/posthog/hogql/printer/utils.py b/posthog/hogql/printer/utils.pyindex b4042ad3f64a..ddbcc07baab0 100644--- a/posthog/hogql/printer/utils.py+++ b/posthog/hogql/printer/utils.py@@ -64,7 +64,13 @@ PRINTER_CLASSES: dict[HogQLDialect, type[BasePrinter]] = {}-def to_printed_hogql(query: ast.Expr, team: Team, modifiers: "HogQLQueryModifiers | None" = None) -> str:+def to_printed_hogql(+ query: ast.Expr,+ team: Team,+ modifiers: "HogQLQueryModifiers | None" = None,+ *,+ bypass_warehouse_access_control: bool = False,+) -> str:"""Prints the HogQL query without mutating the node"""return prepare_and_print_ast(clone_expr(query),@@ -73,6 +79,7 @@ def to_printed_hogql(query: ast.Expr, team: Team, modifiers: "HogQLQueryModifierteam_id=team.pk,enable_select_queries=True,modifiers=create_default_modifiers_for_team(team, modifiers),+ bypass_warehouse_access_control=bypass_warehouse_access_control,),pretty=True,)[0]diff --git a/posthog/hogql_queries/insights/funnels/funnel_correlation_query_runner.py b/posthog/hogql_queries/insights/funnels/funnel_correlation_query_runner.pyindex 0e0478039f5e..bbb27c1fe9b9 100644--- a/posthog/hogql_queries/insights/funnels/funnel_correlation_query_runner.py+++ b/posthog/hogql_queries/insights/funnels/funnel_correlation_query_runner.py@@ -237,7 +237,8 @@ class FunnelCorrelationQueryRunner(AnalyticsQueryRunner[FunnelCorrelationResponsdef _calculate_internal(self) -> tuple[list[EventOddsRatio], bool, str, HogQLQueryResponse]:query = self.to_query()- hogql = to_printed_hogql(query, self.team)+ # Display-only response HogQL (never executed); bypass warehouse ACL so printing doesn't fail closed userless.+ hogql = to_printed_hogql(query, self.team, bypass_warehouse_access_control=True)response = execute_hogql_query(query_type="FunnelsQuery",diff --git a/posthog/hogql_queries/insights/funnels/funnels_query_runner.py b/posthog/hogql_queries/insights/funnels/funnels_query_runner.pyindex af5bbbfab7dd..20396378d9d0 100644--- a/posthog/hogql_queries/insights/funnels/funnels_query_runner.py+++ b/posthog/hogql_queries/insights/funnels/funnels_query_runner.py@@ -146,7 +146,8 @@ class FunnelsQueryRunner(AnalyticsQueryRunner[FunnelsQueryResponse]):timings = []# TODO: can we get this from execute_hogql_query as well?- hogql = to_printed_hogql(query, self.team)+ # Display-only response HogQL (never executed); bypass warehouse ACL so printing doesn't fail closed userless.+ hogql = to_printed_hogql(query, self.team, bypass_warehouse_access_control=True)response = execute_hogql_query(query_type="FunnelsQuery",diff --git a/posthog/hogql_queries/insights/lifecycle/lifecycle_query_runner.py b/posthog/hogql_queries/insights/lifecycle/lifecycle_query_runner.pyindex 032f744a9ed2..57027f731848 100644--- a/posthog/hogql_queries/insights/lifecycle/lifecycle_query_runner.py+++ b/posthog/hogql_queries/insights/lifecycle/lifecycle_query_runner.py@@ -164,7 +164,8 @@ class LifecycleQueryRunner(AnalyticsQueryRunner[LifecycleQueryResponse]):def _calculate(self) -> LifecycleQueryResponse:query = self.to_query()- hogql = to_printed_hogql(query, self.team)+ # Display-only response HogQL (never executed); bypass warehouse ACL so printing doesn't fail closed userless.+ hogql = to_printed_hogql(query, self.team, bypass_warehouse_access_control=True)response = execute_hogql_query(query_type="LifecycleQuery",diff --git a/posthog/hogql_queries/insights/retention/retention_query_runner.py b/posthog/hogql_queries/insights/retention/retention_query_runner.pyindex 2afa8b80b92a..7a9bcd532039 100644--- a/posthog/hogql_queries/insights/retention/retention_query_runner.py+++ b/posthog/hogql_queries/insights/retention/retention_query_runner.py@@ -597,7 +597,8 @@ class RetentionQueryRunner(AnalyticsQueryRunner[RetentionQueryResponse]):def _calculate(self) -> RetentionQueryResponse:query = self.to_query()- hogql = to_printed_hogql(query, self.team)+ # Display-only response HogQL (never executed); bypass warehouse ACL so printing doesn't fail closed userless.+ hogql = to_printed_hogql(query, self.team, bypass_warehouse_access_control=True)response = execute_hogql_query(query_type="RetentionQuery",diff --git a/posthog/hogql_queries/insights/utils/utils.py b/posthog/hogql_queries/insights/utils/utils.pyindex a9a06acaac3f..b533b9b432fb 100644--- a/posthog/hogql_queries/insights/utils/utils.py+++ b/posthog/hogql_queries/insights/utils/utils.py@@ -81,5 +81,8 @@ def get_response_hogql(response_hogql_query = ast.SelectSetQuery.create_from_queries(queries, "UNION ALL")+ # This only prints the query for the response payload — it never executes — and access to the+ # underlying warehouse tables is already enforced on the insight's executed query. Bypass warehouse+ # access control so building the printer's database doesn't fail closed in userless contexts.with timings.measure("printing_hogql_for_response"):- return to_printed_hogql(response_hogql_query, team, modifiers)+ return to_printed_hogql(response_hogql_query, team, modifiers, bypass_warehouse_access_control=True)diff --git a/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py b/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.pyindex 22c754167a15..6e8867b51a09 100644--- a/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py+++ b/products/analytics_platform/backend/lazy_computation/lazy_computation_executor.py@@ -1286,13 +1286,18 @@ def _build_manual_insert_sql(expires_at_expr = ast.Alias(alias="expires_at", expr=ast.Constant(value=ch_expires_at))query.select.append(expires_at_expr)- # Print to SQL+ # Print to SQL. Materialization is a system, team-scoped process with no request user, so bypass+ # warehouse access control to avoid failing closed in this userless context. Caveat: the native+ # pre-agg table this writes is later read WITHOUT warehouse RBAC, so materialization does not enforce+ # per-source access on reads — a user with dashboard access can see aggregates from warehouse sources+ # they can't query directly. Whether the pre-agg read should re-check source access is an open question.context = HogQLContext(team_id=team.id,team=team,enable_select_queries=True,limit_top_select=False,modifiers=create_default_modifiers_for_team(team),+ bypass_warehouse_access_control=True,)select_sql, _ = prepare_and_print_ast(query,diff --git a/products/product_analytics/backend/hogql_queries/paths/paths_query_runner.py b/products/product_analytics/backend/hogql_queries/paths/paths_query_runner.pyindex 87db2d1627ed..8f47593b4630 100644--- a/products/product_analytics/backend/hogql_queries/paths/paths_query_runner.py+++ b/products/product_analytics/backend/hogql_queries/paths/paths_query_runner.py@@ -887,7 +887,8 @@ class PathsQueryRunner(AnalyticsQueryRunner[PathsQueryResponse]):def _calculate(self) -> PathsQueryResponse:query = self.to_query()- hogql = to_printed_hogql(query, self.team)+ # Display-only response HogQL (never executed); bypass warehouse ACL so printing doesn't fail closed userless.+ hogql = to_printed_hogql(query, self.team, bypass_warehouse_access_control=True)response = execute_hogql_query(query_type="PathsQuery",