Skip to content

Commit 406c802

Browse files
authored
fix: Preload contextIsolation issues (#1244)
1 parent 5105fd1 commit 406c802

8 files changed

Lines changed: 47 additions & 52 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
## 7.1.1
66

77
- fix: Preload injection path (#1243)
8+
- fix: Preload `contextIsolation` issues (#1244)
89

910
## 7.1.0
1011

src/preload/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ export function hookupIpc(namespace: string = 'sentry-ipc'): void {
3737
if (contextBridge) {
3838
// This will fail if contextIsolation is not enabled
3939
try {
40-
contextBridge.exposeInMainWorld(ipcUtil.namespace, ipcObject);
40+
// eslint-disable-next-line no-restricted-globals
41+
contextBridge.exposeInMainWorld('__SENTRY_IPC__', window.__SENTRY_IPC__);
4142
} catch (e) {
4243
//
4344
}

src/renderer/ipc.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import { ElectronRendererOptionsInternal } from './sdk.js';
88
function getImplementation(ipcKey: string): IPCInterface {
99
const ipcUtil = ipcChannelUtils(ipcKey);
1010

11-
window.__SENTRY_IPC__ = window.__SENTRY_IPC__ || {};
12-
1311
// Favour IPC if it's been exposed by a preload script
14-
if (window.__SENTRY_IPC__[ipcUtil.namespace]) {
12+
if (window.__SENTRY_IPC__?.[ipcUtil.namespace]) {
1513
return window.__SENTRY_IPC__[ipcUtil.namespace] as IPCInterface;
1614
} else {
1715
debug.log('IPC was not configured in preload script, falling back to custom protocol and fetch');
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// eslint-disable-next-line import/no-unresolved
2-
import { init } from '@sentry/electron/main';
3-
import { app, BrowserWindow } from 'electron';
4-
import * as path from 'path';
5-
import * as url from 'url';
2+
const { init, IPCMode } = require('@sentry/electron/main');
3+
const { app, BrowserWindow } = require('electron');
4+
const path = require('path');
5+
const url = require('url');
66

77
init({
88
dsn: '__DSN__',
99
debug: true,
10+
ipcMode: IPCMode.Classic,
1011
onFatalError: () => {},
1112
});
1213

@@ -22,13 +23,9 @@ app.on('ready', () => {
2223

2324
window.loadURL(
2425
url.format({
25-
pathname: path.join(__dirname, 'index.html'),
26+
pathname: path.join(__dirname, 'dist', 'index.html'),
2627
protocol: 'file:',
2728
slashes: true,
2829
}),
2930
);
30-
31-
setTimeout(() => {
32-
throw new Error('Some main error');
33-
}, 2000);
3431
});

test/e2e/test-apps/other/main-process-module/package.json renamed to test/e2e/test-apps/other/preload-injection/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"name": "webpack-main-process",
3-
"description": "Webpack app with error in main process",
2+
"name": "preload-injection",
3+
"description": "Preload injection",
44
"version": "1.0.0",
55
"scripts": {
66
"start": "electron .",
77
"build": "webpack"
88
},
9-
"main": "dist/main.js",
9+
"main": "main.js",
1010
"devDependencies": {
1111
"@sentry/webpack-plugin": "^2.2.0",
1212
"csp-html-webpack-plugin": "^5.1.0",

test/e2e/test-apps/other/main-process-module/src/renderer.js renamed to test/e2e/test-apps/other/preload-injection/renderer.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ init({
88
console.log('renderer logging');
99

1010
getCurrentScope().setUser({ id: 'abc-123' });
11+
12+
setTimeout(() => {
13+
throw new Error('Some renderer error');
14+
}, 2000);

test/e2e/test-apps/other/main-process-module/test.ts renamed to test/e2e/test-apps/other/preload-injection/test.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ electronTestRunner(__dirname, { skipEsmAutoTransform: true, skip: () => process.
55
await ctx
66
.expect({
77
envelope: eventEnvelope({
8-
level: 'fatal',
9-
platform: 'node',
8+
level: 'error',
9+
platform: 'javascript',
1010
debug_meta: {
1111
images: [
1212
{
13-
code_file: 'app:///dist/main.js',
13+
code_file: 'app:///dist/renderer.js',
1414
type: 'sourcemap',
1515
debug_id: UUID_V4_MATCHER,
1616
},
@@ -20,30 +20,36 @@ electronTestRunner(__dirname, { skipEsmAutoTransform: true, skip: () => process.
2020
values: [
2121
{
2222
type: 'Error',
23-
value: 'Some main error',
23+
value: 'Some renderer error',
2424
stacktrace: {
2525
frames: expect.arrayContaining([
2626
{
2727
colno: expect.any(Number),
28-
filename: 'app:///dist/main.js',
28+
filename: 'app:///dist/renderer.js',
2929
function: expect.any(String),
3030
in_app: true,
3131
lineno: expect.any(Number),
32-
module: 'dist:main',
3332
},
3433
]),
3534
},
3635
mechanism: {
3736
handled: false,
38-
type: 'generic',
37+
type: 'auto.browser.browserapierrors.setTimeout',
3938
},
4039
},
4140
],
4241
},
42+
request: {
43+
headers: {},
44+
url: 'app:///dist/index.html',
45+
},
46+
extra: {
47+
arguments: [],
48+
},
4349
tags: {
4450
'event.environment': 'javascript',
4551
'event.origin': 'electron',
46-
'event.process': 'browser',
52+
'event.process': 'renderer',
4753
},
4854
user: {
4955
id: 'abc-123',

test/e2e/test-apps/other/main-process-module/webpack.config.js renamed to test/e2e/test-apps/other/preload-injection/webpack.config.js

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,20 @@ const sentryWebpackPluginOptions = {
2020
},
2121
};
2222

23-
module.exports = [
24-
{
25-
mode: 'production',
26-
entry: './src/main.js',
27-
target: 'electron-main',
28-
output: {
29-
libraryTarget: 'commonjs2',
30-
filename: 'main.js',
31-
},
32-
plugins: [/* new WarningsToErrorsPlugin(), */ sentryWebpackPlugin(sentryWebpackPluginOptions)],
23+
module.exports = {
24+
mode: 'production',
25+
entry: './renderer.js',
26+
target: 'web',
27+
output: {
28+
filename: 'renderer.js',
3329
},
34-
{
35-
mode: 'production',
36-
entry: './src/renderer.js',
37-
target: 'web',
38-
output: {
39-
filename: 'renderer.js',
40-
},
41-
plugins: [
42-
new HtmlWebpackPlugin(),
43-
// new WarningsToErrorsPlugin(),
44-
new CspHtmlWebpackPlugin({
45-
'default-src': "'self'",
46-
'script-src': "'self'",
47-
}),
48-
sentryWebpackPlugin(sentryWebpackPluginOptions),
49-
],
50-
},
51-
];
30+
plugins: [
31+
new HtmlWebpackPlugin(),
32+
// new WarningsToErrorsPlugin(),
33+
new CspHtmlWebpackPlugin({
34+
'default-src': "'self'",
35+
'script-src': "'self'",
36+
}),
37+
sentryWebpackPlugin(sentryWebpackPluginOptions),
38+
],
39+
};

0 commit comments

Comments
 (0)