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): leave secret inputs out when duplicating a function (#128892)

Michael Reichenbach20661a999c4b
diff --git a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts
index 9b03dce969fe..c22cb2b57334 100644
--- a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts
+++ b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.test.ts
@@ -1,5 +1,6 @@
import { MOCK_DEFAULT_TEAM } from 'lib/api.mock'
+import { router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'
import { lemonToast } from '@posthog/lemon-ui'
@@ -14,6 +15,7 @@ import { NodeKind } from '~/queries/schema/schema-general'
import { initKeaTests } from '~/test/init'
import {
CyclotronJobFilterEvents,
+ CyclotronJobInputType,
CyclotronJobFiltersType,
FilterLogicalOperator,
HogFunctionTemplateType,
@@ -320,6 +322,172 @@ describe('hogFunctionConfigurationLogic', () => {
})
})
+ describe('duplicating a function', () => {
+ const TEMPLATE_WITH_SECRETS: HogFunctionTemplateType = {
+ ...HOG_TEMPLATE,
+ inputs_schema: [
+ ...(HOG_TEMPLATE.inputs_schema ?? []),
+ { key: 'api_key', type: 'string', label: 'API key', secret: true, required: false },
+ { key: 'secret_headers', type: 'dictionary', label: 'Secret headers', secret: true, required: false },
+ ],
+ }
+
+ beforeEach(() => {
+ initKeaTests()
+ mockApi.getTemplate.mockResolvedValue(TEMPLATE_WITH_SECRETS)
+ mockApi.create.mockResolvedValue(HOG_FUNCTION)
+ })
+
+ afterEach(() => {
+ jest.restoreAllMocks()
+ })
+
+ const MASKED_HEADERS = { secret_headers: { secret: true, value: { Authorization: '********' } } }
+ const NOTHING_KEPT = { api_key: undefined, secret_headers: undefined, headers: undefined }
+
+ it.each<
+ [
+ string,
+ Record<string, CyclotronJobInputType>,
+ CyclotronJobInputType | null,
+ Record<string, unknown>,
+ boolean,
+ ]
+ >([
+ ['drops a masked string secret', { api_key: { value: undefined, secret: true } }, null, NOTHING_KEPT, true],
+ ['drops a dictionary secret with masked entries', MASKED_HEADERS, null, NOTHING_KEPT, true],
+ [
+ 'drops a secret dictionary the user typed a new row into',
+ MASKED_HEADERS,
+ { secret: false, value: { Authorization: '********', 'x-new': 'typed' } },
+ NOTHING_KEPT,
+ true,
+ ],
+ [
+ 'drops a secret the user typed and did not save',
+ { api_key: { value: 'typed-key' } },
+ null,
+ NOTHING_KEPT,
+ true,
+ ],
+ [
+ 'keeps the copy enabled when a secret was never set',
+ { api_key: { value: '' } },
+ null,
+ NOTHING_KEPT,
+ false,
+ ],
+ [
+ 'drops an edited secret dictionary that has only masked rows left',
+ MASKED_HEADERS,
+ { secret: false, value: { Authorization: '********' } },
+ NOTHING_KEPT,
+ true,
+ ],
+ [
+ 'keeps a plain header whose value looks like a mask',
+ { headers: { value: { 'x-literal': '********' } } },
+ null,
+ { ...NOTHING_KEPT, headers: { 'x-literal': '********' } },
+ false,
+ ],
+ ])('%s', async (_name, sourceInputs, editedSecretHeaders, expectedKept, dropsSecrets) => {
+ mockApi.get.mockResolvedValue({
+ ...HOG_FUNCTION,
+ inputs_schema: TEMPLATE_WITH_SECRETS.inputs_schema,
+ inputs: { url: { value: 'https://example.com' }, ...sourceInputs },
+ template: TEMPLATE_WITH_SECRETS,
+ })
+ logic = hogFunctionConfigurationLogic({ id: HOG_FUNCTION.id })
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['loadHogFunctionSuccess'])
+ if (editedSecretHeaders) {
+ logic.actions.setConfigurationValue('inputs.secret_headers', editedSecretHeaders)
+ jest.spyOn(window, 'confirm').mockReturnValue(true)
+ }
+ const toast = jest.spyOn(lemonToast, 'info')
+ logic.actions.duplicate()
+ expect(toast).toHaveBeenCalledTimes(dropsSecrets ? 1 : 0)
+
+ const copyLogic = hogFunctionConfigurationLogic({ templateId: TEMPLATE_WITH_SECRETS.id })
+ copyLogic.mount()
+ await expectLogic(copyLogic).toDispatchActions(['loadTemplateSuccess'])
+ await expectLogic(copyLogic, () => {
+ copyLogic.actions.submitConfiguration()
+ }).toDispatchActions(['submitConfigurationSuccess'])
+
+ const sent = mockApi.create.mock.calls[0][0]
+ expect(sent.enabled).toEqual(!dropsSecrets)
+ const sentInputs = sent.inputs ?? {}
+ expect(Object.entries(sentInputs).filter(([, input]) => input?.secret)).toEqual([])
+ expect({
+ api_key: sentInputs.api_key?.value,
+ secret_headers: sentInputs.secret_headers?.value,
+ headers: sentInputs.headers?.value,
+ }).toEqual(expectedKept)
+ expect(sentInputs.url).toMatchObject({ value: 'https://example.com' })
+ })
+
+ it('starts the copy disabled when only a mapping loses its secret', async () => {
+ mockApi.get.mockResolvedValue({
+ ...HOG_FUNCTION,
+ inputs: { url: { value: 'https://example.com' } },
+ mappings: [
+ {
+ name: 'Mapping with a secret',
+ inputs_schema: [
+ { key: 'token', type: 'string', label: 'Token', secret: true, required: false },
+ { key: 'channel', type: 'string', label: 'Channel', required: false },
+ ],
+ inputs: { token: { value: undefined, secret: true }, channel: { value: 'alerts' } },
+ },
+ ],
+ template: TEMPLATE_WITH_SECRETS,
+ })
+ logic = hogFunctionConfigurationLogic({ id: HOG_FUNCTION.id })
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['loadHogFunctionSuccess'])
+ const toast = jest.spyOn(lemonToast, 'info')
+ logic.actions.duplicate()
+ expect(toast).toHaveBeenCalledTimes(1)
+
+ const copyLogic = hogFunctionConfigurationLogic({ templateId: TEMPLATE_WITH_SECRETS.id })
+ copyLogic.mount()
+ await expectLogic(copyLogic).toDispatchActions(['loadTemplateSuccess'])
+ await expectLogic(copyLogic, () => {
+ copyLogic.actions.submitConfiguration()
+ }).toDispatchActions(['submitConfigurationSuccess'])
+
+ const sent = mockApi.create.mock.calls[0][0]
+ expect(sent.enabled).toEqual(false)
+ const mappingInputs = sent.mappings?.[0].inputs ?? {}
+ expect(Object.entries(mappingInputs).filter(([, input]) => input?.secret)).toEqual([])
+ expect(mappingInputs.token?.value).toBeUndefined()
+ expect(mappingInputs.channel).toMatchObject({ value: 'alerts' })
+ })
+
+ it('keeps the user on their edited function without a toast when they cancel leaving it', async () => {
+ mockApi.get.mockResolvedValue({
+ ...HOG_FUNCTION,
+ inputs_schema: TEMPLATE_WITH_SECRETS.inputs_schema,
+ inputs: { api_key: { value: undefined, secret: true } },
+ template: TEMPLATE_WITH_SECRETS,
+ })
+ logic = hogFunctionConfigurationLogic({ id: HOG_FUNCTION.id })
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['loadHogFunctionSuccess'])
+ logic.actions.setConfigurationValue('name', 'Edited name')
+ jest.spyOn(window, 'confirm').mockReturnValue(false)
+ const toast = jest.spyOn(lemonToast, 'info')
+ const { pathname } = router.values.location
+
+ logic.actions.duplicate()
+
+ expect(router.values.location.pathname).toEqual(pathname)
+ expect(toast).not.toHaveBeenCalled()
+ })
+ })
+
describe('mapping preview filters', () => {
beforeEach(() => {
initKeaTests()
diff --git a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx
index f6afa5668111..221a953cfeb8 100644
--- a/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx
+++ b/frontend/src/scenes/hog-functions/configuration/hogFunctionConfigurationLogic.tsx
@@ -33,6 +33,7 @@ import { dayjs } from 'lib/dayjs'
import { integrationsLogic } from 'lib/integrations/integrationsLogic'
import { deleteWithUndo } from 'lib/utils/deleteWithUndo'
import { uuid } from 'lib/utils/dom'
+import { isEmptyObject } from 'lib/utils/guards'
import { escapePropertyAsHogQLIdentifier } from 'lib/utils/hogql'
import { addProductIntent } from 'lib/utils/product-intents'
import { FEATURE_FLAG_CALLED_EVENT } from 'scenes/feature-flags/flagEvaluationsTable'
@@ -200,6 +201,27 @@ export function sanitizeInputs(
return sanitizedInputs
}
+function withoutSecrets<T extends Pick<HogFunctionMappingType, 'inputs' | 'inputs_schema'>>(
+ config: T
+): { config: T; dropped: boolean } {
+ if (!config.inputs) {
+ return { config, dropped: false }
+ }
+ const secretKeys = new Set(config.inputs_schema?.filter((schema) => schema.secret).map((schema) => schema.key))
+ const inputs: Record<string, CyclotronJobInputType> = {}
+ let dropped = false
+ for (const [key, input] of Object.entries(config.inputs)) {
+ if (input?.secret || secretKeys.has(key)) {
+ const value = input?.value
+ dropped ||=
+ !!input?.secret || !(value === undefined || value === null || value === '' || isEmptyObject(value))
+ } else {
+ inputs[key] = input
+ }
+ }
+ return { config: { ...config, inputs }, dropped }
+}
+
export function sanitizeConfiguration(data: HogFunctionConfigurationType): HogFunctionConfigurationType {
const filters = data.filters ?? {}
filters.source = data.type === 'internal_destination' ? 'internal-events' : (filters.source ?? 'events')
@@ -2109,15 +2131,27 @@ export const hogFunctionConfigurationLogic = kea<hogFunctionConfigurationLogicTy
duplicate: async () => {
if (values.hogFunction) {
+ const copied = withoutSecrets(values.configuration)
+ const copiedMappings = values.configuration.mappings?.map(withoutSecrets)
+ const droppedSecrets = copied.dropped || !!copiedMappings?.some((mapping) => mapping.dropped)
const newConfig = {
- ...values.configuration,
+ ...copied.config,
+ mappings: copiedMappings?.map((mapping) => mapping.config) ?? copied.config.mappings,
name: `${values.configuration.name} (copy)`,
+ enabled: droppedSecrets ? false : copied.config.enabled,
}
// TODO: What to do if no template?
const originalTemplate = values.hogFunction.template!
+ const { pathname } = router.values.location
router.actions.push(urls.hogFunctionNew(originalTemplate.id), undefined, {
configuration: newConfig,
})
+ const openedCopy = router.values.location.pathname !== pathname
+ if (openedCopy && droppedSecrets) {
+ lemonToast.info(
+ 'Secret values are not copied, so the copy starts disabled. Enter them again, then enable it.'
+ )
+ }
}
},
duplicateFromTemplate: async () => {