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): block creating hog functions from hidden templates via API/MCP (#63502)

Daniel Marchukc2e9cefacf6a
diff --git a/products/cdp/backend/api/hog_function.py b/products/cdp/backend/api/hog_function.py
index 7a9abb100489..50cd3b520edf 100644
--- a/products/cdp/backend/api/hog_function.py
+++ b/products/cdp/backend/api/hog_function.py
@@ -203,6 +203,34 @@ class HogFunctionSerializer(HogFunctionMinimalSerializer):
"execution_order": {"help_text": "Execution priority for transformations. Lower values run first."},
}
+ def _validate_template_is_creatable(self, template: HogFunctionTemplate) -> None:
+ # Hidden templates are internal building blocks (e.g. the native email destination) that the
+ # workflow editor renders but that are never offered as standalone destinations. Block creating a
+ # function from one via this API/MCP entirely — they are not a supported destination type.
+ if template.status == "hidden":
+ raise serializers.ValidationError(
+ {
+ "template_id": f"Template '{template.template_id}' is internal and cannot be used to create a function."
+ }
+ )
+
+ def _validate_hidden_template_not_enabled(self, attrs: dict, is_create: bool) -> None:
+ # Creating from a hidden template is already blocked outright. For an existing function built from
+ # one (the unsupported standalone destinations this PR is about), allow disabling and deleting so it
+ # can be cleaned up, but never let it stay enabled — block any update that would leave it enabled,
+ # including content edits (hog/inputs/filters) that omit `enabled` while it is currently on.
+ if is_create or not isinstance(self.instance, HogFunction) or attrs.get("deleted") is True:
+ return
+ if attrs.get("enabled", self.instance.enabled) is not True:
+ return
+ template = HogFunctionTemplate.get_template(self.instance.template_id) if self.instance.template_id else None
+ if template is not None and template.status == "hidden":
+ raise serializers.ValidationError(
+ {
+ "enabled": "This function was created from an internal template and can only be disabled or deleted, not kept enabled."
+ }
+ )
+
# NOTE: All pre-validation should be done here such as loading the template info etc.
def to_internal_value(self, data):
self.initial_data = data
@@ -249,6 +277,7 @@ class HogFunctionSerializer(HogFunctionMinimalSerializer):
if template_id:
template = HogFunctionTemplate.objects.get(template_id=data["template_id"])
if template:
+ self._validate_template_is_creatable(template)
data["hog"] = data.get("hog") or template.code
data["inputs_schema"] = data.get("inputs_schema") or template.inputs_schema
data["inputs"] = data.get("inputs") or {}
@@ -281,6 +310,8 @@ class HogFunctionSerializer(HogFunctionMinimalSerializer):
self.context.get("view") and self.context["view"].action == "create"
)
+ self._validate_hidden_template_not_enabled(attrs, bool(is_create))
+
# Check for transformation limit per team when the function will be enabled
# We allow unlimited creation of disabled transformations as they don't run during ingestion
if hog_type == "transformation" and attrs.get("enabled", False):
diff --git a/products/cdp/backend/api/test/test_hog_function.py b/products/cdp/backend/api/test/test_hog_function.py
index 7d92418e0960..30e4ddd9fd6f 100644
--- a/products/cdp/backend/api/test/test_hog_function.py
+++ b/products/cdp/backend/api/test/test_hog_function.py
@@ -221,6 +221,68 @@ class TestHogFunctionAPI(ClickhouseTestMixin, APIBaseTest, QueryMatchingTest):
else:
return actual_data
+ def _create_hidden_template(self) -> None:
+ HogFunctionTemplate.objects.create(
+ template_id="template-hidden-dest",
+ sha="1.0.0",
+ name="Hidden destination",
+ description="Internal building block",
+ code="return event",
+ code_language="hog",
+ inputs_schema=[],
+ type="destination",
+ status="hidden",
+ category=["Other"],
+ free=True,
+ )
+
+ @parameterized.expand([("non_staff", False), ("staff", True)])
+ def test_create_from_hidden_template_is_blocked(self, _name, is_staff):
+ # Hidden templates are internal building blocks, not standalone destinations — nobody can create
+ # a function from one via the API/MCP, staff included.
+ self._create_hidden_template()
+ if is_staff:
+ self.user.is_staff = True
+ self.user.save()
+ response = self.client.post(
+ f"/api/projects/{self.team.id}/hog_functions/",
+ data={"type": "destination", "name": "X", "template_id": "template-hidden-dest", "inputs": {}},
+ )
+ assert response.status_code == status.HTTP_400_BAD_REQUEST, response.json()
+ assert response.json()["attr"] == "template_id"
+ assert not HogFunction.objects.filter(template_id="template-hidden-dest").exists()
+
+ @parameterized.expand(
+ [
+ # An existing hidden-template function (created before the create-path block) can be disabled
+ # or deleted to clean it up, but must never be left enabled: enabling it or editing its config
+ # while it stays enabled is blocked; only disabling, deleting, or editing it while already
+ # disabled is allowed.
+ ("enable_blocked", False, {"enabled": True}, status.HTTP_400_BAD_REQUEST),
+ ("edit_while_enabled_blocked", True, {"name": "renamed"}, status.HTTP_400_BAD_REQUEST),
+ ("disable_allowed", True, {"enabled": False}, status.HTTP_200_OK),
+ ("edit_while_disabled_allowed", False, {"name": "renamed"}, status.HTTP_200_OK),
+ ("delete_allowed", True, {"deleted": True}, status.HTTP_200_OK),
+ ]
+ )
+ def test_enabling_function_from_hidden_template_is_blocked(self, _name, initial_enabled, patch, expected):
+ self._create_hidden_template()
+ fn = HogFunction.objects.create(
+ team=self.team,
+ name="Existing email destination",
+ type="destination",
+ template_id="template-hidden-dest",
+ enabled=initial_enabled,
+ inputs_schema=[],
+ inputs={},
+ hog="return event",
+ )
+ response = self.client.patch(
+ f"/api/projects/{self.team.id}/hog_functions/{fn.id}/",
+ data=patch,
+ )
+ assert response.status_code == expected, response.json()
+
def test_create_hog_function(self, *args):
response = self.client.post(
f"/api/projects/{self.team.id}/hog_functions/",