fix: potential exception when calling webFrameMainBinding.fromIdOrNull() (#35785)

* fix: potential exception when calling webFrameMainBinding.fromIdOrNull()

* replace try/catch in getWebFrameForEvent

Co-authored-by: Milan Burda <miburda@microsoft.com>
This commit is contained in:
Milan Burda 2022-09-24 21:19:07 +02:00 committed by GitHub
parent 324db14969
commit f62aab76b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View file

@ -537,6 +537,11 @@ const addReturnValueToEvent = (event: Electron.IpcMainEvent) => {
}); });
}; };
const getWebFrameForEvent = (event: Electron.IpcMainEvent | Electron.IpcMainInvokeEvent) => {
if (!event.processId || !event.frameId) return null;
return webFrameMainBinding.fromIdOrNull(event.processId, event.frameId);
};
const commandLine = process._linkedBinding('electron_common_command_line'); const commandLine = process._linkedBinding('electron_common_command_line');
const environment = process._linkedBinding('electron_common_environment'); const environment = process._linkedBinding('electron_common_environment');
@ -574,7 +579,7 @@ WebContents.prototype._init = function () {
} else { } else {
addReplyToEvent(event); addReplyToEvent(event);
this.emit('ipc-message', event, channel, ...args); this.emit('ipc-message', event, channel, ...args);
const maybeWebFrame = webFrameMainBinding.fromIdOrNull(event.processId, event.frameId); const maybeWebFrame = getWebFrameForEvent(event);
maybeWebFrame && maybeWebFrame.ipc.emit(channel, event, ...args); maybeWebFrame && maybeWebFrame.ipc.emit(channel, event, ...args);
ipc.emit(channel, event, ...args); ipc.emit(channel, event, ...args);
ipcMain.emit(channel, event, ...args); ipcMain.emit(channel, event, ...args);
@ -588,8 +593,8 @@ WebContents.prototype._init = function () {
console.error(`Error occurred in handler for '${channel}':`, error); console.error(`Error occurred in handler for '${channel}':`, error);
event.sendReply({ error: error.toString() }); event.sendReply({ error: error.toString() });
}; };
const maybeWebFrame = webFrameMainBinding.fromIdOrNull(event.processId, event.frameId); const maybeWebFrame = getWebFrameForEvent(event);
const targets: (ElectronInternal.IpcMainInternal| undefined)[] = internal ? [ipcMainInternal] : [maybeWebFrame && maybeWebFrame.ipc, ipc, ipcMain]; const targets: (ElectronInternal.IpcMainInternal| undefined)[] = internal ? [ipcMainInternal] : [maybeWebFrame?.ipc, ipc, ipcMain];
const target = targets.find(target => target && (target as any)._invokeHandlers.has(channel)); const target = targets.find(target => target && (target as any)._invokeHandlers.has(channel));
if (target) { if (target) {
(target as any)._invokeHandlers.get(channel)(event, ...args); (target as any)._invokeHandlers.get(channel)(event, ...args);
@ -605,7 +610,7 @@ WebContents.prototype._init = function () {
ipcMainInternal.emit(channel, event, ...args); ipcMainInternal.emit(channel, event, ...args);
} else { } else {
addReplyToEvent(event); addReplyToEvent(event);
const maybeWebFrame = webFrameMainBinding.fromIdOrNull(event.processId, event.frameId); const maybeWebFrame = getWebFrameForEvent(event);
if (this.listenerCount('ipc-message-sync') === 0 && ipc.listenerCount(channel) === 0 && ipcMain.listenerCount(channel) === 0 && (!maybeWebFrame || maybeWebFrame.ipc.listenerCount(channel) === 0)) { if (this.listenerCount('ipc-message-sync') === 0 && ipc.listenerCount(channel) === 0 && ipcMain.listenerCount(channel) === 0 && (!maybeWebFrame || maybeWebFrame.ipc.listenerCount(channel) === 0)) {
console.warn(`WebContents #${this.id} called ipcRenderer.sendSync() with '${channel}' channel without listeners.`); console.warn(`WebContents #${this.id} called ipcRenderer.sendSync() with '${channel}' channel without listeners.`);
} }
@ -619,7 +624,7 @@ WebContents.prototype._init = function () {
this.on('-ipc-ports' as any, function (event: Electron.IpcMainEvent, internal: boolean, channel: string, message: any, ports: any[]) { this.on('-ipc-ports' as any, function (event: Electron.IpcMainEvent, internal: boolean, channel: string, message: any, ports: any[]) {
addSenderFrameToEvent(event); addSenderFrameToEvent(event);
event.ports = ports.map(p => new MessagePortMain(p)); event.ports = ports.map(p => new MessagePortMain(p));
const maybeWebFrame = webFrameMainBinding.fromIdOrNull(event.processId, event.frameId); const maybeWebFrame = getWebFrameForEvent(event);
maybeWebFrame && maybeWebFrame.ipc.emit(channel, event, message); maybeWebFrame && maybeWebFrame.ipc.emit(channel, event, message);
ipc.emit(channel, event, message); ipc.emit(channel, event, message);
ipcMain.emit(channel, event, message); ipcMain.emit(channel, event, message);

View file

@ -237,7 +237,7 @@ declare namespace NodeJS {
_linkedBinding(name: 'electron_browser_web_frame_main'): { _linkedBinding(name: 'electron_browser_web_frame_main'): {
WebFrameMain: typeof Electron.WebFrameMain; WebFrameMain: typeof Electron.WebFrameMain;
fromId(processId: number, routingId: number): Electron.WebFrameMain; fromId(processId: number, routingId: number): Electron.WebFrameMain;
fromIdOrNull(processId: number, routingId: number): Electron.WebFrameMain; fromIdOrNull(processId: number, routingId: number): Electron.WebFrameMain | null;
} }
_linkedBinding(name: 'electron_renderer_crash_reporter'): Electron.CrashReporter; _linkedBinding(name: 'electron_renderer_crash_reporter'): Electron.CrashReporter;
_linkedBinding(name: 'electron_renderer_ipc'): { ipc: IpcRendererBinding }; _linkedBinding(name: 'electron_renderer_ipc'): { ipc: IpcRendererBinding };