@@ -60,22 +60,32 @@ function applyDefaultOptions(optionsArg: BrowserOptions = {}): BrowserOptions {
60
60
return { ...defaultOptions , ...optionsArg } ;
61
61
}
62
62
63
+ type ExtensionProperties = {
64
+ chrome ?: Runtime ;
65
+ browser ?: Runtime ;
66
+ } ;
67
+ type Runtime = {
68
+ runtime ?: {
69
+ id ?: string ;
70
+ } ;
71
+ } ;
72
+
63
73
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 ;
79
89
}
80
90
81
91
/**
0 commit comments