: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): stop restricted display property breaking events view (#88530)
posthog[bot]8d8b7ca653db
diff --git a/posthog/hogql_queries/events_query_runner.py b/posthog/hogql_queries/events_query_runner.pyindex 5461c90caa4d..eecb19437160 100644--- a/posthog/hogql_queries/events_query_runner.py+++ b/posthog/hogql_queries/events_query_runner.py@@ -184,10 +184,15 @@ class EventsQueryRunner(AnalyticsQueryRunner[EventsQueryResponse]):return str(row[i])return None- def _raise_on_restricted_property_select(self, select: list[ast.Expr]) -> None:+ def _raise_on_restricted_property_select(self) -> None:# User-authored ``select`` entries that explicitly reference a restricted event or person# property must fail loudly — the printer's silent JSONDropKeys strip would otherwise turn# the request into an empty string, which is surprising when the field name was typed by hand.+ #+ # Check the raw user columns, not the expanded select list. PostHog's own pseudo-columns+ # (``person``, ``person_display_name``) expand to expressions that reference restricted+ # person properties such as ``email``; those values are masked elsewhere, so walking the+ # expansion here would wrongly fail the whole query instead of degrading gracefully.from posthog.hogql.errors import ResolutionErrorfrom posthog.hogql.visitor import TraversingVisitor@@ -222,8 +227,12 @@ class EventsQueryRunner(AnalyticsQueryRunner[EventsQueryResponse]):raise ResolutionError(f"Access to property '{chain[2]}' is restricted")checker = _Checker()- for expr in select:- checker.visit(expr)+ for col in self.select_input_raw():+ stripped = col.split("--")[0].strip()+ # Skip PostHog-generated pseudo-columns; their expansion is masked, not restricted.+ if col == "*" or stripped in ("person", "person_display_name"):+ continue+ checker.visit(map_virtual_properties(parse_expr(col, timings=self.timings)))def to_query(self) -> ast.SelectQuery:# Note: This code is inefficient and problematic, see https://github.com/PostHog/posthog/issues/13485 for details.@@ -231,7 +240,7 @@ class EventsQueryRunner(AnalyticsQueryRunner[EventsQueryResponse]):# columns & group_bywith self.timings.measure("columns"):select_input, select = self.select_cols()- self._raise_on_restricted_property_select(select)+ self._raise_on_restricted_property_select()with self.timings.measure("aggregations"):group_by: list[ast.Expr] = [column for column in select if not has_aggregation(column)]diff --git a/posthog/hogql_queries/test/test_events_query_runner.py b/posthog/hogql_queries/test/test_events_query_runner.pyindex 7800f4f68e5d..26da2e39ffb9 100644--- a/posthog/hogql_queries/test/test_events_query_runner.py+++ b/posthog/hogql_queries/test/test_events_query_runner.py@@ -1283,6 +1283,50 @@ class TestEventsQueryRunner(ClickhouseTestMixin, APIBaseTest):with self.assertRaises(ResolutionError):runner.run()+ @freeze_time("2020-01-11T12:00:05Z")+ def test_restricted_display_property_does_not_break_person_display_name(self):+ from posthog.models import PropertyDefinition++ from products.access_control.backend.models.property_access_control import PropertyAccessControl+ from products.access_control.backend.property_access_control import PropertyAccessLevel++ self._enable_property_access_control()++ _create_person(+ team_id=self.team.pk,+ distinct_ids=["p1"],+ properties={"email": "[email protected]", "name": "Test User"},+ )+ _create_event(+ team=self.team,+ event="$pageview",+ distinct_id="p1",+ timestamp="2020-01-11T12:00:01Z",+ properties={},+ )+ flush_persons_and_events()++ # restrict "email", the first default display-name property+ prop_def = PropertyDefinition.objects.create(+ team=self.team,+ name="email",+ type=PropertyDefinition.Type.PERSON,+ )+ PropertyAccessControl.objects.create(+ team=self.team,+ property_definition=prop_def,+ access_level=PropertyAccessLevel.NONE.value,+ )++ query = EventsQuery(select=["person_display_name -- Person"], after="2020-01-10")+ runner = EventsQueryRunner(query=query, team=self.team, user=self.user)+ response = runner.run()++ # The query must succeed and mask the restricted value, not raise.+ assert isinstance(response, CachedEventsQueryResponse)+ assert len(response.results) > 0+ assert response.results[0][0]["display_name"] == "Test User"+@freeze_time("2020-01-11T12:00:05Z")def test_users_with_different_restrictions_get_different_cache_keys(self):self._enable_property_access_control()