Skip to content

Commit 6e374a0

Browse files
committed
Limit IPC calls to our trusted origin for defense in depth
As noted in the comment: this shouldn't ever matter, but if somehow an untrusted page does get loaded into the app somehow, this ensures it can't directly access the IPC APIs (to get the auth token, for example, or do other weird things that users might trust incorrectly).
1 parent 5ae032c commit 6e374a0

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/index.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -601,14 +601,27 @@ if (!amMainInstance) {
601601
});
602602
}
603603

604-
ipcMain.handle('select-application', () => {
604+
// Restrict calls to IPC handler to our trusted host. This shouldn't be required (we don't allow loading
605+
// 3rd party sites) but it's good practice for defense-in-depth etc.
606+
const ipcHandler = <A, R>(fn: (...args: A[]) => R) => (
607+
event: Electron.IpcMainInvokeEvent,
608+
...args: A[]
609+
): R => {
610+
if (!hasTrustedOrigin(new URL(event.senderFrame.url))) {
611+
throw new Error(`Invalid IPC sender URL: ${event.senderFrame.url}`);
612+
} else {
613+
return fn(...args);
614+
}
615+
};
616+
617+
ipcMain.handle('select-application', ipcHandler(() => {
605618
return dialog.showOpenDialogSync({
606619
properties:
607620
process.platform === 'darwin'
608621
? ['openFile', 'openDirectory', 'treatPackageAsDirectory']
609622
: ['openFile'],
610623
})?.[0];
611-
});
624+
}));
612625

613626
// Enable the default context menu
614627
registerContextMenu({
@@ -617,9 +630,9 @@ registerContextMenu({
617630
});
618631

619632
// Enable custom context menus, for special cases where the UI wants to define the options available
620-
ipcMain.handle('open-context-menu', (_event: {}, options: ContextMenuDefinition) =>
633+
ipcMain.handle('open-context-menu', ipcHandler((options: ContextMenuDefinition) =>
621634
openContextMenu(options)
622-
);
635+
));
623636

624-
ipcMain.handle('get-desktop-version', () => DESKTOP_VERSION);
625-
ipcMain.handle('get-server-auth-token', () => AUTH_TOKEN);
637+
ipcMain.handle('get-desktop-version', ipcHandler(() => DESKTOP_VERSION));
638+
ipcMain.handle('get-server-auth-token', ipcHandler(() => AUTH_TOKEN));

0 commit comments

Comments
 (0)