@posthog/core 1.69.5 → 1.69.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/posthog-core-stateless.d.ts.map +1 -1
- package/dist/posthog-core-stateless.js +14 -1
- package/dist/posthog-core-stateless.mjs +14 -1
- package/dist/surveys/property-matching.d.ts +8 -0
- package/dist/surveys/property-matching.d.ts.map +1 -1
- package/dist/surveys/property-matching.js +263 -3
- package/dist/surveys/property-matching.mjs +261 -4
- package/package.json +1 -1
- package/src/posthog-core-stateless.ts +26 -1
- package/src/surveys/property-matching.spec.ts +359 -1
- package/src/surveys/property-matching.ts +335 -3
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
import type { MockInstance } from 'vitest'
|
|
1
2
|
import type { PropertyFilters, PropertyOperator } from '../types'
|
|
2
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
hasNestedQuantifier,
|
|
5
|
+
isMatchingRegex,
|
|
6
|
+
isValidRegex,
|
|
7
|
+
matchPropertyFilters,
|
|
8
|
+
propertyComparisons,
|
|
9
|
+
} from './property-matching'
|
|
3
10
|
|
|
4
11
|
const operators: PropertyOperator[] = [
|
|
5
12
|
'exact',
|
|
@@ -28,6 +35,357 @@ describe('shared property matching contract', () => {
|
|
|
28
35
|
expect(isValidRegex('[invalid')).toBe(false)
|
|
29
36
|
expect(isMatchingRegex('anything', '[invalid')).toBe(false)
|
|
30
37
|
})
|
|
38
|
+
|
|
39
|
+
it.each([
|
|
40
|
+
['^/docs/.*', '/docs/a', true],
|
|
41
|
+
['^(foo|bar)+$', 'foobarfoo', true],
|
|
42
|
+
['^(?:ab)+$', 'ababab', true],
|
|
43
|
+
['^a+b*$', 'aab', true],
|
|
44
|
+
['\\(a+\\)+', '(aa)))', true],
|
|
45
|
+
['[(a+)]+', 'a+)', true],
|
|
46
|
+
['^(a+)?b$', 'aab', true],
|
|
47
|
+
['^(a+){1}$', 'aaa', true],
|
|
48
|
+
['example\\.com', 'admin@example.org', false],
|
|
49
|
+
['^foo$', 'foo\n', false],
|
|
50
|
+
])('keeps matching for safe pattern %s against %j', (pattern, value, expected) => {
|
|
51
|
+
expect(isMatchingRegex(value, pattern)).toBe(expected)
|
|
52
|
+
expect(isMatchingRegex(value, pattern)).toBe(expected)
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
describe('nested quantifier guard', () => {
|
|
57
|
+
let warnSpy: MockInstance<Parameters<typeof console.warn>, void>
|
|
58
|
+
|
|
59
|
+
// The shared setup fakes timers, which freezes Date.now and would make the timing checks vacuous.
|
|
60
|
+
beforeAll(() => {
|
|
61
|
+
vi.useRealTimers()
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
afterAll(() => {
|
|
65
|
+
vi.useFakeTimers()
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
beforeEach(() => {
|
|
69
|
+
warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
afterEach(() => {
|
|
73
|
+
warnSpy.mockRestore()
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it.each([
|
|
77
|
+
'^(a+)+$',
|
|
78
|
+
'(a*)*',
|
|
79
|
+
'(?:x+y?)+',
|
|
80
|
+
'((ab)+)+',
|
|
81
|
+
'(a+){2,}',
|
|
82
|
+
'(a+){2}',
|
|
83
|
+
'^(a?a?)+$',
|
|
84
|
+
'^(a{0,1}a{0,1})+$',
|
|
85
|
+
'(a{2,3})+',
|
|
86
|
+
'(a+?)+',
|
|
87
|
+
'(a{2,3}?)+',
|
|
88
|
+
'^(a+){3}$',
|
|
89
|
+
'^(a+){0,3}$',
|
|
90
|
+
'(?:.+/)+',
|
|
91
|
+
'([^/]+/?)+',
|
|
92
|
+
'(a|b+)+',
|
|
93
|
+
'(x+x+)+',
|
|
94
|
+
'(?:[^:]+)+',
|
|
95
|
+
'(?:a[^/]*/[^/]*)+',
|
|
96
|
+
'(?:/[^/]+.)+',
|
|
97
|
+
'(\\u0041[A-Z]+)+',
|
|
98
|
+
'(?=a+)+',
|
|
99
|
+
'(a{1,2})+',
|
|
100
|
+
'^(a?){26}a{26}$',
|
|
101
|
+
'^(a{1,10}){10}$',
|
|
102
|
+
'^(?:a{1,3}){20}$',
|
|
103
|
+
'^(\\w{1,8}){10}$',
|
|
104
|
+
'^(a{1,10}){1,10}$',
|
|
105
|
+
'^(\\d{1,3}\\d{1,3}){1,5}$',
|
|
106
|
+
'(?:a?){24}b',
|
|
107
|
+
'(?:a{1,2}){30}b',
|
|
108
|
+
'(?:a{0,2}){20}b',
|
|
109
|
+
'(?:[a-z]{1,3}){15}!',
|
|
110
|
+
])('flags %s', (pattern) => {
|
|
111
|
+
expect(hasNestedQuantifier(pattern)).toBe(true)
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it.each([
|
|
115
|
+
'^(a+)?$',
|
|
116
|
+
'(a+){1}',
|
|
117
|
+
'(a)+',
|
|
118
|
+
'(a+)b+',
|
|
119
|
+
'\\(a+\\)+',
|
|
120
|
+
'[(a+)]+',
|
|
121
|
+
'(a|a)*',
|
|
122
|
+
'(?<n>a)+',
|
|
123
|
+
'(a+)?',
|
|
124
|
+
'a+?',
|
|
125
|
+
'(ab){2}',
|
|
126
|
+
'^(\\d{3})+$',
|
|
127
|
+
'(a{2})+',
|
|
128
|
+
'(a{2,2})+',
|
|
129
|
+
'(a)+?',
|
|
130
|
+
'(\\d+\\.)+',
|
|
131
|
+
'(a[)]+)+',
|
|
132
|
+
'^https://example\\.com/admin(?:/[^/]+)*$',
|
|
133
|
+
'^https?://([a-z0-9-]+\\.)+example\\.com$',
|
|
134
|
+
'^([0-9]{1,3}\\.){3}[0-9]{1,3}$',
|
|
135
|
+
'^(\\d{1,2}/){2}\\d{4}$',
|
|
136
|
+
'^[A-Za-z]{2,3}(-[A-Za-z0-9]{2,8}){0,3}$',
|
|
137
|
+
'(?<seg>/[^/]+)*',
|
|
138
|
+
])('does not flag %s', (pattern) => {
|
|
139
|
+
expect(hasNestedQuantifier(pattern)).toBe(false)
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
it.each([
|
|
143
|
+
['^(?:/[^/]+/?)+$', true],
|
|
144
|
+
['^(?:/[^/]+/{0})+$', true],
|
|
145
|
+
['^(?:/[^/]+[^/]+/)+$', true],
|
|
146
|
+
['^(?:[^/]+//[^/]+)+$', true],
|
|
147
|
+
['^(?:/[^/]+/[^/]+)+$', false],
|
|
148
|
+
['^(?:/[^/]*/[^/]*)+$', false],
|
|
149
|
+
])('counts variable terms between every pair of delimiters in %s', (pattern, expected) => {
|
|
150
|
+
expect(hasNestedQuantifier(pattern)).toBe(expected)
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
describe('guard cost on long patterns', () => {
|
|
154
|
+
const distinctLiterals = (count: number, from = 0x4e00): string[] =>
|
|
155
|
+
Array.from({ length: count }, (_, i) => String.fromCharCode(from + i))
|
|
156
|
+
|
|
157
|
+
const countRegExpConstructions = (run: () => void): number => {
|
|
158
|
+
const OriginalRegExp = globalThis.RegExp
|
|
159
|
+
let constructions = 0
|
|
160
|
+
globalThis.RegExp = new Proxy(OriginalRegExp, {
|
|
161
|
+
construct(target, args, newTarget) {
|
|
162
|
+
constructions++
|
|
163
|
+
return Reflect.construct(target, args, newTarget)
|
|
164
|
+
},
|
|
165
|
+
})
|
|
166
|
+
try {
|
|
167
|
+
run()
|
|
168
|
+
} finally {
|
|
169
|
+
globalThis.RegExp = OriginalRegExp
|
|
170
|
+
}
|
|
171
|
+
return constructions
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
it.each([
|
|
175
|
+
['a long repeated literal run', '^(' + 'ab'.repeat(1000) + '.*)+$', true],
|
|
176
|
+
['a long single-literal run', '^(' + 'a'.repeat(16000) + '.*)+$', true],
|
|
177
|
+
['thousands of distinct literals', '^(' + distinctLiterals(3000).join('') + '.*)+$', true],
|
|
178
|
+
[
|
|
179
|
+
'thousands of paired literals',
|
|
180
|
+
'^(' +
|
|
181
|
+
distinctLiterals(2000)
|
|
182
|
+
.map((literal) => literal + literal)
|
|
183
|
+
.join('') +
|
|
184
|
+
'[0-9]*[0-9]*)+$',
|
|
185
|
+
true,
|
|
186
|
+
],
|
|
187
|
+
['a long delimiter-split group', '^(' + 'ab'.repeat(1000) + '[^/]+/)+$', false],
|
|
188
|
+
])('compiles each distinct term once for %s', (_, pattern, expected) => {
|
|
189
|
+
let flagged: boolean | undefined
|
|
190
|
+
const constructions = countRegExpConstructions(() => {
|
|
191
|
+
flagged = hasNestedQuantifier(pattern)
|
|
192
|
+
})
|
|
193
|
+
expect(flagged).toBe(expected)
|
|
194
|
+
expect(constructions).toBeLessThanOrEqual(1)
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
it('still matches a long delimiter-split group', () => {
|
|
198
|
+
expect(isMatchingRegex('ab'.repeat(1000) + 'z/', '^(' + 'ab'.repeat(1000) + '[^/]+/)+$')).toBe(true)
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
it('rejects a delimiter-split group once its checks exceed the budget', () => {
|
|
202
|
+
const pattern = (ranges: number): string =>
|
|
203
|
+
'^(?:' +
|
|
204
|
+
distinctLiterals(ranges)
|
|
205
|
+
.map((end) => `[\\u4e00-${end}]`)
|
|
206
|
+
.join('') +
|
|
207
|
+
distinctLiterals(100, 0x100).join('') +
|
|
208
|
+
'\\d+)+$'
|
|
209
|
+
|
|
210
|
+
expect(hasNestedQuantifier(pattern(99))).toBe(false)
|
|
211
|
+
expect(hasNestedQuantifier(pattern(100))).toBe(true)
|
|
212
|
+
})
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
it('matches delimiter-split patterns without Array.prototype.findIndex', async () => {
|
|
216
|
+
// A fresh module has an empty compile cache, so the guard really runs without findIndex.
|
|
217
|
+
const isMatching = await freshIsMatchingRegex()
|
|
218
|
+
const originalFindIndex = Array.prototype.findIndex
|
|
219
|
+
try {
|
|
220
|
+
;(Array.prototype as { findIndex?: unknown }).findIndex = undefined
|
|
221
|
+
expect(isMatching('https://example.com/admin/users', '^https://example\\.com/admin(?:/[^/]+)*$')).toBe(true)
|
|
222
|
+
expect(isMatching('192.168.1.10', '^([0-9]{1,3}\\.){3}[0-9]{1,3}$')).toBe(true)
|
|
223
|
+
} finally {
|
|
224
|
+
Array.prototype.findIndex = originalFindIndex
|
|
225
|
+
}
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
it.each([
|
|
229
|
+
['^(?i:(?:a[A-Z]+)+)$', true],
|
|
230
|
+
['^(?s:(?:\\n.+)+)x$', true],
|
|
231
|
+
['^(?i-s:(?:a[A-Z]+)+)$', true],
|
|
232
|
+
['^(?s-i:(?:\\n.+)+)x$', true],
|
|
233
|
+
['^(?i:(?:(?:a[A-Z]+)+)+)$', true],
|
|
234
|
+
['(?i:a[A-Z]+)+', true],
|
|
235
|
+
['^(?-i:(?:a[A-Z]+)+)$', false],
|
|
236
|
+
['^(?i:(?-i:(?:a[A-Z]+)+))$', false],
|
|
237
|
+
['^(?m:(?:a[A-Z]+)+)$', false],
|
|
238
|
+
['^(?i:(?:/[^/]+)*)$', false],
|
|
239
|
+
['^(?:a[A-Z]+)+(?i:b)$', false],
|
|
240
|
+
])('checks delimiters of %s under its inline modifiers', (pattern, expected) => {
|
|
241
|
+
expect(hasNestedQuantifier(pattern)).toBe(expected)
|
|
242
|
+
})
|
|
243
|
+
|
|
244
|
+
it('treats case variants of a delimiter as other terms that can match it under the i modifier', () => {
|
|
245
|
+
expect(hasNestedQuantifier('^(?:a[^a]+A)+$')).toBe(false)
|
|
246
|
+
expect(hasNestedQuantifier('^(?i:(?:a[^a]+A)+)$')).toBe(true)
|
|
247
|
+
})
|
|
248
|
+
|
|
249
|
+
it('returns no match quickly for catastrophic backtracking input', () => {
|
|
250
|
+
const start = Date.now()
|
|
251
|
+
expect(isMatchingRegex('a'.repeat(40) + '!', '^(a+)+$')).toBe(false)
|
|
252
|
+
expect(propertyComparisons.regex(['^(b+)+$'], ['b'.repeat(40) + '!'])).toBe(false)
|
|
253
|
+
expect(propertyComparisons.not_regex(['^(c+)+$'], ['c'.repeat(40) + '!'])).toBe(false)
|
|
254
|
+
expect(Date.now() - start).toBeLessThan(500)
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
it('returns no match quickly for catastrophic backtracking through optional terms', () => {
|
|
258
|
+
const start = Date.now()
|
|
259
|
+
expect(isMatchingRegex('a'.repeat(30) + '!', '^(a?a?)+$')).toBe(false)
|
|
260
|
+
expect(Date.now() - start).toBeLessThan(500)
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
it.each([
|
|
264
|
+
['^([0-9]{1,3}\\.){3}[0-9]{1,3}$', '192.168.1.10', true],
|
|
265
|
+
['^([0-9]{1,3}\\.){3}[0-9]{1,3}$', '1'.repeat(5000) + '.', false],
|
|
266
|
+
['^https://example\\.com/admin(?:/[^/]+)*$', 'https://example.com/admin/users/42', true],
|
|
267
|
+
['^https://example\\.com/admin(?:/[^/]+)*$', 'https://example.com/admin' + '/aaaa'.repeat(5000) + '/', false],
|
|
268
|
+
['^https?://([a-z0-9-]+\\.)+example\\.com$', 'https://app.eu.example.com', true],
|
|
269
|
+
['^https?://([a-z0-9-]+\\.)+example\\.com$', 'https://' + 'a.'.repeat(5000) + 'example.co', false],
|
|
270
|
+
['^[A-Za-z]{2,3}(-[A-Za-z0-9]{2,8}){0,3}$', 'en-US', true],
|
|
271
|
+
['^(\\d{1,2}/){2}\\d{4}$', '12/31/2026', true],
|
|
272
|
+
])('matches bounded or delimiter-split pattern %s quickly', (pattern, value, expected) => {
|
|
273
|
+
const start = Date.now()
|
|
274
|
+
expect(isMatchingRegex(value, pattern)).toBe(expected)
|
|
275
|
+
expect(Date.now() - start).toBeLessThan(500)
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
it('returns no match quickly for a bounded repeat of a variable-length group', () => {
|
|
279
|
+
const start = Date.now()
|
|
280
|
+
expect(isMatchingRegex('a'.repeat(30) + '!', '^(\\w{1,8}){10}$')).toBe(false)
|
|
281
|
+
expect(Date.now() - start).toBeLessThan(500)
|
|
282
|
+
})
|
|
283
|
+
|
|
284
|
+
it('lets not_regex pass for a delimiter-split pattern that does not match', () => {
|
|
285
|
+
expect(
|
|
286
|
+
propertyComparisons.not_regex(['^https://example\\.com/admin(?:/[^/]+)*$'], ['https://example.com/pricing'])
|
|
287
|
+
).toBe(true)
|
|
288
|
+
})
|
|
289
|
+
|
|
290
|
+
it('never lets an unsafe not_regex target pass, even with no values', () => {
|
|
291
|
+
expect(propertyComparisons.not_regex(['^(a+)+$'], [])).toBe(false)
|
|
292
|
+
expect(propertyComparisons.not_regex([], [])).toBe(true)
|
|
293
|
+
expect(propertyComparisons.not_regex([], ['value'])).toBe(true)
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
it.each([null, undefined, 42, /^(a+)+$/])('treats a non-string pattern %s as invalid', (pattern) => {
|
|
297
|
+
expect(() => isMatchingRegex('aa', pattern as unknown as string)).not.toThrow()
|
|
298
|
+
expect(isMatchingRegex('aa', pattern as unknown as string)).toBe(false)
|
|
299
|
+
expect(isMatchingRegex('Desktop', pattern as unknown as string)).toBe(false)
|
|
300
|
+
})
|
|
301
|
+
|
|
302
|
+
it('never lets a rejected pattern pass a regex or not_regex filter', () => {
|
|
303
|
+
expect(isMatchingRegex('dd', '^(d+)+$')).toBe(false)
|
|
304
|
+
expect(propertyComparisons.regex(['^(d+)+$'], ['dd'])).toBe(false)
|
|
305
|
+
expect(propertyComparisons.not_regex(['^(d+)+$'], ['dd'])).toBe(false)
|
|
306
|
+
expect(propertyComparisons.not_regex(['^(d+)+$'], ['zz'])).toBe(false)
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
it('does not let a rejected pattern pass not_regex on a protected URL', () => {
|
|
310
|
+
const pattern = '^https://example\\.com/admin(?:/.+)*$'
|
|
311
|
+
expect(new RegExp(pattern).test('https://example.com/admin/customer')).toBe(true)
|
|
312
|
+
expect(propertyComparisons.not_regex([pattern], ['https://example.com/admin/customer'])).toBe(false)
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
it('keeps invalid patterns as non-matches that pass not_regex', () => {
|
|
316
|
+
expect(propertyComparisons.regex(['('], ['value'])).toBe(false)
|
|
317
|
+
expect(propertyComparisons.not_regex(['('], ['value'])).toBe(true)
|
|
318
|
+
})
|
|
319
|
+
|
|
320
|
+
// Warning suppression is module state, so each test needs a fresh module.
|
|
321
|
+
const freshIsMatchingRegex = async (): Promise<typeof isMatchingRegex> => {
|
|
322
|
+
vi.resetModules()
|
|
323
|
+
return (await import('./property-matching')).isMatchingRegex
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
it('warns once per rejected pattern', async () => {
|
|
327
|
+
const isMatching = await freshIsMatchingRegex()
|
|
328
|
+
|
|
329
|
+
isMatching('x', '^(warn\\w+)+$')
|
|
330
|
+
isMatching('y', '^(warn\\w+)+$')
|
|
331
|
+
isMatching('z', '^(other\\w+)+$')
|
|
332
|
+
|
|
333
|
+
expect(warnSpy).toHaveBeenCalledTimes(2)
|
|
334
|
+
expect(warnSpy).toHaveBeenCalledWith('[PostHog]', expect.stringContaining('^(warn\\w+)+$'))
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
it('keeps warning suppression independent of cache eviction and bounded to 100 warnings', async () => {
|
|
338
|
+
vi.resetModules()
|
|
339
|
+
const { propertyComparisons: fresh } = await import('./property-matching')
|
|
340
|
+
const patterns = Array.from({ length: 101 }, (_, i) => `^https://customer\\.example/(?:segment${i}\\w+)+$`)
|
|
341
|
+
const cumulativeWarnings: number[] = []
|
|
342
|
+
|
|
343
|
+
for (let sweep = 0; sweep < 3; sweep++) {
|
|
344
|
+
patterns.forEach((pattern) => fresh.regex([pattern], ['https://customer.example/x']))
|
|
345
|
+
cumulativeWarnings.push(warnSpy.mock.calls.length)
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
expect(cumulativeWarnings).toEqual([100, 100, 100])
|
|
349
|
+
})
|
|
350
|
+
|
|
351
|
+
it('does not throw when console methods are missing', async () => {
|
|
352
|
+
const originalDebug = console.debug
|
|
353
|
+
const originalWarn = console.warn
|
|
354
|
+
try {
|
|
355
|
+
;(console as { debug?: unknown }).debug = undefined
|
|
356
|
+
;(console as { warn?: unknown }).warn = undefined
|
|
357
|
+
const isMatching = await freshIsMatchingRegex()
|
|
358
|
+
expect(() => isMatching('x', '^(nodebug\\w+)+$')).not.toThrow()
|
|
359
|
+
expect(isMatching('x', '^(nodebug\\w+)+$')).toBe(false)
|
|
360
|
+
} finally {
|
|
361
|
+
console.debug = originalDebug
|
|
362
|
+
console.warn = originalWarn
|
|
363
|
+
}
|
|
364
|
+
})
|
|
365
|
+
|
|
366
|
+
it('warns through the original console method behind __rrweb_original__ wrappers', async () => {
|
|
367
|
+
const wrapper = Object.assign(vi.fn(), { __rrweb_original__: warnSpy })
|
|
368
|
+
const originalWarn = console.warn
|
|
369
|
+
console.warn = wrapper as unknown as typeof console.warn
|
|
370
|
+
try {
|
|
371
|
+
const isMatching = await freshIsMatchingRegex()
|
|
372
|
+
isMatching('x', '^(wrapped\\w+)+$')
|
|
373
|
+
} finally {
|
|
374
|
+
console.warn = originalWarn
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
expect(wrapper).not.toHaveBeenCalled()
|
|
378
|
+
expect(warnSpy).toHaveBeenCalledWith('[PostHog]', expect.stringContaining('^(wrapped\\w+)+$'))
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
it('does not warn for invalid or safe patterns', async () => {
|
|
382
|
+
const isMatching = await freshIsMatchingRegex()
|
|
383
|
+
|
|
384
|
+
isMatching('x', '[invalid')
|
|
385
|
+
isMatching('x', '^x+$')
|
|
386
|
+
|
|
387
|
+
expect(warnSpy).not.toHaveBeenCalled()
|
|
388
|
+
})
|
|
31
389
|
})
|
|
32
390
|
|
|
33
391
|
describe('property comparisons', () => {
|