Skip to content
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

fix: Patch registerSchemesAsPrivileged so sentry scheme isn't overwritten #787

Merged
merged 2 commits into from
Nov 30, 2023
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
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
Loading