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.

feat(cdp): keep a destination config readable after a delete (#127959)

Tom Piccirelloe51559c0578a
diff --git a/docs/internal/activity-logging.md b/docs/internal/activity-logging.md
index 302aad4270f0..25f5defcf4a1 100644
--- a/docs/internal/activity-logging.md
+++ b/docs/internal/activity-logging.md
@@ -151,6 +151,8 @@ Changing an input from secret to non-secret does not copy its stored value into
An explicit replacement value can be stored as a non-secret input.
Existing duplicate input keys do not block disabling or deleting a destination, but requests cannot add a duplicate key.
Destination, draft, and revision responses mask secret mapping values from old records, including defaults.
+Creating a destination writes its first revision, so its starting config stays readable after a delete.
+The revisions endpoints also read a soft-deleted destination. A token then needs `hog_function:write`, as an undelete does.
New browser configuration builds leave out a site function while one of its secret values would reach the browser.
That covers a secret still stored in plaintext inputs, and a mapping secret or its default.
diff --git a/products/cdp/backend/api/hog_function.py b/products/cdp/backend/api/hog_function.py
index 61adaa4f7162..cdc5168c91c6 100644
--- a/products/cdp/backend/api/hog_function.py
+++ b/products/cdp/backend/api/hog_function.py
@@ -189,6 +189,9 @@ def mask_config_secrets(content: dict) -> dict:
return content
+REVISION_READ_ACTIONS = ("revisions", "revision_detail")
+
+
def snapshot_hog_function_content(hog_function: HogFunction) -> dict:
snapshot = {field: getattr(hog_function, field) for field in DRAFT_CONTENT_FIELDS}
# Defensively strip: a row written before secret inputs were encrypted still has plaintext
@@ -1212,8 +1215,21 @@ class HogFunctionViewSet(
# (`hog_function:read` would be a no-op since :write already satisfies it.)
if self.action == "rerun":
required_scopes += ["person:read", "group:read"]
+ # Reading a deleted function's history needs write access, as an undelete does.
+ if self.action in REVISION_READ_ACTIONS and self._target_is_deleted():
+ return required_scopes
return required_scopes if len(required_scopes) > 1 else None
+ def _target_is_deleted(self) -> bool:
+ """False when scopes resolve without a URL, as OpenAPI generation does."""
+ pk = self.kwargs.get("pk")
+ if pk is None:
+ return False
+ try:
+ return HogFunction.objects.filter(team_id=self.team_id, pk=pk, deleted=True).exists()
+ except (ValueError, DjangoValidationError):
+ return False
+
def get_serializer_class(self) -> type[BaseSerializer]:
if self.action == "list":
# Use full serializer (including inputs, mappings, etc.) when ?full=true
@@ -1275,8 +1291,8 @@ class HogFunctionViewSet(
def safely_get_queryset(self, queryset: QuerySet) -> QuerySet:
queryset = queryset.exclude(type=HogFunctionType.WAREHOUSE_SOURCE_WEBHOOK.value)
- if not (self.action == "partial_update" and self.request.data.get("deleted") is False):
- # We only want to include deleted functions if we are un-deleting them
+ undeleting = self.action == "partial_update" and self.request.data.get("deleted") is False
+ if not undeleting and self.action not in REVISION_READ_ACTIONS:
queryset = queryset.filter(deleted=False)
if self.action == "list":
@@ -1525,6 +1541,17 @@ class HogFunctionViewSet(
):
raise exceptions.ValidationError({"enabled": CREATE_ENABLED_MESSAGE})
serializer.save()
+ # No transaction with the save, because its post_save signal tells the workers to reload at once.
+ # A config edit that lands first writes revision 1 itself, so this must not fail on a duplicate.
+ HogFunctionRevision.objects.get_or_create(
+ team_id=self.team_id,
+ hog_function=serializer.instance,
+ version=serializer.instance.version,
+ defaults={
+ "content": snapshot_hog_function_content(serializer.instance),
+ "created_by": self.request.user if self.request.user.is_authenticated else None,
+ },
+ )
log_activity_from_viewset(
self,
serializer.instance,
diff --git a/products/cdp/backend/api/test/test_hog_function_drafts.py b/products/cdp/backend/api/test/test_hog_function_drafts.py
index 3326e7965e21..1262f197c418 100644
--- a/products/cdp/backend/api/test/test_hog_function_drafts.py
+++ b/products/cdp/backend/api/test/test_hog_function_drafts.py
@@ -6,7 +6,9 @@ from unittest.mock import patch
from parameterized import parameterized
from rest_framework import status
+from posthog.models import PersonalAPIKey
from posthog.models.activity_logging.activity_log import ActivityLog
+from posthog.models.utils import generate_random_token_personal, hash_key_value
from products.cdp.backend.api.hog_function import HogFunctionViewSet
from products.cdp.backend.models.hog_functions.hog_function import HogFunction
@@ -614,7 +616,7 @@ class TestHogFunctionRevisions(DraftTestCase):
content = {"inputs_schema": [], "inputs": {}, "mappings": mappings}
HogFunction.objects.filter(id=function_id).update(mappings=mappings, draft=content)
function = HogFunction.objects.get(id=function_id)
- self._revisions(function_id).create(team_id=self.team.id, hog_function=function, version=1, content=content)
+ self._revisions(function_id).filter(version=1).update(content=content)
for suffix in ("", "/revisions/1"):
response = self.client.get(self._url(function_id, suffix))
@@ -631,8 +633,11 @@ class TestHogFunctionRevisions(DraftTestCase):
assert function.draft == content
assert self._revisions(function_id).get(version=1).content == content
- def test_first_live_config_change_also_snapshots_the_outgoing_config(self):
+ @parameterized.expand([("created_with_a_revision", False), ("created_before_creation_revisions", True)])
+ def test_first_live_config_change_keeps_the_outgoing_config(self, _name: str, legacy: bool):
function_id = self._create()
+ if legacy:
+ self._revisions(function_id).delete()
self._live_edit(function_id, {"hog": EDITED_HOG})
@@ -640,7 +645,7 @@ class TestHogFunctionRevisions(DraftTestCase):
assert [revision.version for revision in revisions] == [1, 2]
# Rollback to the state before the revision system saw this function must be possible.
assert revisions[0].content["hog"] == LIVE_HOG
- assert revisions[0].created_by is None
+ assert revisions[0].created_by == (None if legacy else self.user)
assert revisions[1].content["hog"] == EDITED_HOG
assert revisions[1].created_by == self.user
@@ -657,7 +662,7 @@ class TestHogFunctionRevisions(DraftTestCase):
self._live_edit(function_id, payload)
- assert not self._revisions(function_id).exists()
+ assert list(self._revisions(function_id).values_list("version", flat=True)) == [1]
assert HogFunction.objects.get(id=function_id).version == 1
def test_recompiled_filter_bytecode_does_not_create_a_revision(self):
@@ -669,7 +674,7 @@ class TestHogFunctionRevisions(DraftTestCase):
self._live_edit(function_id, {"name": "Renamed"})
- assert not self._revisions(function_id).exists()
+ assert list(self._revisions(function_id).values_list("version", flat=True)) == [1]
assert HogFunction.objects.get(id=function_id).version == 1
def test_revisions_list_is_newest_first_and_omits_config(self):
@@ -693,6 +698,39 @@ class TestHogFunctionRevisions(DraftTestCase):
assert response.json()["content"]["hog"] == LIVE_HOG
assert "token" not in response.json()["content"]["inputs"]
+ @parameterized.expand(
+ [
+ ("deleted_with_a_session", True, None, status.HTTP_200_OK),
+ ("deleted_with_a_read_only_key", True, ["hog_function:read"], status.HTTP_403_FORBIDDEN),
+ ("live_with_a_read_only_key", False, ["hog_function:read"], status.HTTP_200_OK),
+ ]
+ )
+ def test_deleted_function_history_needs_write_access(
+ self, _name: str, deleted: bool, key_scopes: list[str] | None, expected_status: int
+ ):
+ function_id = self._create()
+ if deleted:
+ self._live_edit(function_id, {"deleted": True})
+ headers = {}
+ if key_scopes is not None:
+ key_value = generate_random_token_personal()
+ PersonalAPIKey.objects.create(
+ label="Read only", user=self.user, secure_value=hash_key_value(key_value), scopes=key_scopes
+ )
+ self.client.logout()
+ headers = {"authorization": f"Bearer {key_value}"}
+
+ listed = self.client.get(self._url(function_id, "/revisions"), headers=headers)
+ detail = self.client.get(self._url(function_id, "/revisions/1"), headers=headers)
+
+ assert (listed.status_code, detail.status_code) == (expected_status, expected_status)
+ if expected_status == status.HTTP_200_OK:
+ assert [result["version"] for result in listed.json()["results"]] == [1]
+ assert detail.json()["content"]["hog"] == LIVE_HOG
+ if deleted and key_scopes is None:
+ listed_ids = [function["id"] for function in self.client.get(self._url()).json()["results"]]
+ assert function_id not in listed_ids
+
def test_unknown_revision_is_a_404(self):
function_id = self._create()