fix: emit IPC event in correct context if isolation and sandbox enabled (#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
This commit is contained in:
Alex Garbutt 2019-01-12 05:19:20 -08:00 committed by Alexey Kuzmin
parent 134792a594
commit dcb670fa46
5 changed files with 58 additions and 70 deletions

View file

@ -111,8 +111,10 @@ class AtomSandboxedRenderFrameObserver : public AtomRenderFrameObserver {
auto* isolate = blink::MainThreadIsolate(); auto* isolate = blink::MainThreadIsolate();
v8::HandleScope handle_scope(isolate); v8::HandleScope handle_scope(isolate);
auto context = frame->MainWorldScriptContext();
auto context = renderer_client_->GetContext(frame, isolate);
v8::Context::Scope context_scope(context); v8::Context::Scope context_scope(context);
v8::Local<v8::Value> argv[] = {mate::ConvertToV8(isolate, channel), v8::Local<v8::Value> argv[] = {mate::ConvertToV8(isolate, channel),
mate::ConvertToV8(isolate, args), mate::ConvertToV8(isolate, args),
mate::ConvertToV8(isolate, sender_id)}; mate::ConvertToV8(isolate, sender_id)};

View file

@ -138,63 +138,54 @@ describe('ipc renderer module', () => {
contents = null contents = null
}) })
it('sends message to WebContents', done => { const generateSpecs = (description, webPreferences) => {
contents = webContents.create({ describe(description, () => {
preload: path.join(fixtures, 'module', 'preload-inject-ipc.js') it('sends message to WebContents', done => {
contents = webContents.create({
preload: path.join(fixtures, 'module', 'preload-ipc-ping-pong.js'),
...webPreferences
})
const payload = 'Hello World!'
ipcRenderer.once('pong', (event, data) => {
expect(payload).to.equal(data)
done()
})
contents.once('did-finish-load', () => {
ipcRenderer.sendTo(contents.id, 'ping', payload)
})
contents.loadFile(path.join(fixtures, 'pages', 'base-page.html'))
})
it('sends message to WebContents (channel has special chars)', done => {
contents = webContents.create({
preload: path.join(fixtures, 'module', 'preload-ipc-ping-pong.js'),
...webPreferences
})
const payload = 'Hello World!'
ipcRenderer.once('pong-æøåü', (event, data) => {
expect(payload).to.equal(data)
done()
})
contents.once('did-finish-load', () => {
ipcRenderer.sendTo(contents.id, 'ping-æøåü', payload)
})
contents.loadFile(path.join(fixtures, 'pages', 'base-page.html'))
})
}) })
}
const payload = 'Hello World!' generateSpecs('without sandbox', {})
generateSpecs('with sandbox', { sandbox: true })
ipcRenderer.once('pong', (event, data) => { generateSpecs('with contextIsolation', { contextIsolation: true })
expect(payload).to.equal(data) generateSpecs('with contextIsolation + sandbox', { contextIsolation: true, sandbox: true })
done()
})
contents.once('did-finish-load', () => {
ipcRenderer.sendTo(contents.id, 'ping', payload)
})
contents.loadFile(path.join(fixtures, 'pages', 'ping-pong.html'))
})
it('sends message to WebContents (sanboxed renderer)', done => {
contents = webContents.create({
preload: path.join(fixtures, 'module', 'preload-inject-ipc.js'),
sandbox: true
})
const payload = 'Hello World!'
ipcRenderer.once('pong', (event, data) => {
expect(payload).to.equal(data)
done()
})
contents.once('did-finish-load', () => {
ipcRenderer.sendTo(contents.id, 'ping', payload)
})
contents.loadFile(path.join(fixtures, 'pages', 'ping-pong.html'))
})
it('sends message to WebContents (channel has special chars)', done => {
contents = webContents.create({
preload: path.join(fixtures, 'module', 'preload-inject-ipc.js')
})
const payload = 'Hello World!'
ipcRenderer.once('pong-æøåü', (event, data) => {
expect(payload).to.equal(data)
done()
})
contents.once('did-finish-load', () => {
ipcRenderer.sendTo(contents.id, 'ping-æøåü', payload)
})
contents.loadFile(path.join(fixtures, 'pages', 'ping-pong.html'))
})
}) })
describe('remote listeners', () => { describe('remote listeners', () => {

View file

@ -1,2 +0,0 @@
const { ipcRenderer } = require('electron')
window.ipcRenderer = ipcRenderer

View file

@ -0,0 +1,9 @@
const { ipcRenderer } = require('electron')
ipcRenderer.on('ping', function (event, payload) {
ipcRenderer.sendTo(event.senderId, 'pong', payload)
})
ipcRenderer.on('ping-æøåü', function (event, payload) {
ipcRenderer.sendTo(event.senderId, 'pong-æøåü', payload)
})

View file

@ -1,12 +0,0 @@
<html>
<body>
<script type="text/javascript" charset="utf-8">
ipcRenderer.on('ping', function (event, payload) {
ipcRenderer.sendTo(event.senderId, 'pong', payload)
})
ipcRenderer.on('ping-æøåü', function (event, payload) {
ipcRenderer.sendTo(event.senderId, 'pong-æøåü', payload)
})
</script>
</body>
</html>