Skip to content

Commit

Permalink
fix: Patch registerSchemesAsPrivileged so sentry scheme isn't overw…
Browse files Browse the repository at this point in the history
…ritten (#787)
  • Loading branch information
timfish authored Nov 30, 2023
1 parent c6f8118 commit c098afa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main/anr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function isAnrChildProcess(): boolean {
}

/** Creates a renderer ANR status hook */
export function createRendererAnrStatusHook(): (status: RendererStatus, contents: WebContents) => void {
export function createRendererAnrStatusHandler(): (status: RendererStatus, contents: WebContents) => void {
function log(message: string, ...args: unknown[]): void {
logger.log(`[Renderer ANR] ${message}`, ...args);
}
Expand Down
24 changes: 16 additions & 8 deletions src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,18 @@ import {
PROTOCOL_SCHEME,
RendererStatus,
} from '../common';
import { createRendererAnrStatusHook } from './anr';
import { createRendererAnrStatusHandler } from './anr';
import { registerProtocol, supportsFullProtocol, whenAppReady } from './electron-normalize';
import { ElectronMainOptionsInternal } from './sdk';

let KNOWN_RENDERERS: Set<number> | undefined;
let WINDOW_ID_TO_WEB_CONTENTS: Map<string, number> | undefined;

const SENTRY_CUSTOM_SCHEME = {
scheme: PROTOCOL_SCHEME,
privileges: { bypassCSP: true, corsEnabled: true, supportFetchAPI: true, secure: true },
};

async function newProtocolRenderer(): Promise<void> {
KNOWN_RENDERERS = KNOWN_RENDERERS || new Set();
WINDOW_ID_TO_WEB_CONTENTS = WINDOW_ID_TO_WEB_CONTENTS || new Map();
Expand Down Expand Up @@ -169,14 +174,17 @@ function configureProtocol(options: ElectronMainOptionsInternal): void {
throw new SentryError("Sentry SDK should be initialized before the Electron app 'ready' event is fired");
}

protocol.registerSchemesAsPrivileged([
{
scheme: PROTOCOL_SCHEME,
privileges: { bypassCSP: true, corsEnabled: true, supportFetchAPI: true, secure: true },
protocol.registerSchemesAsPrivileged([SENTRY_CUSTOM_SCHEME]);

// We Proxy this function so that later user calls to registerSchemesAsPrivileged don't overwrite our custom scheme
// eslint-disable-next-line @typescript-eslint/unbound-method
protocol.registerSchemesAsPrivileged = new Proxy(protocol.registerSchemesAsPrivileged, {
apply: (target, __, args: Parameters<typeof protocol.registerSchemesAsPrivileged>) => {
target([...args[0], SENTRY_CUSTOM_SCHEME]);
},
]);
});

const rendererStatusChanged = createRendererAnrStatusHook();
const rendererStatusChanged = createRendererAnrStatusHandler();

whenAppReady
.then(() => {
Expand Down Expand Up @@ -231,7 +239,7 @@ function configureClassic(options: ElectronMainOptionsInternal): void {
ipcMain.on(IPCChannel.SCOPE, (_, jsonScope: string) => handleScope(options, jsonScope));
ipcMain.on(IPCChannel.ENVELOPE, ({ sender }, env: Uint8Array | string) => handleEnvelope(options, env, sender));

const rendererStatusChanged = createRendererAnrStatusHook();
const rendererStatusChanged = createRendererAnrStatusHandler();
ipcMain.on(IPCChannel.STATUS, ({ sender }, status: RendererStatus) => rendererStatusChanged(status, sender));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');

const { app, BrowserWindow } = require('electron');
const { app, BrowserWindow, protocol } = require('electron');
const { init, IPCMode } = require('@sentry/electron');

init({
Expand All @@ -11,6 +11,18 @@ init({
onFatalError: () => {},
});

// Since we patch registerSchemesAsPrivileged, this should not overwrite the sentry scheme
protocol.registerSchemesAsPrivileged([
{
scheme: 'custom1',
privileges: {
standard: true,
secure: true,
supportFetchAPI: true,
},
},
]);

app.on('ready', () => {
const mainWindow = new BrowserWindow({
show: false,
Expand Down

0 comments on commit c098afa

Please sign in to comment.