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.

chore(frontend): keep re2js off the authenticated shell (#126881)

Paul D'Ambra53b160f4ebb1
diff --git a/frontend/bin/check-eager-graph.mjs b/frontend/bin/check-eager-graph.mjs
index 991a9c1c7814..4f2d65ad0dc8 100644
--- a/frontend/bin/check-eager-graph.mjs
+++ b/frontend/bin/check-eager-graph.mjs
@@ -104,8 +104,8 @@ const ROOTS = [
{
root: AUTHENTICATED_SHELL,
label: 'authenticated shell (every logged-in page)',
- // 2026-10-30: 6.75 MiB eager output, stable CSS included. ~0.3% headroom.
- budgetBytes: 7_100_000,
+ // 2026-11-01: 5.55 MiB eager output, stable CSS included. ~0.3% headroom.
+ budgetBytes: 5_840_000,
forbidden: [
'node_modules/monaco-editor/',
'products/product_analytics/frontend/insights/activityDescriptions',
@@ -133,6 +133,9 @@ const ROOTS = [
// The rich text editor stack is for the scenes that edit text. Rendered markdown needs none of it.
'node_modules/@tiptap/',
'node_modules/prosemirror-',
+ // The RE2 regex engine checks regex filters before a query runs, and the shell runs no queries.
+ // A hit means a module on the shell path imports lib/utils/regexp or re2js again.
+ 'node_modules/re2js/',
// The zod-en-locale-only plugin in common/esbuilder/utils.mjs keeps every locale except
// `en` out of the build. A hit on another locale means zod moved its locales barrel and
// the plugin no longer matches it.
@@ -176,8 +179,8 @@ const ROOTS = [
'src/scenes/project-homepage/today/TodayHome.tsx',
],
label: 'today home path',
- // 2026-10-30: 6.94 MiB, stable CSS included. ~0.3% headroom.
- budgetBytes: 7_300_000,
+ // 2026-11-01: 5.99 MiB, stable CSS included. ~0.3% headroom.
+ budgetBytes: 6_300_000,
forbidden: [
'src/scenes/project-homepage/ai-first/AiFirstHomepage.tsx',
'src/scenes/project-homepage/today/TodayReportPage.tsx',
diff --git a/frontend/src/queries/nodes/DataNode/dataNodeLogic.ts b/frontend/src/queries/nodes/DataNode/dataNodeLogic.ts
index e9935026d5ad..610ab2ef8196 100644
--- a/frontend/src/queries/nodes/DataNode/dataNodeLogic.ts
+++ b/frontend/src/queries/nodes/DataNode/dataNodeLogic.ts
@@ -34,6 +34,7 @@ import { teamLogic } from 'scenes/teamLogic'
import { userLogic } from 'scenes/userLogic'
import { DataNodeCollectionProps, dataNodeCollectionLogic } from '~/queries/nodes/DataNode/dataNodeCollectionLogic'
+import { hasInvalidRegexFilter } from '~/queries/nodes/DataNode/hasInvalidRegexFilter'
import { QueryScanPollResult, QueryScanState, resolveQueryScan } from '~/queries/nodes/DataNode/queryScan'
import { getSingleRequestServerMs } from '~/queries/nodes/DataNode/queryTimeBreakdown'
import { removeExpressionComment } from '~/queries/nodes/DataTable/utils'
@@ -1195,7 +1196,7 @@ export const dataNodeLogic = kea<dataNodeLogicType>([
return null
}
- if (!validateQuery(query)) {
+ if (!validateQuery(query, hasInvalidRegexFilter)) {
return null
}
diff --git a/frontend/src/queries/nodes/DataNode/hasInvalidRegexFilter.test.ts b/frontend/src/queries/nodes/DataNode/hasInvalidRegexFilter.test.ts
new file mode 100644
index 000000000000..8cb1cc948a65
--- /dev/null
+++ b/frontend/src/queries/nodes/DataNode/hasInvalidRegexFilter.test.ts
@@ -0,0 +1,74 @@
+import { hasInvalidRegexFilter } from './hasInvalidRegexFilter'
+
+describe('hasInvalidRegexFilter', () => {
+ it.each([
+ ['valid regex in property filter', { operator: 'regex', value: 'test.*' }, false],
+ ['valid not_regex in property filter', { operator: 'not_regex', value: '^foo$' }, false],
+ ['invalid regex - lookahead', { operator: 'regex', value: '(?=test)' }, true],
+ ['invalid regex - unclosed bracket', { operator: 'regex', value: '[unclosed' }, true],
+ ['invalid not_regex - backreference', { operator: 'not_regex', value: '(test)\\1' }, true],
+ ['non-regex operator with invalid pattern is ok', { operator: 'exact', value: '(?=test)' }, false],
+ ['regex operator with non-string value', { operator: 'regex', value: 123 }, false],
+ ['empty object', {}, false],
+ ['null', null, false],
+ ['string', 'test', false],
+ ['number', 42, false],
+ ])('%s', (_name, input, expected) => {
+ expect(hasInvalidRegexFilter(input)).toBe(expected)
+ })
+
+ it('finds invalid regex nested in arrays', () => {
+ const nested = [
+ { operator: 'exact', value: 'ok' },
+ { operator: 'regex', value: '(?=bad)' },
+ ]
+ expect(hasInvalidRegexFilter(nested)).toBe(true)
+ })
+
+ it('finds invalid regex deeply nested in objects', () => {
+ const deeplyNested = {
+ properties: {
+ type: 'AND',
+ values: [
+ {
+ type: 'OR',
+ values: [{ operator: 'regex', value: '(?=lookahead)' }],
+ },
+ ],
+ },
+ }
+ expect(hasInvalidRegexFilter(deeplyNested)).toBe(true)
+ })
+
+ it('returns false for valid regex deeply nested', () => {
+ const deeplyNested = {
+ properties: {
+ type: 'AND',
+ values: [
+ {
+ type: 'OR',
+ values: [{ operator: 'regex', value: 'valid.*pattern' }],
+ },
+ ],
+ },
+ }
+ expect(hasInvalidRegexFilter(deeplyNested)).toBe(false)
+ })
+
+ it('handles query-like structure with multiple property filters', () => {
+ const queryLike = {
+ kind: 'TrendsQuery',
+ series: [
+ {
+ kind: 'EventsNode',
+ event: '$pageview',
+ properties: [
+ { type: 'event', key: 'url', operator: 'regex', value: '^/valid/' },
+ { type: 'event', key: 'path', operator: 'not_regex', value: '(?!invalid)' },
+ ],
+ },
+ ],
+ }
+ expect(hasInvalidRegexFilter(queryLike)).toBe(true)
+ })
+})
diff --git a/frontend/src/queries/nodes/DataNode/hasInvalidRegexFilter.ts b/frontend/src/queries/nodes/DataNode/hasInvalidRegexFilter.ts
new file mode 100644
index 000000000000..294b7a926f48
--- /dev/null
+++ b/frontend/src/queries/nodes/DataNode/hasInvalidRegexFilter.ts
@@ -0,0 +1,22 @@
+import { isValidRE2 } from 'lib/utils/regexp'
+
+export const hasInvalidRegexFilter = (obj: unknown): boolean => {
+ if (Array.isArray(obj)) {
+ return obj.some(hasInvalidRegexFilter)
+ }
+
+ if (obj !== null && typeof obj === 'object') {
+ const record = obj as Record<string, unknown>
+ if (
+ (record.operator === 'regex' || record.operator === 'not_regex') &&
+ typeof record.value === 'string' &&
+ !isValidRE2(record.value)
+ ) {
+ return true
+ }
+
+ return Object.values(record).some(hasInvalidRegexFilter)
+ }
+
+ return false
+}
diff --git a/frontend/src/scenes/insights/utils/queryUtils.test.ts b/frontend/src/scenes/insights/utils/queryUtils.test.ts
index 318678305d4c..27121d167799 100644
--- a/frontend/src/scenes/insights/utils/queryUtils.test.ts
+++ b/frontend/src/scenes/insights/utils/queryUtils.test.ts
@@ -1,3 +1,4 @@
+import { hasInvalidRegexFilter } from '~/queries/nodes/DataNode/hasInvalidRegexFilter'
import { getDefaultQuery } from '~/queries/nodes/InsightViz/utils'
import { Node, NodeKind, TrendsFilter, TrendsQuery } from '~/queries/schema/schema-general'
import { ChartDisplayType, InsightType } from '~/types'
@@ -6,7 +7,6 @@ import {
compareDataNodeQuery,
compareQuery,
filterVariablesReferencedInQuery,
- hasInvalidRegexFilter,
isBoxPlotMissingProperty,
isDraftQueryWorthSaving,
syncSelectedVariablesToQuery,
@@ -61,86 +61,13 @@ describe('syncSelectedVariablesToQuery', () => {
})
})
-describe('hasInvalidRegexFilter', () => {
- it.each([
- ['valid regex in property filter', { operator: 'regex', value: 'test.*' }, false],
- ['valid not_regex in property filter', { operator: 'not_regex', value: '^foo$' }, false],
- ['invalid regex - lookahead', { operator: 'regex', value: '(?=test)' }, true],
- ['invalid regex - unclosed bracket', { operator: 'regex', value: '[unclosed' }, true],
- ['invalid not_regex - backreference', { operator: 'not_regex', value: '(test)\\1' }, true],
- ['non-regex operator with invalid pattern is ok', { operator: 'exact', value: '(?=test)' }, false],
- ['regex operator with non-string value', { operator: 'regex', value: 123 }, false],
- ['empty object', {}, false],
- ['null', null, false],
- ['string', 'test', false],
- ['number', 42, false],
- ])('%s', (_name, input, expected) => {
- expect(hasInvalidRegexFilter(input)).toBe(expected)
- })
-
- it('finds invalid regex nested in arrays', () => {
- const nested = [
- { operator: 'exact', value: 'ok' },
- { operator: 'regex', value: '(?=bad)' },
- ]
- expect(hasInvalidRegexFilter(nested)).toBe(true)
- })
-
- it('finds invalid regex deeply nested in objects', () => {
- const deeplyNested = {
- properties: {
- type: 'AND',
- values: [
- {
- type: 'OR',
- values: [{ operator: 'regex', value: '(?=lookahead)' }],
- },
- ],
- },
- }
- expect(hasInvalidRegexFilter(deeplyNested)).toBe(true)
- })
-
- it('returns false for valid regex deeply nested', () => {
- const deeplyNested = {
- properties: {
- type: 'AND',
- values: [
- {
- type: 'OR',
- values: [{ operator: 'regex', value: 'valid.*pattern' }],
- },
- ],
- },
- }
- expect(hasInvalidRegexFilter(deeplyNested)).toBe(false)
- })
-
- it('handles query-like structure with multiple property filters', () => {
- const queryLike = {
- kind: 'TrendsQuery',
- series: [
- {
- kind: 'EventsNode',
- event: '$pageview',
- properties: [
- { type: 'event', key: 'url', operator: 'regex', value: '^/valid/' },
- { type: 'event', key: 'path', operator: 'not_regex', value: '(?!invalid)' },
- ],
- },
- ],
- }
- expect(hasInvalidRegexFilter(queryLike)).toBe(true)
- })
-})
-
describe('validateQuery', () => {
it('returns false for funnels with less than 2 steps', () => {
const funnelQuery = {
kind: NodeKind.FunnelsQuery,
series: [{ kind: NodeKind.EventsNode, event: '$pageview' }],
}
- expect(validateQuery(funnelQuery)).toBe(false)
+ expect(validateQuery(funnelQuery, hasInvalidRegexFilter)).toBe(false)
})
it('returns true for funnels with 2+ steps', () => {
@@ -151,7 +78,7 @@ describe('validateQuery', () => {
{ kind: NodeKind.EventsNode, event: '$signup' },
],
}
- expect(validateQuery(funnelQuery)).toBe(true)
+ expect(validateQuery(funnelQuery, hasInvalidRegexFilter)).toBe(true)
})
it('returns false for funnels with incomplete data warehouse step', () => {
@@ -167,7 +94,7 @@ describe('validateQuery', () => {
{ kind: NodeKind.EventsNode, event: '$signup' },
],
}
- expect(validateQuery(funnelQuery)).toBe(false)
+ expect(validateQuery(funnelQuery, hasInvalidRegexFilter)).toBe(false)
})
it('returns false for query with invalid regex property filter', () => {
@@ -176,7 +103,7 @@ describe('validateQuery', () => {
series: [{ kind: NodeKind.EventsNode, event: '$pageview' }],
properties: [{ type: 'event', key: 'url', operator: 'regex', value: '(?=lookahead)' }],
}
- expect(validateQuery(trendsQuery)).toBe(false)
+ expect(validateQuery(trendsQuery, hasInvalidRegexFilter)).toBe(false)
})
it('returns true for query with valid regex property filter', () => {
@@ -185,7 +112,7 @@ describe('validateQuery', () => {
series: [{ kind: NodeKind.EventsNode, event: '$pageview' }],
properties: [{ type: 'event', key: 'url', operator: 'regex', value: '^/api/.*' }],
}
- expect(validateQuery(trendsQuery)).toBe(true)
+ expect(validateQuery(trendsQuery, hasInvalidRegexFilter)).toBe(true)
})
it('returns true for query without regex filters', () => {
@@ -194,7 +121,7 @@ describe('validateQuery', () => {
series: [{ kind: NodeKind.EventsNode, event: '$pageview' }],
properties: [{ type: 'event', key: 'url', operator: 'exact', value: '/home' }],
}
- expect(validateQuery(trendsQuery)).toBe(true)
+ expect(validateQuery(trendsQuery, hasInvalidRegexFilter)).toBe(true)
})
})
diff --git a/frontend/src/scenes/insights/utils/queryUtils.ts b/frontend/src/scenes/insights/utils/queryUtils.ts
index 411b8f126958..1e2a9bafd9b5 100644
--- a/frontend/src/scenes/insights/utils/queryUtils.ts
+++ b/frontend/src/scenes/insights/utils/queryUtils.ts
@@ -1,5 +1,4 @@
import { objectCleanWithEmpty, objectsEqual, removeUndefinedAndNull } from 'lib/utils/objects'
-import { isValidRE2 } from 'lib/utils/regexp'
import { Variable } from '~/queries/nodes/DataVisualization/types'
import { nodeKindToInsightType } from '~/queries/nodes/InsightQuery/defaults'
@@ -212,31 +211,11 @@ export const isDraftQueryWorthSaving = (query: Node, filterTestAccountsDefault:
return !compareQuery(overlaidDefault, query, { ignoreVisualizationOnlyChanges: true })
}
-export const hasInvalidRegexFilter = (obj: unknown): boolean => {
- if (Array.isArray(obj)) {
- return obj.some(hasInvalidRegexFilter)
- }
-
- if (obj !== null && typeof obj === 'object') {
- const record = obj as Record<string, unknown>
- if (
- (record.operator === 'regex' || record.operator === 'not_regex') &&
- typeof record.value === 'string' &&
- !isValidRE2(record.value)
- ) {
- return true
- }
-
- return Object.values(record).some(hasInvalidRegexFilter)
- }
-
- return false
-}
-
export const isBoxPlotMissingProperty = (series: TrendsQuery['series'] | null | undefined): boolean =>
!series?.length || series.some((s) => !s?.math_property)
-export const validateQuery = (q: DataNode): boolean => {
+// The caller passes the regex check so that the RE2 engine stays out of every bundle that imports this module.
+export const validateQuery = (q: DataNode, hasInvalidRegexFilter: (q: DataNode) => boolean): boolean => {
if (isFunnelsQuery(q)) {
return isFunnelWithEnoughSteps(q.series) && !isFunnelWithIncompleteDataWarehouseStep(q.series)
}