: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(hogql): load the database schema when a join target is denied (#110845)
Alex Liderc0f7ee2dc1bb
diff --git a/posthog/hogql/database/database.py b/posthog/hogql/database/database.pyindex 9c61f2a05310..94a1736e33f5 100644--- a/posthog/hogql/database/database.py+++ b/posthog/hogql/database/database.py@@ -3394,6 +3394,16 @@ def _settled_catalog_certifications(return {}, {}+def _resolve_readable_join(join: LazyJoin, context: HogQLContext) -> Table | None:+ """The join's target table, or None when the user cannot read it. The database keeps a join+ to a denied table so that a query that uses the join raises the access error, but the schema+ must not list what the user cannot read."""+ try:+ return join.resolve_table(context)+ except TableAccessDeniedError:+ return None++def serialize_fields(field_input,context: HogQLContext,@@ -3564,7 +3574,9 @@ def serialize_fields())elif isinstance(field, LazyJoin):- resolved_table = field.resolve_table(context)+ resolved_table = _resolve_readable_join(field, context)+ if resolved_table is None:+ continueif isinstance(resolved_table, SavedQuery):type = DatabaseSerializedFieldType.VIEW@@ -3579,8 +3591,12 @@ def serialize_fields(hogql_value=hogql_value,type=type,schema_valid=schema_valid,- table=field.resolve_table(context).to_printed_hogql(),- fields=list(field.resolve_table(context).fields.keys()),+ table=resolved_table.to_printed_hogql(),+ fields=[+ name+ for name, nested in resolved_table.fields.items()+ if not isinstance(nested, LazyJoin) or _resolve_readable_join(nested, context) is not None+ ],id=id or field_key,))diff --git a/posthog/hogql/printer/test/test_hogql_access_control.py b/posthog/hogql/printer/test/test_hogql_access_control.pyindex 11a4a358eca8..acc4d4de013a 100644--- a/posthog/hogql/printer/test/test_hogql_access_control.py+++ b/posthog/hogql/printer/test/test_hogql_access_control.py@@ -947,6 +947,47 @@ class TestWarehouseAccessControlEndToEnd(BaseTest):)assert cm.exception.table_name == "denied_warehouse_table"+ def test_schema_serialization_omits_a_join_to_a_denied_warehouse_table(self):+ from products.access_control.backend.models.access_control import AccessControl+ from products.data_tools.backend.models.join import DataWarehouseJoin++ DataWarehouseJoin.objects.create(+ team=self.team,+ source_table_name="persons",+ source_table_key="properties.email",+ joining_table_name="denied_warehouse_table",+ joining_table_key="id",+ field_name="denied_join",+ )++ def serialized_fields() -> tuple[set[str], set[str]]:+ database = Database.create_for(team=self.team, user=self.user)+ context = HogQLContext(team_id=self.team.pk, team=self.team, database=database, user=self.user)+ serialized = database.serialize(+ context, include_only={"persons", "events"}, include_hidden_posthog_tables=True+ )+ persons_fields = set(serialized["persons"].fields.keys())+ # events.person is itself a lazy join, so its nested field list names the persons joins.+ person_join_fields = set(serialized["events"].fields["person"].fields or [])+ return persons_fields, person_join_fields++ persons_fields, person_join_fields = serialized_fields()+ assert "denied_join" in persons_fields+ assert "denied_join" in person_join_fields++ AccessControl.objects.create(+ team=self.team,+ resource="warehouse_table",+ resource_id=str(self.denied_table.id),+ access_level="none",+ organization_member=self.membership,+ )++ persons_fields, person_join_fields = serialized_fields()+ assert "denied_join" not in persons_fields+ assert "id" in persons_fields+ assert "denied_join" not in person_join_fields+def test_execute_hogql_query_bypass_warehouse_access_control_skips_denial(self):"""bypass_warehouse_access_control opt-in should let the query past the access control gate(downstream may still fail because there's no real S3 data, but the