-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(browser): Add debugId sync APIs between web worker and main thread #16981
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
Changes from all commits
808245c
683e95e
a294ec4
0426e92
3acf7a8
a52eb3c
6f773f6
e656490
9ff304c
fd6cec8
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,14 @@ | ||
| self._sentryDebugIds = { | ||
| 'Error at http://sentry-test.io/worker.js': 'worker-debug-id-789', | ||
| }; | ||
|
|
||
| self.postMessage({ | ||
| _sentryMessage: true, | ||
| _sentryDebugIds: self._sentryDebugIds, | ||
| }); | ||
|
|
||
| self.addEventListener('message', event => { | ||
| if (event.data.type === 'throw-error') { | ||
| throw new Error('Worker error for testing'); | ||
| } | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import * as Sentry from '@sentry/browser'; | ||
|
|
||
| // Initialize Sentry with webWorker integration | ||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| }); | ||
|
|
||
| const worker = new Worker('/worker.js'); | ||
|
|
||
| Sentry.addIntegration(Sentry.webWorkerIntegration({ worker })); | ||
|
|
||
| const btn = document.getElementById('errWorker'); | ||
|
|
||
| btn.addEventListener('click', () => { | ||
| worker.postMessage({ | ||
| type: 'throw-error', | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <!doctype html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| </head> | ||
| <body> | ||
| <button id="errWorker">Throw error in worker</button> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import type { Event } from '@sentry/core'; | ||
| import { sentryTest } from '../../../utils/fixtures'; | ||
| import { getFirstSentryEnvelopeRequest } from '../../../utils/helpers'; | ||
|
|
||
| sentryTest('Assigns web worker debug IDs when using webWorkerIntegration', async ({ getLocalTestUrl, page }) => { | ||
| const bundle = process.env.PW_BUNDLE as string | undefined; | ||
| if (bundle != null && !bundle.includes('esm') && !bundle.includes('cjs')) { | ||
| sentryTest.skip(); | ||
| } | ||
|
|
||
| const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
|
||
| const errorEventPromise = getFirstSentryEnvelopeRequest<Event>(page, url); | ||
|
|
||
| page.route('**/worker.js', route => { | ||
| route.fulfill({ | ||
| path: `${__dirname}/assets/worker.js`, | ||
| }); | ||
| }); | ||
|
|
||
| const button = page.locator('#errWorker'); | ||
| await button.click(); | ||
|
|
||
| const errorEvent = await errorEventPromise; | ||
|
|
||
| expect(errorEvent.debug_meta?.images).toBeDefined(); | ||
|
|
||
| const debugImages = errorEvent.debug_meta?.images || []; | ||
|
|
||
| expect(debugImages.length).toBe(1); | ||
|
|
||
| debugImages.forEach(image => { | ||
| expect(image.type).toBe('sourcemap'); | ||
| expect(image.debug_id).toEqual('worker-debug-id-789'); | ||
| expect(image.code_file).toEqual('http://sentry-test.io/worker.js'); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,7 @@ function fixPackageJson(cwd: string): void { | |
|
|
||
| // 2. Fix volta extends | ||
| if (!packageJson.volta) { | ||
| throw new Error('No volta config found, please provide one!'); | ||
| throw new Error("No volta config found, please add one to the test app's package.json!"); | ||
|
Member
Author
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. adjusted this message because I didn't realize the volta config needed to be added to the e2e test apps. Might be a me-problem but I think the message is now fool-proof :D |
||
| } | ||
|
|
||
| if (typeof packageJson.volta.extends === 'string') { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| @sentry:registry=http://127.0.0.1:4873 | ||
| @sentry-internal:registry=http://127.0.0.1:4873 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>Vite + TS</title> | ||
| </head> | ||
| <body> | ||
| <div id="app"></div> | ||
| <script type="module" src="/src/main.ts"></script> | ||
| <button id="trigger-error" type="button" style="background-color: #dc3545; color: white"> | ||
| Trigger Worker Error | ||
| </button> | ||
| <button id="trigger-error-2" type="button" style="background-color: #dc3545; color: white"> | ||
| Trigger Worker 2 Error | ||
| </button> | ||
| <button id="trigger-error-3" type="button" style="background-color: #dc3545; color: white"> | ||
| Trigger Worker 3 (lazily added) Error | ||
| </button> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "name": "browser-webworker-vite", | ||
| "private": true, | ||
| "version": "0.0.0", | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "vite", | ||
| "build": "rm -rf dist && tsc && vite build", | ||
| "preview": "vite preview --port 3030", | ||
| "test": "playwright test", | ||
| "test:build": "pnpm install && pnpm build", | ||
| "test:assert": "pnpm test" | ||
| }, | ||
| "devDependencies": { | ||
| "@playwright/test": "~1.53.2", | ||
| "@sentry-internal/test-utils": "link:../../../test-utils", | ||
| "typescript": "~5.8.3", | ||
| "vite": "^7.0.4" | ||
| }, | ||
| "dependencies": { | ||
| "@sentry/browser": "latest || *", | ||
| "@sentry/vite-plugin": "^3.5.0" | ||
| }, | ||
| "volta": { | ||
| "node": "20.19.2", | ||
| "yarn": "1.22.22", | ||
| "pnpm": "9.15.9" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { getPlaywrightConfig } from '@sentry-internal/test-utils'; | ||
|
|
||
| const config = getPlaywrightConfig({ | ||
| startCommand: `pnpm preview`, | ||
| eventProxyFile: 'start-event-proxy.mjs', | ||
| eventProxyPort: 3031, | ||
| port: 3030, | ||
| }); | ||
|
|
||
| export default config; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import MyWorker from './worker.ts?worker'; | ||
| import MyWorker2 from './worker2.ts?worker'; | ||
| import * as Sentry from '@sentry/browser'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: import.meta.env.PUBLIC_E2E_TEST_DSN, | ||
| environment: import.meta.env.MODE || 'development', | ||
| tracesSampleRate: 1.0, | ||
| debug: true, | ||
| integrations: [Sentry.browserTracingIntegration()], | ||
| tunnel: 'http://localhost:3031/', // proxy server | ||
| }); | ||
|
|
||
| const worker = new MyWorker(); | ||
| const worker2 = new MyWorker2(); | ||
|
|
||
| const webWorkerIntegration = Sentry.webWorkerIntegration({ worker: [worker, worker2] }); | ||
| Sentry.addIntegration(webWorkerIntegration); | ||
|
|
||
| worker.addEventListener('message', event => { | ||
| // this is part of the test, do not delete | ||
| console.log('received message from worker:', event.data.msg); | ||
| }); | ||
|
|
||
| document.querySelector<HTMLButtonElement>('#trigger-error')!.addEventListener('click', () => { | ||
| worker.postMessage({ | ||
| msg: 'TRIGGER_ERROR', | ||
| }); | ||
| }); | ||
|
|
||
| document.querySelector<HTMLButtonElement>('#trigger-error-2')!.addEventListener('click', () => { | ||
| worker2.postMessage({ | ||
| msg: 'TRIGGER_ERROR', | ||
| }); | ||
| }); | ||
|
|
||
| document.querySelector<HTMLButtonElement>('#trigger-error-3')!.addEventListener('click', async () => { | ||
| const Worker3 = await import('./worker3.ts?worker'); | ||
| const worker3 = new Worker3.default(); | ||
| webWorkerIntegration.addWorker(worker3); | ||
| worker3.postMessage({ | ||
| msg: 'TRIGGER_ERROR', | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /// <reference types="vite/client" /> |
| Original file line number | Diff line number | Diff line change | |||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,17 @@ | |||||||||||||||||||||||||||||||||||||||||||||||
| import * as Sentry from '@sentry/browser'; | |||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||
| // type cast necessary because TS thinks this file is part of the main | |||||||||||||||||||||||||||||||||||||||||||||||
| // thread where self is of type `Window` instead of `Worker` | |||||||||||||||||||||||||||||||||||||||||||||||
| Sentry.registerWebWorker({ self: self as unknown as Worker }); | |||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||
| // Let the main thread know the worker is ready | |||||||||||||||||||||||||||||||||||||||||||||||
| self.postMessage({ | |||||||||||||||||||||||||||||||||||||||||||||||
| msg: 'WORKER_READY', | |||||||||||||||||||||||||||||||||||||||||||||||
| }); | |||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||
| self.addEventListener('message', event => { | |||||||||||||||||||||||||||||||||||||||||||||||
Check warningCode scanning / CodeQL Missing origin verification in `postMessage` handler
Postmessage handler has no origin check.
Copilot AutofixAI about 1 year ago The fix involves verifying the
No new imports or dependencies are required to implement this fix.
Suggested changeset
1
dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker.ts
Copilot is powered by AI and may make mistakes. Always verify output.
Refresh and try again.
|
|||||||||||||||||||||||||||||||||||||||||||||||
| if (event.data.msg === 'TRIGGER_ERROR') { | |||||||||||||||||||||||||||||||||||||||||||||||
| // This will throw an uncaught error in the worker | |||||||||||||||||||||||||||||||||||||||||||||||
| throw new Error(`Uncaught error in worker`); | |||||||||||||||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||||||||||||||
| }); | |||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,17 @@ | ||||||||||||||||||||||||||||||||||||||||||||
| import * as Sentry from '@sentry/browser'; | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // type cast necessary because TS thinks this file is part of the main | ||||||||||||||||||||||||||||||||||||||||||||
| // thread where self is of type `Window` instead of `Worker` | ||||||||||||||||||||||||||||||||||||||||||||
| Sentry.registerWebWorker({ self: self as unknown as Worker }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| // Let the main thread know the worker is ready | ||||||||||||||||||||||||||||||||||||||||||||
| self.postMessage({ | ||||||||||||||||||||||||||||||||||||||||||||
| msg: 'WORKER_2_READY', | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
| self.addEventListener('message', event => { | ||||||||||||||||||||||||||||||||||||||||||||
Check warningCode scanning / CodeQL Missing origin verification in `postMessage` handler
Postmessage handler has no origin check.
Copilot AutofixAI about 1 year ago To fix the issue, the
The fix will involve modifying the
Suggested changeset
1
dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker2.ts
Copilot is powered by AI and may make mistakes. Always verify output.
Refresh and try again.
|
||||||||||||||||||||||||||||||||||||||||||||
| if (event.data.msg === 'TRIGGER_ERROR') { | ||||||||||||||||||||||||||||||||||||||||||||
| // This will throw an uncaught error in the worker | ||||||||||||||||||||||||||||||||||||||||||||
| throw new Error(`Uncaught error in worker 2`); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | |||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,17 @@ | |||||||||||||||||||||||||||||
| import * as Sentry from '@sentry/browser'; | |||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||
| // type cast necessary because TS thinks this file is part of the main | |||||||||||||||||||||||||||||
| // thread where self is of type `Window` instead of `Worker` | |||||||||||||||||||||||||||||
| Sentry.registerWebWorker({ self: self as unknown as Worker }); | |||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||
| // Let the main thread know the worker is ready | |||||||||||||||||||||||||||||
| self.postMessage({ | |||||||||||||||||||||||||||||
| msg: 'WORKER_3_READY', | |||||||||||||||||||||||||||||
| }); | |||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||
| self.addEventListener('message', event => { | |||||||||||||||||||||||||||||
Check warningCode scanning / CodeQL Missing origin verification in `postMessage` handler
Postmessage handler has no origin check.
Copilot AutofixAI about 1 year ago To fix the issue, we need to validate the source of the incoming message in the Web Worker. Since the
Suggested changeset
1
dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker3.ts
Copilot is powered by AI and may make mistakes. Always verify output.
Refresh and try again.
|
|||||||||||||||||||||||||||||
| if (event.data.msg === 'TRIGGER_ERROR') { | |||||||||||||||||||||||||||||
| // This will throw an uncaught error in the worker | |||||||||||||||||||||||||||||
| throw new Error(`Uncaught error in worker 3`); | |||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||
| }); | |||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { startEventProxyServer } from '@sentry-internal/test-utils'; | ||
|
|
||
| startEventProxyServer({ | ||
| port: 3031, | ||
| proxyServerName: 'browser-webworker-vite', | ||
| }); |
Check warning
Code scanning / CodeQL
Missing origin verification in `postMessage` handler
Copilot Autofix
AI about 1 year ago
To fix the issue, we need to ensure that the origin of incoming messages is checked before processing them. In the context of web workers, the
event.originproperty is not available. Instead, we should verify the source of the message viaevent.sourceor other data provided in the message, such as custom identifiers or tokens.In this case, we can use a simple check to ensure that the incoming message contains a specific trusted property (e.g.,
_sentryMessage) or other predefined criteria. If the criteria are not met, the message should be ignored.