: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(cdp): refuse input templates that call a function the runtime lacks (#112713)
Meikel Ratz1bcd09d3c8d4
diff --git a/posthog/cdp/test/test_validation.py b/posthog/cdp/test/test_validation.pyindex 5b62181f0dd0..15057b5a6422 100644--- a/posthog/cdp/test/test_validation.py+++ b/posthog/cdp/test/test_validation.py@@ -1134,6 +1134,9 @@ class TestHogFunctionValidation(ClickhouseTestMixin, APIBaseTest, QueryMatchingTfor template in ("{arrayMap(a -> { let b := a return b }, [1])}","{arrayMap(tryBase64Decode, event.properties.ids)}",+ "{lower(event.event)}",+ "{sortableSemver(event.properties.version)}",+ "{print(event.event)}",):assert generate_template_bytecode(template, set(), function_type="destination"), template@@ -1167,6 +1170,21 @@ class TestHogFunctionValidation(ClickhouseTestMixin, APIBaseTest, QueryMatchingTgenerate_template_bytecode("{arrayMap(max2, [1, 2])}", set(), function_type="destination")assert "Variable not available in inputs: max2" in str(ctx.exception)+ @parameterized.expand(+ [+ ("unknown_everywhere", "{splitByChar(',', event.properties.domain)}", "splitByChar is not a function"),+ ("python_only", "{max2(1, 2)}", "max2 is not a function"),+ ("inside_a_branch", "{if(event.properties.x, intDiv(4, 2), 0)}", "intDiv is not a function"),+ ("wrong_argument_count", "{lower()}", "lower needs at least 1 argument(s), got 0"),+ ("core_async_function", "{fetch('https://example.com')}", "fetch is not a function"),+ ("product_async_function", "{postHogGetTicket('1')}", "postHogGetTicket is not a function"),+ ]+ )+ def test_destination_templates_refuse_calls_the_node_vm_cannot_make(self, _name, template, message):+ with self.assertRaises(Exception) as ctx:+ generate_template_bytecode(template, set(), function_type="destination")+ assert message in str(ctx.exception)+def test_destination_templates_skip_the_globals_check_when_the_function_stays_off(self):with self.assertRaises(Exception):generate_template_bytecode("{distinct_id}", set(), function_type="destination")diff --git a/posthog/cdp/validation.py b/posthog/cdp/validation.pyindex d179bbc64f6b..709411f7e862 100644--- a/posthog/cdp/validation.py+++ b/posthog/cdp/validation.py@@ -293,8 +293,8 @@ class TemplateGlobalsValidator(TraversingVisitor):"""invalid_globals: set[str]- # Calls the Node runtime would refuse for their argument count, as messages. Only checked for- # names the runtime table knows, so a name it does not know is left to the globals check.+ # Calls the Node runtime would refuse, as messages: a name it does not have, or the wrong+ # argument count for one it does.invalid_calls: list[str]def __init__(@@ -346,6 +346,12 @@ class TemplateGlobalsValidator(TraversingVisitor):returnarity = FILTER_FUNCTIONS.get(node.name)if arity is None:+ # The globals check never sees this name, because a call is not a field. Without this the+ # template compiles, and the VM refuses the call on every event that reaches it.+ # print is left out of the runtime table, but the Node VM has it. The async functions+ # (fetch, postHogCapture, product ones) are not exempt: inputs run without them.+ if node.name != "print":+ self.invalid_calls.append(f"{node.name} is not a function inputs can use")returnminimum, maximum = aritycount = len(node.args)