1
1
import type { Client , Integration , Options } from '@sentry/core' ;
2
2
import {
3
- consoleSandbox ,
4
3
dedupeIntegration ,
5
4
functionToStringIntegration ,
6
5
getIntegrationsToSetup ,
7
- getLocationHref ,
8
6
inboundFiltersIntegration ,
9
7
initAndBind ,
10
8
stackParserFromStackParserOptions ,
11
9
} from '@sentry/core' ;
12
10
import type { BrowserClientOptions , BrowserOptions } from './client' ;
13
11
import { BrowserClient } from './client' ;
14
- import { DEBUG_BUILD } from './debug-build' ;
15
- import { WINDOW } from './helpers' ;
16
12
import { breadcrumbsIntegration } from './integrations/breadcrumbs' ;
17
13
import { browserApiErrorsIntegration } from './integrations/browserapierrors' ;
18
14
import { browserSessionIntegration } from './integrations/browsersession' ;
@@ -21,22 +17,7 @@ import { httpContextIntegration } from './integrations/httpcontext';
21
17
import { linkedErrorsIntegration } from './integrations/linkederrors' ;
22
18
import { defaultStackParser } from './stack-parsers' ;
23
19
import { makeFetchTransport } from './transports/fetch' ;
24
-
25
- type ExtensionProperties = {
26
- chrome ?: Runtime ;
27
- browser ?: Runtime ;
28
- nw ?: unknown ;
29
- } ;
30
- type Runtime = {
31
- runtime ?: {
32
- id ?: string ;
33
- } ;
34
- } ;
35
-
36
- /**
37
- * A magic string that build tooling can leverage in order to inject a release value into the SDK.
38
- */
39
- declare const __SENTRY_RELEASE__ : string | undefined ;
20
+ import { checkAndWarnIfIsEmbeddedBrowserExtension } from './utils/detectBrowserExtension' ;
40
21
41
22
/** Get the default integrations for the browser SDK. */
42
23
export function getDefaultIntegrations ( _options : Options ) : Integration [ ] {
@@ -59,40 +40,6 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
59
40
] ;
60
41
}
61
42
62
- /** Exported only for tests. */
63
- export function applyDefaultOptions ( optionsArg : BrowserOptions = { } ) : BrowserOptions {
64
- const defaultOptions : BrowserOptions = {
65
- defaultIntegrations : getDefaultIntegrations ( optionsArg ) ,
66
- release :
67
- typeof __SENTRY_RELEASE__ === 'string' // This allows build tooling to find-and-replace __SENTRY_RELEASE__ to inject a release value
68
- ? __SENTRY_RELEASE__
69
- : WINDOW . SENTRY_RELEASE ?. id , // This supports the variable that sentry-webpack-plugin injects
70
- sendClientReports : true ,
71
- } ;
72
-
73
- return {
74
- ...defaultOptions ,
75
- ...dropTopLevelUndefinedKeys ( optionsArg ) ,
76
- } ;
77
- }
78
-
79
- /**
80
- * In contrast to the regular `dropUndefinedKeys` method,
81
- * this one does not deep-drop keys, but only on the top level.
82
- */
83
- function dropTopLevelUndefinedKeys < T extends object > ( obj : T ) : Partial < T > {
84
- const mutatetedObj : Partial < T > = { } ;
85
-
86
- for ( const k of Object . getOwnPropertyNames ( obj ) ) {
87
- const key = k as keyof T ;
88
- if ( obj [ key ] !== undefined ) {
89
- mutatetedObj [ key ] = obj [ key ] ;
90
- }
91
- }
92
-
93
- return mutatetedObj ;
94
- }
95
-
96
43
/**
97
44
* The Sentry Browser SDK Client.
98
45
*
@@ -139,19 +86,21 @@ function dropTopLevelUndefinedKeys<T extends object>(obj: T): Partial<T> {
139
86
*
140
87
* @see {@link BrowserOptions } for documentation on configuration options.
141
88
*/
142
- export function init ( browserOptions : BrowserOptions = { } ) : Client | undefined {
143
- if ( ! browserOptions . skipBrowserExtensionCheck && _checkForBrowserExtension ( ) ) {
144
- return ;
145
- }
89
+ export function init ( options : BrowserOptions = { } ) : Client | undefined {
90
+ const shouldDisableBecauseIsBrowserExtenstion =
91
+ ! options . skipBrowserExtensionCheck && checkAndWarnIfIsEmbeddedBrowserExtension ( ) ;
146
92
147
- const options = applyDefaultOptions ( browserOptions ) ;
148
93
const clientOptions : BrowserClientOptions = {
94
+ enabled : ! shouldDisableBecauseIsBrowserExtenstion ,
149
95
...options ,
150
96
stackParser : stackParserFromStackParserOptions ( options . stackParser || defaultStackParser ) ,
151
- integrations : getIntegrationsToSetup ( options ) ,
97
+ integrations : getIntegrationsToSetup ( {
98
+ integrations : options . integrations ,
99
+ defaultIntegrations :
100
+ options . defaultIntegrations == null ? getDefaultIntegrations ( options ) : options . defaultIntegrations ,
101
+ } ) ,
152
102
transport : options . transport || makeFetchTransport ,
153
103
} ;
154
-
155
104
return initAndBind ( BrowserClient , clientOptions ) ;
156
105
}
157
106
@@ -170,48 +119,3 @@ export function forceLoad(): void {
170
119
export function onLoad ( callback : ( ) => void ) : void {
171
120
callback ( ) ;
172
121
}
173
-
174
- function _isEmbeddedBrowserExtension ( ) : boolean {
175
- if ( typeof WINDOW . window === 'undefined' ) {
176
- // No need to show the error if we're not in a browser window environment (e.g. service workers)
177
- return false ;
178
- }
179
-
180
- const _window = WINDOW as typeof WINDOW & ExtensionProperties ;
181
-
182
- // Running the SDK in NW.js, which appears like a browser extension but isn't, is also fine
183
- // see: https://github.com/getsentry/sentry-javascript/issues/12668
184
- if ( _window . nw ) {
185
- return false ;
186
- }
187
-
188
- const extensionObject = _window [ 'chrome' ] || _window [ 'browser' ] ;
189
-
190
- if ( ! extensionObject ?. runtime ?. id ) {
191
- return false ;
192
- }
193
-
194
- const href = getLocationHref ( ) ;
195
- const extensionProtocols = [ 'chrome-extension' , 'moz-extension' , 'ms-browser-extension' , 'safari-web-extension' ] ;
196
-
197
- // Running the SDK in a dedicated extension page and calling Sentry.init is fine; no risk of data leakage
198
- const isDedicatedExtensionPage =
199
- WINDOW === WINDOW . top && extensionProtocols . some ( protocol => href . startsWith ( `${ protocol } ://` ) ) ;
200
-
201
- return ! isDedicatedExtensionPage ;
202
- }
203
-
204
- function _checkForBrowserExtension ( ) : true | void {
205
- if ( _isEmbeddedBrowserExtension ( ) ) {
206
- if ( DEBUG_BUILD ) {
207
- consoleSandbox ( ( ) => {
208
- // eslint-disable-next-line no-console
209
- console . error (
210
- '[Sentry] You cannot use Sentry.init() in a browser extension, see: https://docs.sentry.io/platforms/javascript/best-practices/browser-extensions/' ,
211
- ) ;
212
- } ) ;
213
- }
214
-
215
- return true ;
216
- }
217
- }
0 commit comments