mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 19:08:33 +01:00
fix(ui): drop errors thrown by browser extension scripts from browser Sentry (#23357)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The browser UI sends unhandled errors to Sentry through `ui/src/lib/sentry.ts` > - Browser extensions run their own scripts in the page. The Sentry SDK wraps their event listeners, so an extension error is reported as a Paperclip error > - The Jam bug-capture extension listens for the Navigation API `navigate` event. When its recorded network events become too large, its listener throws `RangeError: Invalid string length` from `JSON.stringify` > - No Paperclip code is at fault. These reports are noise and hide real errors > - This pull request drops errors that extension code threw, with the built-in Sentry `denyUrls` option > - The benefit is fewer false error reports. Errors that Paperclip code throws are still sent, also when an extension frame is lower on the stack ## Linked Issues or Issue Description **What happened?** Hosted staging browser Sentry recorded one unhandled `RangeError: Invalid string length`. A Paperclip link click called React Router's `History.pushState`. The browser then fired the Navigation API `navigate` event. The Sentry `BrowserApiErrors` wrapper caught the error from a listener that the Jam extension added. The top frames are `JSON.stringify (<anonymous>)`, then `re.addMany`, `t2.persistEphemeral` and `t2.beforeNavigation` in `webpack://jam-extension/injected-scripts/host-network-events.js`. A second Sentry group with the same title and the same trace is already ignored by hand. **Steps to reproduce** 1. Install the Jam bug-capture extension and let it record network events on a Paperclip page for a long time. 2. Click a Paperclip link that navigates in the app. 3. See `RangeError: Invalid string length` in the browser Sentry project. **Expected behavior** Sentry does not record errors that a browser extension's own script throws. **Actual behavior** Sentry records them as unhandled Paperclip errors. Related: #22378 (the same kind of filter for Firefox for iOS injected scripts). No duplicate PR was found (searched for `denyUrls`, `jam-extension`, and "browser extension sentry"). ## What Changed - `ui/src/lib/sentry.ts`: add the exported `BROWSER_EXTENSION_SCRIPT_URLS` list and pass it to `Sentry.init` as `denyUrls`. The list holds the `chrome-`, `moz-`, `safari-`, `safari-web-` and `ms-browser-extension://` URL schemes and Jam's `webpack://jam-extension/` script URL. A comment tells why. - `ui/src/lib/sentry.test.ts`: add a test with the real Sentry SDK. It sends the reported Jam stack and a `chrome-extension://` stack, and makes sure that Sentry drops them. It also sends an app error with a Jam frame lower on the stack and a plain app error, and makes sure that Sentry keeps them. Add an options test for which URLs are denied and which are not. - `doc/observability.md`: describe the filter next to the `__firefox__` note. ## Verification - `cd ui && pnpm exec vitest run src/lib/sentry.test.ts src/lib/browser-error-context.test.ts`: 66 passed. - Without the `ui/src/lib/sentry.ts` change (tests kept), the 2 new tests fail. With the change, all 45 tests in `sentry.test.ts` pass. - `@sentry/core` 10.71.0 `eventFilters` reads the innermost frame whose file name is not `<anonymous>` or `[native code]`. For the reported event, that is the Jam script URL. - `cd ui && pnpm exec tsc --noEmit -p .`: no errors in the changed files. The local tree has only errors from an unbuilt plugin SDK and missing generated Storybook files. CI covers the full typecheck. ## Risks - Low risk. The filter matches only the frame that threw. An extension that throws from its own script while it runs Paperclip code would also be dropped, but such an error is not a Paperclip bug. - A Paperclip error is not dropped when an extension frame is lower on the stack. A test covers this. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, with tool use and code execution in Claude Code. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: devinfoley <139239+devinfoley@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
3 files changed
+108
No files matched your search
@@ -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.<anonymous> (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<typeof import("@sentry/browser")>("@sentry/browser");
|
||||
const events: Array<Record<string, unknown>> = [];
|
||||
vi.doMock("@sentry/browser", () => ({
|
||||
...Sentry,
|
||||
init: (options: Parameters<typeof Sentry.init>[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<string, unknown>);
|
||||
return enriched;
|
||||
},
|
||||
}),
|
||||
}));
|
||||
const gate = await importFreshSentry();
|
||||
await gate.initBrowserErrorMonitoring(DSN, "staging");
|
||||
try {
|
||||
gate.captureBrowserException(errorWithStack("RangeError", "Invalid string length", [
|
||||
"JSON.stringify (<anonymous>)",
|
||||
`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");
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in new issue
Block a user