: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(error-tracking): drop injected-script ReferenceErrors from error tracking (#86824)
posthog[bot]7a86dc97afb2
diff --git a/frontend/src/layout/navigation/SelfReadOnlyNotice/selfReadOnlyModeLogic.test.ts b/frontend/src/layout/navigation/SelfReadOnlyNotice/selfReadOnlyModeLogic.test.tsindex cacc91fb547b..2e7bcf41501c 100644--- a/frontend/src/layout/navigation/SelfReadOnlyNotice/selfReadOnlyModeLogic.test.ts+++ b/frontend/src/layout/navigation/SelfReadOnlyNotice/selfReadOnlyModeLogic.test.ts@@ -2,6 +2,7 @@ import { NETWORK_ERROR_MESSAGES } from 'lib/api-error'import {dropHandledAuthGateExceptions,+ dropInjectedScriptReferenceErrors,dropReadOnlyExceptions,dropUnactionableNetworkExceptions,} from './selfReadOnlyModeLogic'@@ -142,3 +143,69 @@ describe('dropUnactionableNetworkExceptions', () => {expect(dropUnactionableNetworkExceptions(null)).toBeNull()})})++describe('dropInjectedScriptReferenceErrors', () => {+ const injected = (value: string): { event: string; properties: any } => ({+ event: '$exception',+ properties: {+ $exception_list: [{ type: 'ReferenceError', value, mechanism: { type: 'generic', handled: false } }],+ },+ })++ // Real user-reported injected identifiers, in both the WebKit and the V8/SpiderMonkey wording.+ it.each([+ ["Can't find variable: pos"],+ ["Can't find variable: __firefox__"],+ ["Can't find variable: WeixinJSBridge"],+ ['zaloJSV2 is not defined'],+ ['chrome is not defined'],+ ['Rt is not defined'],+ ['ehrMain is not defined'],+ ])('drops the unhandled, frameless ReferenceError "%s"', (value) => {+ expect(dropInjectedScriptReferenceErrors(injected(value))).toBeNull()+ })++ it('keeps a ReferenceError from our own bundle, which carries a stack', () => {+ const event = {+ event: '$exception',+ properties: {+ $exception_list: [+ {+ type: 'ReferenceError',+ value: 'foo is not defined',+ mechanism: { type: 'generic', handled: false },+ stacktrace: { frames: [{ source: 'https://us.posthog.com/static/chunk.js' }] },+ },+ ],+ },+ }+ expect(dropInjectedScriptReferenceErrors(event)).toBe(event)+ })++ it('keeps a handled ReferenceError, which some code path already recovered from', () => {+ const event = {+ event: '$exception',+ properties: {+ $exception_list: [+ { type: 'ReferenceError', value: 'foo is not defined', mechanism: { handled: true } },+ ],+ },+ }+ expect(dropInjectedScriptReferenceErrors(event)).toBe(event)+ })++ it('keeps a non-ReferenceError that happens to match the message shape', () => {+ const event = {+ event: '$exception',+ properties: {+ $exception_list: [{ type: 'TypeError', value: 'foo is not defined', mechanism: { handled: false } }],+ },+ }+ expect(dropInjectedScriptReferenceErrors(event)).toBe(event)+ })++ it('tolerates missing properties and a missing exception list', () => {+ expect(dropInjectedScriptReferenceErrors({ event: '$exception' })).toEqual({ event: '$exception' })+ expect(dropInjectedScriptReferenceErrors(null)).toBeNull()+ })+})diff --git a/frontend/src/layout/navigation/SelfReadOnlyNotice/selfReadOnlyModeLogic.ts b/frontend/src/layout/navigation/SelfReadOnlyNotice/selfReadOnlyModeLogic.tsindex c7c7f70f0d53..c2d6f0daf896 100644--- a/frontend/src/layout/navigation/SelfReadOnlyNotice/selfReadOnlyModeLogic.ts+++ b/frontend/src/layout/navigation/SelfReadOnlyNotice/selfReadOnlyModeLogic.ts@@ -102,6 +102,41 @@ export function dropUnactionableNetworkExceptions<return event}+// Browser engines word an unknown-identifier ReferenceError in one of two ways: WebKit says+// "Can't find variable: X", V8 and SpiderMonkey say "X is not defined". Both name a single+// identifier. Our own bundle never leaves such an identifier undefined at runtime.+const UNKNOWN_IDENTIFIER_REFERENCE_ERROR = /^(Can't find variable: \S+|\S+ is not defined)$/++// Filters `$exception` events that a browser extension or an in-app browser injected into the page.+// Each one is an unhandled ReferenceError for an identifier our code never defines (for example+// `WeixinJSBridge`, `__firefox__`, or `Can't find variable: pos`), and posthog-js captures it+// through the global handler with no stack frames because the throwing script is not ours. A real+// ReferenceError in our own bundle carries a stack, so the empty-stack check keeps those. Each+// injected variant fingerprints to its own error tracking issue, so drop the whole class here.+// Exported for unit testing.+export function dropInjectedScriptReferenceErrors<+ T extends { event?: string; properties?: Record<string, any> } | null,+>(event: T): T | null {+ if (!event || event.event !== '$exception') {+ return event+ }+ const list = (event.properties?.$exception_list ?? []) as Array<{+ type?: string+ value?: string+ mechanism?: { handled?: boolean }+ stacktrace?: { frames?: unknown[] }+ }>+ const hasFrames = list.some((ex) => (ex?.stacktrace?.frames?.length ?? 0) > 0)+ const isUnhandled = list.every((ex) => ex?.mechanism?.handled === false)+ const isUnknownIdentifierReferenceError = list.some(+ (ex) => ex?.type === 'ReferenceError' && ex?.value != null && UNKNOWN_IDENTIFIER_REFERENCE_ERROR.test(ex.value)+ )+ if (!hasFrames && isUnhandled && isUnknownIdentifierReferenceError) {+ return null+ }+ return event+}+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface selfReadOnlyModeLogicValues {featureFlags: FeatureFlagsSet // featureFlagLogic@@ -216,13 +251,19 @@ export const selfReadOnlyModeLogic = kea<selfReadOnlyModeLogicType>([setReadOnlyNotifier((method) => actions.notifyBlocked(method))// Central error-tracking filter chain — drops `$exception` events for a ReadOnlyModeError- // (a block by design), for the handled auth gates (2FA setup/verify, re-auth), and for the- // network failures that only describe the client's situation. Catches direct captures *and*- // wrapped errors (`new Error('...', { cause: readOnlyErr })`). This logic mounts in the- // authenticated app, where every gated request happens, and no other code that runs there- // sets `before_send`, so we own this config slot.+ // (a block by design), for the handled auth gates (2FA setup/verify, re-auth), for the+ // network failures that only describe the client's situation, and for the ReferenceErrors+ // that browser extensions and in-app browsers inject. Catches direct captures *and* wrapped+ // errors (`new Error('...', { cause: readOnlyErr })`). This logic mounts in the authenticated+ // app, where every gated request happens, and no other code that runs there sets+ // `before_send`, so we own this config slot.posthog.set_config({- before_send: [dropReadOnlyExceptions, dropHandledAuthGateExceptions, dropUnactionableNetworkExceptions],+ before_send: [+ dropReadOnlyExceptions,+ dropHandledAuthGateExceptions,+ dropUnactionableNetworkExceptions,+ dropInjectedScriptReferenceErrors,+ ],})// The user-facing toast for blocked writes is shown by the standard