11import type { Client , Integration , Options } from '@sentry/core' ;
22import {
3- consoleSandbox ,
43 dedupeIntegration ,
54 functionToStringIntegration ,
65 getIntegrationsToSetup ,
7- getLocationHref ,
86 inboundFiltersIntegration ,
97 initAndBind ,
108 stackParserFromStackParserOptions ,
119} from '@sentry/core' ;
1210import type { BrowserClientOptions , BrowserOptions } from './client' ;
1311import { BrowserClient } from './client' ;
14- import { DEBUG_BUILD } from './debug-build' ;
15- import { WINDOW } from './helpers' ;
1612import { breadcrumbsIntegration } from './integrations/breadcrumbs' ;
1713import { browserApiErrorsIntegration } from './integrations/browserapierrors' ;
1814import { browserSessionIntegration } from './integrations/browsersession' ;
@@ -21,22 +17,7 @@ import { httpContextIntegration } from './integrations/httpcontext';
2117import { linkedErrorsIntegration } from './integrations/linkederrors' ;
2218import { defaultStackParser } from './stack-parsers' ;
2319import { 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' ;
4021
4122/** Get the default integrations for the browser SDK. */
4223export function getDefaultIntegrations ( _options : Options ) : Integration [ ] {
@@ -59,40 +40,6 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
5940 ] ;
6041}
6142
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-
9643/**
9744 * The Sentry Browser SDK Client.
9845 *
@@ -139,19 +86,21 @@ function dropTopLevelUndefinedKeys<T extends object>(obj: T): Partial<T> {
13986 *
14087 * @see {@link BrowserOptions } for documentation on configuration options.
14188 */
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 ( ) ;
14692
147- const options = applyDefaultOptions ( browserOptions ) ;
14893 const clientOptions : BrowserClientOptions = {
14994 ...options ,
95+ enabled : shouldDisableBecauseIsBrowserExtenstion ? false : options . enabled ,
15096 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+ } ) ,
152102 transport : options . transport || makeFetchTransport ,
153103 } ;
154-
155104 return initAndBind ( BrowserClient , clientOptions ) ;
156105}
157106
@@ -170,48 +119,3 @@ export function forceLoad(): void {
170119export function onLoad ( callback : ( ) => void ) : void {
171120 callback ( ) ;
172121}
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