Skip to content

Commit

Permalink
fix(browser): Improve browser extension error message check
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed May 21, 2024
1 parent 7e59832 commit e200dff
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions packages/browser/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit e200dff

Please sign in to comment.