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(cdp): check the template of an unsaved function on test runs (#121720)

Daniel Marchuk6c3d69a4e9af
diff --git a/products/cdp/backend/api/hog_function.py b/products/cdp/backend/api/hog_function.py
index 8df10aeb1498..1e788be88e0c 100644
--- a/products/cdp/backend/api/hog_function.py
+++ b/products/cdp/backend/api/hog_function.py
@@ -646,6 +646,14 @@ class HogFunctionSerializer(HogFunctionMinimalSerializer):
raise serializers.ValidationError({"template_id": f"No template found for id '{data['template_id']}'"})
+ # A test run of an unsaved function takes template_id from the caller, and the template decides
+ # which reserved functions the code may call, so it gets the same check as a create.
+ is_unsaved_test = (
+ instance is None and self.context.get("view") and self.context["view"].action == "invocations"
+ )
+ if is_unsaved_test:
+ self._validate_template_is_creatable(template)
+
if is_create:
# Set defaults for new functions
data["inputs_schema"] = data.get("inputs_schema") or []
diff --git a/products/cdp/backend/api/test/test_hog_function.py b/products/cdp/backend/api/test/test_hog_function.py
index e68d3d566a74..5d3a830f61ec 100644
--- a/products/cdp/backend/api/test/test_hog_function.py
+++ b/products/cdp/backend/api/test/test_hog_function.py
@@ -2802,6 +2802,30 @@ class TestHogFunctionAPI(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
assert "PostHog connection" in json.dumps(response.json())
mock_create_hog_invocation_test.assert_not_called()
+ def test_test_invocation_of_unsaved_function_cannot_borrow_a_hidden_templates_reserved_calls(self):
+ self._create_email_template()
+
+ with patch(
+ "products.cdp.backend.api.hog_function.create_hog_invocation_test"
+ ) as mock_create_hog_invocation_test:
+ response = self.client.post(
+ f"/api/projects/{self.team.id}/hog_functions/new/invocations/",
+ data={
+ "configuration": {
+ "type": "destination",
+ "name": "Sneaky",
+ "template_id": "template-email-test",
+ "hog": "sendEmail({'to': {'email': '[email protected]'}, 'subject': 'hi'})",
+ "inputs": {},
+ },
+ "mock_async_functions": False,
+ },
+ )
+
+ assert response.status_code == status.HTTP_400_BAD_REQUEST, response.json()
+ assert "is internal and cannot be used" in json.dumps(response.json())
+ mock_create_hog_invocation_test.assert_not_called()
+
@parameterized.expand(
[
("errors_list", {"errors": ["Missing event"]}, ["Missing event"]),