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): redact secret input values in mocked async function logs (#60220)

Meikel Ratz4bec45f21b7d
diff --git a/nodejs/src/cdp/cdp-api.test.ts b/nodejs/src/cdp/cdp-api.test.ts
index 568565cabe16..339283955fd8 100644
--- a/nodejs/src/cdp/cdp-api.test.ts
+++ b/nodejs/src/cdp/cdp-api.test.ts
@@ -492,6 +492,55 @@ describe('CDP API', () => {
`)
})
+ it('redacts secret input values in mocked async function logs', async () => {
+ const SECRET_TOKEN = 'super-secret-bearer-token-xyz'
+
+ const hogFunctionWithSecret = await insertHogFunction({
+ name: 'test hog function with secret in headers',
+ ...HOG_EXAMPLES.simple_fetch,
+ ...HOG_FILTERS_EXAMPLES.no_filters,
+ inputs_schema: [
+ { key: 'url', type: 'string', label: 'URL', secret: false, required: true },
+ { key: 'access_token', type: 'string', label: 'Access token', secret: true, required: true },
+ {
+ key: 'method',
+ type: 'choice',
+ label: 'HTTP Method',
+ secret: false,
+ choices: [
+ { label: 'POST', value: 'POST' },
+ { label: 'GET', value: 'GET' },
+ ],
+ required: true,
+ },
+ { key: 'headers', type: 'dictionary', label: 'Headers', secret: false, required: false },
+ { key: 'body', type: 'json', label: 'Body', secret: false, required: true },
+ ],
+ inputs: {
+ url: { value: 'https://example.com/posthog-webhook' },
+ access_token: { value: SECRET_TOKEN },
+ method: { value: 'POST' },
+ headers: { value: { Authorization: `Bearer ${SECRET_TOKEN}` } },
+ body: { value: {} },
+ },
+ })
+
+ const res = await supertest(app)
+ .post(
+ `/api/projects/${hogFunctionWithSecret.team_id}/hog_functions/${hogFunctionWithSecret.id}/invocations`
+ )
+ .send({ globals, mock_async_functions: true })
+
+ expect(res.status).toEqual(200)
+ expect(res.body.errors).toEqual([])
+
+ const allLogText = res.body.logs.map((log: any) => log.message).join('\n')
+ expect(allLogText).not.toContain(SECRET_TOKEN)
+ // Confirm the sanitization path actually ran rather than the test passing by virtue of
+ // no fetch log being emitted at all.
+ expect(allLogText).toContain('***REDACTED***')
+ })
+
describe('transformations', () => {
let configuration: HogFunctionType
diff --git a/nodejs/src/cdp/cdp-api.ts b/nodejs/src/cdp/cdp-api.ts
index 6a054e2d0fef..ab81f078232d 100644
--- a/nodejs/src/cdp/cdp-api.ts
+++ b/nodejs/src/cdp/cdp-api.ts
@@ -41,7 +41,12 @@ import { NativeDestinationExecutorService } from './services/native-destination-
import { SegmentDestinationExecutorService } from './services/segment-destination-executor.service'
import { HOG_FUNCTION_TEMPLATES } from './templates'
import { HogFunctionInvocationGlobals, HogFunctionType, MinimalLogEntry } from './types'
-import { convertToHogFunctionInvocationGlobals, isNativeHogFunction, isSegmentPluginHogFunction } from './utils'
+import {
+ convertToHogFunctionInvocationGlobals,
+ isNativeHogFunction,
+ isSegmentPluginHogFunction,
+ sanitizeLogMessage,
+} from './utils'
import { convertToHogFunctionFilterGlobal } from './utils/hog-function-filtering'
// Allowlist of safe content types for webhook responses to prevent XSS
@@ -405,9 +410,14 @@ export class CdpApi {
for (const invocation of invocations) {
invocation.id = invocationID
+ const sensitiveValues = this.hogExecutor.getSensitiveValues(
+ invocation.hogFunction,
+ invocation.state.globals.inputs ?? {}
+ )
const options: HogExecutorExecuteAsyncOptions = buildHogExecutorAsyncOptions(
mock_async_functions,
- logs
+ logs,
+ sensitiveValues
)
let response: any = null
@@ -930,7 +940,8 @@ export class CdpApi {
const buildHogExecutorAsyncOptions = (
mockAsyncFunctions: boolean,
- logs: MinimalLogEntry[]
+ logs: MinimalLogEntry[],
+ sensitiveValues?: string[]
): HogExecutorExecuteAsyncOptions => {
let mockFunctions: Record<string, (...args: any[]) => any> | undefined
@@ -938,7 +949,19 @@ const buildHogExecutorAsyncOptions = (
mockFunctions = {}
for (const name of getRegisteredAsyncFunctionNames()) {
const handler = getAsyncFunctionHandler(name)!
- mockFunctions[name] = (...args: any[]) => handler.mock(args, logs)
+ mockFunctions[name] = (...args: any[]) => {
+ const startIndex = logs.length
+ const result = handler.mock(args, logs)
+ if (sensitiveValues?.length) {
+ for (let i = startIndex; i < logs.length; i++) {
+ logs[i] = {
+ ...logs[i],
+ message: sanitizeLogMessage([logs[i].message], sensitiveValues),
+ }
+ }
+ }
+ return result
+ }
}
}