From ded530253a8018ec8cc553e7c160151a5db12502 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Wed, 30 Oct 2024 12:16:49 +0100 Subject: [PATCH 1/5] feat(browser): Add `skipBrowserExtensionCheck` escape hatch option --- .../force-init-chrome-extension/init.js | 11 ++++++++++ .../force-init-chrome-extension/test.ts | 13 ++++++++++++ packages/browser/src/client.ts | 20 ++++++++++++++++++- packages/browser/src/sdk.ts | 2 +- 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 dev-packages/browser-integration-tests/suites/manual-client/force-init-chrome-extension/init.js create mode 100644 dev-packages/browser-integration-tests/suites/manual-client/force-init-chrome-extension/test.ts diff --git a/dev-packages/browser-integration-tests/suites/manual-client/force-init-chrome-extension/init.js b/dev-packages/browser-integration-tests/suites/manual-client/force-init-chrome-extension/init.js new file mode 100644 index 000000000000..5d5cb2799a4e --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/manual-client/force-init-chrome-extension/init.js @@ -0,0 +1,11 @@ +import * as Sentry from '@sentry/browser'; + +window.Sentry = Sentry; + +// We mock this here to simulate a Chrome browser extension +window.chrome = { runtime: { id: 'mock-extension-id' } }; + +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + skipBrowserExtensionCheck: true, +}); diff --git a/dev-packages/browser-integration-tests/suites/manual-client/force-init-chrome-extension/test.ts b/dev-packages/browser-integration-tests/suites/manual-client/force-init-chrome-extension/test.ts new file mode 100644 index 000000000000..613d5ae1fb25 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/manual-client/force-init-chrome-extension/test.ts @@ -0,0 +1,13 @@ +import { expect } from '@playwright/test'; +import { sentryTest } from '../../../utils/fixtures'; + +sentryTest('should not initialize when inside a Chrome browser extension', async ({ getLocalTestUrl, page }) => { + const url = await getLocalTestUrl({ testDir: __dirname }); + await page.goto(url); + + const isInitialized = await page.evaluate(() => { + return !!(window as any).Sentry.isInitialized(); + }); + + expect(isInitialized).toBe(true); +}); diff --git a/packages/browser/src/client.ts b/packages/browser/src/client.ts index 177d787a438d..d70bd6eb085b 100644 --- a/packages/browser/src/client.ts +++ b/packages/browser/src/client.ts @@ -26,7 +26,25 @@ import { createUserFeedbackEnvelope } from './userfeedback'; */ export type BrowserOptions = Options & BrowserClientReplayOptions & - BrowserClientProfilingOptions; + BrowserClientProfilingOptions & { + /** + * By default, the SDK will check if it is initialized in a browser extension + * if you call `Sentry.init`. In case it is, it will stop initialization + * because browser extensions require a different Sentry initialization process: + * https://docs.sentry.io/platforms/javascript/best-practices/shared-environments/ + * + * If this check wrongfully flags your setup as a browser extension, you can set this + * option to `true` to skip the check. + * + * Important: Only set this option if you know what you are doing! + * + * Setting up the SDK in a browser extension with global error monitoring is not recommended + * and will likely flood you with errors from other web sites or extensions. + * + * @default false + */ + skipBrowserExtensionCheck?: boolean; + }; /** * Configuration options for the Sentry Browser SDK Client class diff --git a/packages/browser/src/sdk.ts b/packages/browser/src/sdk.ts index 1a0296341341..cd15af8fef1f 100644 --- a/packages/browser/src/sdk.ts +++ b/packages/browser/src/sdk.ts @@ -160,7 +160,7 @@ declare const __SENTRY_RELEASE__: string | undefined; export function init(browserOptions: BrowserOptions = {}): Client | undefined { const options = applyDefaultOptions(browserOptions); - if (shouldShowBrowserExtensionError()) { + if (!options.skipBrowserExtensionCheck && shouldShowBrowserExtensionError()) { consoleSandbox(() => { // eslint-disable-next-line no-console console.error( From 4726237bb67242685a02acd540df8e56cffcb757 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Thu, 31 Oct 2024 09:52:54 +0100 Subject: [PATCH 2/5] test name --- .../force-init-chrome-extension/test.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/dev-packages/browser-integration-tests/suites/manual-client/force-init-chrome-extension/test.ts b/dev-packages/browser-integration-tests/suites/manual-client/force-init-chrome-extension/test.ts index 613d5ae1fb25..204544aaa7bc 100644 --- a/dev-packages/browser-integration-tests/suites/manual-client/force-init-chrome-extension/test.ts +++ b/dev-packages/browser-integration-tests/suites/manual-client/force-init-chrome-extension/test.ts @@ -1,13 +1,16 @@ import { expect } from '@playwright/test'; import { sentryTest } from '../../../utils/fixtures'; -sentryTest('should not initialize when inside a Chrome browser extension', async ({ getLocalTestUrl, page }) => { - const url = await getLocalTestUrl({ testDir: __dirname }); - await page.goto(url); +sentryTest( + 'initializes inside a Chrome browser extension if `skipBrowserExtensionCheck` is set', + async ({ getLocalTestUrl, page }) => { + const url = await getLocalTestUrl({ testDir: __dirname }); + await page.goto(url); - const isInitialized = await page.evaluate(() => { - return !!(window as any).Sentry.isInitialized(); - }); + const isInitialized = await page.evaluate(() => { + return !!(window as any).Sentry.isInitialized(); + }); - expect(isInitialized).toBe(true); -}); + expect(isInitialized).toBe(true); + }, +); From 39e123b35d379f0ff4f9261fe3ba7fe014163d6c Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Thu, 31 Oct 2024 10:04:09 +0100 Subject: [PATCH 3/5] jsdoc --- packages/browser/src/client.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/browser/src/client.ts b/packages/browser/src/client.ts index d70bd6eb085b..97c0a3da0135 100644 --- a/packages/browser/src/client.ts +++ b/packages/browser/src/client.ts @@ -28,6 +28,8 @@ export type BrowserOptions = Options & BrowserClientReplayOptions & BrowserClientProfilingOptions & { /** + * Important: Only set this option if you know what you are doing! + * * By default, the SDK will check if it is initialized in a browser extension * if you call `Sentry.init`. In case it is, it will stop initialization * because browser extensions require a different Sentry initialization process: @@ -36,8 +38,6 @@ export type BrowserOptions = Options & * If this check wrongfully flags your setup as a browser extension, you can set this * option to `true` to skip the check. * - * Important: Only set this option if you know what you are doing! - * * Setting up the SDK in a browser extension with global error monitoring is not recommended * and will likely flood you with errors from other web sites or extensions. * From ab9177455fa28bbc8c3bd36998a62ac8c62d0885 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Thu, 31 Oct 2024 10:40:54 +0100 Subject: [PATCH 4/5] adjust jsdoc --- packages/browser/src/client.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/browser/src/client.ts b/packages/browser/src/client.ts index 97c0a3da0135..965f3832a185 100644 --- a/packages/browser/src/client.ts +++ b/packages/browser/src/client.ts @@ -35,12 +35,13 @@ export type BrowserOptions = Options & * because browser extensions require a different Sentry initialization process: * https://docs.sentry.io/platforms/javascript/best-practices/shared-environments/ * + * Setting up the SDK in a browser extension with global error monitoring is not recommended + * and will likely flood you with errors from other web sites or extensions. This can heavily + * impact your quota and cause interference with your and other Sentry SDKs in shared environments. + * * If this check wrongfully flags your setup as a browser extension, you can set this * option to `true` to skip the check. * - * Setting up the SDK in a browser extension with global error monitoring is not recommended - * and will likely flood you with errors from other web sites or extensions. - * * @default false */ skipBrowserExtensionCheck?: boolean; From 60a43fbdacd49c93f6c4196f9144355077c51224 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Thu, 31 Oct 2024 11:11:54 +0100 Subject: [PATCH 5/5] review suggestion --- packages/browser/src/client.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/browser/src/client.ts b/packages/browser/src/client.ts index 965f3832a185..851f830d6955 100644 --- a/packages/browser/src/client.ts +++ b/packages/browser/src/client.ts @@ -30,8 +30,8 @@ export type BrowserOptions = Options & /** * Important: Only set this option if you know what you are doing! * - * By default, the SDK will check if it is initialized in a browser extension - * if you call `Sentry.init`. In case it is, it will stop initialization + * By default, the SDK will check if `Sentry.init` is called in a browser extension. + * In case it is, it will stop initialization and log a warning * because browser extensions require a different Sentry initialization process: * https://docs.sentry.io/platforms/javascript/best-practices/shared-environments/ *