@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
|
@@ -11,13 +11,343 @@ export const isValidRegex = (pattern: string): boolean => {
|
|
|
11
11
|
return true
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
interface Quantifier {
|
|
15
|
+
length: number
|
|
16
|
+
min: number
|
|
17
|
+
max: number
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const quantifierAt = (pattern: string, index: number): Quantifier | null => {
|
|
21
|
+
const character = pattern[index]
|
|
22
|
+
if (character === '*') {
|
|
23
|
+
return { length: 1, min: 0, max: Infinity }
|
|
24
|
+
}
|
|
25
|
+
if (character === '+') {
|
|
26
|
+
return { length: 1, min: 1, max: Infinity }
|
|
27
|
+
}
|
|
28
|
+
if (character === '?') {
|
|
29
|
+
return { length: 1, min: 0, max: 1 }
|
|
30
|
+
}
|
|
31
|
+
if (character !== '{') {
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
34
|
+
const repetition = /^\{(\d+)(,(\d*))?\}/.exec(pattern.slice(index))
|
|
35
|
+
if (!repetition) {
|
|
36
|
+
return null
|
|
37
|
+
}
|
|
38
|
+
const min = Number(repetition[1])
|
|
39
|
+
const max = repetition[2] ? (repetition[3] === '' ? Infinity : Number(repetition[3])) : min
|
|
40
|
+
return { length: repetition[0].length, min, max }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface Atom {
|
|
44
|
+
source: string
|
|
45
|
+
literal?: string
|
|
46
|
+
zeroWidth?: boolean
|
|
47
|
+
min: number
|
|
48
|
+
max: number
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface Group {
|
|
52
|
+
atoms: Atom[]
|
|
53
|
+
variable: boolean
|
|
54
|
+
// Alternation, nested groups and escapes we can't evaluate rule out the delimiter exemption.
|
|
55
|
+
complex: boolean
|
|
56
|
+
// The `i` and `s` flags that inline modifier groups such as `(?i:...)` switch on for this group.
|
|
57
|
+
flags: string
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const newGroup = (flags: string): Group => ({ atoms: [], variable: false, complex: false, flags })
|
|
61
|
+
|
|
62
|
+
const MODIFIER_GROUP_PREFIX = /^\(\?([ims]*)(?:-([ims]*))?:/
|
|
63
|
+
|
|
64
|
+
const applyModifiers = (flags: string, added: string, removed: string): string =>
|
|
65
|
+
['i', 's'].filter((flag) => removed.indexOf(flag) < 0 && (flags + added).indexOf(flag) >= 0).join('')
|
|
66
|
+
|
|
67
|
+
const ESCAPED_CONTROL_CHARACTERS: Record<string, string> = { n: '\n', r: '\r', t: '\t', f: '\f', v: '\v' }
|
|
68
|
+
|
|
69
|
+
const isRequiredLiteral = (atom: Atom): boolean => atom.literal !== undefined && atom.min === atom.max && atom.min > 0
|
|
70
|
+
|
|
71
|
+
// Caps the regex tests one group may cost, so a long pattern can't make the guard itself slow.
|
|
72
|
+
const MAX_DELIMITER_CHECKS = 10000
|
|
73
|
+
|
|
74
|
+
interface GroupFacts {
|
|
75
|
+
delimiterPositions: Record<string, number[]>
|
|
76
|
+
optionalLiterals: Record<string, boolean>
|
|
77
|
+
// Each spelling maps to the one literal that can take it, or null when several literals share it.
|
|
78
|
+
literalsBySpelling: Record<string, string | null>
|
|
79
|
+
nonLiteralSources: string[]
|
|
80
|
+
variableTermsBefore: number[]
|
|
81
|
+
variableTerms: number
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// A superset of the characters the engine treats as equal under `i`, which compares upper-cased
|
|
85
|
+
// characters; two characters that share a spelling may collide and so are kept apart.
|
|
86
|
+
const spellings = (character: string, ignoreCase: boolean): string[] =>
|
|
87
|
+
ignoreCase ? [character, character.toUpperCase()] : [character]
|
|
88
|
+
|
|
89
|
+
const collectGroupFacts = (group: Group, ignoreCase: boolean): GroupFacts => {
|
|
90
|
+
const facts: GroupFacts = {
|
|
91
|
+
delimiterPositions: Object.create(null),
|
|
92
|
+
optionalLiterals: Object.create(null),
|
|
93
|
+
literalsBySpelling: Object.create(null),
|
|
94
|
+
nonLiteralSources: [],
|
|
95
|
+
variableTermsBefore: [],
|
|
96
|
+
variableTerms: 0,
|
|
97
|
+
}
|
|
98
|
+
const seenSources: Record<string, boolean> = Object.create(null)
|
|
99
|
+
group.atoms.forEach((atom, position) => {
|
|
100
|
+
facts.variableTermsBefore.push(facts.variableTerms)
|
|
101
|
+
if (atom.max > atom.min) {
|
|
102
|
+
facts.variableTerms++
|
|
103
|
+
}
|
|
104
|
+
const literal = atom.literal
|
|
105
|
+
if (literal === undefined) {
|
|
106
|
+
if (!atom.zeroWidth && !seenSources[atom.source]) {
|
|
107
|
+
seenSources[atom.source] = true
|
|
108
|
+
facts.nonLiteralSources.push(atom.source)
|
|
109
|
+
}
|
|
110
|
+
return
|
|
111
|
+
}
|
|
112
|
+
if (isRequiredLiteral(atom)) {
|
|
113
|
+
;(facts.delimiterPositions[literal] ||= []).push(position)
|
|
114
|
+
} else {
|
|
115
|
+
facts.optionalLiterals[literal] = true
|
|
116
|
+
}
|
|
117
|
+
spellings(literal, ignoreCase).forEach((spelling) => {
|
|
118
|
+
const owner = facts.literalsBySpelling[spelling]
|
|
119
|
+
facts.literalsBySpelling[spelling] = owner === undefined || owner === literal ? literal : null
|
|
120
|
+
})
|
|
121
|
+
})
|
|
122
|
+
return facts
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const compileAtomTests = (sources: string[], flags: string): RegExp[] =>
|
|
126
|
+
sources.map((source) => {
|
|
127
|
+
try {
|
|
128
|
+
return new RegExp(`^(?:${source})$`, flags)
|
|
129
|
+
} catch {
|
|
130
|
+
// A term we can't compile is assumed to match any delimiter.
|
|
131
|
+
return /[^]/
|
|
132
|
+
}
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
const isSoleMatchOf = (delimiter: string, facts: GroupFacts, ignoreCase: boolean, atomTests: RegExp[]): boolean =>
|
|
136
|
+
!facts.optionalLiterals[delimiter] &&
|
|
137
|
+
spellings(delimiter, ignoreCase).every((spelling) => facts.literalsBySpelling[spelling] === delimiter) &&
|
|
138
|
+
!atomTests.some((atomTest) => atomTest.test(delimiter))
|
|
139
|
+
|
|
140
|
+
// Walks the segments between consecutive occurrences, including the one that wraps into the next iteration.
|
|
141
|
+
const hasAtMostOneVariableTermPerSegment = (occurrences: number[], facts: GroupFacts): boolean => {
|
|
142
|
+
const before = facts.variableTermsBefore
|
|
143
|
+
for (let i = 0; i < occurrences.length; i++) {
|
|
144
|
+
const end = i + 1 < occurrences.length ? before[occurrences[i + 1]] : facts.variableTerms + before[occurrences[0]]
|
|
145
|
+
if (end - before[occurrences[i]] > 1) {
|
|
146
|
+
return false
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return true
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* A repeated group is linear when one required literal splits every iteration and no other term can
|
|
154
|
+
* match it, with at most one variable term between consecutive occurrences, as in `(?:/[^/]+)*`.
|
|
155
|
+
*/
|
|
156
|
+
const isDelimiterSplit = (group: Group): boolean => {
|
|
157
|
+
if (group.complex) {
|
|
158
|
+
return false
|
|
159
|
+
}
|
|
160
|
+
const ignoreCase = group.flags.indexOf('i') >= 0
|
|
161
|
+
const facts = collectGroupFacts(group, ignoreCase)
|
|
162
|
+
const delimiters = Object.keys(facts.delimiterPositions)
|
|
163
|
+
if (delimiters.length === 0 || facts.nonLiteralSources.length * delimiters.length > MAX_DELIMITER_CHECKS) {
|
|
164
|
+
return false
|
|
165
|
+
}
|
|
166
|
+
const atomTests = compileAtomTests(facts.nonLiteralSources, group.flags)
|
|
167
|
+
return delimiters.some(
|
|
168
|
+
(delimiter) =>
|
|
169
|
+
isSoleMatchOf(delimiter, facts, ignoreCase, atomTests) &&
|
|
170
|
+
hasAtMostOneVariableTermPerSegment(facts.delimiterPositions[delimiter], facts)
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Detects repeated groups that can backtrack exponentially or polynomially, which would stall the
|
|
176
|
+
* synchronous capture path. A group repeated more than once that contains a variable-length term, as in
|
|
177
|
+
* `(a+)+` or `(a{1,3}){20}`, is flagged unless a required delimiter that no other term can match splits
|
|
178
|
+
* every iteration, as in `(?:/[^/]+)*` or `([0-9]{1,3}\.){3}`.
|
|
179
|
+
* Overlapping alternation such as `(a|a)*` is not detected.
|
|
180
|
+
*/
|
|
181
|
+
export const hasNestedQuantifier = (pattern: string): boolean => {
|
|
182
|
+
const groups: Group[] = [newGroup('')]
|
|
183
|
+
let closedGroup: Group | undefined
|
|
184
|
+
let lastAtom: Atom | undefined
|
|
185
|
+
let previousWasQuantifier = false
|
|
186
|
+
|
|
187
|
+
for (let index = 0; index < pattern.length; index++) {
|
|
188
|
+
const character = pattern[index]
|
|
189
|
+
const justClosed = closedGroup
|
|
190
|
+
const atomBefore = lastAtom
|
|
191
|
+
const isLazyModifier = previousWasQuantifier && character === '?'
|
|
192
|
+
closedGroup = undefined
|
|
193
|
+
lastAtom = undefined
|
|
194
|
+
previousWasQuantifier = false
|
|
195
|
+
const current = groups[groups.length - 1]
|
|
196
|
+
|
|
197
|
+
if (isLazyModifier) {
|
|
198
|
+
continue
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const quantifier = justClosed || atomBefore ? quantifierAt(pattern, index) : null
|
|
202
|
+
if (quantifier) {
|
|
203
|
+
if (justClosed && quantifier.max > 1 && justClosed.variable && !isDelimiterSplit(justClosed)) {
|
|
204
|
+
return true
|
|
205
|
+
}
|
|
206
|
+
if (atomBefore) {
|
|
207
|
+
atomBefore.min *= quantifier.min
|
|
208
|
+
atomBefore.max *= quantifier.max
|
|
209
|
+
}
|
|
210
|
+
current.variable ||= quantifier.max > quantifier.min
|
|
211
|
+
index += quantifier.length - 1
|
|
212
|
+
previousWasQuantifier = true
|
|
213
|
+
continue
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
let atom: Atom | undefined
|
|
217
|
+
if (character === '\\') {
|
|
218
|
+
const escaped = pattern[index + 1] ?? ''
|
|
219
|
+
index++
|
|
220
|
+
if (/[uxckpP0-9]/.test(escaped)) {
|
|
221
|
+
current.complex = true
|
|
222
|
+
atom = { source: '[^]', min: 1, max: 1 }
|
|
223
|
+
} else if (escaped === 'b' || escaped === 'B') {
|
|
224
|
+
atom = { source: '', zeroWidth: true, min: 1, max: 1 }
|
|
225
|
+
} else if (/[dDwWsS]/.test(escaped)) {
|
|
226
|
+
atom = { source: `\\${escaped}`, min: 1, max: 1 }
|
|
227
|
+
} else {
|
|
228
|
+
atom = { source: `\\${escaped}`, literal: ESCAPED_CONTROL_CHARACTERS[escaped] ?? escaped, min: 1, max: 1 }
|
|
229
|
+
}
|
|
230
|
+
} else if (character === '[') {
|
|
231
|
+
const start = index
|
|
232
|
+
for (index++; index < pattern.length && pattern[index] !== ']'; index++) {
|
|
233
|
+
if (pattern[index] === '\\') {
|
|
234
|
+
index++
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
atom = { source: pattern.slice(start, index + 1), min: 1, max: 1 }
|
|
238
|
+
} else if (character === '(') {
|
|
239
|
+
const group = newGroup(current.flags)
|
|
240
|
+
if (pattern[index + 1] === '?') {
|
|
241
|
+
const rest = pattern.slice(index)
|
|
242
|
+
const prefix = /^\(\?(?::|=|!|<=|<!|<[^>]*>)/.exec(rest)
|
|
243
|
+
const modifiers = prefix ? null : MODIFIER_GROUP_PREFIX.exec(rest)
|
|
244
|
+
if (prefix) {
|
|
245
|
+
index += prefix[0].length - 1
|
|
246
|
+
group.complex = /^\(\?<?[=!]/.test(prefix[0])
|
|
247
|
+
} else if (modifiers) {
|
|
248
|
+
index += modifiers[0].length - 1
|
|
249
|
+
group.complex = true
|
|
250
|
+
group.flags = applyModifiers(current.flags, modifiers[1], modifiers[2] ?? '')
|
|
251
|
+
} else {
|
|
252
|
+
index++
|
|
253
|
+
group.complex = true
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
groups.push(group)
|
|
257
|
+
} else if (character === ')') {
|
|
258
|
+
if (groups.length > 1) {
|
|
259
|
+
const group = groups.pop() as Group
|
|
260
|
+
const parent = groups[groups.length - 1]
|
|
261
|
+
parent.variable ||= group.variable
|
|
262
|
+
parent.complex = true
|
|
263
|
+
closedGroup = group
|
|
264
|
+
}
|
|
265
|
+
} else if (character === '|') {
|
|
266
|
+
current.complex = true
|
|
267
|
+
} else if (character === '^' || character === '$') {
|
|
268
|
+
atom = { source: '', zeroWidth: true, min: 1, max: 1 }
|
|
269
|
+
} else if (character === '.') {
|
|
270
|
+
atom = { source: '.', min: 1, max: 1 }
|
|
271
|
+
} else {
|
|
272
|
+
atom = { source: character, literal: character, min: 1, max: 1 }
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (atom) {
|
|
276
|
+
current.atoms.push(atom)
|
|
277
|
+
lastAtom = atom
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return false
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const MAX_COMPILED_PATTERNS = 100
|
|
284
|
+
const INVALID = 'invalid'
|
|
285
|
+
const UNSAFE = 'unsafe'
|
|
286
|
+
type CompiledPattern = RegExp | typeof INVALID | typeof UNSAFE
|
|
287
|
+
const compiledPatterns = new Map<string, CompiledPattern>()
|
|
288
|
+
const warnedPatterns = new Set<string>()
|
|
289
|
+
|
|
290
|
+
// Reads console.warn at call time and skips console plugins (session replay, logs capture) that
|
|
291
|
+
// mark their wrappers with `__rrweb_original__`, so the warning never becomes a captured customer log.
|
|
292
|
+
const warnRejectedPattern = (pattern: string): void => {
|
|
293
|
+
try {
|
|
294
|
+
let warn: unknown = typeof console !== 'undefined' ? console.warn : undefined
|
|
295
|
+
while (typeof warn === 'function' && '__rrweb_original__' in warn) {
|
|
296
|
+
warn = (warn as { __rrweb_original__?: unknown }).__rrweb_original__
|
|
297
|
+
}
|
|
298
|
+
if (typeof warn === 'function') {
|
|
299
|
+
warn.call(
|
|
300
|
+
console,
|
|
301
|
+
'[PostHog]',
|
|
302
|
+
`Ignoring regex "${pattern}": a repeated group containing repetition can stall matching, so filters using it never match.`
|
|
303
|
+
)
|
|
304
|
+
}
|
|
305
|
+
} catch {
|
|
306
|
+
// Logging must never interrupt matching.
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
const compilePattern = (pattern: string): CompiledPattern => {
|
|
311
|
+
if (typeof pattern !== 'string') {
|
|
312
|
+
return INVALID
|
|
313
|
+
}
|
|
314
|
+
const cached = compiledPatterns.get(pattern)
|
|
315
|
+
if (cached !== undefined) {
|
|
316
|
+
return cached
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
let compiled: CompiledPattern
|
|
320
|
+
try {
|
|
321
|
+
compiled = new RegExp(pattern)
|
|
322
|
+
} catch {
|
|
323
|
+
compiled = INVALID
|
|
324
|
+
}
|
|
325
|
+
if (compiled !== INVALID && hasNestedQuantifier(pattern)) {
|
|
326
|
+
compiled = UNSAFE
|
|
327
|
+
if (!warnedPatterns.has(pattern) && warnedPatterns.size < MAX_COMPILED_PATTERNS) {
|
|
328
|
+
warnedPatterns.add(pattern)
|
|
329
|
+
warnRejectedPattern(pattern)
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Map.prototype.keys is unavailable in IE11, so drop the whole cache instead of the oldest entry.
|
|
334
|
+
if (compiledPatterns.size >= MAX_COMPILED_PATTERNS) {
|
|
335
|
+
compiledPatterns.clear()
|
|
336
|
+
}
|
|
337
|
+
compiledPatterns.set(pattern, compiled)
|
|
338
|
+
return compiled
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
const isUnsafePattern = (pattern: string): boolean => compilePattern(pattern) === UNSAFE
|
|
342
|
+
|
|
14
343
|
export const isMatchingRegex = (value: string, pattern: string): boolean => {
|
|
15
|
-
|
|
344
|
+
const regex = compilePattern(pattern)
|
|
345
|
+
if (regex === INVALID || regex === UNSAFE) {
|
|
16
346
|
return false
|
|
17
347
|
}
|
|
18
348
|
|
|
19
349
|
try {
|
|
20
|
-
return
|
|
350
|
+
return regex.test(value)
|
|
21
351
|
} catch {
|
|
22
352
|
return false
|
|
23
353
|
}
|
|
@@ -29,7 +359,9 @@ export const propertyComparisons: Record<PropertyOperator, (targets: string[], v
|
|
|
29
359
|
exact: (targets, values) => values.some((value) => targets.some((target) => value === target)),
|
|
30
360
|
is_not: (targets, values) => values.every((value) => targets.every((target) => value !== target)),
|
|
31
361
|
regex: (targets, values) => values.some((value) => targets.some((target) => isMatchingRegex(value, target))),
|
|
32
|
-
not_regex: (targets, values) =>
|
|
362
|
+
not_regex: (targets, values) =>
|
|
363
|
+
!targets.some(isUnsafePattern) &&
|
|
364
|
+
values.every((value) => targets.every((target) => !isMatchingRegex(value, target))),
|
|
33
365
|
icontains: (targets, values) =>
|
|
34
366
|
values.map(toLowerCase).some((value) => targets.map(toLowerCase).some((target) => value.includes(target))),
|
|
35
367
|
not_icontains: (targets, values) =>
|