: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(workflows): show secret header names so an edit keeps the other headers (#121719)
Daniel Marchukd836c20cdd13
diff --git a/products/workflows/backend/services/hog_flow_secrets.py b/products/workflows/backend/services/hog_flow_secrets.pyindex 9cfc84357fbf..8d197714af4e 100644--- a/products/workflows/backend/services/hog_flow_secrets.py+++ b/products/workflows/backend/services/hog_flow_secrets.py@@ -2,6 +2,11 @@ from copy import deepcopyfrom typing import Any, Optionalfrom products.cdp.backend.models.hog_function_template import HogFunctionTemplate+from products.cdp.backend.validation import (+ exposes_secret_dictionary_keys,+ masked_dictionary_secret,+ restore_masked_dictionary_entries,+)# Function/email/sms steps (and function-shaped triggers) can carry secret inputs - API keys, auth# headers - declared `secret: true` on their template's inputs_schema. We split those values out of@@ -97,6 +102,14 @@ def recover_or_drop_masked_inputs(inputs: Any, secret_keys: set[str], existing:inputs[key] = storedelse:inputs.pop(key, None)+ elif isinstance(value, dict):+ # The same goes for the per-entry mask of a secret dictionary: each masked entry takes its+ # stored value back, and an entry with nothing stored is dropped.+ restored, missing = restore_masked_dictionary_entries(value, existing.get(key))+ if isinstance(restored.get("value"), dict):+ for entry in missing:+ restored["value"].pop(entry, None)+ inputs[key] = restoreddef mask_secret_action_inputs(@@ -105,8 +118,12 @@ def mask_secret_action_inputs(# Replace every set secret input with the {"secret": True} presence marker for read-back. Mutates# the given action dicts (must be a copy - callers deepcopy first). A value counts as set if it# lives in the encrypted map or, for legacy rows written before the split, still sits in plaintext.+ # A secret dictionary paired with a plain one (a webhook's secret headers) keeps its entry names,+ # so the editor can list them and a save changes one entry without wiping the others.for flow_action in actions:- secret_keys = secret_keys_for_action(flow_action, template_cache)+ template = _function_template_for_action(flow_action, template_cache)+ schemas = (template.inputs_schema or []) if template else []+ secret_keys = {schema["key"] for schema in schemas if schema.get("secret")}if not secret_keys:continueinputs = (flow_action.get("config") or {}).get("inputs")@@ -115,8 +132,15 @@ def mask_secret_action_inputs(action_id = flow_action.get("id")action_secrets = secrets_by_action.get(action_id, {}) if isinstance(action_id, str) else {}for key in secret_keys:- if action_secrets.get(key) or inputs.get(key):- inputs[key] = {"secret": True}+ stored = action_secrets.get(key) or inputs.get(key)+ if not stored:+ continue+ schema = next(s for s in schemas if s.get("key") == key)+ inputs[key] = (+ masked_dictionary_secret(stored)+ if exposes_secret_dictionary_keys(schema, schemas)+ else {"secret": True}+ )return actionsdiff --git a/products/workflows/backend/tests/api/test_hog_flow.py b/products/workflows/backend/tests/api/test_hog_flow.pyindex deb7d82e3037..3e9d032cc8b3 100644--- a/products/workflows/backend/tests/api/test_hog_flow.py+++ b/products/workflows/backend/tests/api/test_hog_flow.py@@ -116,6 +116,14 @@ def _secret_input_template() -> dict:template["inputs_schema"] = [{"key": "url", "type": "string", "label": "URL", "secret": False, "hidden": False, "required": True},{"key": "api_key", "type": "string", "label": "API key", "secret": True, "hidden": False, "required": False},+ {+ "key": "headers",+ "type": "dictionary",+ "label": "Headers",+ "secret": False,+ "hidden": False,+ "required": False,+ },{"key": "secret_headers","type": "dictionary",@@ -6887,16 +6895,19 @@ class TestHogFlowSecretInputs(APIBaseTest):@parameterized.expand([- ("string", {}, "api_key", "SUPER-SECRET"),+ ("string", {}, "api_key", "SUPER-SECRET", {"secret": True}),("dictionary_without_templating",{"secret_headers": {"value": {"Authorization": "Bearer SUPER-SECRET"}}},"secret_headers",{"Authorization": "Bearer SUPER-SECRET"},+ {"secret": True, "value": {"Authorization": "********"}},),])- def test_secret_action_input_is_encrypted_at_rest_and_masked_on_read(self, _name, extra_inputs, key, stored_value):+ def test_secret_action_input_is_encrypted_at_rest_and_masked_on_read(+ self, _name, extra_inputs, key, stored_value, masked+ ):create = self.client.post(f"/api/projects/{self.team.id}/hog_flows", self._flow_payload(extra_inputs=extra_inputs))@@ -6907,7 +6918,7 @@ class TestHogFlowSecretInputs(APIBaseTest):retrieve = self.client.get(f"/api/projects/{self.team.id}/hog_flows/{flow_id}")for body in (create.json(), retrieve.json()):inputs = self._function_inputs(body)- assert inputs[key] == {"secret": True}+ assert inputs[key] == maskedassert inputs["url"]["value"] == "https://example.com"# The plaintext never reaches a project member, including through the list the web app reads.@@ -6946,6 +6957,45 @@ class TestHogFlowSecretInputs(APIBaseTest):flow = HogFlow.objects.get(id=flow_id)assert flow.encrypted_inputs["action_1"]["api_key"]["value"] == expected_stored+ @parameterized.expand(+ [+ (+ "add_entry",+ {"A": "********", "B": "********", "C": "c-new"},+ False,+ {"A": "a-old", "B": "b-old", "C": "c-new"},+ ),+ ("rotate_entry", {"A": "a-new", "B": "********"}, False, {"A": "a-new", "B": "b-old"}),+ ("remove_entry", {"A": "********"}, False, {"A": "a-old"}),+ # A failed validation keeps the raw inputs, so the masks must still turn back into stored values.+ ("add_entry_lenient_save", {"A": "********", "C": "c-new"}, True, {"A": "a-old", "C": "c-new"}),+ ]+ )+ def test_secret_header_edit_keeps_the_entries_it_does_not_change(+ self, _name, edited_entries, invalid_url, expected_stored+ ):+ create = self.client.post(+ f"/api/projects/{self.team.id}/hog_flows",+ self._flow_payload(extra_inputs={"secret_headers": {"value": {"A": "a-old", "B": "b-old"}}}),+ )+ assert create.status_code == 201, create.json()+ flow_id = create.json()["id"]++ body = self.client.get(f"/api/projects/{self.team.id}/hog_flows/{flow_id}").json()+ inputs = self._function_inputs(body)+ assert inputs["secret_headers"] == {"secret": True, "value": {"A": "********", "B": "********"}}+ inputs["secret_headers"] = {"secret": True, "value": edited_entries}+ if invalid_url:+ inputs["url"] = {"value": "{event.properties.url", "templating": "hog"}++ patch_res = self.client.patch(+ f"/api/projects/{self.team.id}/hog_flows/{flow_id}", {"actions": body["actions"], "edges": body["edges"]}+ )+ assert patch_res.status_code == 200, patch_res.json()++ flow = HogFlow.objects.get(id=flow_id)+ assert flow.encrypted_inputs["action_1"]["secret_headers"]["value"] == expected_stored+def test_metadata_only_update_leaves_secret_intact(self):# A PATCH that carries no actions must not clear the stored secret.flow_id = self._create()