From 56a8eb3a946c2855aef425575cba5391ea8c1e42 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 24 Jan 2017 15:05:34 -0800 Subject: [PATCH] Check that sender is a WebContents instance --- lib/browser/rpc-server.js | 6 ++++-- spec/api-ipc-spec.js | 10 ++++++---- spec/static/main.js | 14 ++++++++------ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/browser/rpc-server.js b/lib/browser/rpc-server.js index 1277e51ff439..1dac51645926 100644 --- a/lib/browser/rpc-server.js +++ b/lib/browser/rpc-server.js @@ -3,6 +3,8 @@ const {Buffer} = require('buffer') const electron = require('electron') const v8Util = process.atomBinding('v8_util') +const {WebContents} = process.atomBinding('web_contents') + const {ipcMain, isPromise, webContents} = electron const fs = require('fs') @@ -150,8 +152,8 @@ const removeRemoteListenersAndLogWarning = (meta, args, callIntoRenderer) => { let message = `Attempting to call a function in a renderer window that has been closed or released.` + `\nFunction provided here: ${meta.location}` - if (args.length > 0 && args[0].sender && args[0].sender.eventNames) { - const sender = args[0].sender + if (args.length > 0 && (args[0].sender instanceof WebContents)) { + const {sender} = args[0] const remoteEvents = sender.eventNames().filter((eventName) => { return sender.listeners(eventName).includes(callIntoRenderer) }) diff --git a/spec/api-ipc-spec.js b/spec/api-ipc-spec.js index 32cc25041c52..b1ca29c6a158 100644 --- a/spec/api-ipc-spec.js +++ b/spec/api-ipc-spec.js @@ -506,10 +506,12 @@ describe('ipc module', function () { 'Function provided here: remote-event-handler.html:11:33', 'Remote event names: remote-handler, other-remote-handler' ].join('\n') - const {warningMessage, listenerCountBefore, listenerCountAfter} = - ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler') - assert.equal(warningMessage, expectedMessage) - assert.equal(listenerCountAfter, listenerCountBefore - 1) + const results = ipcRenderer.sendSync('try-emit-web-contents-event', w.webContents.id, 'remote-handler') + assert.deepEqual(results, { + warningMessage: expectedMessage, + listenerCountBefore: 2, + listenerCountAfter: 1 + }) done() }) w.webContents.reload() diff --git a/spec/static/main.js b/spec/static/main.js index 50fe5352eb5f..ab16e19c9e3c 100644 --- a/spec/static/main.js +++ b/spec/static/main.js @@ -253,13 +253,16 @@ ipcMain.on('prevent-next-new-window', (event, id) => { ipcMain.on('try-emit-web-contents-event', (event, id, eventName) => { const consoleWarn = console.warn let warningMessage = null - console.warn = (message) => { - warningMessage = message - } - const contents = webContents.fromId(id) const listenerCountBefore = contents.listenerCount(eventName) - contents.emit(eventName, {sender: contents}) + + try { + console.warn = (message) => warningMessage = message + contents.emit(eventName, {sender: contents}) + } finally { + console.warn = consoleWarn + } + const listenerCountAfter = contents.listenerCount(eventName) event.returnValue = { @@ -267,5 +270,4 @@ ipcMain.on('try-emit-web-contents-event', (event, id, eventName) => { listenerCountBefore, listenerCountAfter } - console.warn = consoleWarn })