Skip to content

Commit

Permalink
fix: emit IPC event in correct context if isolation and sandbox enabl…
Browse files Browse the repository at this point in the history
…ed (#16352)

* 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
  • Loading branch information
aegarbutt authored and miniak committed Jun 6, 2019
1 parent 93655d6 commit bb72bec
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
4 changes: 3 additions & 1 deletion atom/renderer/atom_sandboxed_renderer_client.cc
Expand Up @@ -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<v8::Value> argv[] = {mate::ConvertToV8(isolate, channel),
mate::ConvertToV8(isolate, args)};
renderer_client_->InvokeIpcCallback(
Expand Down
35 changes: 25 additions & 10 deletions spec/api-ipc-renderer-spec.js
Expand Up @@ -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', () => {
Expand Down
2 changes: 0 additions & 2 deletions spec/fixtures/module/preload-inject-ipc.js

This file was deleted.

5 changes: 5 additions & 0 deletions 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)
})
11 changes: 0 additions & 11 deletions spec/fixtures/pages/ping-pong.html

This file was deleted.

0 comments on commit bb72bec

Please sign in to comment.