Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 => {

Check warning

Code scanning / CodeQL

Missing origin verification in `postMessage` handler

Postmessage handler has no origin check.

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.origin property is not available. Instead, we should verify the source of the message via event.source or 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.


Suggested changeset 1
dev-packages/browser-integration-tests/suites/integrations/webWorker/assets/worker.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/dev-packages/browser-integration-tests/suites/integrations/webWorker/assets/worker.js b/dev-packages/browser-integration-tests/suites/integrations/webWorker/assets/worker.js
--- a/dev-packages/browser-integration-tests/suites/integrations/webWorker/assets/worker.js
+++ b/dev-packages/browser-integration-tests/suites/integrations/webWorker/assets/worker.js
@@ -8,7 +8,10 @@
 });
 
 self.addEventListener('message', event => {
-  if (event.data.type === 'throw-error') {
-    throw new Error('Worker error for testing');
+  // Verify that the message is from a trusted source
+  if (event.data && event.data._sentryMessage) {
+    if (event.data.type === 'throw-error') {
+      throw new Error('Worker error for testing');
+    }
   }
 });
EOF
@@ -8,7 +8,10 @@
});

self.addEventListener('message', event => {
if (event.data.type === 'throw-error') {
throw new Error('Worker error for testing');
// Verify that the message is from a trusted source
if (event.data && event.data._sentryMessage) {
if (event.data.type === 'throw-error') {
throw new Error('Worker error for testing');
}
}
});
Copilot is powered by AI and may make mistakes. Always verify output.
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');
});
});
2 changes: 1 addition & 1 deletion dev-packages/e2e-tests/lib/copyToTemp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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') {
Expand Down
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 warning

Code scanning / CodeQL

Missing origin verification in `postMessage` handler

Postmessage handler has no origin check.

Copilot Autofix

AI about 1 year ago

The fix involves verifying the origin of incoming messages in the message event handler before processing them. This ensures that only messages from trusted sources are acted upon. Specifically:

  1. Identify the trusted origin (e.g., 'https://www.example.com').
  2. Add a conditional check in the message event handler to validate the origin of incoming messages against this trusted value.
  3. Only proceed with processing the message if the origin matches the trusted value.

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

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker.ts b/dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker.ts
--- a/dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker.ts
+++ b/dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker.ts
@@ -10,8 +10,11 @@
 });
 
 self.addEventListener('message', event => {
-  if (event.data.msg === 'TRIGGER_ERROR') {
-    // This will throw an uncaught error in the worker
-    throw new Error(`Uncaught error in worker`);
+  const trustedOrigin = 'https://www.example.com'; // Define the trusted origin
+  if (event.origin === trustedOrigin) {
+    if (event.data.msg === 'TRIGGER_ERROR') {
+      // This will throw an uncaught error in the worker
+      throw new Error(`Uncaught error in worker`);
+    }
   }
 });
EOF
@@ -10,8 +10,11 @@
});

self.addEventListener('message', event => {
if (event.data.msg === 'TRIGGER_ERROR') {
// This will throw an uncaught error in the worker
throw new Error(`Uncaught error in worker`);
const trustedOrigin = 'https://www.example.com'; // Define the trusted origin
if (event.origin === trustedOrigin) {
if (event.data.msg === 'TRIGGER_ERROR') {
// This will throw an uncaught error in the worker
throw new Error(`Uncaught error in worker`);
}
}
});
Copilot is powered by AI and may make mistakes. Always verify output.
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 warning

Code scanning / CodeQL

Missing origin verification in `postMessage` handler

Postmessage handler has no origin check.

Copilot Autofix

AI about 1 year ago

To fix the issue, the postMessage handler should verify the origin of the incoming message to ensure it comes from a trusted source. This involves:

  1. Adding a conditional check for event.origin against a predefined trusted origin (e.g., 'https://trusted-origin.com').
  2. Ensuring that only messages from the trusted origin are processed, while others are ignored.

The fix will involve modifying the self.addEventListener('message', ...) block to include the origin verification logic. No additional imports or dependencies are required.


Suggested changeset 1
dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker2.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker2.ts b/dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker2.ts
--- a/dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker2.ts
+++ b/dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker2.ts
@@ -12,5 +12,10 @@
 self.addEventListener('message', event => {
-  if (event.data.msg === 'TRIGGER_ERROR') {
-    // This will throw an uncaught error in the worker
-    throw new Error(`Uncaught error in worker 2`);
+  const trustedOrigin = 'https://trusted-origin.com';
+  if (event.origin === trustedOrigin) {
+    if (event.data.msg === 'TRIGGER_ERROR') {
+      // This will throw an uncaught error in the worker
+      throw new Error(`Uncaught error in worker 2`);
+    }
+  } else {
+    console.warn(`Message received from untrusted origin: ${event.origin}`);
   }
EOF
@@ -12,5 +12,10 @@
self.addEventListener('message', event => {
if (event.data.msg === 'TRIGGER_ERROR') {
// This will throw an uncaught error in the worker
throw new Error(`Uncaught error in worker 2`);
const trustedOrigin = 'https://trusted-origin.com';
if (event.origin === trustedOrigin) {
if (event.data.msg === 'TRIGGER_ERROR') {
// This will throw an uncaught error in the worker
throw new Error(`Uncaught error in worker 2`);
}
} else {
console.warn(`Message received from untrusted origin: ${event.origin}`);
}
Copilot is powered by AI and may make mistakes. Always verify output.
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 warning

Code scanning / CodeQL

Missing origin verification in `postMessage` handler

Postmessage handler has no origin check.

Copilot Autofix

AI about 1 year ago

To fix the issue, we need to validate the source of the incoming message in the Web Worker. Since the origin property is not available in Web Workers, we can use a custom property in the event.data object (e.g., source) to identify trusted messages. The main thread should include this property when sending messages to the worker, and the worker should verify it before processing the message.

  1. Modify the self.addEventListener('message', ...) handler to include a check for a trusted source property in the event.data object.
  2. Ensure that only messages with the expected source value are processed.

Suggested changeset 1
dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker3.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker3.ts b/dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker3.ts
--- a/dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker3.ts
+++ b/dev-packages/e2e-tests/test-applications/browser-webworker-vite/src/worker3.ts
@@ -12,2 +12,8 @@
 self.addEventListener('message', event => {
+  // Verify the source of the message
+  if (event.data.source !== 'trusted-main-thread') {
+    console.warn('Untrusted message source:', event.data.source);
+    return;
+  }
+
   if (event.data.msg === 'TRIGGER_ERROR') {
EOF
@@ -12,2 +12,8 @@
self.addEventListener('message', event => {
// Verify the source of the message
if (event.data.source !== 'trusted-main-thread') {
console.warn('Untrusted message source:', event.data.source);
return;
}

if (event.data.msg === 'TRIGGER_ERROR') {
Copilot is powered by AI and may make mistakes. Always verify output.
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',
});
Loading