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): list the tracking pixel source and keep credentials out of its events (#105619)

Daniel Marchuk60dd1fec2d13
diff --git a/nodejs/src/cdp/templates/_sources/pixel/pixel.template.test.ts b/nodejs/src/cdp/templates/_sources/pixel/pixel.template.test.ts
index 16c1cd251b1a..2d8dbd7fb1cd 100644
--- a/nodejs/src/cdp/templates/_sources/pixel/pixel.template.test.ts
+++ b/nodejs/src/cdp/templates/_sources/pixel/pixel.template.test.ts
@@ -20,12 +20,13 @@ describe('pixel template', () => {
},
}
- it('should respond with a 1x1 pixel', async () => {
+ it('should respond with a 1x1 pixel and keep credentials out of the event and the log', async () => {
const response = await tester.invoke(
{
event: '{request.query.ph_event}',
distinct_id: 'hardcoded',
properties: { query_params: '{request.query}' },
+ debug: true,
},
{
request: {
@@ -33,7 +34,14 @@ describe('pixel template', () => {
body: {},
stringBody: '',
headers: {},
- query: { ph_event: 'the event', other: 'other', params: '2' },
+ query: {
+ ph_event: 'the event',
+ other: 'other',
+ params: '2',
+ api_key: 'secret-key',
+ 'x-api-key': 'secret-dashed',
+ access_token: 'secret-token',
+ },
ip: '127.0.0.1',
},
}
@@ -60,9 +68,111 @@ describe('pixel template', () => {
]
`
)
+ expect(response.logs.map((x) => x.message)).toEqual([
+ `Incoming request:, {"ph_event":"the event","other":"other","params":"2"}`,
+ expect.stringContaining('Function completed'),
+ ])
expect(response.execResult).toEqual(GOOD_RESPONSE)
})
+ it.each([
+ ['api_key'],
+ ['X-API-Key'],
+ ['access_token'],
+ ['accessToken'],
+ ['refresh-token'],
+ ['clientSecret'],
+ ['client_secret'],
+ ['Token'],
+ ['signature'],
+ ['has_password'],
+ ['account[password]'],
+ ['account[token]'],
+ ['private_key'],
+ ['aws_secret_access_key'],
+ ['AWS-Secret-Access-Key'],
+ ['access_key'],
+ ['accessKey'],
+ ])('drops the credential key %s from the captured query', async (key) => {
+ const response = await tester.invoke(
+ { event: 'the event', distinct_id: 'hardcoded', properties: { query_params: '{request.query}' } },
+ {
+ request: {
+ method: 'GET',
+ body: {},
+ stringBody: '',
+ headers: {},
+ query: { utm_source: 'newsletter', [key]: 'secret-value' },
+ ip: '127.0.0.1',
+ },
+ }
+ )
+ expect(response.capturedPostHogEvents[0].properties.query_params).toEqual({ utm_source: 'newsletter' })
+ })
+
+ it.each([
+ ['api_key', 'utm_source'],
+ ['token', 'tokens_used'],
+ ])('drops the credential %s mapped at the top level and keeps %s', async (credential, kept) => {
+ const response = await tester.invoke(
+ {
+ event: 'the event',
+ distinct_id: 'hardcoded',
+ properties: {
+ [credential]: `{request.query.${credential}}`,
+ [kept]: `{request.query.${kept}}`,
+ },
+ },
+ {
+ request: {
+ method: 'GET',
+ body: {},
+ stringBody: '',
+ headers: {},
+ query: { [credential]: 'secret-value', [kept]: 'kept-value' },
+ ip: '127.0.0.1',
+ },
+ }
+ )
+ expect(response.capturedPostHogEvents[0].properties).toEqual({
+ $hog_function_execution_count: 1,
+ [kept]: 'kept-value',
+ })
+ })
+
+ it.each([
+ ['tokens_used'],
+ ['total_tokens'],
+ ['api_key_hint'],
+ ['api_key_status'],
+ ['apiXkey'],
+ ['password_reset'],
+ ['reset_token_id'],
+ ['top_secret_sale'],
+ ['isToken'],
+ ['designToken'],
+ ['monkey'],
+ ['aws_access_key_id'],
+ ])('keeps the look-alike key %s in the captured query', async (key) => {
+ const response = await tester.invoke(
+ { event: 'the event', distinct_id: 'hardcoded', properties: { query_params: '{request.query}' } },
+ {
+ request: {
+ method: 'GET',
+ body: {},
+ stringBody: '',
+ headers: {},
+ query: { utm_source: 'newsletter', [key]: 'kept-value' },
+ ip: '127.0.0.1',
+ },
+ }
+ )
+ expect(response.capturedPostHogEvents[0].properties.query_params).toEqual({
+ utm_source: 'newsletter',
+ [key]: 'kept-value',
+ })
+ })
+
it('should respond with pixel even if event cannot be parsed', async () => {
const response = await tester.invoke(
{
@@ -83,6 +193,10 @@ describe('pixel template', () => {
expect(response.error).toBeUndefined()
expect(response.finished).toEqual(true)
expect(response.capturedPostHogEvents).toEqual([])
+ expect(response.logs.map((x) => x.message)).toEqual([
+ 'No event captured because the event name or the distinct ID is empty. By default they come from the ph_event and ph_distinct_id query parameters.',
+ expect.stringContaining('Function completed'),
+ ])
expect(response.execResult).toEqual(GOOD_RESPONSE)
})
})
diff --git a/nodejs/src/cdp/templates/_sources/pixel/pixel.template.ts b/nodejs/src/cdp/templates/_sources/pixel/pixel.template.ts
index fc4bc3ded80e..78527352de95 100644
--- a/nodejs/src/cdp/templates/_sources/pixel/pixel.template.ts
+++ b/nodejs/src/cdp/templates/_sources/pixel/pixel.template.ts
@@ -1,5 +1,7 @@
import { HogFunctionTemplate } from '~/cdp/types'
+import { WITHOUT_CREDENTIALS_HOG } from '../without-credentials'
+
export const template: HogFunctionTemplate = {
free: false,
status: 'alpha',
@@ -11,17 +13,23 @@ export const template: HogFunctionTemplate = {
icon_url: '/static/services/webhook.svg',
category: ['Email', 'Tracking'],
code_language: 'hog',
- code: `
+ code: `${WITHOUT_CREDENTIALS_HOG}
if(inputs.debug) {
- print('Incoming request:', request.query)
+ print('Incoming request:', withoutCredentials(request.query))
}
if(not empty(inputs.distinct_id) and not empty(inputs.event)) {
+ let properties := {}
+ for (let propertyKey, propertyValue in withoutCredentials(inputs.properties ?? {})) {
+ properties[propertyKey] := withoutCredentials(propertyValue)
+ }
postHogCapture({
'event': inputs.event,
'distinct_id': inputs.distinct_id,
- 'properties': inputs.properties
+ 'properties': properties
})
+} else {
+ print('No event captured because the event name or the distinct ID is empty. By default they come from the ph_event and ph_distinct_id query parameters.')
}
return {
diff --git a/nodejs/src/cdp/templates/_sources/without-credentials.ts b/nodejs/src/cdp/templates/_sources/without-credentials.ts
new file mode 100644
index 000000000000..f7f75b070343
--- /dev/null
+++ b/nodejs/src/cdp/templates/_sources/without-credentials.ts
@@ -0,0 +1,23 @@
+// A request often carries the caller's credential in the query string or body (?api_key=...),
+// and a source template stores or prints whatever arrives. Templates prepend this Hog function
+// to their code and pass each mapped object through it, so a key that names a credential never
+// reaches an event, a person's properties or the function logs.
+// It drops the matching top-level keys only. A credential nested deeper in the object stays.
+// A flat bracketed key such as account[password] is judged by its last segment. CamelCase names
+// are listed one by one, because a pattern cannot tell accessToken from designToken.
+export const WITHOUT_CREDENTIALS_HOG = `
+fun withoutCredentials(source) {
+ if (typeof(source) != 'object') {
+ return source
+ }
+ let kept := {}
+ for (let key, value in source) {
+ let name := replaceAll(replaceAll(replaceAll(lower(key), '-', '_'), '[', '.'), ']', '')
+ let isCredential := match(name, '(^|[.])(auth|authorization|key|apikey|password|passwd|secret|signature|token|accesskey|accesstoken|refreshtoken|authtoken|idtoken|apitoken|sessiontoken|bearertoken|clientsecret|apisecret|secretkey|secretaccesskey|privatekey)$') or match(name, '(api_key|secret_key|access_key|private_key|_token|_secret|password)$')
+ if (not isCredential) {
+ kept[key] := value
+ }
+ }
+ return kept
+}
+`
diff --git a/nodejs/tests/fixtures/hog-bytecode/085d7721eecc7438a828553b3de5db19c53e46b2ce73fc05920f64aa0b74ec82.json b/nodejs/tests/fixtures/hog-bytecode/085d7721eecc7438a828553b3de5db19c53e46b2ce73fc05920f64aa0b74ec82.json
new file mode 100644
index 000000000000..ff62a8ba5c26
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/085d7721eecc7438a828553b3de5db19c53e46b2ce73fc05920f64aa0b74ec82.json
@@ -0,0 +1 @@
+["_H",1,32,"token",32,"query",32,"request",1,3,38]
diff --git a/nodejs/tests/fixtures/hog-bytecode/0dfabfd6225888a3b18bf22548e9979d4352c6aa52c19be4a62b71b40de3acac.json b/nodejs/tests/fixtures/hog-bytecode/0dfabfd6225888a3b18bf22548e9979d4352c6aa52c19be4a62b71b40de3acac.json
new file mode 100644
index 000000000000..38cf06c46243
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/0dfabfd6225888a3b18bf22548e9979d4352c6aa52c19be4a62b71b40de3acac.json
@@ -0,0 +1 @@
+["_H",1,32,"api_key",32,"query",32,"request",1,3,38]
diff --git a/nodejs/tests/fixtures/hog-bytecode/3a897f3732037c6b85ed78917752a19fc735b95d46ff1bef775339eb627dc41d.json b/nodejs/tests/fixtures/hog-bytecode/3a897f3732037c6b85ed78917752a19fc735b95d46ff1bef775339eb627dc41d.json
new file mode 100644
index 000000000000..570ee4644848
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/3a897f3732037c6b85ed78917752a19fc735b95d46ff1bef775339eb627dc41d.json
@@ -0,0 +1 @@
+["_H",1,32,"tokens_used",32,"query",32,"request",1,3,38]
diff --git a/nodejs/tests/fixtures/hog-bytecode/75488a2982a076ba67297dd57bd51ded175ab9a7f6f394c719f766b32a0c3b0f.json b/nodejs/tests/fixtures/hog-bytecode/75488a2982a076ba67297dd57bd51ded175ab9a7f6f394c719f766b32a0c3b0f.json
new file mode 100644
index 000000000000..586e22f8a107
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/75488a2982a076ba67297dd57bd51ded175ab9a7f6f394c719f766b32a0c3b0f.json
@@ -0,0 +1 @@
+["_H",1,52,"withoutCredentials",1,0,133,32,"object",36,0,2,"typeof",1,12,40,3,36,0,38,42,0,36,0,36,2,2,"keys",1,36,2,2,"values",1,33,1,36,4,2,"length",1,31,31,36,6,36,5,16,40,79,36,3,36,5,45,37,7,36,4,36,5,45,37,8,36,7,2,"lower",1,32,"-",32,"_",2,"replaceAll",3,32,"[",32,".",2,"replaceAll",3,32,"]",32,"",2,"replaceAll",3,36,9,32,"(^|[.])(auth|authorization|key|apikey|password|passwd|secret|signature|token|accesskey|accesstoken|refreshtoken|authtoken|idtoken|apitoken|sessiontoken|bearertoken|clientsecret|apisecret|secretkey|secretaccesskey|privatekey)$",2,"match",2,36,9,32,"(api_key|secret_key|access_key|private_key|_token|_secret|password)$",2,"match",2,4,2,36,10,5,40,7,36,1,36,7,36,8,46,35,35,36,5,33,1,6,37,5,39,-86,35,35,35,35,35,35,35,36,1,38,35,53,0,32,"debug",32,"inputs",1,2,40,16,32,"Incoming request:",32,"query",32,"request",1,2,36,0,54,1,2,"print",2,35,32,"distinct_id",32,"inputs",1,2,2,"empty",1,5,32,"event",32,"inputs",1,2,2,"empty",1,5,3,2,40,113,42,0,32,"properties",32,"inputs",1,2,47,3,35,42,0,36,0,54,1,36,2,2,"keys",1,36,2,2,"values",1,33,1,36,4,2,"length",1,31,31,36,6,36,5,16,40,34,36,3,36,5,45,37,7,36,4,36,5,45,37,8,36,1,36,7,36,8,36,0,54,1,46,36,5,33,1,6,37,5,39,-41,35,35,35,35,35,35,35,32,"event",32,"event",32,"inputs",1,2,32,"distinct_id",32,"distinct_id",32,"inputs",1,2,32,"properties",36,1,42,3,2,"postHogCapture",1,35,35,39,6,32,"No event captured because the event name or the distinct ID is empty. By default they come from the ph_event and ph_distinct_id query parameters.",2,"print",1,35,32,"httpResponse",32,"status",33,200,32,"body",32,"R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==",32,"contentType",32,"image/gif",32,"isBase64Encoded",29,42,4,42,1,38,35]
diff --git a/nodejs/tests/fixtures/hog-bytecode/7be6c23d8200fe9302f44d3fc2595320687ab00e5efdb99d3bede7f93e0c9ec4.json b/nodejs/tests/fixtures/hog-bytecode/7be6c23d8200fe9302f44d3fc2595320687ab00e5efdb99d3bede7f93e0c9ec4.json
new file mode 100644
index 000000000000..c2aefe101f6a
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/7be6c23d8200fe9302f44d3fc2595320687ab00e5efdb99d3bede7f93e0c9ec4.json
@@ -0,0 +1 @@
+["_H",1,32,"the event",38]
diff --git a/nodejs/tests/fixtures/hog-bytecode/9372890f879588fc5d1f66cfc6f49d5288b780e18f9332bcbc4a85e3b579c496.json b/nodejs/tests/fixtures/hog-bytecode/9372890f879588fc5d1f66cfc6f49d5288b780e18f9332bcbc4a85e3b579c496.json
new file mode 100644
index 000000000000..66fdafb9255f
--- /dev/null
+++ b/nodejs/tests/fixtures/hog-bytecode/9372890f879588fc5d1f66cfc6f49d5288b780e18f9332bcbc4a85e3b579c496.json
@@ -0,0 +1 @@
+["_H",1,32,"utm_source",32,"query",32,"request",1,3,38]
diff --git a/nodejs/tests/fixtures/hog-bytecode/b900296f48c9c534a28bb942b675ca03c19bae83f22fa81c9dae6133a4c3aa4f.json b/nodejs/tests/fixtures/hog-bytecode/b900296f48c9c534a28bb942b675ca03c19bae83f22fa81c9dae6133a4c3aa4f.json
deleted file mode 100644
index 2a68d6250dda..000000000000
--- a/nodejs/tests/fixtures/hog-bytecode/b900296f48c9c534a28bb942b675ca03c19bae83f22fa81c9dae6133a4c3aa4f.json
+++ /dev/null
@@ -1 +0,0 @@
-["_H",1,32,"debug",32,"inputs",1,2,40,12,32,"Incoming request:",32,"query",32,"request",1,2,2,"print",2,35,32,"distinct_id",32,"inputs",1,2,2,"empty",1,5,32,"event",32,"inputs",1,2,2,"empty",1,5,3,2,40,30,32,"event",32,"event",32,"inputs",1,2,32,"distinct_id",32,"distinct_id",32,"inputs",1,2,32,"properties",32,"properties",32,"inputs",1,2,42,3,2,"postHogCapture",1,35,32,"httpResponse",32,"status",33,200,32,"body",32,"R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==",32,"contentType",32,"image/gif",32,"isBase64Encoded",29,42,4,42,1,38]
diff --git a/products/data_warehouse/frontend/scenes/NewSourceScene/SourceCatalog.tsx b/products/data_warehouse/frontend/scenes/NewSourceScene/SourceCatalog.tsx
index 58927211b90f..bd56c146f5b7 100644
--- a/products/data_warehouse/frontend/scenes/NewSourceScene/SourceCatalog.tsx
+++ b/products/data_warehouse/frontend/scenes/NewSourceScene/SourceCatalog.tsx
@@ -53,6 +53,9 @@ const SourceTile = memo(function SourceTile({
</div>
<div className="flex flex-col items-start gap-2 min-w-0 text-left">
<div className="font-medium text-sm leading-tight line-clamp-2">{item.label}</div>
+ {item.description && (
+ <div className="text-xs text-secondary leading-snug line-clamp-2">{item.description}</div>
+ )}
{comingSoon ? (
<>
<LemonTag type="warning">Coming soon</LemonTag>
diff --git a/products/data_warehouse/frontend/scenes/NewSourceScene/sourceCatalogLogic.ts b/products/data_warehouse/frontend/scenes/NewSourceScene/sourceCatalogLogic.ts
index f11a66c93b6d..dbc50576972a 100644
--- a/products/data_warehouse/frontend/scenes/NewSourceScene/sourceCatalogLogic.ts
+++ b/products/data_warehouse/frontend/scenes/NewSourceScene/sourceCatalogLogic.ts
@@ -95,6 +95,8 @@ export interface CatalogItem {
/** The source kind passed to the new-source URL (e.g. "Stripe", or "aws" for self-managed). */
name: string
label: string
+ /** One line under the label, for entries whose name alone doesn't say what they do. */
+ description?: string
iconType: string
iconClassName?: string
category: DataWarehouseSourceCategoryApi
@@ -110,19 +112,33 @@ export interface CatalogItem {
featured?: boolean
}
-// PostHog's own webhook endpoint isn't a warehouse connector, so it never reaches this catalog
-// through `availableSources` — but it's where people look for one, and the only other entry point
+// PostHog's own event sources aren't warehouse connectors, so they never reach this catalog
+// through `availableSources` — but it's where people look for them, and the only other entry point
// is the sources list. Gated on the same preview flag as that list's section.
-const EVENT_WEBHOOK_CATALOG_ITEM: CatalogItem = {
- name: 'event-webhook',
- label: 'Incoming webhook',
- iconType: 'PostHog',
- category: 'Engineering & monitoring',
- keywords: ['webhook', 'webhooks', 'http', 'https', 'endpoint', 'events', 'incoming', 'custom', 'real time'],
- status: 'stable',
- releaseStatus: 'alpha',
- url: urls.hogFunctionNew('template-source-webhook'),
-}
+const EVENT_SOURCE_CATALOG_ITEMS: CatalogItem[] = [
+ {
+ name: 'event-webhook',
+ label: 'Incoming webhook',
+ description: 'Turn HTTP requests from any service into events.',
+ iconType: 'PostHog',
+ category: 'Engineering & monitoring',
+ keywords: ['webhook', 'webhooks', 'http', 'https', 'endpoint', 'events', 'incoming', 'custom', 'real time'],
+ status: 'stable',
+ releaseStatus: 'alpha',
+ url: urls.hogFunctionNew('template-source-webhook'),
+ },
+ {
+ name: 'event-tracking-pixel',
+ label: 'Tracking pixel',
+ description: 'Capture an event each time an email or page loads a 1x1 image.',
+ iconType: 'PostHog',
+ category: 'Engineering & monitoring',
+ keywords: ['pixel', 'tracking pixel', 'image', 'gif', 'email', 'open tracking', 'beacon', 'events'],
+ status: 'stable',
+ releaseStatus: 'alpha',
+ url: urls.hogFunctionNew('template-source-webhook-pixel'),
+ },
+]
export interface CatalogCategory {
category: SourceCategoryFilter
@@ -416,12 +432,12 @@ export const sourceCatalogLogic = kea<sourceCatalogLogicType>([
})
)
- // `allowedSources` restricts the catalog to warehouse connectors, so the webhook
- // source has no place in it.
- const eventWebhook =
- !allowedSources && featureFlags[FEATURE_FLAGS.CDP_HOG_SOURCES] ? [EVENT_WEBHOOK_CATALOG_ITEM] : []
+ // `allowedSources` restricts the catalog to warehouse connectors, so event sources have
+ // no place in it.
+ const eventSources =
+ !allowedSources && featureFlags[FEATURE_FLAGS.CDP_HOG_SOURCES] ? EVENT_SOURCE_CATALOG_ITEMS : []
- return [...managed, ...selfManaged, ...fileUpload, ...eventWebhook]
+ return [...managed, ...selfManaged, ...fileUpload, ...eventSources]
},
// featureFlags is a broad dependency that changes identity on every flag refresh;
// keeping the previous array when the derived catalog is unchanged stops the Fuse
diff --git a/products/data_warehouse/frontend/scenes/NewSourceScene/tests/sourceCatalogLogic.test.ts b/products/data_warehouse/frontend/scenes/NewSourceScene/tests/sourceCatalogLogic.test.ts
index a63076392abd..72a2f02a728f 100644
--- a/products/data_warehouse/frontend/scenes/NewSourceScene/tests/sourceCatalogLogic.test.ts
+++ b/products/data_warehouse/frontend/scenes/NewSourceScene/tests/sourceCatalogLogic.test.ts
@@ -258,22 +258,22 @@ describe('sourceCatalogLogic', () => {
})
it.each([
- { previewEnabled: true, expectedMatches: 1 },
- { previewEnabled: false, expectedMatches: 0 },
+ { search: 'webhook', name: 'event-webhook', previewEnabled: true, expectedMatches: 1 },
+ { search: 'webhook', name: 'event-webhook', previewEnabled: false, expectedMatches: 0 },
+ { search: 'pixel', name: 'event-tracking-pixel', previewEnabled: true, expectedMatches: 1 },
+ { search: 'pixel', name: 'event-tracking-pixel', previewEnabled: false, expectedMatches: 0 },
])(
- 'shows the incoming webhook source in a "webhook" search when the preview is $previewEnabled',
- ({ previewEnabled, expectedMatches }) => {
+ 'shows $name in a "$search" search when the preview is $previewEnabled',
+ ({ search, name, previewEnabled, expectedMatches }) => {
const logic = sourceCatalogLogic()
featureFlagLogic.mount()
featureFlagLogic.actions.setFeatureFlags(previewEnabled ? [FEATURE_FLAGS.CDP_HOG_SOURCES] : [], {
[FEATURE_FLAGS.CDP_HOG_SOURCES]: previewEnabled,
})
- logic.actions.setSearch('webhook')
+ logic.actions.setSearch(search)
- expect(logic.values.filteredItems.filter((item) => item.name === 'event-webhook')).toHaveLength(
- expectedMatches
- )
+ expect(logic.values.filteredItems.filter((item) => item.name === name)).toHaveLength(expectedMatches)
}
)
@@ -308,7 +308,7 @@ describe('sourceCatalogLogic', () => {
[FEATURE_FLAGS.CDP_HOG_SOURCES]: true,
})
- expect(logic.values.catalogItems.some((item) => item.name === 'event-webhook')).toBe(false)
+ expect(logic.values.catalogItems.some((item) => item.name.startsWith('event-'))).toBe(false)
unmountRestricted()
})