: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
chore(cdp): harden filter-out regex and deprecate the property-filter plugin (#93951)
Meikel Ratz18171c454efd
diff --git a/nodejs/src/cdp/legacy-plugins/_transformations/posthog-filter-out-plugin/index.test.ts b/nodejs/src/cdp/legacy-plugins/_transformations/posthog-filter-out-plugin/index.test.tsindex 45dac0b236b3..2d3cc3573c32 100644--- a/nodejs/src/cdp/legacy-plugins/_transformations/posthog-filter-out-plugin/index.test.ts+++ b/nodejs/src/cdp/legacy-plugins/_transformations/posthog-filter-out-plugin/index.test.ts@@ -255,3 +255,89 @@ test('Event satisfies no filter groups and is dropped', () => {const processedEvent = processEvent(event, meta_or)expect(processedEvent).toBeUndefined()})++test('a regex filter with a pathological pattern stays linear', () => {+ // The built-in engine backtracks exponentially on this nested-quantifier pattern.+ // RE2 evaluates it in linear time, so the bound stays small.+ const regexFilters: Filter[] = [{ property: '$pathname', type: 'string', operator: 'regex', value: '(x+x+)+y' }]+ const regexMeta = {+ global: { filters: regexFilters, eventsToDrop: [] },+ } as unknown as LegacyTransformationPluginMeta+ const event = createEvent({ properties: { $pathname: 'x'.repeat(40) } }) as unknown as PluginEvent++ const start = performance.now()+ processEvent(event, regexMeta)+ const elapsedMs = performance.now() - start++ expect(elapsedMs).toBeLessThan(1000)+})++test('a regex pattern RE2 cannot compile does not throw and is treated as no match', () => {+ // Lookahead is valid JavaScript regex but RE2 rejects it. The keep-if-match filter should+ // process the event without throwing, treating the incompatible pattern as no match.+ const regexFilters: Filter[] = [{ property: '$pathname', type: 'string', operator: 'regex', value: '(?=/admin)' }]+ const regexMeta = {+ global: { filters: regexFilters, eventsToDrop: [] },+ } as unknown as LegacyTransformationPluginMeta+ const event = createEvent({ properties: { $pathname: '/admin' } }) as unknown as PluginEvent++ expect(() => processEvent(event, regexMeta)).not.toThrow()+ // No match, so the keep-if-match filter is not satisfied and the event is dropped.+ expect(processEvent(event, regexMeta)).toBeUndefined()+})++// Patterns cover the feature families real filter-out configs use in production: alternation,+// escaped separators (\. \/), anchors, wildcard tails, and shorthand classes (\d). RE2 must+// match these identically to the old RegExp engine, otherwise events are wrongly kept or dropped.+test.each([+ [+ 'alternation and escaped dot matches',+ '$current_url',+ 'regex',+ '(google|bing|duckduckgo)\\.com',+ 'https://google.com/search',+ true,+ ],+ [+ 'alternation and escaped dot does not match',+ '$current_url',+ 'regex',+ '(google|bing|duckduckgo)\\.com',+ 'https://example.org/',+ false,+ ],+ ['escaped path segment matches', '$pathname', 'regex', '\\/(admin|internal)\\/', '/app/admin/users', true],+ ['anchored full string matches', 'page', 'regex', '^(home|quests)$', 'home', true],+ ['anchored full string rejects a superstring', 'page', 'regex', '^(home|quests)$', 'homepage', false],+ ['wildcard tail matches', 'quickActionId', 'regex', 'app\\/.*', 'app/anything/here', true],+ ['shorthand digit class matches', 'userId', 'regex', 'user_\\d+', 'user_1234', true],+ [+ 'not_regex keeps a non-match',+ '$current_url',+ 'not_regex',+ '(facebook|twitter)\\.com',+ 'https://example.org/',+ true,+ ],+ [+ 'not_regex drops a match',+ '$current_url',+ 'not_regex',+ '(facebook|twitter)\\.com',+ 'https://facebook.com/me',+ false,+ ],+])('regex operator via RE2: %s', (_label, property, operator, value, propValue, shouldKeep) => {+ const regexMeta = {+ global: { filters: [{ property, type: 'string', operator, value }], eventsToDrop: [] },+ } as unknown as LegacyTransformationPluginMeta+ const event = createEvent({ properties: { [property]: propValue } }) as unknown as PluginEvent++ const processed = processEvent(event, regexMeta)++ if (shouldKeep) {+ expect(processed).toEqual(event)+ } else {+ expect(processed).toBeUndefined()+ }+})diff --git a/nodejs/src/cdp/legacy-plugins/_transformations/posthog-filter-out-plugin/index.ts b/nodejs/src/cdp/legacy-plugins/_transformations/posthog-filter-out-plugin/index.tsindex 585a0777b03a..15f08ff8fc2f 100644--- a/nodejs/src/cdp/legacy-plugins/_transformations/posthog-filter-out-plugin/index.ts+++ b/nodejs/src/cdp/legacy-plugins/_transformations/posthog-filter-out-plugin/index.ts@@ -1,4 +1,5 @@import { parseJSON } from '~/common/utils/json-parse'+import { createTrackedRE2 } from '~/common/utils/tracked-re2'import { Meta, PluginAttachment, PluginEvent } from '~/plugin-scaffold'import { LegacyTransformationPluginMeta } from '../../types'@@ -25,14 +26,27 @@ export type PluginMeta = Meta<{}}>+// RE2 rejects some JavaScript-valid patterns, such as lookahead and backreferences. Treat an+// uncompilable pattern as no match rather than letting it throw and error the whole event, matching+// how the action matcher handles user-supplied patterns.+const safeRegexTest = (value: any, pattern: any): boolean => {+ try {+ return createTrackedRE2(pattern, undefined, 'filter-out:regex').test(value)+ } catch {+ return false+ }+}+const operations: Record<Filter['type'], Record<string, (a: any, b: any) => boolean>> = {string: {is: (a, b) => a === b,is_not: (a, b) => a !== b,contains: (a, b) => a.includes(b),not_contains: (a, b) => !a.includes(b),- regex: (a, b) => new RegExp(b).test(a),- not_regex: (a, b) => !new RegExp(b).test(a),+ // RE2 evaluates user patterns in linear time, matching the engine used elsewhere for+ // user-supplied patterns (HogVM match, action matcher).+ regex: (a, b) => safeRegexTest(a, b),+ not_regex: (a, b) => !safeRegexTest(a, b),},number: {gt: (a, b) => a > b,diff --git a/nodejs/src/cdp/legacy-plugins/_transformations/property-filter-plugin/template.ts b/nodejs/src/cdp/legacy-plugins/_transformations/property-filter-plugin/template.tsindex 8b636fc51e58..5c22635df84c 100644--- a/nodejs/src/cdp/legacy-plugins/_transformations/property-filter-plugin/template.ts+++ b/nodejs/src/cdp/legacy-plugins/_transformations/property-filter-plugin/template.ts@@ -7,7 +7,7 @@ export const propertyFilterPlugin: LegacyTransformationPlugin = {setupPlugin,template: {free: true,- status: 'stable',+ status: 'deprecated',type: 'transformation',id: 'plugin-property-filter-plugin',name: 'Property Filter',