From 516233c0daf0ac72f627d580a07dfa04e527b25c Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Thu, 6 Jun 2019 19:11:41 +0200 Subject: [PATCH] fix: emit IPC event in correct context if isolation and sandbox enabled (#16352) (#18668) * fix: emit IPC event in correct context if isolation and sandbox enabled IPC events were not being delivered to renderer processes when both `contextIsolation` and `sandbox` were enabled. This is because the `AtomSandboxedRenderFrameObserver` class was incorrectly using the `MainWorldScriptContext`, rather than conditionally selecting the context based on if isolation was enabled. Fixes #11922 --- .../atom_sandboxed_renderer_client.cc | 4 ++- spec/api-ipc-renderer-spec.js | 35 +++++++++++++------ spec/fixtures/module/preload-inject-ipc.js | 2 -- spec/fixtures/module/preload-ipc-ping-pong.js | 5 +++ spec/fixtures/pages/ping-pong.html | 11 ------ 5 files changed, 33 insertions(+), 24 deletions(-) delete mode 100644 spec/fixtures/module/preload-inject-ipc.js create mode 100644 spec/fixtures/module/preload-ipc-ping-pong.js delete mode 100644 spec/fixtures/pages/ping-pong.html diff --git a/atom/renderer/atom_sandboxed_renderer_client.cc b/atom/renderer/atom_sandboxed_renderer_client.cc index ae2412fd73974..9784cb1ed7f85 100644 --- a/atom/renderer/atom_sandboxed_renderer_client.cc +++ b/atom/renderer/atom_sandboxed_renderer_client.cc @@ -125,8 +125,10 @@ class AtomSandboxedRenderFrameObserver : public AtomRenderFrameObserver { auto* isolate = blink::MainThreadIsolate(); v8::HandleScope handle_scope(isolate); - auto context = frame->MainWorldScriptContext(); + + auto context = renderer_client_->GetContext(frame, isolate); v8::Context::Scope context_scope(context); + v8::Local argv[] = {mate::ConvertToV8(isolate, channel), mate::ConvertToV8(isolate, args)}; renderer_client_->InvokeIpcCallback( diff --git a/spec/api-ipc-renderer-spec.js b/spec/api-ipc-renderer-spec.js index 748851ec1c0a7..411c560c20233 100644 --- a/spec/api-ipc-renderer-spec.js +++ b/spec/api-ipc-renderer-spec.js @@ -140,20 +140,35 @@ describe('ipc renderer module', () => { contents = null }) - it('sends message to WebContents', done => { - const webContentsId = remote.getCurrentWebContents().id + const generateSpecs = (description, webPreferences) => { + describe(description, () => { + it('sends message to WebContents', done => { + contents = webContents.create({ + preload: path.join(fixtures, 'module', 'preload-ipc-ping-pong.js'), + ...webPreferences + }) - ipcRenderer.once('pong', (event, id) => { - expect(webContentsId).to.equal(id) - done() - }) + const payload = 'Hello World!' + const webContentsId = remote.getCurrentWebContents().id + + ipcRenderer.once('pong', (event, data) => { + expect(payload).to.equal(data) + done() + }) - contents.once('did-finish-load', () => { - ipcRenderer.sendTo(contents.id, 'ping', webContentsId) + contents.once('did-finish-load', () => { + ipcRenderer.sendTo(contents.id, 'ping', webContentsId, payload) + }) + + contents.loadFile(path.join(fixtures, 'pages', 'base-page.html')) + }) }) + } - contents.loadURL(`file://${path.join(fixtures, 'pages', 'ping-pong.html')}`) - }) + generateSpecs('without sandbox', {}) + generateSpecs('with sandbox', { sandbox: true }) + generateSpecs('with contextIsolation', { contextIsolation: true }) + generateSpecs('with contextIsolation + sandbox', { contextIsolation: true, sandbox: true }) }) describe('remote listeners', () => { diff --git a/spec/fixtures/module/preload-inject-ipc.js b/spec/fixtures/module/preload-inject-ipc.js deleted file mode 100644 index 3475cd69a636d..0000000000000 --- a/spec/fixtures/module/preload-inject-ipc.js +++ /dev/null @@ -1,2 +0,0 @@ -const {ipcRenderer} = require('electron') -window.ipcRenderer = ipcRenderer diff --git a/spec/fixtures/module/preload-ipc-ping-pong.js b/spec/fixtures/module/preload-ipc-ping-pong.js new file mode 100644 index 0000000000000..d1ee12cc13623 --- /dev/null +++ b/spec/fixtures/module/preload-ipc-ping-pong.js @@ -0,0 +1,5 @@ +const { ipcRenderer } = require('electron') + +ipcRenderer.on('ping', function (event, senderId, payload) { + ipcRenderer.sendTo(senderId, 'pong', payload) +}) diff --git a/spec/fixtures/pages/ping-pong.html b/spec/fixtures/pages/ping-pong.html deleted file mode 100644 index d10e7898653b1..0000000000000 --- a/spec/fixtures/pages/ping-pong.html +++ /dev/null @@ -1,11 +0,0 @@ - - - - - -