: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(cdp): bypass bot filter for non-browser $lib events (#60557)
Daniel Marchuk113690ac5ac7
diff --git a/nodejs/src/cdp/templates/_transformations/bot-detection/bot-detection.template.test.ts b/nodejs/src/cdp/templates/_transformations/bot-detection/bot-detection.template.test.tsindex cbccf65ec175..96f4cb099803 100644--- a/nodejs/src/cdp/templates/_transformations/bot-detection/bot-detection.template.test.ts+++ b/nodejs/src/cdp/templates/_transformations/bot-detection/bot-detection.template.test.ts@@ -224,6 +224,113 @@ describe('bot-detection.template', () => {expect(response.execResult).toBeTruthy()})+ it.each([+ ['posthog-python', 'python-httpx'],+ ['posthog-node', 'axios/1.4.0'],+ ['posthog-webhook', 'okhttp/4.10.0'],+ ['posthog-ios', 'CFNetwork/1410.0.3'],+ ])('should skip PostHog-curated bot UA list when $lib=%s (non-browser SDK)', async (lib, ua) => {+ mockGlobals = tester.createGlobals({+ event: {+ properties: {+ $lib: lib,+ $raw_user_agent: ua,+ },+ },+ })++ const response = await tester.invoke(DEFAULT_INPUTS, mockGlobals)++ expect(response.finished).toBeTruthy()+ expect(response.error).toBeFalsy()+ expect(response.execResult).toBeTruthy()+ })++ it('should skip PostHog-curated bot IP list when $lib is a non-browser SDK', async () => {+ mockGlobals = tester.createGlobals({+ event: {+ properties: {+ $lib: 'posthog-python',+ $ip: '5.39.1.225',+ $raw_user_agent: 'python-httpx',+ },+ },+ })++ const response = await tester.invoke(DEFAULT_INPUTS, mockGlobals)++ expect(response.finished).toBeTruthy()+ expect(response.error).toBeFalsy()+ expect(response.execResult).toBeTruthy()+ })++ it('should still apply customBotPatterns even for non-browser $lib', async () => {+ mockGlobals = tester.createGlobals({+ event: {+ properties: {+ $lib: 'posthog-python',+ $raw_user_agent: 'MyCustomBot/1.0',+ },+ },+ })++ const response = await tester.invoke(+ {+ ...DEFAULT_INPUTS,+ customBotPatterns: 'mycustombot',+ },+ mockGlobals+ )++ expect(response.finished).toBeTruthy()+ expect(response.error).toBeFalsy()+ expect(response.execResult).toBeFalsy()+ })++ it('should still apply customIpPrefixes even for non-browser $lib', async () => {+ mockGlobals = tester.createGlobals({+ event: {+ properties: {+ $lib: 'posthog-python',+ $ip: '1.2.3.4',+ $raw_user_agent: 'python-httpx',+ },+ },+ })++ const response = await tester.invoke(+ {+ ...DEFAULT_INPUTS,+ customIpPrefixes: '1.2.3.0/24',+ },+ mockGlobals+ )++ expect(response.finished).toBeTruthy()+ expect(response.error).toBeFalsy()+ expect(response.execResult).toBeFalsy()+ })++ it.each([+ ['web', 'web' as string | undefined],+ ['js', 'js' as string | undefined],+ ['(missing)', undefined as string | undefined],+ ])('should still filter known bot UA when $lib=%s (browser or unknown source)', async (_label, lib) => {+ const properties: Record<string, string> = {+ $raw_user_agent: 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',+ }+ if (lib !== undefined) {+ properties.$lib = lib+ }+ mockGlobals = tester.createGlobals({ event: { properties } })++ const response = await tester.invoke(DEFAULT_INPUTS, mockGlobals)++ expect(response.finished).toBeTruthy()+ expect(response.error).toBeFalsy()+ expect(response.execResult).toBeFalsy()+ })+it('should not filter out known bot ips if filterKnownBotIps is false', async () => {mockGlobals = tester.createGlobals({event: {diff --git a/nodejs/src/cdp/templates/_transformations/bot-detection/bot-detection.template.ts b/nodejs/src/cdp/templates/_transformations/bot-detection/bot-detection.template.tsindex 5ea9416aed76..8e201ea44bdf 100644--- a/nodejs/src/cdp/templates/_transformations/bot-detection/bot-detection.template.ts+++ b/nodejs/src/cdp/templates/_transformations/bot-detection/bot-detection.template.ts@@ -12,6 +12,14 @@ export const template: HogFunctionTemplate = {category: ['Custom'],code_language: 'hog',code: `+// PostHog's curated bot UA and IP lists are tuned for browser traffic. Server-side+// SDKs (posthog-python, posthog-node, ...) send HTTP-client UAs like 'python-httpx'+// and backend IPs that match those lists but are not bots. Treat anything other than+// the browser SDKs ($lib in {web, js}) as non-browser and skip the curated checks.+// Customer-configured customBotPatterns and customIpPrefixes still apply regardless.+let lib := event.properties['$lib']+let is_browser_traffic := empty(lib) or lib == 'web' or lib == 'js'+// Get the user agent valuelet user_agent := event.properties[inputs.userAgent]@@ -20,7 +28,7 @@ if (empty(user_agent) and inputs.keepUndefinedUseragent == 'No') {return null}-if (inputs.filterKnownBotUserAgents and isKnownBotUserAgent(user_agent)) {+if (is_browser_traffic and inputs.filterKnownBotUserAgents and isKnownBotUserAgent(user_agent)) {return null}@@ -46,7 +54,7 @@ if (empty(ip)) {return event}-if (inputs.filterKnownBotIps and isKnownBotIp(ip)) {+if (is_browser_traffic and inputs.filterKnownBotIps and isKnownBotIp(ip)) {return null}