From 6c88e3b8e71408e095446a4e956e4cb087066041 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 13 Jan 2022 14:52:48 +0100 Subject: [PATCH] fix: optional postMessage transfer arg (#32433) --- shell/browser/api/electron_api_web_frame_main.cc | 2 +- shell/renderer/api/electron_api_ipc_renderer.cc | 2 +- spec-main/api-ipc-spec.ts | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/shell/browser/api/electron_api_web_frame_main.cc b/shell/browser/api/electron_api_web_frame_main.cc index c5a5945ba0a2..e7ba32a33f78 100644 --- a/shell/browser/api/electron_api_web_frame_main.cc +++ b/shell/browser/api/electron_api_web_frame_main.cc @@ -210,7 +210,7 @@ void WebFrameMain::PostMessage(v8::Isolate* isolate, } std::vector> wrapped_ports; - if (transfer) { + if (transfer && !transfer.value()->IsUndefined()) { if (!gin::ConvertFromV8(isolate, *transfer, &wrapped_ports)) { isolate->ThrowException(v8::Exception::Error( gin::StringToV8(isolate, "Invalid value for transfer"))); diff --git a/shell/renderer/api/electron_api_ipc_renderer.cc b/shell/renderer/api/electron_api_ipc_renderer.cc index 8f983fd0f535..74bf0e1dccc0 100644 --- a/shell/renderer/api/electron_api_ipc_renderer.cc +++ b/shell/renderer/api/electron_api_ipc_renderer.cc @@ -146,7 +146,7 @@ class IPCRenderer : public gin::Wrappable, } std::vector> transferables; - if (transfer) { + if (transfer && !transfer.value()->IsUndefined()) { if (!gin::ConvertFromV8(isolate, *transfer, &transferables)) { thrower.ThrowTypeError("Invalid value for transfer"); return; diff --git a/spec-main/api-ipc-spec.ts b/spec-main/api-ipc-spec.ts index 5b3963cc4a55..c25a01d320dc 100644 --- a/spec-main/api-ipc-spec.ts +++ b/spec-main/api-ipc-spec.ts @@ -216,6 +216,18 @@ describe('ipc module', () => { expect(port).to.be.an.instanceOf(EventEmitter); }); + it('can sent a message without a transfer', async () => { + const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } }); + w.loadURL('about:blank'); + const p = emittedOnce(ipcMain, 'port'); + await w.webContents.executeJavaScript(`(${function () { + require('electron').ipcRenderer.postMessage('port', 'hi'); + }})()`); + const [ev, msg] = await p; + expect(msg).to.equal('hi'); + expect(ev.ports).to.deep.equal([]); + }); + it('can communicate between main and renderer', async () => { const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false } }); w.loadURL('about:blank');