From e200dffbe984c668a4e58eaa65196a4a28514764 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 21 May 2024 16:54:45 +0200 Subject: [PATCH] fix(browser): Improve browser extension error message check --- packages/browser/src/sdk.ts | 40 +++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/packages/browser/src/sdk.ts b/packages/browser/src/sdk.ts index ba058c966754..ea24a475d959 100644 --- a/packages/browser/src/sdk.ts +++ b/packages/browser/src/sdk.ts @@ -60,22 +60,32 @@ function applyDefaultOptions(optionsArg: BrowserOptions = {}): BrowserOptions { return { ...defaultOptions, ...optionsArg }; } +type ExtensionProperties = { + chrome?: Runtime; + browser?: Runtime; +}; +type Runtime = { + runtime?: { + id?: string; + }; +}; + function shouldShowBrowserExtensionError(): boolean { - const windowWithMaybeChrome = WINDOW as typeof WINDOW & { chrome?: { runtime?: { id?: string } } }; - const isInsideChromeExtension = - windowWithMaybeChrome && - windowWithMaybeChrome.chrome && - windowWithMaybeChrome.chrome.runtime && - windowWithMaybeChrome.chrome.runtime.id; - - const windowWithMaybeBrowser = WINDOW as typeof WINDOW & { browser?: { runtime?: { id?: string } } }; - const isInsideBrowserExtension = - windowWithMaybeBrowser && - windowWithMaybeBrowser.browser && - windowWithMaybeBrowser.browser.runtime && - windowWithMaybeBrowser.browser.runtime.id; - - return !!isInsideBrowserExtension || !!isInsideChromeExtension; + const windowWithMaybeExtension = WINDOW as typeof WINDOW & ExtensionProperties; + + const extensionKey = windowWithMaybeExtension.chrome ? 'chrome' : 'browser'; + const extensionObject = windowWithMaybeExtension[extensionKey]; + + const runtimeId = extensionObject && extensionObject.runtime && extensionObject.runtime.id; + const href = (WINDOW.location && WINDOW.location.href) || ''; + + const extensionProtocols = ['chrome-extension:', 'moz-extension:', 'ms-browser-extension:']; + + // Running the SDK in a dedicated extension page and calling Sentry.init is fine; no risk of data leakage + const isDedicatedExtensionPage = + !!runtimeId && WINDOW === WINDOW.top && extensionProtocols.some(protocol => href.startsWith(`${protocol}//`)); + + return !!runtimeId && !isDedicatedExtensionPage; } /**