Skip to content

: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.

fix(data-warehouse): hide denied warehouse objects from metadata listings (#116591)

Daniel RCae6b457e1190
diff --git a/posthog/hogql/ACCESS_CONTROL.md b/posthog/hogql/ACCESS_CONTROL.md
index 63d2e72bb061..eeea2e7f38d6 100644
--- a/posthog/hogql/ACCESS_CONTROL.md
+++ b/posthog/hogql/ACCESS_CONTROL.md
@@ -89,6 +89,10 @@ For these, denied objects are filtered out of the results.
At print time the printer appends a guard that excludes the user's denied object IDs by primary key — effectively `WHERE <id> NOT IN (<denied ids>)`.
Child tables filter on the parent's FK — e.g. `system.dashboard_tiles` uses `access_control_id_field="dashboard_id"`.
+A table whose rows can also be denied by a rule on another object overrides `PostgresTable.blocked_object_ids`.
+`system.data_warehouse_tables` does this: it resolves each table through its source, as described in [how a table's access level resolves](#how-a-tables-access-level-resolves), so denying a source also hides its tables there.
+A child table drops the creator exemption by default, because its rows carry no creator of the parent.
+A child table that must keep it overrides `PostgresTable.creator_owned_object_ids`: `system.data_modeling_jobs` returns the views the reader created, so the creator of a denied view still sees its jobs.
When rows are filtered, the response carries a warning: "Results may exclude {resources} you don't have access to".
diff --git a/posthog/hogql/database/postgres_table.py b/posthog/hogql/database/postgres_table.py
index fb3361097963..6037bcfbe00f 100644
--- a/posthog/hogql/database/postgres_table.py
+++ b/posthog/hogql/database/postgres_table.py
@@ -21,6 +21,8 @@ from posthog.scopes import APIScopeObject
if TYPE_CHECKING:
from posthog.models.team.team import Team
+ from products.access_control.backend.facade.user_access_control import UserAccessControl
+
@cache
def _pk_column_for_pg_table(postgres_table_name: str) -> Optional[str]:
@@ -179,6 +181,27 @@ class PostgresTable(FunctionCallTable):
return None
return self.access_control_creator_id_field
+ def blocked_object_ids(self, user_access_control: "UserAccessControl", team_id: Optional[int]) -> frozenset[str]:
+ """Ids in the `access_control_id` column that the user is denied.
+
+ The default reads the rules on the objects of `access_scope`. A table overrides it when a
+ rule on another object also denies its rows, because those rules do not name the rows.
+ """
+ if self.access_scope is None:
+ return frozenset()
+ return user_access_control.blocked_resource_ids_by_scope.get(self.access_scope, frozenset())
+
+ def creator_owned_object_ids(
+ self, user_access_control: "UserAccessControl", team_id: Optional[int], candidate_ids: Optional[frozenset[str]]
+ ) -> frozenset[str]:
+ """Ids in the `access_control_id` column of objects the user created, among `candidate_ids`
+ (or among all ids when it is None).
+
+ The guard keeps these rows, so a child table can follow its parent's creator exemption. The
+ default is empty: a child row carries no creator of the parent to compare against.
+ """
+ return frozenset()
+
def to_printed_hogql(self):
return escape_hogql_identifier(self.name)
diff --git a/posthog/hogql/database/schema/system.py b/posthog/hogql/database/schema/system.py
index ed9bf1b36dbc..923718833797 100644
--- a/posthog/hogql/database/schema/system.py
+++ b/posthog/hogql/database/schema/system.py
@@ -40,6 +40,8 @@ from posthog.scopes import APIScopeObject
if TYPE_CHECKING:
from posthog.models.team.team import Team
+ from products.access_control.backend.facade.user_access_control import UserAccessControl
+
from products.aeo.backend.facade.hogql import aeo_citation_checks
from products.customer_analytics.backend.facade.customer_tasks_hogql import customer_tasks
from products.customer_analytics.backend.facade.hogql import (
@@ -774,6 +776,8 @@ data_warehouse_sources: PostgresTable = PostgresTable(
data_modeling_views: PostgresTable = PostgresTable(
name="data_modeling_views",
postgres_table_name="posthog_datawarehousesavedquery",
+ access_scope="warehouse_view",
+ access_control_creator_id_field="created_by_id",
description="Saved queries / data-modeling views built on top of warehouse data; one row per view, optionally materialized.",
fields={
"id": StringDatabaseField(name="id", description="View UUID."),
@@ -802,14 +806,33 @@ data_modeling_views: PostgresTable = PostgresTable(
"deleted_at": DateTimeDatabaseField(
name="deleted_at", description="When the view was deleted; NULL if not deleted."
),
+ "created_by_id": IntegerDatabaseField(
+ name="created_by_id", nullable=True, description="User who created the view."
+ ),
"created_at": DateTimeDatabaseField(name="created_at", description="When the view was created."),
"updated_at": DateTimeDatabaseField(name="updated_at", description="When the view was last updated."),
},
)
-data_warehouse_tables: PostgresTable = PostgresTable(
+
+class _DataWarehouseTablesTable(PostgresTable):
+ def blocked_object_ids(self, user_access_control: "UserAccessControl", team_id: Optional[int]) -> frozenset[str]:
+ # A table resolves its access through its source, so a rule on a source denies tables that
+ # no rule names. Only the per-table resolution finds them.
+ from products.warehouse_sources.backend.facade.api import (
+ denied_table_ids, # noqa: PLC0415 — keeps the ORM off this module's import path
+ )
+
+ if team_id is None:
+ raise ValueError("system.data_warehouse_tables needs a team to resolve table access")
+ return frozenset(str(table_id) for table_id in denied_table_ids(team_id, user_access_control))
+
+
+data_warehouse_tables: _DataWarehouseTablesTable = _DataWarehouseTablesTable(
name="data_warehouse_tables",
postgres_table_name="posthog_datawarehousetable",
+ access_scope="warehouse_table",
+ access_control_creator_id_field="created_by_id",
description="Tables belonging to a data warehouse source; one row per table. A row whose source is a "
"direct connection is not queryable by name from this catalog — query it through that connection "
"instead, using the source id as the query's connection id.",
@@ -823,6 +846,9 @@ data_warehouse_tables: PostgresTable = PostgresTable(
name="external_data_source_id",
description="Source that produced this table; joins to data_warehouse_sources.id.",
),
+ "created_by_id": IntegerDatabaseField(
+ name="created_by_id", nullable=True, description="User who created the table."
+ ),
"created_at": DateTimeDatabaseField(name="created_at", description="When the table was first synced."),
"updated_at": DateTimeDatabaseField(name="updated_at", description="When the table metadata was last updated."),
"_deleted": BooleanDatabaseField(name="deleted", hidden=True),
@@ -1993,9 +2019,28 @@ notebooks: PostgresTable = PostgresTable(
},
)
-data_modeling_jobs: PostgresTable = PostgresTable(
+
+class _DataModelingJobsTable(PostgresTable):
+ def creator_owned_object_ids(
+ self, user_access_control: "UserAccessControl", team_id: Optional[int], candidate_ids: Optional[frozenset[str]]
+ ) -> frozenset[str]:
+ # The creator of a view keeps it under a deny rule, so they keep the jobs that ran for it.
+ from products.data_modeling.backend.facade.api import (
+ saved_query_ids_created_by, # noqa: PLC0415 — keeps the ORM off this module's import path
+ )
+
+ if team_id is None:
+ raise ValueError("system.data_modeling_jobs needs a team to resolve view access")
+ owned = saved_query_ids_created_by(team_id, user_access_control.user.pk, ids=candidate_ids)
+ return frozenset(str(view_id) for view_id in owned)
+
+
+data_modeling_jobs: _DataModelingJobsTable = _DataModelingJobsTable(
name="data_modeling_jobs",
postgres_table_name="posthog_datamodelingjob",
+ access_scope="warehouse_view",
+ # Child of warehouse_view: object-level access control applies to the view the job ran for.
+ access_control_id_field="saved_query_id",
description="Materialization job runs for data-modeling views; one row per run that materializes a view.",
fields={
"id": StringDatabaseField(name="id", description="Job UUID."),
@@ -2003,6 +2048,8 @@ data_modeling_jobs: PostgresTable = PostgresTable(
"data_modeling_view_id": StringDatabaseField(
name="saved_query_id", description="View being materialized; joins to data_modeling_views.id."
),
+ # The access control guard reads the column under its own name.
+ "saved_query_id": StringDatabaseField(name="saved_query_id", hidden=True),
"status": StringDatabaseField(name="status", description="Job status, e.g. Running, Completed, Failed."),
"rows_materialized": IntegerDatabaseField(
name="rows_materialized", description="Number of rows written so far."
diff --git a/posthog/hogql/database/test/__snapshots__/test_database.ambr b/posthog/hogql/database/test/__snapshots__/test_database.ambr
index d8853e158e22..3077b9a516f5 100644
--- a/posthog/hogql/database/test/__snapshots__/test_database.ambr
+++ b/posthog/hogql/database/test/__snapshots__/test_database.ambr
@@ -5524,6 +5524,16 @@
"table": null,
"type": "datetime"
},
+ "created_by_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "created_by_id",
+ "id": null,
+ "name": "created_by_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "integer"
+ },
"created_at": {
"chain": null,
"fields": null,
@@ -5950,6 +5960,16 @@
"table": null,
"type": "string"
},
+ "created_by_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "created_by_id",
+ "id": null,
+ "name": "created_by_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "integer"
+ },
"created_at": {
"chain": null,
"fields": null,
@@ -20152,6 +20172,16 @@
"table": null,
"type": "datetime"
},
+ "created_by_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "created_by_id",
+ "id": null,
+ "name": "created_by_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "integer"
+ },
"created_at": {
"chain": null,
"fields": null,
@@ -20578,6 +20608,16 @@
"table": null,
"type": "string"
},
+ "created_by_id": {
+ "chain": null,
+ "fields": null,
+ "hogql_value": "created_by_id",
+ "id": null,
+ "name": "created_by_id",
+ "schema_valid": true,
+ "table": null,
+ "type": "integer"
+ },
"created_at": {
"chain": null,
"fields": null,
diff --git a/posthog/hogql/printer/access_control.py b/posthog/hogql/printer/access_control.py
index cc31c9ab81cc..1873f3ba10f8 100644
--- a/posthog/hogql/printer/access_control.py
+++ b/posthog/hogql/printer/access_control.py
@@ -49,9 +49,10 @@ def build_access_control_guard(
Mirrors `UserAccessControl.filter_queryset_by_access_level`, the REST equivalent, branch for
branch: with no resource-level access the rows are narrowed to the user's explicit grants,
otherwise the objects denied to them are removed, and either way the objects they created stay
- visible. The id sets live on UserAccessControl (`allowlisted_resource_ids_by_scope` /
- `blocked_resource_ids_by_scope`) — single source of truth, shared with the cache-key fingerprint
- in query_runner.py.
+ visible. The grant set comes from `UserAccessControl.allowlisted_resource_ids_by_scope`, shared
+ with the cache-key fingerprint in query_runner.py. The deny set comes from
+ `table.blocked_object_ids`, and the parent objects a child table's reader created come from
+ `table.creator_owned_object_ids`. Both default to the rules on UserAccessControl.
"""
resource = table.access_scope
if not resource:
@@ -75,8 +76,24 @@ def build_access_control_guard(
right=ast.Constant(value=sorted(allowlisted_ids), is_sensitive=True),
type=ast.BooleanType(),
)
+ creator_owned_ids = table.creator_owned_object_ids(user_access_control, context.team_id, None) - allowlisted_ids
+ if creator_owned_ids:
+ guard = ast.Or(
+ exprs=[
+ guard,
+ ast.CompareOperation(
+ op=ast.CompareOperationOp.In,
+ left=_id_as_string(id_field, table_type),
+ right=ast.Constant(value=sorted(creator_owned_ids), is_sensitive=True),
+ type=ast.BooleanType(),
+ ),
+ ],
+ type=ast.BooleanType(),
+ )
else:
- blocked_ids = user_access_control.blocked_resource_ids_by_scope.get(resource, frozenset())
+ blocked_ids = table.blocked_object_ids(user_access_control, context.team_id)
+ if blocked_ids:
+ blocked_ids -= table.creator_owned_object_ids(user_access_control, context.team_id, blocked_ids)
if not blocked_ids:
return None
guard = ast.CompareOperation(
diff --git a/posthog/hogql/printer/test/test_hogql_access_control.py b/posthog/hogql/printer/test/test_hogql_access_control.py
index 88143bcf56ad..7c290dfaf57e 100644
--- a/posthog/hogql/printer/test/test_hogql_access_control.py
+++ b/posthog/hogql/printer/test/test_hogql_access_control.py
@@ -446,6 +446,72 @@ class TestRestParityForObjectGrants(BaseTest):
database = Database.create_for(team=self.team, user=self.user)
assert "system.message_categories" in database._denied_tables
+ @parameterized.expand([("data_modeling_views", "id"), ("data_modeling_jobs", "saved_query_id")])
+ def test_denied_view_is_filtered_from_its_metadata_tables(self, table: str, column: str) -> None:
+ view_id = "018f0000-0000-0000-0000-000000000002"
+ self._ac(
+ resource="warehouse_view", resource_id=view_id, access_level="none", organization_member=self.membership
+ )
+
+ sql, context = self._compile(f"SELECT status FROM system.{table}")
+
+ placeholders = self._id_list_placeholders(context)
+ assert list(placeholders.values()) == [[view_id]]
+ deny_key = next(iter(placeholders))
+ assert f"notIn(toString(system__{table}.{column}), %({deny_key})s)" in sql
+
+ def _own_views(self, *names: str) -> list:
+ from products.data_modeling.backend.facade.models import DataWarehouseSavedQuery
+
+ return [
+ DataWarehouseSavedQuery.objects.create(
+ team=self.team, name=name, query={"kind": "HogQLQuery", "query": "SELECT 1"}, created_by=self.user
+ )
+ for name in names
+ ]
+
+ def _view_rules(self, access_level: str, view_ids: list[str]) -> None:
+ for view_id in view_ids:
+ self._ac(
+ resource="warehouse_view",
+ resource_id=view_id,
+ access_level=access_level,
+ organization_member=self.membership,
+ )
+
+ def test_view_creator_drops_out_of_the_jobs_deny_list(self) -> None:
+ own_view, _own_unruled_view = self._own_views("own_view", "own_unruled_view")
+ other_view_id = "018f0000-0000-0000-0000-000000000003"
+ self._view_rules("none", [str(own_view.id), other_view_id])
+
+ sql, context = self._compile("SELECT status FROM system.data_modeling_jobs")
+
+ assert list(self._id_list_placeholders(context).values()) == [[other_view_id]]
+ assert "notIn(toString(system__data_modeling_jobs.saved_query_id)" in sql
+
+ def test_view_creator_denied_only_their_own_view_gets_no_jobs_guard(self) -> None:
+ (own_view,) = self._own_views("own_view")
+ self._view_rules("none", [str(own_view.id)])
+
+ sql, context = self._compile("SELECT status FROM system.data_modeling_jobs")
+
+ assert self._id_list_placeholders(context) == {}
+ assert "warehouse_view" not in context.access_control_restricted_resources
+
+ def test_view_creator_without_resource_access_keeps_the_jobs_of_their_views(self) -> None:
+ own_view, own_unruled_view = self._own_views("own_view", "own_unruled_view")
+ other_view_id = "018f0000-0000-0000-0000-000000000003"
+ self._ac(resource="warehouse_objects", access_level="none")
+ self._view_rules("viewer", [str(own_view.id), other_view_id])
+
+ sql, context = self._compile("SELECT status FROM system.data_modeling_jobs")
+
+ keys = {tuple(v): k for k, v in self._id_list_placeholders(context).items()}
+ grant_key = keys[tuple(sorted([str(own_view.id), other_view_id]))]
+ owned_key = keys[(str(own_unruled_view.id),)]
+ column = "toString(system__data_modeling_jobs.saved_query_id)"
+ assert f"or(in({column}, %({grant_key})s), in({column}, %({owned_key})s))" in sql
+
@parameterized.expand(
[
(f"{name}_{table}", resource_level, scope, parent, table, operator)
@@ -719,6 +785,55 @@ class TestWarehouseTableAccessControl(BaseTest):
# allowed_table has no source, so no rule about sources may reach it.
assert "allowed_table" not in database._denied_tables
+ def test_system_tables_listing_hides_denied_tables_and_tables_of_a_denied_source(self):
+ from posthog.hogql.parser import parse_select
+
+ from products.warehouse_sources.backend.facade.models import DataWarehouseTable, ExternalDataSource
+
+ source = ExternalDataSource.objects.create(
+ team=self.team,
+ source_id="src",
+ connection_id="conn",
+ destination_id="dest",
+ source_type="Stripe",
+ prefix="stripe_",
+ )
+ sourced_table = DataWarehouseTable.objects.create(
+ name="stripe_customers",
+ format=DataWarehouseTable.TableFormat.Parquet,
+ team=self.team,
+ credential=self.credential,
+ external_data_source=source,
+ url_pattern="s3://bucket/stripe/*",
+ columns={"id": "String"},
+ )
+ self._create_ac(
+ resource="external_data_source",
+ resource_id=str(source.id),
+ access_level="none",
+ member=self._membership(),
+ )
+ self._create_ac(
+ resource="warehouse_table",
+ resource_id=str(self.denied_table.id),
+ access_level="none",
+ member=self._membership(),
+ )
+
+ context = HogQLContext(team_id=self.team.pk, team=self.team, user=self.user, enable_select_queries=True)
+ prepared = prepare_ast_for_printing(
+ parse_select("SELECT name, columns FROM system.data_warehouse_tables"),
+ context=context,
+ dialect="clickhouse",
+ )
+ assert prepared is not None
+ sql = print_prepared_ast(prepared, context=context, dialect="clickhouse")
+
+ deny_lists = {k: v for k, v in context.values.items() if k.endswith("_sensitive") and isinstance(v, list)}
+ assert list(deny_lists.values()) == [sorted([str(sourced_table.id), str(self.denied_table.id)])]
+ deny_key = next(iter(deny_lists))
+ assert f"notIn(toString(system__data_warehouse_tables.id), %({deny_key})s)" in sql
+
def test_warehouse_objects_resource_none_denies_all_tables(self):
self._create_ac(resource="warehouse_objects", access_level="none")
@@ -727,6 +842,9 @@ class TestWarehouseTableAccessControl(BaseTest):
# Both tables become inaccessible because warehouse_table inherits warehouse_objects
assert "denied_table" in database._denied_tables
assert "allowed_table" in database._denied_tables
+ assert "system.data_warehouse_tables" in database._denied_tables
+ assert "system.data_modeling_views" in database._denied_tables
+ assert "system.data_modeling_jobs" in database._denied_tables
def test_warehouse_objects_resource_none_plus_specific_member_grant(self):
# User has no warehouse_objects access, but has explicit viewer on allowed_table.
diff --git a/posthog/hogql_queries/access_controlled_resources.py b/posthog/hogql_queries/access_controlled_resources.py
index 7de889b0436f..94f4fcae11c2 100644
--- a/posthog/hogql_queries/access_controlled_resources.py
+++ b/posthog/hogql_queries/access_controlled_resources.py
@@ -186,10 +186,10 @@ def queried_access_controlled_resources(
otherwise a denied user could be served an allowed user's cached rows on a cache hit (the hit
short-circuits the schema strip that would otherwise raise "You don't have access to table").
- `bypassed_scopes` are the scopes whose access control the principal bypasses. The parent such a
- scope falls back to through `RESOURCE_FALLBACK_MAP` is left out, because the principal never
- reaches the parent's rules; the parent still partitions the cache when a table carries that
- scope directly.
+ `bypassed_scopes` are the scopes whose access control the principal bypasses on warehouse objects.
+ A bypassed scope is left out, and so is the parent it falls back to through `RESOURCE_FALLBACK_MAP`,
+ because the principal never reaches those rules. A system table that carries a bypassed scope keeps
+ it: system tables gate such a principal on its readable scopes, not on warehouse access control.
`_catalog` carries the warehouse reads and the saved views already walked into nested calls. Each
view is walked once per fingerprint: its scopes join the top-level result the first time, so a
@@ -240,6 +240,7 @@ def queried_access_controlled_resources(
table_names = set(get_table_names(select))
system_scopes = {f"system.{name}": scope for name, scope in access_controlled_system_tables().items()}
scopes: set[str] = {system_scopes[name] for name in table_names if name in system_scopes}
+ scopes_from_system_tables = set(scopes)
# Cache partitioning runs before lazy joins resolve, so their resource scopes do not appear as table names yet.
if "system.accounts" in table_names and any(
@@ -319,12 +320,14 @@ def queried_access_controlled_resources(
if nested is None:
return None
scopes |= nested
+ # A nested result keeps a bypassed scope only when a system table carries it.
+ scopes_from_system_tables |= nested & bypassed_scopes
- return _with_fallback_parents(scopes, bypassed_scopes)
+ return _with_fallback_parents(scopes, bypassed_scopes) - (bypassed_scopes - scopes_from_system_tables)
# Structured queries can reference warehouse tables without a HogQLQuery in their tree.
return (
- _with_fallback_parents({"warehouse_table", "warehouse_view"}, bypassed_scopes)
+ _with_fallback_parents({"warehouse_table", "warehouse_view"}, bypassed_scopes) - bypassed_scopes
if _references_data_warehouse(query)
else set()
)
diff --git a/posthog/hogql_queries/query_runner.py b/posthog/hogql_queries/query_runner.py
index fa7acfbb80cb..ebe18cf3149f 100644
--- a/posthog/hogql_queries/query_runner.py
+++ b/posthog/hogql_queries/query_runner.py
@@ -3790,12 +3790,11 @@ class AnalyticsQueryRunner(QueryRunner, Generic[AR]):
# Non-real principals (service tokens, shared-link viewers) are scope-gated on system tables;
# partition on the readable scopes so a narrower token can't be served a broader principal's
- # cached result. Bypassed scopes are readable for them, and listing them as restricted would
- # collide with users who are genuinely denied those resources.
+ # cached result. `queried_resources` already leaves out the scopes they bypass.
if not isinstance(user, User):
if queried_resources is None:
return ["*"]
- restricted = queried_resources - user.readable_system_table_access_scopes() - self._bypassed_access_scopes
+ restricted = queried_resources - user.readable_system_table_access_scopes()
return sorted(restricted) or None
user_access_control = self.user_access_control
diff --git a/posthog/hogql_queries/test/test_access_controlled_resources.py b/posthog/hogql_queries/test/test_access_controlled_resources.py
index 82fd30d85a81..035f4396f50b 100644
--- a/posthog/hogql_queries/test/test_access_controlled_resources.py
+++ b/posthog/hogql_queries/test/test_access_controlled_resources.py
@@ -389,7 +389,7 @@ class TestQueriedAccessControlledResources(BaseTest):
result = queried_access_controlled_resources(
query, self.team, bypassed_scopes=frozenset({"warehouse_table"})
)
- assert result == {"warehouse_table", "notebook", "dashboard"}
+ assert result == {"notebook", "dashboard"}
def test_catalog_fetch_loads_only_name_fields(self):
source = ExternalDataSource.objects.create(
diff --git a/posthog/hogql_queries/test/test_query_runner.py b/posthog/hogql_queries/test/test_query_runner.py
index e5f5299152b3..25919d026c95 100644
--- a/posthog/hogql_queries/test/test_query_runner.py
+++ b/posthog/hogql_queries/test/test_query_runner.py
@@ -2184,6 +2184,18 @@ class TestQueryRunnerAccessControlFingerprint(BaseTest):
False,
),
("saved view over a system table", "select * from notebook_view", {"notebook"}, False),
+ (
+ "system table with a warehouse scope",
+ "select * from system.data_modeling_views",
+ {"warehouse_view"},
+ False,
+ ),
+ (
+ "saved view over a system table with a warehouse scope",
+ "select * from tables_view",
+ {"warehouse_table"},
+ False,
+ ),
]
)
def test_shared_link_viewer_partitions_only_on_scopes_a_table_carries(
@@ -2203,6 +2215,11 @@ class TestQueryRunnerAccessControlFingerprint(BaseTest):
name="notebook_view",
query={"kind": "HogQLQuery", "query": "select * from system.notebooks"},
)
+ DataWarehouseSavedQuery.objects.create(
+ team=self.team,
+ name="tables_view",
+ query={"kind": "HogQLQuery", "query": "select name from system.data_warehouse_tables"},
+ )
query = {"kind": "HogQLQuery", "query": sql}
shared_runner = HogQLQueryRunner(query=query, team=self.team, user=_shared_link_user(self.team))
user_runner = HogQLQueryRunner(query=query, team=self.team, user=self.user)
diff --git a/products/data_modeling/backend/facade/api.py b/products/data_modeling/backend/facade/api.py
index a37aadb0f3be..b1064857cc52 100644
--- a/products/data_modeling/backend/facade/api.py
+++ b/products/data_modeling/backend/facade/api.py
@@ -49,6 +49,7 @@ _LAZY = {
"all_saved_query_columns": "logic.saved_query_reads",
"all_saved_query_names": "logic.saved_query_reads",
"allowed_saved_query_ids": "logic.saved_query_reads",
+ "saved_query_ids_created_by": "logic.saved_query_reads",
"backing_table_ids_by_saved_query": "logic.saved_query_reads",
"reachable_node_ids": "logic.saved_query_reads",
"saved_query_definitions": "logic.saved_query_reads",
diff --git a/products/data_modeling/backend/logic/saved_query_reads.py b/products/data_modeling/backend/logic/saved_query_reads.py
index 29287a299b85..8aff3c43d896 100644
--- a/products/data_modeling/backend/logic/saved_query_reads.py
+++ b/products/data_modeling/backend/logic/saved_query_reads.py
@@ -13,6 +13,7 @@ from posthog.hogql.functions.prompt_jev import DECISION_FUNCTIONS, PromptJevFind
from posthog.hogql.parser import parse_select
from posthog.models.activity_logging.activity_log import ActivityLog
+from posthog.uuidt import UUIDT
from ..facade.contracts import SavedQueryDefinition, SavedQuerySummary, UpstreamTableRef
from ..models.data_modeling_job import (
@@ -163,6 +164,21 @@ def calls_decision_functions(hogql: str) -> bool:
return True
+def saved_query_ids_created_by(team_id: int, user_id: int, *, ids: Collection[str] | None = None) -> frozenset[UUID]:
+ """The saved queries this user created, soft-deleted ones included. One query.
+
+ ``ids`` narrows the lookup to those saved queries; ``None`` asks about every one. An id that is
+ not a UUID matches nothing.
+ """
+ candidates = DataWarehouseSavedQuery.objects.filter(team_id=team_id, created_by_id=user_id)
+ if ids is not None:
+ valid_ids = [UUID(value) for value in ids if UUIDT.is_valid_uuid(value)]
+ if not valid_ids:
+ return frozenset()
+ candidates = candidates.filter(id__in=valid_ids)
+ return frozenset(candidates.values_list("id", flat=True))
+
+
def allowed_saved_query_ids(
team_id: int,
user_access_control: "UserAccessControl",
diff --git a/products/data_warehouse/backend/presentation/views/table.py b/products/data_warehouse/backend/presentation/views/table.py
index 2fd76a80e45c..556edd64767e 100644
--- a/products/data_warehouse/backend/presentation/views/table.py
+++ b/products/data_warehouse/backend/presentation/views/table.py
@@ -3,6 +3,7 @@ import uuid
from typing import Any, cast
from django.conf import settings
+from django.db.models import QuerySet
import boto3
import posthoganalytics
@@ -28,6 +29,7 @@ from posthog.event_usage import EventSource, get_event_source, is_wizard_self_dr
from posthog.exceptions_capture import capture_exception
from posthog.models import Team
from posthog.models.user import User
+from posthog.permissions import is_service_auth
from products.access_control.backend.presentation.access_control import (
AccessControlViewSetMixin,
@@ -43,6 +45,7 @@ from products.warehouse_sources.backend.facade.api import (
SUPPORTED_FILE_FORMATS,
build_file_upload_s3_path,
build_file_upload_url_pattern,
+ denied_table_ids,
hosted_upload_s3_path,
)
from products.warehouse_sources.backend.facade.contracts import (
@@ -610,6 +613,18 @@ class TableViewSet(TeamAndOrgViewSetMixin, AccessControlViewSetMixin, viewsets.M
.order_by(self.ordering)
)
+ def _filter_queryset_by_access_level(self, queryset: QuerySet) -> QuerySet:
+ queryset = super()._filter_queryset_by_access_level(queryset)
+ if self.action != "list" or is_service_auth(self.request):
+ return queryset
+ # Without resource access the generic filter already keeps only explicit grants and the
+ # reader's own tables, and a rule on a source cannot override either.
+ if not self.user_access_control.has_resource_access("warehouse_table"):
+ return queryset
+ # The generic filter reads rules on tables only. A table also resolves through its source,
+ # so a rule that denies the source must hide its tables from the list too.
+ return queryset.exclude(id__in=denied_table_ids(self.team_id, self.user_access_control))
+
def destroy(self, request: request.Request, *args: Any, **kwargs: Any) -> response.Response:
instance: DataWarehouseTable = self.get_object()
diff --git a/products/data_warehouse/backend/test/__snapshots__/test_hogql_fixer_ai.ambr b/products/data_warehouse/backend/test/__snapshots__/test_hogql_fixer_ai.ambr
index e3fe3d667f6c..37e4f2d23d5b 100644
--- a/products/data_warehouse/backend/test/__snapshots__/test_hogql_fixer_ai.ambr
+++ b/products/data_warehouse/backend/test/__snapshots__/test_hogql_fixer_ai.ambr
@@ -110,7 +110,7 @@
This is a list of all the available tables in the database:
```
- ['events', 'groups', 'persons', 'sessions', 'logs', 'query_log', 'system.aeo_citation_checks', 'system.cohort_calculation_history', 'system.cohorts', 'system.data_modeling_jobs', 'system.data_modeling_views', 'system.data_warehouse_tables', 'system.exports', 'system.file_system', 'system.groups', 'system.group_type_mappings', 'system.information_schema.tables', 'system.information_schema.columns', 'system.information_schema.relationships', 'system.information_schema.data_types', 'system.information_schema.metrics', 'system.information_schema.certifications', 'system.information_schema.relationship_proposals', 'system.information_schema.data_quality_checks', 'system.information_schema.data_quality_check_runs', 'system.information_schema.data_quality_health', 'system.ingestion_warnings', 'system.insight_variables', 'system.tags', 'system.teams']
+ ['events', 'groups', 'persons', 'sessions', 'logs', 'query_log', 'system.aeo_citation_checks', 'system.cohort_calculation_history', 'system.cohorts', 'system.exports', 'system.file_system', 'system.groups', 'system.group_type_mappings', 'system.information_schema.tables', 'system.information_schema.columns', 'system.information_schema.relationships', 'system.information_schema.data_types', 'system.information_schema.metrics', 'system.information_schema.certifications', 'system.information_schema.relationship_proposals', 'system.information_schema.data_quality_checks', 'system.information_schema.data_quality_check_runs', 'system.information_schema.data_quality_health', 'system.ingestion_warnings', 'system.insight_variables', 'system.tags', 'system.teams']
```
`person` or `event` metadata unspecified above (emails, names, etc.) is stored in `properties` fields, accessed like: `properties.foo.bar`.
diff --git a/products/data_warehouse/backend/tests/api/test_table_access_control.py b/products/data_warehouse/backend/tests/api/test_table_access_control.py
index ed749c5ab0f4..8ddcc79b3ffe 100644
--- a/products/data_warehouse/backend/tests/api/test_table_access_control.py
+++ b/products/data_warehouse/backend/tests/api/test_table_access_control.py
@@ -7,7 +7,7 @@ from posthog.models.organization import OrganizationMembership
from posthog.models.user import User
from products.access_control.backend.models.access_control import AccessControl
-from products.warehouse_sources.backend.facade.models import DataWarehouseTable
+from products.warehouse_sources.backend.facade.models import DataWarehouseTable, ExternalDataSource
from products.warehouse_sources.backend.facade.testing import WarehouseAccessControlTestMixin
@@ -100,6 +100,43 @@ class TestDataWarehouseTableAccessControl(WarehouseAccessControlTestMixin):
self.assertIn(str(self.table.id), ids)
self.assertNotIn(str(other_table.id), ids)
+ @parameterized.expand([("the source", True), ("every source", False)])
+ def test_list_omits_tables_of_a_denied_source(self, _name: str, deny_one_source: bool) -> None:
+ source = ExternalDataSource.objects.create(
+ team=self.team,
+ source_id="src",
+ connection_id="conn",
+ destination_id="dest",
+ source_type="Stripe",
+ prefix="stripe_",
+ )
+ sourced_table = DataWarehouseTable.objects.create(
+ name="stripe_customers",
+ format=DataWarehouseTable.TableFormat.Parquet,
+ team=self.team,
+ external_data_source=source,
+ url_pattern="s3://bucket/stripe_customers",
+ columns={"name": "String"},
+ )
+ membership = OrganizationMembership.objects.get(user=self.viewer_user, organization=self.organization)
+ AccessControl.objects.create(
+ team=self.team,
+ resource="external_data_source",
+ resource_id=str(source.id) if deny_one_source else None,
+ access_level="none",
+ organization_member=membership,
+ )
+ self.client.force_login(self.viewer_user)
+
+ response = self.client.get(self._list_url())
+
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ ids = [t["id"] for t in response.json()["results"]]
+ self.assertIn(str(self.table.id), ids)
+ self.assertNotIn(str(sourced_table.id), ids)
+ detail = self.client.get(f"/api/environments/{self.team.pk}/warehouse_tables/{sourced_table.id}/")
+ self.assertEqual(detail.status_code, status.HTTP_403_FORBIDDEN)
+
def test_user_access_level_field_is_present(self):
self._create_access_control(self.viewer_user, access_level="viewer")
self.client.force_login(self.viewer_user)
diff --git a/products/warehouse_sources/backend/facade/api.py b/products/warehouse_sources/backend/facade/api.py
index b66e70c35ffa..20a1dd4d42b0 100644
--- a/products/warehouse_sources/backend/facade/api.py
+++ b/products/warehouse_sources/backend/facade/api.py
@@ -26,6 +26,8 @@ from uuid import UUID
from django.db.models import OuterRef, Prefetch, Q, QuerySet, Subquery
from django.utils import timezone
+from posthog.uuidt import UUIDT
+
# Source-agnostic storage contract for user-uploaded files — shared with the upload endpoint.
from products.warehouse_sources.backend.file_uploads import (
FILE_FORMAT_READ_HINTS,
@@ -93,6 +95,7 @@ __all__ = [
"discard_backing_table",
"record_table_row_count",
"allowed_table_ids",
+ "denied_table_ids",
"all_queryable_tables",
"all_queryable_table_columns",
"list_tables_for_source",
@@ -527,11 +530,49 @@ def _resolve_allowed_table_ids(
candidates = _DataWarehouseTable.raw_objects.queryable().filter(team_id=team_id)
if ids is not None:
candidates = candidates.filter(id__in=ids)
- tables = list(
+ return _allowed_tables(_tables_for_access_check(candidates), user_access_control, required_level)
+
+
+def denied_table_ids(team_id: int, user_access_control: "UserAccessControl") -> frozenset[UUID]:
+ """Every table this caller may not view.
+
+ Unlike :func:`allowed_table_ids`, this covers soft-deleted tables and tables of a soft-deleted
+ source, for surfaces that list those rows.
+ """
+ if user_access_control.is_organization_admin:
+ return frozenset()
+ candidates = _DataWarehouseTable.raw_objects.filter(team_id=team_id)
+ if user_access_control.has_resource_access("warehouse_table") and user_access_control.has_resource_access(
+ "external_data_source"
+ ):
+ # With access to both resources, only a rule on a table or on its source can deny the table,
+ # so the tables those rules name are the only candidates.
+ blocked = user_access_control.blocked_resource_ids_by_scope
+ # A rule's resource_id is free text, and one malformed id must not break the id__in lookup.
+ table_ids = [UUID(i) for i in blocked.get("warehouse_table", ()) if UUIDT.is_valid_uuid(i)]
+ source_ids = [UUID(i) for i in blocked.get("external_data_source", ()) if UUIDT.is_valid_uuid(i)]
+ if not table_ids and not source_ids:
+ return frozenset()
+ candidates = candidates.filter(Q(id__in=table_ids) | Q(external_data_source_id__in=source_ids))
+ tables = _tables_for_access_check(candidates)
+ allowed = _allowed_tables(tables, user_access_control, "viewer")
+ return frozenset(table.id for table in tables if table.id not in allowed)
+
+
+def _tables_for_access_check(candidates: QuerySet[_DataWarehouseTable]) -> list[_DataWarehouseTable]:
+ return list(
candidates.select_related("external_data_source").only(
"id", "created_by_id", "external_data_source_id", "external_data_source__id"
)
)
+
+
+def _allowed_tables(
+ tables: list[_DataWarehouseTable],
+ user_access_control: "UserAccessControl",
+ required_level: "AccessControlLevel",
+) -> frozenset[UUID]:
+ """The ids among ``tables`` this caller may reach. A table resolves through its source."""
sources = {
table.external_data_source_id: table.external_data_source
for table in tables
diff --git a/products/warehouse_sources/backend/tests/test_facade.py b/products/warehouse_sources/backend/tests/test_facade.py
index 31754f979417..e1b9bd45c70b 100644
--- a/products/warehouse_sources/backend/tests/test_facade.py
+++ b/products/warehouse_sources/backend/tests/test_facade.py
@@ -328,6 +328,9 @@ class TestWarehouseSourcesFacade(BaseTest):
with self.assertNumQueries(0):
assert api.allowed_table_ids(self.team.id, access, required_level=required_level) == expected
+ # Without resource access every table is a candidate, soft-deleted ones included.
+ assert api.denied_table_ids(self.team.id, access) == frozenset({overridden.id, ungranted.id})
+
narrowed = api.allowed_table_ids(
self.team.id, access, required_level=required_level, ids=[granted.id, ungranted.id]
)
@@ -342,6 +345,44 @@ class TestWarehouseSourcesFacade(BaseTest):
{self.table.id, granted.id, overridden.id, ungranted.id, owned.id}
)
+ @parameterized.expand([("legacy", False), ("most_specific", True)])
+ def test_denied_table_ids_resolves_rule_named_tables_through_their_source(
+ self, _name: str, most_specific: bool
+ ) -> None:
+ self.organization.available_product_features = [
+ {"key": AvailableFeature.ACCESS_CONTROL, "name": AvailableFeature.ACCESS_CONTROL}
+ ]
+ self.organization.uses_most_specific_access_resolution = most_specific
+ self.organization.save(update_fields=["available_product_features", "uses_most_specific_access_resolution"])
+ member = self._create_user("[email protected]")
+ membership = OrganizationMembership.objects.get(user=member, organization=self.organization)
+ access = UserAccessControl(member, team=self.team)
+ assert api.denied_table_ids(self.team.id, access) == frozenset()
+
+ granted = DataWarehouseTable.objects.create(
+ team=self.team, name="granted", format="Parquet", external_data_source=self.source
+ )
+ deleted = DataWarehouseTable.objects.create(
+ team=self.team, name="deleted", format="Parquet", external_data_source=self.source, deleted=True
+ )
+ denied = DataWarehouseTable.objects.create(team=self.team, name="denied", format="Parquet")
+ DataWarehouseTable.objects.create(team=self.team, name="untouched", format="Parquet")
+ for resource, resource_id, level in [
+ ("external_data_source", self.source.id, "none"),
+ ("warehouse_table", granted.id, "viewer"),
+ ("warehouse_table", denied.id, "none"),
+ ]:
+ AccessControl.objects.create(
+ team=self.team,
+ resource=resource,
+ resource_id=str(resource_id),
+ organization_member=membership,
+ access_level=level,
+ )
+
+ access = UserAccessControl(member, team=self.team)
+ assert api.denied_table_ids(self.team.id, access) == frozenset({self.table.id, deleted.id, denied.id})
+
def test_list_jobs_for_source_carries_source_fields(self) -> None:
job = ExternalDataJob.objects.create(
team_id=self.team.pk,