@posthog/core 1.55.3 → 1.56.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":"featureFlagUtils.d.ts","sourceRoot":"","sources":["../src/featureFlagUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,iBAAiB,EACjB,gBAAgB,EAEhB,oBAAoB,EACpB,sBAAsB,EACtB,sBAAsB,EAEtB,mBAAmB,EACnB,2BAA2B,EAC5B,MAAM,SAAS,CAAA;AAEhB,eAAO,MAAM,sBAAsB,kBAE7B,mBAAmB,CAAC,sBAAsB,EAAE,OAAO,CAAC,GACpD,mBAAmB,CAAC,sBAAsB,EAAE,cAAc,GAAG,qBAAqB,CAAC,KACtF,2BAgCF,CAAA;AAqBD;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,UAAW,oBAAoB,CAAC,OAAO,CAAC,KAAG,oBAAoB,CAAC,cAAc,CAMhH,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,oBAAoB,UACxB,oBAAoB,CAAC,OAAO,CAAC,KACnC,oBAAoB,CAAC,qBAAqB,CAa5C,CAAA;AAED,eAAO,MAAM,mBAAmB,WAAY,iBAAiB,GAAG,SAAS,KAAG,gBAAgB,GAAG,SAE9F,CAAA;AAED,eAAO,MAAM,YAAY,aAAc,GAAG,KAAG,GAU5C,CAAA;AAED;;;;;;;;;GASG;AACH,eAAO,MAAM,uCAAuC,iBACpC,sBAAsB,CAAC,cAAc,CAAC,uBAC/B,sBAAsB,CAAC,qBAAqB,CAAC,KACjE,2BAaF,CAAA;AAED,eAAO,MAAM,eAAe,SAAU,iBAAiB,SAAS,gBAAgB,KAAG,iBAMlF,CAAA;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAEpE;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,gBAAgB,GAAG,MAAM,GAAG,SAAS,C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-180,7 +180,8 @@ const MINIMAL_FLAG_CALLED_EVENT_PROPERTIES = [
180
180
  '$lib',
181
181
  '$lib_version',
182
182
  '$device_id',
183
- '$is_server'
183
+ '$is_server',
184
+ '$react_native_version'
184
185
  ];
185
186
  const minimizeFlagCalledEventProperties = (properties, transportKeys = [])=>{
186
187
  const minimal = {};
@@ -151,7 +151,8 @@ const MINIMAL_FLAG_CALLED_EVENT_PROPERTIES = [
151
151
  '$lib',
152
152
  '$lib_version',
153
153
  '$device_id',
154
- '$is_server'
154
+ '$is_server',
155
+ '$react_native_version'
155
156
  ];
156
157
  const minimizeFlagCalledEventProperties = (properties, transportKeys = [])=>{
157
158
  const minimal = {};
@@ -1,6 +1,12 @@
1
1
  export declare const DEFAULT_BLOCKED_UA_STRS: string[];
2
+ /** Exposed for tests only. Not part of the public API. */
3
+ export declare function __resetBotDetectionCacheForTests(): void;
2
4
  /**
3
- * Block various web spiders from executing our JS and sending false capturing data
5
+ * Block various web spiders from executing our JS and sending false capturing data.
6
+ *
7
+ * @param ua - User agent string to check
8
+ * @param customBlockedUserAgents - Additional UA substrings to block
9
+ * @returns true if the UA matches a known-bot substring
4
10
  */
5
11
  export declare const isBlockedUA: (ua: string | undefined, customBlockedUserAgents?: string[]) => boolean;
6
12
  //# sourceMappingURL=bot-detection.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bot-detection.d.ts","sourceRoot":"","sources":["../../src/utils/bot-detection.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,uBAAuB,UAmGnC,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,WAAW,OAAiB,MAAM,GAAG,SAAS,4BAA2B,MAAM,EAAE,KAAQ,OAWrG,CAAA"}
1
+ {"version":3,"file":"bot-detection.d.ts","sourceRoot":"","sources":["../../src/utils/bot-detection.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,uBAAuB,UAmGnC,CAAA;AAeD,0DAA0D;AAC1D,wBAAgB,gCAAgC,IAAI,IAAI,CAEvD;AAaD;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,OAAiB,MAAM,GAAG,SAAS,4BAA2B,MAAM,EAAE,KAAQ,OAsBrG,CAAA"}
@@ -106,22 +106,42 @@ const DEFAULT_BLOCKED_UA_STRS = [
106
106
  'google-inspectiontool',
107
107
  'bytespider'
108
108
  ];
109
+ const UA_CACHE = new Map();
110
+ const MAX_CACHE_ENTRIES = 256;
111
+ function __resetBotDetectionCacheForTests() {
112
+ UA_CACHE.clear();
113
+ }
114
+ function buildCacheKey(ua, custom) {
115
+ let customEncoded = '';
116
+ for(let i = 0; i < custom.length; i++){
117
+ const c = custom[i];
118
+ customEncoded += c.length + ':' + c;
119
+ }
120
+ return ua.length + ':' + ua + '|' + custom.length + ':' + customEncoded;
121
+ }
109
122
  const isBlockedUA = function(ua, customBlockedUserAgents = []) {
110
123
  if (!ua) return false;
124
+ const key = buildCacheKey(ua, customBlockedUserAgents);
125
+ const cached = UA_CACHE.get(key);
126
+ if (void 0 !== cached) return cached;
111
127
  const uaLower = ua.toLowerCase();
112
- return DEFAULT_BLOCKED_UA_STRS.concat(customBlockedUserAgents).some((blockedUA)=>{
113
- const blockedUaLower = blockedUA.toLowerCase();
114
- return -1 !== uaLower.indexOf(blockedUaLower);
115
- });
128
+ const result = DEFAULT_BLOCKED_UA_STRS.concat(customBlockedUserAgents).some((blockedUA)=>-1 !== uaLower.indexOf(blockedUA.toLowerCase()));
129
+ if (UA_CACHE.size >= MAX_CACHE_ENTRIES) UA_CACHE.clear();
130
+ UA_CACHE.set(key, result);
131
+ return result;
116
132
  };
117
- __webpack_require__.d(__webpack_exports__, {}, {
133
+ __webpack_require__.d(__webpack_exports__, {
134
+ __resetBotDetectionCacheForTests: ()=>__resetBotDetectionCacheForTests
135
+ }, {
118
136
  DEFAULT_BLOCKED_UA_STRS: DEFAULT_BLOCKED_UA_STRS,
119
137
  isBlockedUA: isBlockedUA
120
138
  });
121
139
  exports.DEFAULT_BLOCKED_UA_STRS = __webpack_exports__.DEFAULT_BLOCKED_UA_STRS;
140
+ exports.__resetBotDetectionCacheForTests = __webpack_exports__.__resetBotDetectionCacheForTests;
122
141
  exports.isBlockedUA = __webpack_exports__.isBlockedUA;
123
142
  for(var __rspack_i in __webpack_exports__)if (-1 === [
124
143
  "DEFAULT_BLOCKED_UA_STRS",
144
+ "__resetBotDetectionCacheForTests",
125
145
  "isBlockedUA"
126
146
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
127
147
  Object.defineProperty(exports, '__esModule', {
@@ -77,12 +77,28 @@ const DEFAULT_BLOCKED_UA_STRS = [
77
77
  'google-inspectiontool',
78
78
  'bytespider'
79
79
  ];
80
+ const UA_CACHE = new Map();
81
+ const MAX_CACHE_ENTRIES = 256;
82
+ function __resetBotDetectionCacheForTests() {
83
+ UA_CACHE.clear();
84
+ }
85
+ function buildCacheKey(ua, custom) {
86
+ let customEncoded = '';
87
+ for(let i = 0; i < custom.length; i++){
88
+ const c = custom[i];
89
+ customEncoded += c.length + ':' + c;
90
+ }
91
+ return ua.length + ':' + ua + '|' + custom.length + ':' + customEncoded;
92
+ }
80
93
  const isBlockedUA = function(ua, customBlockedUserAgents = []) {
81
94
  if (!ua) return false;
95
+ const key = buildCacheKey(ua, customBlockedUserAgents);
96
+ const cached = UA_CACHE.get(key);
97
+ if (void 0 !== cached) return cached;
82
98
  const uaLower = ua.toLowerCase();
83
- return DEFAULT_BLOCKED_UA_STRS.concat(customBlockedUserAgents).some((blockedUA)=>{
84
- const blockedUaLower = blockedUA.toLowerCase();
85
- return -1 !== uaLower.indexOf(blockedUaLower);
86
- });
99
+ const result = DEFAULT_BLOCKED_UA_STRS.concat(customBlockedUserAgents).some((blockedUA)=>-1 !== uaLower.indexOf(blockedUA.toLowerCase()));
100
+ if (UA_CACHE.size >= MAX_CACHE_ENTRIES) UA_CACHE.clear();
101
+ UA_CACHE.set(key, result);
102
+ return result;
87
103
  };
88
- export { DEFAULT_BLOCKED_UA_STRS, isBlockedUA };
104
+ export { DEFAULT_BLOCKED_UA_STRS, __resetBotDetectionCacheForTests, isBlockedUA };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/core",
3
- "version": "1.55.3",
3
+ "version": "1.56.1",
4
4
  "bugs": {
5
5
  "url": "https://github.com/PostHog/posthog-js/issues"
6
6
  },
@@ -252,6 +252,8 @@ export const MINIMAL_FLAG_CALLED_EVENT_PROPERTIES: readonly string[] = [
252
252
  '$lib_version',
253
253
  '$device_id',
254
254
  '$is_server',
255
+ // Runtime identity, kept for runtime breakdowns of flag calls
256
+ '$react_native_version',
255
257
  ]
256
258
 
257
259
  /**
@@ -0,0 +1,100 @@
1
+ import { DEFAULT_BLOCKED_UA_STRS, isBlockedUA, __resetBotDetectionCacheForTests } from './bot-detection'
2
+
3
+ beforeEach(() => {
4
+ __resetBotDetectionCacheForTests()
5
+ })
6
+
7
+ const REAL_CHROME_UAS = [
8
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.7499.193 Safari/537.36',
9
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.6099.199 Safari/537.36',
10
+ 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/123.0.6312.122 Mobile/15E148 Safari/604.1',
11
+ 'Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Mobile Safari/537.36',
12
+ // Chrome 154 and 155 stable — released 2026-09.
13
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/154.0.8037.58 Safari/537.36',
14
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.8059.12 Safari/537.36',
15
+ ]
16
+
17
+ describe('DEFAULT_BLOCKED_UA_STRS', () => {
18
+ it('contains at least 50 entries', () => {
19
+ expect(DEFAULT_BLOCKED_UA_STRS.length).toBeGreaterThan(50)
20
+ })
21
+
22
+ it('contains critical entries', () => {
23
+ expect(DEFAULT_BLOCKED_UA_STRS).toContain('googlebot')
24
+ expect(DEFAULT_BLOCKED_UA_STRS).toContain('headlesschrome')
25
+ expect(DEFAULT_BLOCKED_UA_STRS).toContain('bot/')
26
+ })
27
+ })
28
+
29
+ describe('isBlockedUA', () => {
30
+ it('returns false for undefined ua', () => {
31
+ expect(isBlockedUA(undefined)).toBe(false)
32
+ })
33
+
34
+ it('returns false for real Chrome UAs (including Chrome 154 and 155)', () => {
35
+ for (const ua of REAL_CHROME_UAS) {
36
+ expect(isBlockedUA(ua)).toBe(false)
37
+ }
38
+ })
39
+
40
+ it('returns true for known bot substrings', () => {
41
+ expect(isBlockedUA('Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)')).toBe(true)
42
+ expect(isBlockedUA('Mozilla/5.0 bingbot/2.0')).toBe(true)
43
+ expect(isBlockedUA('headlessChrome/119.0')).toBe(true)
44
+ })
45
+
46
+ it('is case-insensitive on the UA input', () => {
47
+ const bot = 'Mozilla/5.0 (compatible; Googlebot/2.1)'
48
+ expect(isBlockedUA(bot)).toBe(true)
49
+ expect(isBlockedUA(bot.toLowerCase())).toBe(true)
50
+ expect(isBlockedUA(bot.toUpperCase())).toBe(true)
51
+ })
52
+
53
+ it('accepts customBlockedUserAgents', () => {
54
+ expect(isBlockedUA('custombot', ['custombot'])).toBe(true)
55
+ expect(isBlockedUA('custombot', [])).toBe(false)
56
+ })
57
+ })
58
+
59
+ describe('isBlockedUA session memoization', () => {
60
+ it('returns the same result on repeated calls with identical inputs', () => {
61
+ const ua = REAL_CHROME_UAS[0]
62
+ const first = isBlockedUA(ua)
63
+ const second = isBlockedUA(ua)
64
+ expect(second).toBe(first)
65
+ })
66
+
67
+ it('cache is bucketed per customBlockedUserAgents list', () => {
68
+ const ua = 'Mozilla/5.0 custombot'
69
+ expect(isBlockedUA(ua, [])).toBe(false)
70
+ expect(isBlockedUA(ua, ['custombot'])).toBe(true)
71
+ expect(isBlockedUA(ua, [])).toBe(false)
72
+ })
73
+
74
+ it('__resetBotDetectionCacheForTests wipes the cache', () => {
75
+ isBlockedUA(REAL_CHROME_UAS[0])
76
+ __resetBotDetectionCacheForTests()
77
+ // Still correct after reset; this just proves the reset doesn't corrupt state.
78
+ expect(isBlockedUA(REAL_CHROME_UAS[0])).toBe(false)
79
+ })
80
+
81
+ it('handles a burst of many distinct UAs without breaking correctness', () => {
82
+ for (let i = 0; i < 500; i++) {
83
+ const ua = `Mozilla/5.0 (burst-${i}) AppleWebKit/537.36`
84
+ expect(isBlockedUA(ua)).toBe(false)
85
+ }
86
+ })
87
+ })
88
+
89
+ describe('isBlockedUA cache-key delimiter injection', () => {
90
+ it('does not collide when custom list contains delimiter bytes', () => {
91
+ const ua = 'MyAgent/1.0'
92
+ // Length-prefixed keys mean embedded ':' or '|' in list entries cannot
93
+ // forge a key that aliases another (ua, custom[]) pair.
94
+ const a = isBlockedUA(ua, ['foo:1', 'bar'])
95
+ const b = isBlockedUA(ua, ['foo', '1:bar'])
96
+ // Both lookups are independent cache slots and both correctly return false.
97
+ expect(a).toBe(false)
98
+ expect(b).toBe(false)
99
+ })
100
+ })
@@ -102,17 +102,61 @@ export const DEFAULT_BLOCKED_UA_STRS = [
102
102
  ]
103
103
 
104
104
  /**
105
- * Block various web spiders from executing our JS and sending false capturing data
105
+ * Session-level memoization for isBlockedUA.
106
+ *
107
+ * navigator.userAgent is constant across all events captured in a browser
108
+ * session, and PostHog's autocapture path calls isBlockedUA on every event.
109
+ * Memoizing the boolean result turns an O(N) substring-scan over all N events
110
+ * into O(1) after the first lookup. The map is keyed on (ua, customBlockedUserAgents)
111
+ * — a config change yields a fresh slot. If the cache grows past MAX_CACHE_ENTRIES
112
+ * we drop it entirely (healthy sessions see O(1) distinct keys).
113
+ */
114
+ const UA_CACHE = new Map<string, boolean>()
115
+ const MAX_CACHE_ENTRIES = 256
116
+
117
+ /** Exposed for tests only. Not part of the public API. */
118
+ export function __resetBotDetectionCacheForTests(): void {
119
+ UA_CACHE.clear()
120
+ }
121
+
122
+ function buildCacheKey(ua: string, custom: string[]): string {
123
+ // Length-prefixed encoding is collision-free regardless of what bytes
124
+ // appear inside ua or custom entries.
125
+ let customEncoded = ''
126
+ for (let i = 0; i < custom.length; i++) {
127
+ const c = custom[i]
128
+ customEncoded += c.length + ':' + c
129
+ }
130
+ return ua.length + ':' + ua + '|' + custom.length + ':' + customEncoded
131
+ }
132
+
133
+ /**
134
+ * Block various web spiders from executing our JS and sending false capturing data.
135
+ *
136
+ * @param ua - User agent string to check
137
+ * @param customBlockedUserAgents - Additional UA substrings to block
138
+ * @returns true if the UA matches a known-bot substring
106
139
  */
107
140
  export const isBlockedUA = function (ua: string | undefined, customBlockedUserAgents: string[] = []): boolean {
108
141
  if (!ua) {
109
142
  return false
110
143
  }
111
144
 
145
+ const key = buildCacheKey(ua, customBlockedUserAgents)
146
+ const cached = UA_CACHE.get(key)
147
+ if (cached !== undefined) {
148
+ return cached
149
+ }
150
+
112
151
  const uaLower = ua.toLowerCase()
113
- return DEFAULT_BLOCKED_UA_STRS.concat(customBlockedUserAgents).some((blockedUA) => {
114
- const blockedUaLower = blockedUA.toLowerCase()
152
+ const result = DEFAULT_BLOCKED_UA_STRS.concat(customBlockedUserAgents).some((blockedUA) => {
115
153
  // can't use includes because IE 11 :/
116
- return uaLower.indexOf(blockedUaLower) !== -1
154
+ return uaLower.indexOf(blockedUA.toLowerCase()) !== -1
117
155
  })
156
+
157
+ if (UA_CACHE.size >= MAX_CACHE_ENTRIES) {
158
+ UA_CACHE.clear()
159
+ }
160
+ UA_CACHE.set(key, result)
161
+ return result
118
162
  }