-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(browser): Prevent initialization in browser extensions #10844
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import * as Sentry from '@sentry/browser'; | ||
|
||
window.Sentry = Sentry; | ||
|
||
// We mock this here to simulate a Firefox/Safari browser extension | ||
window.browser = { runtime: { id: 'mock-extension-id' } }; | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { expect } from '@playwright/test'; | ||
import { sentryTest } from '../../../utils/fixtures'; | ||
|
||
sentryTest( | ||
'should not initialize when inside a Firefox/Safari browser extension', | ||
async ({ getLocalTestUrl, page }) => { | ||
await page.route('https://dsn.ingest.sentry.io/**/*', route => { | ||
return route.fulfill({ | ||
status: 200, | ||
contentType: 'application/json', | ||
body: JSON.stringify({ id: 'test-id' }), | ||
}); | ||
}); | ||
|
||
const errorLogs: string[] = []; | ||
|
||
page.on('console', message => { | ||
if (message.type() === 'error') errorLogs.push(message.text()); | ||
}); | ||
|
||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
await page.goto(url); | ||
|
||
const isInitialized = await page.evaluate(() => { | ||
return !!(window as any).Sentry.isInitialized(); | ||
}); | ||
|
||
expect(isInitialized).toEqual(false); | ||
expect(errorLogs.length).toEqual(1); | ||
expect(errorLogs[0]).toEqual( | ||
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/troubleshooting/#setting-up-sentry-in-shared-environments-eg-browser-extensions', | ||
); | ||
}, | ||
); |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,10 @@ | ||||||||
import * as Sentry from '@sentry/browser'; | ||||||||
|
||||||||
window.Sentry = Sentry; | ||||||||
|
||||||||
// We mock this here to simulate a Chrome browser extension | ||||||||
window.chrome = { runtime: { id: 'mock-extension-id' } }; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
Sentry.init({ | ||||||||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||||||||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { expect } from '@playwright/test'; | ||
import { sentryTest } from '../../../utils/fixtures'; | ||
|
||
sentryTest('should not initialize when inside a Chrome browser extension', async ({ getLocalTestUrl, page }) => { | ||
await page.route('https://dsn.ingest.sentry.io/**/*', route => { | ||
return route.fulfill({ | ||
status: 200, | ||
contentType: 'application/json', | ||
body: JSON.stringify({ id: 'test-id' }), | ||
}); | ||
}); | ||
|
||
const errorLogs: string[] = []; | ||
|
||
page.on('console', message => { | ||
if (message.type() === 'error') errorLogs.push(message.text()); | ||
}); | ||
|
||
const url = await getLocalTestUrl({ testDir: __dirname }); | ||
await page.goto(url); | ||
|
||
const isInitialized = await page.evaluate(() => { | ||
return !!(window as any).Sentry.isInitialized(); | ||
}); | ||
|
||
expect(isInitialized).toEqual(false); | ||
expect(errorLogs.length).toEqual(1); | ||
expect(errorLogs[0]).toEqual( | ||
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/troubleshooting/#setting-up-sentry-in-shared-environments-eg-browser-extensions', | ||
); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import type { Client, Integration } from '@sentry/types'; | |
import { resolvedSyncPromise } from '@sentry/utils'; | ||
|
||
import type { BrowserOptions } from '../../src'; | ||
import { WINDOW } from '../../src'; | ||
import { init } from '../../src/sdk'; | ||
|
||
const PUBLIC_DSN = 'https://username@domain/123'; | ||
|
@@ -127,4 +128,61 @@ describe('init', () => { | |
expect(newIntegration.setupOnce as jest.Mock).toHaveBeenCalledTimes(1); | ||
expect(DEFAULT_INTEGRATIONS[1].setupOnce as jest.Mock).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
describe('initialization error in browser extension', () => { | ||
const DEFAULT_INTEGRATIONS: Integration[] = [ | ||
new MockIntegration('MockIntegration 0.1'), | ||
new MockIntegration('MockIntegration 0.2'), | ||
]; | ||
|
||
const options = getDefaultBrowserOptions({ dsn: PUBLIC_DSN, defaultIntegrations: DEFAULT_INTEGRATIONS }); | ||
|
||
afterEach(() => { | ||
Object.defineProperty(WINDOW, 'chrome', { value: undefined, writable: true }); | ||
Object.defineProperty(WINDOW, 'browser', { value: undefined, writable: true }); | ||
}); | ||
|
||
it('should log a browser extension error if executed inside a Chrome extension', () => { | ||
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
|
||
Object.defineProperty(WINDOW, 'chrome', { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this block to reset it and I also added the test for the regular browser environment at last to make sure this fails directly if |
||
value: { runtime: { id: 'mock-extension-id' } }, | ||
writable: true, | ||
}); | ||
|
||
init(options); | ||
|
||
expect(consoleErrorSpy).toBeCalledTimes(1); | ||
expect(consoleErrorSpy).toHaveBeenCalledWith( | ||
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/troubleshooting/#setting-up-sentry-in-shared-environments-eg-browser-extensions', | ||
); | ||
|
||
consoleErrorSpy.mockRestore(); | ||
}); | ||
|
||
it('should log a browser extension error if executed inside a Firefox/Safari extension', () => { | ||
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
|
||
Object.defineProperty(WINDOW, 'browser', { value: { runtime: { id: 'mock-extension-id' } }, writable: true }); | ||
|
||
init(options); | ||
|
||
expect(consoleErrorSpy).toBeCalledTimes(1); | ||
expect(consoleErrorSpy).toHaveBeenCalledWith( | ||
'[Sentry] You cannot run Sentry this way in a browser extension, check: https://docs.sentry.io/platforms/javascript/troubleshooting/#setting-up-sentry-in-shared-environments-eg-browser-extensions', | ||
); | ||
|
||
consoleErrorSpy.mockRestore(); | ||
}); | ||
|
||
it('should not log a browser extension error if executed inside regular browser environment', () => { | ||
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
|
||
init(options); | ||
|
||
expect(consoleErrorSpy).toBeCalledTimes(0); | ||
|
||
consoleErrorSpy.mockRestore(); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe leave a comment here for our future selves to know why this exists xD