diff --git a/doc/observability.md b/doc/observability.md index fbc32a552af..f5994ce3286 100644 --- a/doc/observability.md +++ b/doc/observability.md @@ -505,6 +505,17 @@ Paperclip code defines or reads `__firefox__`, so these errors do not come from Paperclip. The built-in `ignoreErrors` option drops them. All other errors from the same browser are still sent. +The browser also does not send an error that a browser extension's own script +threw. The built-in `denyUrls` option lists the extension URL schemes +(`chrome-extension://`, `moz-extension://`, `safari-extension://`, +`safari-web-extension://`, and `ms-browser-extension://`) and the Jam +bug-capture extension's script URL (`webpack://jam-extension/`), which Jam +reports instead of an extension URL. Sentry compares only the innermost stack +frame that has a real file name, so an error that Paperclip code throws is +still sent when an extension frame is lower on the same stack. Jam's +navigation listener can throw "RangeError: Invalid string length" when its +recorded network events become too large to serialize. + ### Fail-open behavior A failed Sentry import or a failed init never stops the server and never diff --git a/ui/src/lib/sentry.test.ts b/ui/src/lib/sentry.test.ts index 25842e9f9dd..7e40f9b083c 100644 --- a/ui/src/lib/sentry.test.ts +++ b/ui/src/lib/sentry.test.ts @@ -487,6 +487,68 @@ describe("unhandled rejection attribution through the gate", () => { }); }); +function errorWithStack(type: "Error" | "RangeError", message: string, frames: string[]): Error { + const error = type === "RangeError" ? new RangeError(message) : new Error(message); + error.stack = [`${type}: ${message}`, ...frames.map((frame) => ` at ${frame}`)].join("\n"); + return error; +} + +const JAM_SCRIPT = "webpack://jam-extension/injected-scripts/host-network-events.js"; +const APP_FRAMES = [ + "Navigation. (https://paperclip.staging.paperclip.app/assets/prod-C6Q7tISe.js:488:6566)", + "History.pushState (https://paperclip.staging.paperclip.app/assets/index-DmlKhyw1.js:426:382165)", + "push (https://paperclip.staging.paperclip.app/assets/index-DmlKhyw1.js:115:397846)", +]; + +describe("browser extension script errors with the real SDK", () => { + it("drops an error thrown by extension code and still sends an app error with an extension frame below it", async () => { + const Sentry = await vi.importActual("@sentry/browser"); + const events: Array> = []; + vi.doMock("@sentry/browser", () => ({ + ...Sentry, + init: (options: Parameters[0]) => Sentry.init({ + ...options, + transport: () => ({ send: async () => ({}), flush: async () => true }), + beforeSend: async (event, hint) => { + const enriched = await options?.beforeSend?.(event, hint) ?? event; + events.push(enriched as unknown as Record); + return enriched; + }, + }), + })); + const gate = await importFreshSentry(); + await gate.initBrowserErrorMonitoring(DSN, "staging"); + try { + gate.captureBrowserException(errorWithStack("RangeError", "Invalid string length", [ + "JSON.stringify ()", + `re.addMany (${JAM_SCRIPT}:2:13452)`, + `t2.persistEphemeral (${JAM_SCRIPT}:2:9996)`, + `t2.beforeNavigation (${JAM_SCRIPT}:2:10571)`, + ...APP_FRAMES, + ])); + gate.captureBrowserException(errorWithStack("Error", "extension listener failed", [ + "onMessage (chrome-extension://abcdefghijklmnopabcdefghijklmnop/content.js:1:42)", + ...APP_FRAMES, + ])); + gate.captureBrowserException(errorWithStack("Error", "app failure under an extension wrapper", [ + "loadIssue (https://paperclip.staging.paperclip.app/assets/index-DmlKhyw1.js:12:34)", + `t2.beforeNavigation (${JAM_SCRIPT}:2:10571)`, + ...APP_FRAMES, + ])); + gate.captureBrowserException(errorWithStack("Error", "plain app failure", APP_FRAMES)); + await gate.initBrowserErrorMonitoring(DSN, "staging"); + await Sentry.flush(2000); + + const messages = events.map((event) => + (event.exception as { values: Array<{ value: string }> }).values[0]?.value, + ); + expect(messages).toEqual(["app failure under an extension wrapper", "plain app failure"]); + } finally { + await gate.teardownBrowserErrorMonitoring(); + } + }); +}); + /** * Call the `integrations` option with the given default list. The real * `@sentry/browser` option type allows `integrations` to be an array instead @@ -542,6 +604,35 @@ describe("buildBrowserSentryInitOptions", () => { expect(buildBrowserSentryInitOptions(DSN).ignoreErrors).toEqual(["__firefox__"]); }); + it("denies only browser extension script URLs", async () => { + const { buildBrowserSentryInitOptions, BROWSER_EXTENSION_SCRIPT_URLS } = await importFreshSentry(); + const denyUrls = buildBrowserSentryInitOptions(DSN).denyUrls ?? []; + const denied = (url: string) => denyUrls.some((pattern) => + typeof pattern === "string" ? url.includes(pattern) : pattern.test(url), + ); + + expect(denyUrls).toEqual([...BROWSER_EXTENSION_SCRIPT_URLS]); + for (const url of [ + "webpack://jam-extension/injected-scripts/host-network-events.js", + "chrome-extension://abcdefghijklmnop/content.js", + "moz-extension://0f1e2d3c/content.js", + "safari-extension://com.example.ext/content.js", + "safari-web-extension://4B2A/content.js", + "ms-browser-extension://ext/content.js", + ]) { + expect(denied(url), url).toBe(true); + } + for (const url of [ + "https://paperclip.staging.paperclip.app/assets/index-DmlKhyw1.js", + "/assets/index-DmlKhyw1.js", + "blob:https://paperclip.staging.paperclip.app/0b6e", + "webpack://paperclip/src/main.tsx", + "https://cdn.example/chrome-extension-helper.js", + ]) { + expect(denied(url), url).toBe(false); + } + }); + it("names the plugin whose blob module raised an error, without its URL", async () => { const { buildBrowserSentryInitOptions } = await importFreshSentry(); const origins = await import("./plugin-module-origins"); diff --git a/ui/src/lib/sentry.ts b/ui/src/lib/sentry.ts index 1368c4d3618..a6c97a3480b 100644 --- a/ui/src/lib/sentry.ts +++ b/ui/src/lib/sentry.ts @@ -210,6 +210,11 @@ function enqueueCapture( }); } +export const BROWSER_EXTENSION_SCRIPT_URLS: readonly RegExp[] = [ + /^(?:chrome|moz|safari|safari-web|ms-browser)-extension:\/\//i, + /^webpack:\/\/jam-extension\//, +]; + const POSTHOG_REQUEST_TIMEOUT_MESSAGE = /^PostHog request timed out(?: after \d+ms)?$/; export function isPostHogRequestTimeout(reason: unknown): boolean { @@ -250,6 +255,7 @@ export function buildBrowserSentryInitOptions( tracesSampleRate: 0, sendDefaultPii: false, ignoreErrors: ["__firefox__"], + denyUrls: [...BROWSER_EXTENSION_SCRIPT_URLS], beforeSend: (event, hint) => { const isUnhandledRejection = event.exception?.values?.some((value) => value.mechanism?.type === "onunhandledrejection"