@posthog/core 1.60.0 → 1.60.1

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.
@@ -1 +1 @@
1
- {"version":3,"file":"featureFlagLocalEvaluation.d.ts","sourceRoot":"","sources":["../src/featureFlagLocalEvaluation.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AAEzD,MAAM,MAAM,wBAAwB,GAAG,QAAQ,CAAA;AAE/C,MAAM,MAAM,mBAAmB,GAAG;IAChC,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,wBAAwB,CAAA;IAC/B,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,MAAM,MAAM,8BAA8B,GAAG,QAAQ,GAAG,mBAAmB,CAAA;AAE3E,MAAM,MAAM,+BAA+B,GAAG;IAC5C,YAAY,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,IAAI,CAAA;IACxC,mBAAmB,CAAC,EAAE,8BAA8B,CAAA;IACpD,mGAAmG;IACnG,uBAAuB,CAAC,EAAE,MAAM,CAAA;CACjC,CAAA;AAED,MAAM,MAAM,kBAAkB,GAAG;IAC/B,GAAG,EAAE,MAAM,CAAA;IACX,kBAAkB,EAAE,MAAM,CAAA;CAC3B,CAAA;AAED,MAAM,MAAM,6BAA6B,GAAG;IAC1C,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;IAChB,GAAG,EAAE,MAAM,CAAA;CACZ,CAAA;AAqBD;;;;GAIG;AACH,MAAM,MAAM,oBAAoB,GAC5B,uBAAuB,GACvB,wBAAwB,GACxB,iBAAiB,GACjB,uBAAuB,CAAA;AAE3B,qBAAa,sBAAuB,SAAQ,KAAK;IAC/C,QAAQ,CAAC,MAAM,EAAE,oBAAoB,CAAA;IAErC,YAAY,OAAO,EAAE,MAAM,EAAE,MAAM,GAAE,oBAA+C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AAED,wBAAsB,qBAAqB,CACzC,GAAG,EAAE,MAAM,EACX,cAAc,EAAE,MAAM,EACtB,QAAQ,EAAE,SAAS,kBAAkB,EAAE,GACtC,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAK7B;AAED,wBAAgB,yBAAyB,CACvC,QAAQ,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,GAAG,SAAS,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS,EAC3E,SAAS,EAAE,gBAAgB,GAAG,IAAI,GAAG,SAAS,GAC7C,QAAQ,GAAG,IAAI,CAMjB"}
@@ -194,12 +194,96 @@ function exactMatchString(value) {
194
194
  }
195
195
  return serializeJsonValue(value);
196
196
  }
197
- function isValidRegex(regex) {
197
+ const LITERAL_ESCAPED_PUNCTUATION = /[\x20-\x2f:;=?@\x5b-\x60\x7b-\x7e]/;
198
+ function getPortableRegexClasses(pattern) {
199
+ const classes = [];
200
+ let classStart = -1;
201
+ let previousToken = '';
202
+ for(let index = 0; index < pattern.length; index++){
203
+ const character = pattern[index];
204
+ if (character.charCodeAt(0) > 0x7f) return null;
205
+ if ('\\' === character) {
206
+ const escaped = pattern[++index];
207
+ if (void 0 === escaped) break;
208
+ if ('x' === escaped) {
209
+ if (!/^[\da-f]{2}$/i.test(pattern.slice(index + 1, index + 3))) return null;
210
+ index += 2;
211
+ } else if (!'dDsSwWbBfnrtv'.includes(escaped) && !LITERAL_ESCAPED_PUNCTUATION.test(escaped)) return null;
212
+ if (-1 === classStart && 'bB'.includes(escaped)) return null;
213
+ previousToken = 'literal';
214
+ continue;
215
+ }
216
+ if ('[' === character) {
217
+ if (-1 !== classStart) return null;
218
+ classStart = index;
219
+ } else if (']' === character && -1 !== classStart) {
220
+ const contentStart = classStart + ('^' === pattern[classStart + 1] ? 2 : 1);
221
+ if (index === contentStart) return null;
222
+ classes.push(pattern.slice(classStart, index + 1));
223
+ classStart = -1;
224
+ previousToken = 'literal';
225
+ continue;
226
+ } else if (-1 !== classStart) {
227
+ if ([
228
+ '&&',
229
+ '--',
230
+ '~~'
231
+ ].includes(pattern.slice(index, index + 2))) return null;
232
+ continue;
233
+ } else if ('(' === character && '?' === pattern[index + 1]) {
234
+ if (':' !== pattern[index + 2]) return null;
235
+ index += 2;
236
+ } else if ('{' === character) {
237
+ const repetition = /^\{(\d+)(?:,(\d*))?\}/.exec(pattern.slice(index));
238
+ if (!repetition || [
239
+ '',
240
+ '|',
241
+ '('
242
+ ].includes(previousToken) || repetition.slice(1).some((count)=>Number(count) > 0xffffffff)) return null;
243
+ } else if ('*+?}'.includes(character) && ('+' === pattern[index + 1] || '{' === pattern[index + 1])) return null;
244
+ if ('*+?{'.includes(character) && [
245
+ '^',
246
+ '$',
247
+ 'empty group'
248
+ ].includes(previousToken)) return null;
249
+ previousToken = ')' === character && [
250
+ '(',
251
+ 'empty group'
252
+ ].includes(previousToken) ? 'empty group' : character;
253
+ }
254
+ return classes;
255
+ }
256
+ function regexRequiresServerEvaluation(message) {
257
+ const error = new InconclusiveMatchError(message);
258
+ Object.defineProperty(error, Symbol.for('posthog.requiresServerRegexEvaluation'), {
259
+ value: true
260
+ });
261
+ return error;
262
+ }
263
+ function matchFeatureFlagRegex(pattern, value) {
264
+ if ('' === pattern) return true;
265
+ const characterClasses = getPortableRegexClasses(pattern);
266
+ if (null === characterClasses) throw regexRequiresServerEvaluation('Regex syntax requires server evaluation');
267
+ let regex;
198
268
  try {
199
- new RegExp(regex);
200
- return true;
269
+ regex = new RegExp(pattern);
270
+ } catch (error) {
271
+ if (error instanceof SyntaxError) return null;
272
+ throw regexRequiresServerEvaluation('Regex compilation requires server evaluation');
273
+ }
274
+ for (const characterClass of characterClasses)try {
275
+ new RegExp(characterClass, 'u');
201
276
  } catch {
202
- return false;
277
+ throw regexRequiresServerEvaluation('Regex character class syntax requires server evaluation');
278
+ }
279
+ for(let index = 0; index < value.length; index++){
280
+ const character = value[index];
281
+ if (character.charCodeAt(0) > 0x7f || '\r' === character) throw regexRequiresServerEvaluation('Regex matching for Unicode or carriage returns requires server evaluation');
282
+ }
283
+ try {
284
+ return regex.test(value);
285
+ } catch {
286
+ throw regexRequiresServerEvaluation('Regex execution requires server evaluation');
203
287
  }
204
288
  }
205
289
  function asciiLowercase(value) {
@@ -473,9 +557,12 @@ function matchPropertyValue(property, propertyValues, options) {
473
557
  case 'not_ends_with':
474
558
  return !asciiLowercase(overrideValue).endsWith(asciiLowercase(value));
475
559
  case 'regex':
476
- return isValidRegex(String(value)) && null !== String(overrideValue).match(String(value));
477
560
  case 'not_regex':
478
- return isValidRegex(String(value)) && null === String(overrideValue).match(String(value));
561
+ {
562
+ const matched = matchFeatureFlagRegex(String(value), String(overrideValue));
563
+ if (null === matched) return false;
564
+ return 'regex' === operator ? matched : !matched;
565
+ }
479
566
  case 'gt':
480
567
  case 'gte':
481
568
  case 'lt':
@@ -152,12 +152,96 @@ function exactMatchString(value) {
152
152
  }
153
153
  return serializeJsonValue(value);
154
154
  }
155
- function isValidRegex(regex) {
155
+ const LITERAL_ESCAPED_PUNCTUATION = /[\x20-\x2f:;=?@\x5b-\x60\x7b-\x7e]/;
156
+ function getPortableRegexClasses(pattern) {
157
+ const classes = [];
158
+ let classStart = -1;
159
+ let previousToken = '';
160
+ for(let index = 0; index < pattern.length; index++){
161
+ const character = pattern[index];
162
+ if (character.charCodeAt(0) > 0x7f) return null;
163
+ if ('\\' === character) {
164
+ const escaped = pattern[++index];
165
+ if (void 0 === escaped) break;
166
+ if ('x' === escaped) {
167
+ if (!/^[\da-f]{2}$/i.test(pattern.slice(index + 1, index + 3))) return null;
168
+ index += 2;
169
+ } else if (!'dDsSwWbBfnrtv'.includes(escaped) && !LITERAL_ESCAPED_PUNCTUATION.test(escaped)) return null;
170
+ if (-1 === classStart && 'bB'.includes(escaped)) return null;
171
+ previousToken = 'literal';
172
+ continue;
173
+ }
174
+ if ('[' === character) {
175
+ if (-1 !== classStart) return null;
176
+ classStart = index;
177
+ } else if (']' === character && -1 !== classStart) {
178
+ const contentStart = classStart + ('^' === pattern[classStart + 1] ? 2 : 1);
179
+ if (index === contentStart) return null;
180
+ classes.push(pattern.slice(classStart, index + 1));
181
+ classStart = -1;
182
+ previousToken = 'literal';
183
+ continue;
184
+ } else if (-1 !== classStart) {
185
+ if ([
186
+ '&&',
187
+ '--',
188
+ '~~'
189
+ ].includes(pattern.slice(index, index + 2))) return null;
190
+ continue;
191
+ } else if ('(' === character && '?' === pattern[index + 1]) {
192
+ if (':' !== pattern[index + 2]) return null;
193
+ index += 2;
194
+ } else if ('{' === character) {
195
+ const repetition = /^\{(\d+)(?:,(\d*))?\}/.exec(pattern.slice(index));
196
+ if (!repetition || [
197
+ '',
198
+ '|',
199
+ '('
200
+ ].includes(previousToken) || repetition.slice(1).some((count)=>Number(count) > 0xffffffff)) return null;
201
+ } else if ('*+?}'.includes(character) && ('+' === pattern[index + 1] || '{' === pattern[index + 1])) return null;
202
+ if ('*+?{'.includes(character) && [
203
+ '^',
204
+ '$',
205
+ 'empty group'
206
+ ].includes(previousToken)) return null;
207
+ previousToken = ')' === character && [
208
+ '(',
209
+ 'empty group'
210
+ ].includes(previousToken) ? 'empty group' : character;
211
+ }
212
+ return classes;
213
+ }
214
+ function regexRequiresServerEvaluation(message) {
215
+ const error = new InconclusiveMatchError(message);
216
+ Object.defineProperty(error, Symbol.for('posthog.requiresServerRegexEvaluation'), {
217
+ value: true
218
+ });
219
+ return error;
220
+ }
221
+ function matchFeatureFlagRegex(pattern, value) {
222
+ if ('' === pattern) return true;
223
+ const characterClasses = getPortableRegexClasses(pattern);
224
+ if (null === characterClasses) throw regexRequiresServerEvaluation('Regex syntax requires server evaluation');
225
+ let regex;
156
226
  try {
157
- new RegExp(regex);
158
- return true;
227
+ regex = new RegExp(pattern);
228
+ } catch (error) {
229
+ if (error instanceof SyntaxError) return null;
230
+ throw regexRequiresServerEvaluation('Regex compilation requires server evaluation');
231
+ }
232
+ for (const characterClass of characterClasses)try {
233
+ new RegExp(characterClass, 'u');
159
234
  } catch {
160
- return false;
235
+ throw regexRequiresServerEvaluation('Regex character class syntax requires server evaluation');
236
+ }
237
+ for(let index = 0; index < value.length; index++){
238
+ const character = value[index];
239
+ if (character.charCodeAt(0) > 0x7f || '\r' === character) throw regexRequiresServerEvaluation('Regex matching for Unicode or carriage returns requires server evaluation');
240
+ }
241
+ try {
242
+ return regex.test(value);
243
+ } catch {
244
+ throw regexRequiresServerEvaluation('Regex execution requires server evaluation');
161
245
  }
162
246
  }
163
247
  function asciiLowercase(value) {
@@ -431,9 +515,12 @@ function matchPropertyValue(property, propertyValues, options) {
431
515
  case 'not_ends_with':
432
516
  return !asciiLowercase(overrideValue).endsWith(asciiLowercase(value));
433
517
  case 'regex':
434
- return isValidRegex(String(value)) && null !== String(overrideValue).match(String(value));
435
518
  case 'not_regex':
436
- return isValidRegex(String(value)) && null === String(overrideValue).match(String(value));
519
+ {
520
+ const matched = matchFeatureFlagRegex(String(value), String(overrideValue));
521
+ if (null === matched) return false;
522
+ return 'regex' === operator ? matched : !matched;
523
+ }
437
524
  case 'gt':
438
525
  case 'gte':
439
526
  case 'lt':
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/core",
3
- "version": "1.60.0",
3
+ "version": "1.60.1",
4
4
  "bugs": {
5
5
  "url": "https://github.com/PostHog/posthog-js/issues"
6
6
  },
@@ -249,12 +249,110 @@ function exactMatchString(value: unknown): string {
249
249
  return serializeJsonValue(value)
250
250
  }
251
251
 
252
- function isValidRegex(regex: string): boolean {
252
+ // Both engines treat escaped spaces and ASCII punctuation as literals, except Rust's \< and \> word boundaries.
253
+ const LITERAL_ESCAPED_PUNCTUATION = /[\x20-\x2f:;=?@\x5b-\x60\x7b-\x7e]/
254
+
255
+ function getPortableRegexClasses(pattern: string): string[] | null {
256
+ const classes: string[] = []
257
+ let classStart = -1
258
+ let previousToken = ''
259
+ for (let index = 0; index < pattern.length; index++) {
260
+ const character = pattern[index]
261
+ if (character.charCodeAt(0) > 0x7f) return null
262
+ if (character === '\\') {
263
+ const escaped = pattern[++index]
264
+ if (escaped === undefined) break
265
+ if (escaped === 'x') {
266
+ if (!/^[\da-f]{2}$/i.test(pattern.slice(index + 1, index + 3))) return null
267
+ index += 2
268
+ } else if (!'dDsSwWbBfnrtv'.includes(escaped) && !LITERAL_ESCAPED_PUNCTUATION.test(escaped)) {
269
+ return null
270
+ }
271
+ // Word boundaries can exhaust the service's backtracking limit even for short ASCII values.
272
+ if (classStart === -1 && 'bB'.includes(escaped)) return null
273
+ previousToken = 'literal'
274
+ continue
275
+ }
276
+
277
+ if (character === '[') {
278
+ if (classStart !== -1) return null
279
+ classStart = index
280
+ } else if (character === ']' && classStart !== -1) {
281
+ const contentStart = classStart + (pattern[classStart + 1] === '^' ? 2 : 1)
282
+ if (index === contentStart) return null
283
+ classes.push(pattern.slice(classStart, index + 1))
284
+ classStart = -1
285
+ previousToken = 'literal'
286
+ continue
287
+ } else if (classStart !== -1) {
288
+ if (['&&', '--', '~~'].includes(pattern.slice(index, index + 2))) return null
289
+ continue
290
+ } else if (character === '(' && pattern[index + 1] === '?') {
291
+ if (pattern[index + 2] !== ':') return null
292
+ index += 2
293
+ } else if (character === '{') {
294
+ // Rust reads `{n}` with nothing before it as literal text. JavaScript rejects it.
295
+ // The flags service rejects repetition counts above 4,294,967,295. JavaScript accepts them.
296
+ const repetition = /^\{(\d+)(?:,(\d*))?\}/.exec(pattern.slice(index))
297
+ if (
298
+ !repetition ||
299
+ ['', '|', '('].includes(previousToken) ||
300
+ repetition.slice(1).some((count) => Number(count) > 0xffffffff)
301
+ ) {
302
+ return null
303
+ }
304
+ } else if ('*+?}'.includes(character) && (pattern[index + 1] === '+' || pattern[index + 1] === '{')) {
305
+ return null
306
+ }
307
+ // JavaScript rejects a repeated anchor such as `^?`. Rust accepts it.
308
+ // Rust rejects a repeated empty group such as `(?:)*`. JavaScript accepts it.
309
+ if ('*+?{'.includes(character) && ['^', '$', 'empty group'].includes(previousToken)) return null
310
+ previousToken = character === ')' && ['(', 'empty group'].includes(previousToken) ? 'empty group' : character
311
+ }
312
+ return classes
313
+ }
314
+
315
+ // Adapters must preserve regex uncertainty across variant rules without changing missing-context fallback.
316
+ function regexRequiresServerEvaluation(message: string): InconclusiveMatchError {
317
+ const error = new InconclusiveMatchError(message)
318
+ Object.defineProperty(error, Symbol.for('posthog.requiresServerRegexEvaluation'), { value: true })
319
+ return error
320
+ }
321
+
322
+ function matchFeatureFlagRegex(pattern: string, value: string): boolean | null {
323
+ if (pattern === '') return true
324
+
325
+ // Rust uses Unicode scalar semantics and additional syntax. Keep local matching to the shared ASCII subset.
326
+ const characterClasses = getPortableRegexClasses(pattern)
327
+ if (characterClasses === null) {
328
+ throw regexRequiresServerEvaluation('Regex syntax requires server evaluation')
329
+ }
330
+
331
+ let regex: RegExp
253
332
  try {
254
- new RegExp(regex)
255
- return true
333
+ regex = new RegExp(pattern)
334
+ } catch (error) {
335
+ if (error instanceof SyntaxError) return null
336
+ throw regexRequiresServerEvaluation('Regex compilation requires server evaluation')
337
+ }
338
+ for (const characterClass of characterClasses) {
339
+ try {
340
+ // Strict parsing rejects legacy JS ranges such as [\s-9], which the flags service does not accept.
341
+ new RegExp(characterClass, 'u')
342
+ } catch {
343
+ throw regexRequiresServerEvaluation('Regex character class syntax requires server evaluation')
344
+ }
345
+ }
346
+ for (let index = 0; index < value.length; index++) {
347
+ const character = value[index]
348
+ if (character.charCodeAt(0) > 0x7f || character === '\r') {
349
+ throw regexRequiresServerEvaluation('Regex matching for Unicode or carriage returns requires server evaluation')
350
+ }
351
+ }
352
+ try {
353
+ return regex.test(value)
256
354
  } catch {
257
- return false
355
+ throw regexRequiresServerEvaluation('Regex execution requires server evaluation')
258
356
  }
259
357
  }
260
358
 
@@ -562,9 +660,11 @@ function matchPropertyValue(
562
660
  case 'not_ends_with':
563
661
  return !asciiLowercase(overrideValue).endsWith(asciiLowercase(value))
564
662
  case 'regex':
565
- return isValidRegex(String(value)) && String(overrideValue).match(String(value)) !== null
566
- case 'not_regex':
567
- return isValidRegex(String(value)) && String(overrideValue).match(String(value)) === null
663
+ case 'not_regex': {
664
+ const matched = matchFeatureFlagRegex(String(value), String(overrideValue))
665
+ if (matched === null) return false
666
+ return operator === 'regex' ? matched : !matched
667
+ }
568
668
  case 'gt':
569
669
  case 'gte':
570
670
  case 'lt':