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): stop masked secret re-saves from wiping the stored value (#85595)

posthog[bot]b04cdb78f1b6
diff --git a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts
index f4be03f83b3a..8db785ab13ae 100644
--- a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts
+++ b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts
@@ -8,7 +8,7 @@ import { ApiError } from 'lib/api-error'
import { initKeaTests } from '~/test/init'
import { CyclotronJobFiltersType, HogFunctionTemplateType, HogFunctionType } from '~/types'
-import { hogFunctionConfigurationLogic } from './hogFunctionConfigurationLogic'
+import { hogFunctionConfigurationLogic, sanitizeInputs } from './hogFunctionConfigurationLogic'
jest.mock('lib/api', () => ({
...jest.requireActual('lib/api'),
@@ -187,6 +187,20 @@ describe('hogFunctionConfigurationLogic', () => {
})
})
+ describe('sanitizeInputs', () => {
+ it('does not send a placeholder value for an untouched secret', () => {
+ // A value here can be encrypted over the stored secret, so an untouched secret must
+ // carry only { secret: true }.
+ const result = sanitizeInputs({
+ inputs_schema: [{ key: 'api_key', label: 'API key', type: 'string', secret: true }],
+ inputs: { api_key: { value: '********', secret: true } },
+ })
+
+ expect(result.api_key.value).toBeUndefined()
+ expect(result.api_key.secret).toBe(true)
+ })
+ })
+
describe('log transformation', () => {
const LOG_TEMPLATE: HogFunctionTemplateType = {
free: true,
diff --git a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx
index 9df56a2d8af9..c0ccd36be262 100644
--- a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx
+++ b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx
@@ -161,9 +161,11 @@ export function sanitizeInputs(
let value = input?.value
if (secret) {
- // If set this means we haven't changed the value
+ // The value was not retyped, so keep the stored secret. Leave value undefined - it is
+ // dropped from the JSON payload, and a placeholder here can be encrypted as the new
+ // secret if the backend does not strip it.
sanitizedInputs[inputSchema.key] = {
- value: '********', // Don't send the actual value
+ value: undefined,
secret: true,
}
return
diff --git a/posthog/cdp/test/test_validation.py b/posthog/cdp/test/test_validation.py
index 20ae3964e705..6719ee6295d7 100644
--- a/posthog/cdp/test/test_validation.py
+++ b/posthog/cdp/test/test_validation.py
@@ -791,6 +791,16 @@ class TestHogFunctionValidation(ClickhouseTestMixin, APIBaseTest, QueryMatchingT
"secret_field": {"value": "EXISTING_SECRET_VALUE"},
},
),
+ # The UI sends the read-back mask as the value when a secret is left untouched. This
+ # must keep the stored secret, not encrypt the mask over it.
+ (
+ {
+ "secret_field": {"value": "********", "secret": True},
+ },
+ {
+ "secret_field": {"value": "EXISTING_SECRET_VALUE"},
+ },
+ ),
]:
serializer = MappingsSerializer(
data={
@@ -805,6 +815,30 @@ class TestHogFunctionValidation(ClickhouseTestMixin, APIBaseTest, QueryMatchingT
values_only = {k: {"value": v["value"]} for k, v in validated.items()}
assert values_only == expected_result
+ @parameterized.expand(
+ [
+ # Read-back mask flagged as secret, nothing stored to restore.
+ ({"value": "********", "secret": True},),
+ # Mask that lost its secret flag - the persistence guard must still refuse it.
+ ({"value": "********"},),
+ ]
+ )
+ def test_masked_secret_without_stored_value_is_rejected(self, input_value):
+ # The mask must never be encrypted as the real credential when there is nothing to restore.
+ inputs_schema = [
+ {"key": "secret_field", "type": "string", "required": True, "secret": True},
+ ]
+
+ serializer = MappingsSerializer(
+ data={
+ "inputs_schema": inputs_schema,
+ "inputs": {"secret_field": input_value},
+ },
+ context={"function_type": "destination", "encrypted_inputs": {}},
+ )
+ with self.assertRaises(ValidationError):
+ serializer.is_valid(raise_exception=True)
+
def test_validate_filters_builds_bytecode(self):
filters = {
"properties": [{"key": "email", "value": ["[email protected]"], "operator": "exact", "type": "person"}],
diff --git a/posthog/cdp/validation.py b/posthog/cdp/validation.py
index 91cd2c47be8e..d343a4199f80 100644
--- a/posthog/cdp/validation.py
+++ b/posthog/cdp/validation.py
@@ -31,6 +31,10 @@ logger = logging.getLogger(__name__)
CORE_SUPPORTED_FUNCTIONS = {"fetch", "postHogCapture"}
MAX_WORKFLOW_EMAIL_SENDERS = 10
+# The mask the UI shows in place of a stored secret. A re-save that did not touch the secret
+# sends this back, meaning "keep the stored value". It must never be persisted as a real secret.
+MASKED_SECRET_VALUE = "********"
+
# Mirrors FROM_OVERRIDE_EMAIL_REGEX in nodejs/src/cdp/services/messaging/email.service.ts, which
# is what the send path enforces after rendering. Keep the two in sync.
FROM_OVERRIDE_EMAIL_REGEX = re.compile(r'^[^\s@"<>,;]+@[^\s@"<>,;]+\.[^\s@"<>,;]+$')
@@ -692,10 +696,15 @@ class InputsSerializer(serializers.DictField):
value = data.get(key) or {}
if schema.get("secret"):
- # A {"secret": true} value with no "value" is the read-back mask, meaning "keep the
- # stored secret". One that also carries a "value" is a rotation and must win, so it
- # falls through to normal validation.
- is_masked = isinstance(value, dict) and bool(value.get("secret")) and "value" not in value
+ # A {"secret": true} value the user did not retype is the read-back mask, meaning
+ # "keep the stored secret". The UI sends it either without a "value" key or with the
+ # literal mask as the value, so both shapes must count as masked. A different "value"
+ # is a rotation and must win, so it falls through to normal validation.
+ is_masked = (
+ isinstance(value, dict)
+ and bool(value.get("secret"))
+ and ("value" not in value or value.get("value") == MASKED_SECRET_VALUE)
+ )
if is_masked or value == {}:
existing_value = (existing_secret_inputs or {}).get(key)
if existing_value:
@@ -725,6 +734,12 @@ class InputsSerializer(serializers.DictField):
# Indicates no value is provided and no error was thrown which is fine so we can exclude it
continue
+ if schema.get("secret") and input_value.get("value") == MASKED_SECRET_VALUE:
+ # The mask reached persistence, so recovery of the stored secret failed. Refuse
+ # rather than encrypt the mask and silently destroy the real credential.
+ errors[key] = "This secret input was not updated correctly. Enter the value again."
+ continue
+
result[key] = input_value
except ValidationError as e:
# TRICKY: Need to get the nested error message to ensure the structure is correct