: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): postgres JSON property keys (#64477)
George Munyoroc09d0aaadf8b
diff --git a/posthog/hogql/printer/postgres.py b/posthog/hogql/printer/postgres.pyindex 1c09f6ae1f9b..fb6f7eff620d 100644--- a/posthog/hogql/printer/postgres.py+++ b/posthog/hogql/printer/postgres.py@@ -420,7 +420,12 @@ class PostgresPrinter(BasePrinter):return candidatedef _json_property_args(self, chain):- return [self._print_escaped_string(name) for name in chain]+ # Parameterize JSON property keys via ``context.add_value`` so psycopg binds them+ # safely. Inlining them through ``_print_escaped_string`` would emit ClickHouse-style+ # ``\'`` escapes, which Postgres/DuckDB do not recognize (``standard_conforming_strings``+ # defaults to ``on``), allowing statement-terminator SQL injection. Same reasoning as+ # ``visit_constant`` above.+ return [self.context.add_value(name) for name in chain]def _print_table(self, table) -> str:if isinstance(table, DirectPostgresTable):diff --git a/posthog/hogql/printer/test/test_printer.py b/posthog/hogql/printer/test/test_printer.pyindex 1dff8453345c..4aeedbdc2c7a 100644--- a/posthog/hogql/printer/test/test_printer.py+++ b/posthog/hogql/printer/test/test_printer.py@@ -5931,17 +5931,33 @@ class TestPostgresPrinter(BaseTest):self.assertEqual(self._expr("null"), "NULL")def test_json_properties_render_as_postgres_json_access(self):+ context = HogQLContext(team_id=self.team.pk, enable_select_queries=True)self.assertEqual(- self._expr("properties.a.b.c.$browser"),- "((((events.properties) -> 'a') -> 'b') -> 'c') ->> '$browser'",+ self._expr("properties.a.b.c.$browser", context=context),+ "((((events.properties) -> %(hogql_val_0)s) -> %(hogql_val_1)s) -> %(hogql_val_2)s) ->> %(hogql_val_3)s",)+ self.assertEqual(list(context.values.values()), ["a", "b", "c", "$browser"])def test_json_properties_in_select_render_as_postgres_json_access(self):- printed = self._select("SELECT properties.detail.name FROM events")+ context = HogQLContext(team_id=self.team.pk, enable_select_queries=True)+ printed = self._select("SELECT properties.detail.name FROM events", context=context)self.assertIn("(events.properties) ->", printed)- self.assertIn("->> 'name'", printed)+ self.assertIn("->> %(hogql_val", printed)self.assertIn('AS "properties.detail.name"', printed)+ self.assertIn("name", context.values.values())++ def test_json_property_key_injection_is_parameterized_not_inlined(self):+ # A property key containing a single quote must not break out of the string literal.+ # The ClickHouse ``\'`` escape does not work in Postgres (standard_conforming_strings=on),+ # so the key must be parameterized rather than escape-inlined.+ # The doubled '' is an escaped single quote in HogQL, so the key value contains a literal '.+ context = HogQLContext(team_id=self.team.pk, enable_select_queries=True)+ printed = self._expr("properties['x''); DROP TABLE users; --']", context=context)++ self.assertNotIn("DROP TABLE", printed)+ self.assertNotIn("\\'", printed)+ self.assertIn("x'); DROP TABLE users; --", context.values.values())def test_allows_dollar_identifiers(self):printed = self._select("SELECT event AS $value FROM events")diff --git a/posthog/hogql/query.py b/posthog/hogql/query.pyindex df48d0902ca6..ad259062280b 100644--- a/posthog/hogql/query.py+++ b/posthog/hogql/query.py@@ -667,11 +667,13 @@ class HogQLQueryExecutor:dialect=dialect,)- self.direct_sql = print_prepared_ast(- node=cast(ast.SelectQuery | ast.SelectSetQuery, direct_prepared_ast),- context=direct_context,- dialect=dialect,- pretty=self.pretty if self.pretty is not None else True,+ self.direct_sql = ensure_single_direct_statement(+ print_prepared_ast(+ node=cast(ast.SelectQuery | ast.SelectSetQuery, direct_prepared_ast),+ context=direct_context,+ dialect=dialect,+ pretty=self.pretty if self.pretty is not None else True,+ ))self.direct_context = direct_contextself.direct_values = direct_context.values