chore: refactor context isolation spec (#14394)

* spec: refactor BrowserWindow module contextIsolation option

* spec: check for serialzation in isolated renderers separately
This commit is contained in:
Robo 2018-09-01 02:21:29 +05:30 committed by Samuel Attard
parent 3a79eacb6f
commit 32158ca5dd
5 changed files with 63 additions and 59 deletions

View file

@ -23,6 +23,7 @@ chai.use(dirtyChai)
describe('BrowserWindow module', () => {
const fixtures = path.resolve(__dirname, 'fixtures')
let w = null
let iw = null
let ws = null
let server
let postData
@ -3234,10 +3235,10 @@ describe('BrowserWindow module', () => {
typeofRequire: 'function',
typeofProcess: 'object',
typeofArrayPush: 'function',
typeofFunctionApply: 'function'
typeofFunctionApply: 'function',
typeofPreloadExecuteJavaScriptProperty: 'undefined'
},
pageContext: {
openedLocation: '',
preloadProperty: 'undefined',
pageProperty: 'string',
typeofRequire: 'undefined',
@ -3250,8 +3251,8 @@ describe('BrowserWindow module', () => {
}
beforeEach(() => {
if (w != null) w.destroy()
w = new BrowserWindow({
if (iw != null) iw.destroy()
iw = new BrowserWindow({
show: false,
webPreferences: {
contextIsolation: true,
@ -3270,49 +3271,62 @@ describe('BrowserWindow module', () => {
})
afterEach(() => {
if (iw != null) iw.destroy()
if (ws != null) ws.destroy()
})
it('separates the page context from the Electron/preload context', (done) => {
ipcMain.once('isolated-world', (event, data) => {
assert.deepEqual(data, expectedContextData)
done()
})
w.loadURL(`file://${fixtures}/api/isolated.html`)
it('separates the page context from the Electron/preload context', async () => {
iw.loadURL(`file://${fixtures}/api/isolated.html`)
const [, data] = await emittedOnce(ipcMain, 'isolated-world')
assert.deepEqual(data, expectedContextData)
})
it('recreates the contexts on reload', (done) => {
w.webContents.once('did-finish-load', () => {
ipcMain.once('isolated-world', (event, data) => {
assert.deepEqual(data, expectedContextData)
done()
})
w.webContents.reload()
})
w.loadURL(`file://${fixtures}/api/isolated.html`)
it('recreates the contexts on reload', async () => {
iw.loadURL(`file://${fixtures}/api/isolated.html`)
await emittedOnce(iw.webContents, 'did-finish-load')
iw.webContents.reload()
const [, data] = await emittedOnce(ipcMain, 'isolated-world')
assert.deepEqual(data, expectedContextData)
})
it('enables context isolation on child windows', (done) => {
app.once('browser-window-created', (event, window) => {
assert.equal(window.webContents.getLastWebPreferences().contextIsolation, true)
done()
})
w.loadURL(`file://${fixtures}/pages/window-open.html`)
it('enables context isolation on child windows', async () => {
iw.loadURL(`file://${fixtures}/pages/window-open.html`)
const [, window] = await emittedOnce(app, 'browser-window-created')
assert.ok(window.webContents.getLastWebPreferences().contextIsolation)
})
it('separates the page context from the Electron/preload context with sandbox on', (done) => {
ipcMain.once('isolated-sandbox-world', (event, data) => {
assert.deepEqual(data, expectedContextData)
done()
})
w.loadURL(`file://${fixtures}/api/isolated.html`)
it('separates the page context from the Electron/preload context with sandbox on', async () => {
ws.loadURL(`file://${fixtures}/api/isolated.html`)
const [, data] = await emittedOnce(ipcMain, 'isolated-world')
assert.deepEqual(data, expectedContextData)
})
it('recreates the contexts on reload with sandbox on', (done) => {
w.webContents.once('did-finish-load', () => {
ipcMain.once('isolated-sandbox-world', (event, data) => {
assert.deepEqual(data, expectedContextData)
done()
})
w.webContents.reload()
it('recreates the contexts on reload with sandbox on', async () => {
ws.loadURL(`file://${fixtures}/api/isolated.html`)
await emittedOnce(ws.webContents, 'did-finish-load')
ws.webContents.reload()
const [, data] = await emittedOnce(ipcMain, 'isolated-world')
assert.deepEqual(data, expectedContextData)
})
it('supports fetch api', async () => {
const fetchWindow = new BrowserWindow({
show: false,
webPreferences: {
contextIsolation: true,
preload: path.join(fixtures, 'api', 'isolated-fetch-preload.js')
}
})
w.loadURL(`file://${fixtures}/api/isolated.html`)
fetchWindow.loadURL('about:blank')
const [, error] = await emittedOnce(ipcMain, 'isolated-fetch-error')
fetchWindow.destroy()
assert.equal(error, 'Failed to fetch')
})
it('doesn\'t break ipc serialization', async () => {
iw.loadURL('about:blank')
iw.webContents.executeJavaScript(`
const opened = window.open()
openedLocation = opened.location
opened.close()
window.postMessage({openedLocation}, '*')
`)
const [, data] = await emittedOnce(ipcMain, 'isolated-world')
assert.equal(data.pageContext.openedLocation, '')
})
})

View file

@ -0,0 +1,6 @@
const {ipcRenderer} = require('electron')
// Ensure fetch works from isolated world origin
fetch('https://localhost:1234').catch(err => {
ipcRenderer.send('isolated-fetch-error', err.message)
})

View file

@ -1,7 +1,3 @@
// Ensure fetch works from isolated world origin
fetch('http://localhost:1234')
fetch('https://localhost:1234')
const {ipcRenderer, webFrame} = require('electron')
window.foo = 3
@ -16,18 +12,8 @@ window.addEventListener('message', (event) => {
typeofRequire: typeof require,
typeofProcess: typeof process,
typeofArrayPush: typeof Array.prototype.push,
typeofFunctionApply: typeof Function.prototype.apply
},
pageContext: event.data
})
ipcRenderer.send('isolated-sandbox-world', {
preloadContext: {
preloadProperty: typeof window.foo,
pageProperty: typeof window.hello,
typeofRequire: typeof require,
typeofProcess: typeof process,
typeofArrayPush: typeof Array.prototype.push,
typeofFunctionApply: typeof Function.prototype.apply
typeofFunctionApply: typeof Function.prototype.apply,
typeofPreloadExecuteJavaScriptProperty: typeof window.preloadExecuteJavaScriptProperty
},
pageContext: event.data
})

View file

@ -9,11 +9,9 @@
Function.prototype.apply = true
const opened = window.open()
const openedLocation = opened.location
opened.close()
window.postMessage({
openedLocation,
preloadProperty: typeof window.foo,
pageProperty: typeof window.hello,
typeofRequire: typeof require,

View file

@ -535,10 +535,10 @@ describe('<webview> tag', function () {
typeofRequire: 'function',
typeofProcess: 'object',
typeofArrayPush: 'function',
typeofFunctionApply: 'function'
typeofFunctionApply: 'function',
typeofPreloadExecuteJavaScriptProperty: 'undefined'
},
pageContext: {
openedLocation: '',
preloadProperty: 'undefined',
pageProperty: 'string',
typeofRequire: 'undefined',