Skip to content

Commit e200dff

Browse files
committed
fix(browser): Improve browser extension error message check
1 parent 7e59832 commit e200dff

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

packages/browser/src/sdk.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,32 @@ function applyDefaultOptions(optionsArg: BrowserOptions = {}): BrowserOptions {
6060
return { ...defaultOptions, ...optionsArg };
6161
}
6262

63+
type ExtensionProperties = {
64+
chrome?: Runtime;
65+
browser?: Runtime;
66+
};
67+
type Runtime = {
68+
runtime?: {
69+
id?: string;
70+
};
71+
};
72+
6373
function shouldShowBrowserExtensionError(): boolean {
64-
const windowWithMaybeChrome = WINDOW as typeof WINDOW & { chrome?: { runtime?: { id?: string } } };
65-
const isInsideChromeExtension =
66-
windowWithMaybeChrome &&
67-
windowWithMaybeChrome.chrome &&
68-
windowWithMaybeChrome.chrome.runtime &&
69-
windowWithMaybeChrome.chrome.runtime.id;
70-
71-
const windowWithMaybeBrowser = WINDOW as typeof WINDOW & { browser?: { runtime?: { id?: string } } };
72-
const isInsideBrowserExtension =
73-
windowWithMaybeBrowser &&
74-
windowWithMaybeBrowser.browser &&
75-
windowWithMaybeBrowser.browser.runtime &&
76-
windowWithMaybeBrowser.browser.runtime.id;
77-
78-
return !!isInsideBrowserExtension || !!isInsideChromeExtension;
74+
const windowWithMaybeExtension = WINDOW as typeof WINDOW & ExtensionProperties;
75+
76+
const extensionKey = windowWithMaybeExtension.chrome ? 'chrome' : 'browser';
77+
const extensionObject = windowWithMaybeExtension[extensionKey];
78+
79+
const runtimeId = extensionObject && extensionObject.runtime && extensionObject.runtime.id;
80+
const href = (WINDOW.location && WINDOW.location.href) || '';
81+
82+
const extensionProtocols = ['chrome-extension:', 'moz-extension:', 'ms-browser-extension:'];
83+
84+
// Running the SDK in a dedicated extension page and calling Sentry.init is fine; no risk of data leakage
85+
const isDedicatedExtensionPage =
86+
!!runtimeId && WINDOW === WINDOW.top && extensionProtocols.some(protocol => href.startsWith(`${protocol}//`));
87+
88+
return !!runtimeId && !isDedicatedExtensionPage;
7989
}
8090

8191
/**

0 commit comments

Comments
 (0)