: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): stop reporting handled 2FA gate 403s as exceptions (#81330)
posthog[bot]f01206c3f7b2
diff --git a/frontend/src/layout/navigation/SelfReadOnlyNotice/selfReadOnlyModeLogic.test.ts b/frontend/src/layout/navigation/SelfReadOnlyNotice/selfReadOnlyModeLogic.test.tsindex 9f5929715270..53eba3cdf081 100644--- a/frontend/src/layout/navigation/SelfReadOnlyNotice/selfReadOnlyModeLogic.test.ts+++ b/frontend/src/layout/navigation/SelfReadOnlyNotice/selfReadOnlyModeLogic.test.ts@@ -1,4 +1,4 @@-import { dropReadOnlyExceptions } from './selfReadOnlyModeLogic'+import { dropHandledAuthGateExceptions, dropReadOnlyExceptions } from './selfReadOnlyModeLogic'describe('dropReadOnlyExceptions', () => {it('passes non-exception events through unchanged', () => {@@ -54,3 +54,42 @@ describe('dropReadOnlyExceptions', () => {expect(dropReadOnlyExceptions(null)).toBeNull()})})++describe('dropHandledAuthGateExceptions', () => {+ it('passes real crashes through', () => {+ const event = {+ event: '$exception',+ properties: { $exception_list: [{ type: 'TypeError', value: 'x is not a function' }] },+ }+ expect(dropHandledAuthGateExceptions(event)).toBe(event)+ })++ // Each value is the message `ApiError` sets from the backend `detail` for a handled auth gate,+ // so a rejected request is expected control flow rather than a bug to report.+ it.each([+ ['2FA setup required'],+ ['2FA verification required'],+ ['This action requires you to be recently authenticated.'],+ ])('drops the handled auth-gate exception with message "%s"', (value) => {+ const event = { event: '$exception', properties: { $exception_list: [{ type: 'Error', value }] } }+ expect(dropHandledAuthGateExceptions(event)).toBeNull()+ })++ it('drops the gate even when it sits deeper in the exception list', () => {+ const event = {+ event: '$exception',+ properties: {+ $exception_list: [+ { type: 'Error', value: 'reverseProxyCheckerLogic: query failed' },+ { type: 'Error', value: '2FA setup required' },+ ],+ },+ }+ expect(dropHandledAuthGateExceptions(event)).toBeNull()+ })++ it('tolerates missing properties and a missing exception list', () => {+ expect(dropHandledAuthGateExceptions({ event: '$exception' })).toEqual({ event: '$exception' })+ expect(dropHandledAuthGateExceptions(null)).toBeNull()+ })+})diff --git a/frontend/src/layout/navigation/SelfReadOnlyNotice/selfReadOnlyModeLogic.ts b/frontend/src/layout/navigation/SelfReadOnlyNotice/selfReadOnlyModeLogic.tsindex 39c640cd92c7..85d79eb86e56 100644--- a/frontend/src/layout/navigation/SelfReadOnlyNotice/selfReadOnlyModeLogic.ts+++ b/frontend/src/layout/navigation/SelfReadOnlyNotice/selfReadOnlyModeLogic.ts@@ -42,6 +42,41 @@ export function dropReadOnlyExceptions<T extends { event?: string; properties?:return event}+// Detail messages the backend sends with the three handled auth-gate 403s. The frontend already+// recovers from each of them in `apiStatusLogic` (it opens the 2FA setup modal, shows the+// re-verification toast, or prompts a re-auth), so a rejected request is expected control flow,+// not a bug. posthog-js's error conversion keeps only the error type, message, and stack in+// `$exception_list` — the DRF `code` never reaches `before_send` — so we match on the message,+// which `ApiError` sets from the response `detail`.+//+// two_factor_setup_required — posthog/helpers/two_factor_session.py+// two_factor_verification_required — posthog/helpers/two_factor_session.py+// sensitive_action_required_reauth — posthog/permissions.py (TimeSensitiveActionPermission)+const HANDLED_AUTH_GATE_MESSAGES = new Set([+ '2FA setup required',+ '2FA verification required',+ 'This action requires you to be recently authenticated.',+])++// Filters `$exception` events that are really a handled auth gate, not a crash. When an org+// enforces 2FA, or an action needs recent re-authentication, the API returns a 403 that+// `apiStatusLogic` turns into the setup/verify flow. The many call sites that also+// `captureException` the rejection — kea loaders via `initKea`, and direct captures like+// `reverseProxyCheckerLogic` — would each file it as a fresh error tracking issue, so drop+// them centrally here. Exported for unit testing.+export function dropHandledAuthGateExceptions<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<{ value?: string }>+ if (list.some((ex) => ex?.value != null && HANDLED_AUTH_GATE_MESSAGES.has(ex.value))) {+ 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@@ -155,11 +190,12 @@ export const selfReadOnlyModeLogic = kea<selfReadOnlyModeLogicType>([setReadOnlyGetter(() => selfReadOnlyModeLogic.findMounted()?.values.isReadOnly ?? false)setReadOnlyNotifier((method) => actions.notifyBlocked(method))- // Central error-tracking filter — drops any `$exception` event whose- // chain contains a ReadOnlyModeError. Catches direct captures *and*- // wrapped errors (`new Error('...', { cause: readOnlyErr })`). No- // existing code sets `before_send`, so we own this config slot.- posthog.set_config({ before_send: dropReadOnlyExceptions })+ // Central error-tracking filter chain — drops `$exception` events for a ReadOnlyModeError+ // (a block by design) and for the handled auth gates (2FA setup/verify, re-auth). 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] })// The user-facing toast for blocked writes is shown by the standard// `e instanceof ApiError → lemonToast.error(e.detail)` pattern that@@ -171,8 +207,7 @@ export const selfReadOnlyModeLogic = kea<selfReadOnlyModeLogicType>([beforeUnmount(() => {setReadOnlyGetter(null)setReadOnlyNotifier(null)- // Releasing ownership of `before_send` — if PostHog adds another filter- // here in the future, it should compose rather than be clobbered.+ // Releasing ownership of `before_send` — this clears the whole filter chain set on mount.posthog.set_config({ before_send: undefined })}),])