chore: make "nodeIntegration" and "webviewTag" default to false (#16235)

This commit is contained in:
Milan Burda 2019-01-07 20:19:27 +01:00 committed by Alexey Kuzmin
parent cdf4bfa68f
commit fade3eb679
23 changed files with 261 additions and 140 deletions

View file

@ -99,9 +99,9 @@ WebContentsPreferences::WebContentsPreferences(
// Set WebPreferences defaults onto the JS object // Set WebPreferences defaults onto the JS object
SetDefaultBoolIfUndefined(options::kPlugins, false); SetDefaultBoolIfUndefined(options::kPlugins, false);
SetDefaultBoolIfUndefined(options::kExperimentalFeatures, false); SetDefaultBoolIfUndefined(options::kExperimentalFeatures, false);
bool node = SetDefaultBoolIfUndefined(options::kNodeIntegration, true); SetDefaultBoolIfUndefined(options::kNodeIntegration, false);
SetDefaultBoolIfUndefined(options::kNodeIntegrationInWorker, false); SetDefaultBoolIfUndefined(options::kNodeIntegrationInWorker, false);
SetDefaultBoolIfUndefined(options::kWebviewTag, node); SetDefaultBoolIfUndefined(options::kWebviewTag, false);
SetDefaultBoolIfUndefined(options::kSandbox, false); SetDefaultBoolIfUndefined(options::kSandbox, false);
SetDefaultBoolIfUndefined(options::kNativeWindowOpen, false); SetDefaultBoolIfUndefined(options::kNativeWindowOpen, false);
SetDefaultBoolIfUndefined(options::kContextIsolation, false); SetDefaultBoolIfUndefined(options::kContextIsolation, false);

View file

@ -18,7 +18,6 @@ exports.load = async (appUrl) => {
backgroundColor: '#FFFFFF', backgroundColor: '#FFFFFF',
webPreferences: { webPreferences: {
contextIsolation: true, contextIsolation: true,
nodeIntegration: false,
preload: path.resolve(__dirname, 'renderer.js'), preload: path.resolve(__dirname, 'renderer.js'),
webviewTag: false webviewTag: false
}, },

View file

@ -20,11 +20,7 @@ win.on('closed', () => {
win = null win = null
}) })
let view = new BrowserView({ let view = new BrowserView()
webPreferences: {
nodeIntegration: false
}
})
win.setBrowserView(view) win.setBrowserView(view)
view.setBounds({ x: 0, y: 0, width: 300, height: 300 }) view.setBounds({ x: 0, y: 0, width: 300, height: 300 })
view.webContents.loadURL('https://electronjs.org') view.webContents.loadURL('https://electronjs.org')

View file

@ -250,8 +250,8 @@ It creates a new `BrowserWindow` with native properties as set by the `options`.
`new-window-for-tab` event. `new-window-for-tab` event.
* `webPreferences` Object (optional) - Settings of web page's features. * `webPreferences` Object (optional) - Settings of web page's features.
* `devTools` Boolean (optional) - Whether to enable DevTools. If it is set to `false`, can not use `BrowserWindow.webContents.openDevTools()` to open DevTools. Default is `true`. * `devTools` Boolean (optional) - Whether to enable DevTools. If it is set to `false`, can not use `BrowserWindow.webContents.openDevTools()` to open DevTools. Default is `true`.
* `nodeIntegration` Boolean (optional) - Whether node integration is enabled. Default * `nodeIntegration` Boolean (optional) - Whether node integration is enabled.
is `true`. Default is `false`.
* `nodeIntegrationInWorker` Boolean (optional) - Whether node integration is * `nodeIntegrationInWorker` Boolean (optional) - Whether node integration is
enabled in web workers. Default is `false`. More about this can be found enabled in web workers. Default is `false`. More about this can be found
in [Multithreading](../tutorial/multithreading.md). in [Multithreading](../tutorial/multithreading.md).
@ -353,7 +353,7 @@ It creates a new `BrowserWindow` with native properties as set by the `options`.
integration disabled. **Note:** This option is currently integration disabled. **Note:** This option is currently
experimental. experimental.
* `webviewTag` Boolean (optional) - Whether to enable the [`<webview>` tag](webview-tag.md). * `webviewTag` Boolean (optional) - Whether to enable the [`<webview>` tag](webview-tag.md).
Defaults to the value of the `nodeIntegration` option. **Note:** The Defaults to `false`. **Note:** The
`preload` script configured for the `<webview>` will have node integration `preload` script configured for the `<webview>` will have node integration
enabled when it is executed so you should ensure remote/untrusted content enabled when it is executed so you should ensure remote/untrusted content
is not able to create a `<webview>` tag with a possibly malicious `preload` is not able to create a `<webview>` tag with a possibly malicious `preload`

View file

@ -38,7 +38,7 @@ const parseOption = function (name, defaultValue, converter = value => value) {
} }
const contextIsolation = hasSwitch('context-isolation') const contextIsolation = hasSwitch('context-isolation')
let nodeIntegration = hasSwitch('node-integration') const nodeIntegration = hasSwitch('node-integration')
const webviewTag = hasSwitch('webview-tag') const webviewTag = hasSwitch('webview-tag')
const isHiddenPage = hasSwitch('hidden-page') const isHiddenPage = hasSwitch('hidden-page')
const isBackgroundPage = hasSwitch('background-page') const isBackgroundPage = hasSwitch('background-page')
@ -64,14 +64,11 @@ if (contextIsolation) {
if (window.location.protocol === 'chrome-devtools:') { if (window.location.protocol === 'chrome-devtools:') {
// Override some inspector APIs. // Override some inspector APIs.
require('@electron/internal/renderer/inspector') require('@electron/internal/renderer/inspector')
nodeIntegration = false
} else if (window.location.protocol === 'chrome-extension:') { } else if (window.location.protocol === 'chrome-extension:') {
// Add implementations of chrome API. // Add implementations of chrome API.
require('@electron/internal/renderer/chrome-api').injectTo(window.location.hostname, isBackgroundPage, window) require('@electron/internal/renderer/chrome-api').injectTo(window.location.hostname, isBackgroundPage, window)
nodeIntegration = false
} else if (window.location.protocol === 'chrome:') { } else if (window.location.protocol === 'chrome:') {
// Disable node integration for chrome UI scheme. // Disable node integration for chrome UI scheme.
nodeIntegration = false
} else { } else {
// Override default web functions. // Override default web functions.
require('@electron/internal/renderer/window-setup')(ipcRenderer, guestInstanceId, openerId, isHiddenPage, usesNativeWindowOpen) require('@electron/internal/renderer/window-setup')(ipcRenderer, guestInstanceId, openerId, isHiddenPage, usesNativeWindowOpen)

View file

@ -33,7 +33,10 @@ describe('electron module', () => {
window = new BrowserWindow({ window = new BrowserWindow({
show: false, show: false,
width: 400, width: 400,
height: 400 height: 400,
webPreferences: {
nodeIntegration: true
}
}) })
}) })
@ -299,7 +302,12 @@ describe('app module', () => {
password: 'electron' password: 'electron'
} }
w = new BrowserWindow({ show: false }) w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
w.webContents.on('did-finish-load', () => { w.webContents.on('did-finish-load', () => {
expect(w.webContents.getTitle()).to.equal('authorized') expect(w.webContents.getTitle()).to.equal('authorized')
@ -376,7 +384,12 @@ describe('app module', () => {
expect(webContents).to.equal(w.webContents) expect(webContents).to.equal(w.webContents)
done() done()
}) })
w = new BrowserWindow({ show: false }) w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
w.loadURL('about:blank') w.loadURL('about:blank')
w.webContents.executeJavaScript(`require('electron').desktopCapturer.getSources({ types: ['screen'] }, () => {})`) w.webContents.executeJavaScript(`require('electron').desktopCapturer.getSources({ types: ['screen'] }, () => {})`)
}) })
@ -387,7 +400,12 @@ describe('app module', () => {
expect(moduleName).to.equal('test') expect(moduleName).to.equal('test')
done() done()
}) })
w = new BrowserWindow({ show: false }) w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
w.loadURL('about:blank') w.loadURL('about:blank')
w.webContents.executeJavaScript(`require('electron').remote.require('test')`) w.webContents.executeJavaScript(`require('electron').remote.require('test')`)
}) })
@ -398,7 +416,12 @@ describe('app module', () => {
expect(globalName).to.equal('test') expect(globalName).to.equal('test')
done() done()
}) })
w = new BrowserWindow({ show: false }) w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
w.loadURL('about:blank') w.loadURL('about:blank')
w.webContents.executeJavaScript(`require('electron').remote.getGlobal('test')`) w.webContents.executeJavaScript(`require('electron').remote.getGlobal('test')`)
}) })
@ -591,6 +614,7 @@ describe('app module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
nodeIntegration: true,
partition: 'empty-certificate' partition: 'empty-certificate'
} }
}) })

View file

@ -34,7 +34,8 @@ describe('BrowserWindow module', () => {
width: 400, width: 400,
height: 400, height: 400,
webPreferences: { webPreferences: {
backgroundThrottling: false backgroundThrottling: false,
nodeIntegration: true
} }
} }
@ -1283,7 +1284,10 @@ describe('BrowserWindow module', () => {
w.destroy() w.destroy()
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { preload } webPreferences: {
nodeIntegration: true,
preload
}
}) })
const p = emittedOnce(ipcMain, 'answer') const p = emittedOnce(ipcMain, 'answer')
w.loadFile(path.join(fixtures, 'api', 'preload.html')) w.loadFile(path.join(fixtures, 'api', 'preload.html'))
@ -1296,7 +1300,8 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
preload: preload nodeIntegration: true,
preload
} }
}) })
const p = emittedOnce(ipcMain, 'answer') const p = emittedOnce(ipcMain, 'answer')
@ -1308,7 +1313,10 @@ describe('BrowserWindow module', () => {
const preload = path.join(fixtures, 'module', 'access-blink-apis.js') const preload = path.join(fixtures, 'module', 'access-blink-apis.js')
const w = await openTheWindow({ const w = await openTheWindow({
show: false, show: false,
webPreferences: { preload } webPreferences: {
nodeIntegration: true,
preload
}
}) })
const p = emittedOnce(ipcMain, 'answer') const p = emittedOnce(ipcMain, 'answer')
w.loadFile(path.join(fixtures, 'api', 'preload.html')) w.loadFile(path.join(fixtures, 'api', 'preload.html'))
@ -1350,6 +1358,7 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
nodeIntegration: true,
preload: path.join(fixtures, 'module', 'set-global-preload-3.js') preload: path.join(fixtures, 'module', 'set-global-preload-3.js')
} }
}) })
@ -1368,7 +1377,8 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
preload: preload, nodeIntegration: true,
preload,
additionalArguments: ['--my-magic-arg'] additionalArguments: ['--my-magic-arg']
} }
}) })
@ -1385,7 +1395,8 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
preload: preload, nodeIntegration: true,
preload,
additionalArguments: ['--my-magic-arg=foo'] additionalArguments: ['--my-magic-arg=foo']
} }
}) })
@ -1394,7 +1405,7 @@ describe('BrowserWindow module', () => {
}) })
describe('"node-integration" option', () => { describe('"node-integration" option', () => {
it('disables node integration when specified to false', (done) => { it('disables node integration by default', (done) => {
const preload = path.join(fixtures, 'module', 'send-later.js') const preload = path.join(fixtures, 'module', 'send-later.js')
ipcMain.once('answer', (event, typeofProcess, typeofBuffer) => { ipcMain.once('answer', (event, typeofProcess, typeofBuffer) => {
assert.strictEqual(typeofProcess, 'undefined') assert.strictEqual(typeofProcess, 'undefined')
@ -1405,8 +1416,7 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
preload: preload, preload
nodeIntegration: false
} }
}) })
w.loadFile(path.join(fixtures, 'api', 'blank.html')) w.loadFile(path.join(fixtures, 'api', 'blank.html'))
@ -1422,7 +1432,6 @@ describe('BrowserWindow module', () => {
const w = await openTheWindow({ const w = await openTheWindow({
show: false, show: false,
webPreferences: { webPreferences: {
nodeIntegration: false,
preload, preload,
sandbox sandbox
} }
@ -1437,7 +1446,6 @@ describe('BrowserWindow module', () => {
const w = await openTheWindow({ const w = await openTheWindow({
show: false, show: false,
webPreferences: { webPreferences: {
nodeIntegration: false,
preload, preload,
sandbox, sandbox,
enableRemoteModule: false enableRemoteModule: false
@ -1476,8 +1484,9 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
nodeIntegration: true,
sandbox: true, sandbox: true,
preload: preload preload
} }
}) })
w.loadFile(path.join(fixtures, 'api', 'preload.html')) w.loadFile(path.join(fixtures, 'api', 'preload.html'))
@ -1493,6 +1502,7 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
nodeIntegration: true,
sandbox: true, sandbox: true,
preload: preloadSpecialChars preload: preloadSpecialChars
} }
@ -1505,8 +1515,9 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
nodeIntegration: true,
sandbox: true, sandbox: true,
preload: preload preload
} }
}) })
const htmlPath = path.join(fixtures, 'api', 'sandbox.html?exit-event') const htmlPath = path.join(fixtures, 'api', 'sandbox.html?exit-event')
@ -1527,8 +1538,9 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
nodeIntegration: true,
sandbox: true, sandbox: true,
preload: preload preload
} }
}) })
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload) ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload)
@ -1653,7 +1665,7 @@ describe('BrowserWindow module', () => {
show: false, show: false,
webPreferences: { webPreferences: {
sandbox: true, sandbox: true,
preload: preload preload
} }
}) })
ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload) ipcRenderer.send('set-web-preferences-on-next-new-window', w.webContents.id, 'preload', preload)
@ -1738,7 +1750,7 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
preload: preload, preload,
sandbox: true sandbox: true
} }
}) })
@ -1761,7 +1773,7 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
preload: preload, preload,
sandbox: true sandbox: true
} }
}) })
@ -1794,7 +1806,7 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
preload: preload, preload,
sandbox: true sandbox: true
} }
}) })
@ -1843,7 +1855,7 @@ describe('BrowserWindow module', () => {
show: false, show: false,
webPreferences: { webPreferences: {
sandbox: true, sandbox: true,
preload: preload preload
} }
}) })
w.loadFile(path.join(fixtures, 'api', 'preload.html')) w.loadFile(path.join(fixtures, 'api', 'preload.html'))
@ -1855,7 +1867,7 @@ describe('BrowserWindow module', () => {
show: false, show: false,
webPreferences: { webPreferences: {
sandbox: true, sandbox: true,
preload: preload, preload,
webviewTag: true webviewTag: true
} }
}) })
@ -1875,6 +1887,7 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
nodeIntegration: true,
nativeWindowOpen: true nativeWindowOpen: true
} }
}) })
@ -1962,7 +1975,6 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: true, show: true,
webPreferences: { webPreferences: {
nodeIntegration: false,
nativeWindowOpen: true nativeWindowOpen: true
} }
}) })
@ -2093,7 +2105,14 @@ describe('BrowserWindow module', () => {
afterEach(() => { ipcMain.removeAllListeners('pong') }) afterEach(() => { ipcMain.removeAllListeners('pong') })
it('visibilityState is initially visible despite window being hidden', (done) => { it('visibilityState is initially visible despite window being hidden', (done) => {
w = new BrowserWindow({ show: false, width: 100, height: 100 }) w = new BrowserWindow({
show: false,
width: 100,
height: 100,
webPreferences: {
nodeIntegration: true
}
})
let readyToShow = false let readyToShow = false
w.once('ready-to-show', () => { w.once('ready-to-show', () => {
@ -2111,7 +2130,13 @@ describe('BrowserWindow module', () => {
w.loadFile(path.join(fixtures, 'pages', 'visibilitychange.html')) w.loadFile(path.join(fixtures, 'pages', 'visibilitychange.html'))
}) })
it('visibilityState changes when window is hidden', (done) => { it('visibilityState changes when window is hidden', (done) => {
w = new BrowserWindow({ width: 100, height: 100 }) w = new BrowserWindow({
width: 100,
height: 100,
webPreferences: {
nodeIntegration: true
}
})
onNextVisibilityChange((visibilityState, hidden) => { onNextVisibilityChange((visibilityState, hidden) => {
assert.strictEqual(visibilityState, 'visible') assert.strictEqual(visibilityState, 'visible')
@ -2129,7 +2154,13 @@ describe('BrowserWindow module', () => {
w.loadFile(path.join(fixtures, 'pages', 'visibilitychange.html')) w.loadFile(path.join(fixtures, 'pages', 'visibilitychange.html'))
}) })
it('visibilityState changes when window is shown', (done) => { it('visibilityState changes when window is shown', (done) => {
w = new BrowserWindow({ width: 100, height: 100 }) w = new BrowserWindow({
width: 100,
height: 100,
webPreferences: {
nodeIntegration: true
}
})
onNextVisibilityChange((visibilityState, hidden) => { onNextVisibilityChange((visibilityState, hidden) => {
onVisibilityChange((visibilityState, hidden) => { onVisibilityChange((visibilityState, hidden) => {
@ -2155,7 +2186,13 @@ describe('BrowserWindow module', () => {
return done() return done()
} }
w = new BrowserWindow({ width: 100, height: 100 }) w = new BrowserWindow({
width: 100,
height: 100,
webPreferences: {
nodeIntegration: true
}
})
onNextVisibilityChange((visibilityState, hidden) => { onNextVisibilityChange((visibilityState, hidden) => {
onVisibilityChange((visibilityState, hidden) => { onVisibilityChange((visibilityState, hidden) => {
@ -2181,7 +2218,13 @@ describe('BrowserWindow module', () => {
return done() return done()
} }
w = new BrowserWindow({ width: 100, height: 100 }) w = new BrowserWindow({
width: 100,
height: 100,
webPreferences: {
nodeIntegration: true
}
})
onNextVisibilityChange((visibilityState, hidden) => { onNextVisibilityChange((visibilityState, hidden) => {
assert.strictEqual(visibilityState, 'visible') assert.strictEqual(visibilityState, 'visible')
@ -2204,7 +2247,8 @@ describe('BrowserWindow module', () => {
width: 100, width: 100,
height: 100, height: 100,
webPreferences: { webPreferences: {
backgroundThrottling: false backgroundThrottling: false,
nodeIntegration: true
} }
}) })
@ -3189,6 +3233,7 @@ describe('BrowserWindow module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
nodeIntegration: true,
partition: 'temp' partition: 'temp'
} }
}) })

View file

@ -10,7 +10,7 @@ const url = require('url')
const { closeWindow } = require('./window-helpers') const { closeWindow } = require('./window-helpers')
const { remote } = require('electron') const { remote } = require('electron')
const { app, BrowserWindow, crashReporter } = remote.require('electron') const { app, BrowserWindow, crashReporter } = remote
describe('crashReporter module', () => { describe('crashReporter module', () => {
if (process.mas || process.env.DISABLE_CRASH_REPORTER_TESTS) return if (process.mas || process.env.DISABLE_CRASH_REPORTER_TESTS) return
@ -188,7 +188,11 @@ describe('crashReporter module', () => {
}) })
} }
generateSpecs('without sandbox', {}) generateSpecs('without sandbox', {
webPreferences: {
nodeIntegration: true
}
})
generateSpecs('with sandbox', { generateSpecs('with sandbox', {
webPreferences: { webPreferences: {
sandbox: true, sandbox: true,
@ -197,6 +201,7 @@ describe('crashReporter module', () => {
}) })
generateSpecs('with remote module disabled', { generateSpecs('with remote module disabled', {
webPreferences: { webPreferences: {
nodeIntegration: true,
enableRemoteModule: false enableRemoteModule: false
} }
}) })

View file

@ -24,7 +24,12 @@ describe('ipc main module', () => {
afterEach(() => { ipcMain.removeAllListeners('send-sync-message') }) afterEach(() => { ipcMain.removeAllListeners('send-sync-message') })
it('does not crash when reply is not sent and browser is destroyed', (done) => { it('does not crash when reply is not sent and browser is destroyed', (done) => {
w = new BrowserWindow({ show: false }) w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
ipcMain.once('send-sync-message', (event) => { ipcMain.once('send-sync-message', (event) => {
event.returnValue = null event.returnValue = null
done() done()
@ -33,7 +38,12 @@ describe('ipc main module', () => {
}) })
it('does not crash when reply is sent by multiple listeners', (done) => { it('does not crash when reply is sent by multiple listeners', (done) => {
w = new BrowserWindow({ show: false }) w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
ipcMain.on('send-sync-message', (event) => { ipcMain.on('send-sync-message', (event) => {
event.returnValue = null event.returnValue = null
}) })
@ -59,7 +69,12 @@ describe('ipc main module', () => {
describe('remote objects registry', () => { describe('remote objects registry', () => {
it('does not dereference until the render view is deleted (regression)', (done) => { it('does not dereference until the render view is deleted (regression)', (done) => {
w = new BrowserWindow({ show: false }) w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
ipcMain.once('error-message', (event, message) => { ipcMain.once('error-message', (event, message) => {
const correctMsgStart = message.startsWith('Cannot call function \'getURL\' on missing remote object') const correctMsgStart = message.startsWith('Cannot call function \'getURL\' on missing remote object')

View file

@ -199,7 +199,12 @@ describe('ipc renderer module', () => {
describe('remote listeners', () => { describe('remote listeners', () => {
it('detaches listeners subscribed to destroyed renderers, and shows a warning', (done) => { it('detaches listeners subscribed to destroyed renderers, and shows a warning', (done) => {
w = new BrowserWindow({ show: false }) w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
w.webContents.once('did-finish-load', () => { w.webContents.once('did-finish-load', () => {
w.webContents.once('did-finish-load', () => { w.webContents.once('did-finish-load', () => {
@ -227,7 +232,12 @@ describe('ipc renderer module', () => {
describe('ipcRenderer.on', () => { describe('ipcRenderer.on', () => {
it('is not used for internals', async () => { it('is not used for internals', async () => {
w = new BrowserWindow({ show: false }) w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
await w.loadURL('about:blank') await w.loadURL('about:blank')
const script = `require('electron').ipcRenderer.eventNames()` const script = `require('electron').ipcRenderer.eventNames()`

View file

@ -11,7 +11,7 @@ const dbus = require('dbus-native')
const Promise = require('bluebird') const Promise = require('bluebird')
const { remote } = require('electron') const { remote } = require('electron')
const { app } = remote.require('electron') const { app } = remote
const skip = process.platform !== 'linux' || const skip = process.platform !== 'linux' ||
process.arch === 'ia32' || process.arch === 'ia32' ||

View file

@ -1045,7 +1045,12 @@ describe('protocol module', () => {
let success = null let success = null
beforeEach(() => { beforeEach(() => {
w = new BrowserWindow({ show: false }) w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
success = false success = false
}) })

View file

@ -526,7 +526,7 @@ describe('remote module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
preload: preload preload
} }
}) })
w.once('closed', () => done()) w.once('closed', () => done())

View file

@ -26,7 +26,10 @@ describe('session module', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
width: 400, width: 400,
height: 400 height: 400,
webPreferences: {
nodeIntegration: true
}
}) })
}) })

View file

@ -32,7 +32,9 @@ describe('webContents module', () => {
width: 400, width: 400,
height: 400, height: 400,
webPreferences: { webPreferences: {
backgroundThrottling: false backgroundThrottling: false,
nodeIntegration: true,
webviewTag: true
} }
}) })
}) })

View file

@ -146,7 +146,12 @@ describe('webFrame module', function () {
}) })
it('calls a spellcheck provider', async () => { it('calls a spellcheck provider', async () => {
w = new BrowserWindow({ show: false }) w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
await w.loadFile(path.join(fixtures, 'pages', 'webframe-spell-check.html')) await w.loadFile(path.join(fixtures, 'pages', 'webframe-spell-check.html'))
w.focus() w.focus()
await w.webContents.executeJavaScript('document.querySelector("input").focus()', true) await w.webContents.executeJavaScript('document.querySelector("input").focus()', true)

View file

@ -10,8 +10,7 @@ const { closeWindow } = require('./window-helpers')
const nativeImage = require('electron').nativeImage const nativeImage = require('electron').nativeImage
const remote = require('electron').remote const remote = require('electron').remote
const ipcMain = remote.require('electron').ipcMain const { ipcMain, BrowserWindow } = remote
const BrowserWindow = remote.require('electron').BrowserWindow
describe('asar package', function () { describe('asar package', function () {
const fixtures = path.join(__dirname, 'fixtures') const fixtures = path.join(__dirname, 'fixtures')
@ -1134,7 +1133,10 @@ describe('asar package', function () {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
width: 400, width: 400,
height: 400 height: 400,
webPreferences: {
nodeIntegration: true
}
}) })
const p = path.resolve(fixtures, 'asar', 'web.asar', 'index.html') const p = path.resolve(fixtures, 'asar', 'web.asar', 'index.html')
ipcMain.once('dirname', function (event, dirname) { ipcMain.once('dirname', function (event, dirname) {
@ -1152,7 +1154,10 @@ describe('asar package', function () {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
width: 400, width: 400,
height: 400 height: 400,
webPreferences: {
nodeIntegration: true
}
}) })
const p = path.resolve(fixtures, 'asar', 'script.asar', 'index.html') const p = path.resolve(fixtures, 'asar', 'script.asar', 'index.html')
w.loadFile(p) w.loadFile(p)
@ -1172,7 +1177,10 @@ describe('asar package', function () {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
width: 400, width: 400,
height: 400 height: 400,
webPreferences: {
nodeIntegration: true
}
}) })
const p = path.resolve(fixtures, 'asar', 'video.asar', 'index.html') const p = path.resolve(fixtures, 'asar', 'video.asar', 'index.html')
w.loadFile(p) w.loadFile(p)

View file

@ -112,12 +112,7 @@ describe('chromium feature', () => {
describe('loading jquery', () => { describe('loading jquery', () => {
it('does not crash', (done) => { it('does not crash', (done) => {
w = new BrowserWindow({ w = new BrowserWindow({ show: false })
show: false,
webPreferences: {
nodeIntegration: false
}
})
w.webContents.once('did-finish-load', () => { done() }) w.webContents.once('did-finish-load', () => { done() })
w.webContents.once('crashed', () => done(new Error('WebContents crashed.'))) w.webContents.once('crashed', () => done(new Error('WebContents crashed.')))
w.loadFile(path.join(fixtures, 'pages', 'jquery.html')) w.loadFile(path.join(fixtures, 'pages', 'jquery.html'))
@ -176,6 +171,7 @@ describe('chromium feature', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
nodeIntegration: true,
session: ses session: ses
} }
}) })
@ -216,6 +212,7 @@ describe('chromium feature', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
nodeIntegration: true,
partition: 'sw-file-scheme-spec' partition: 'sw-file-scheme-spec'
} }
}) })
@ -253,7 +250,10 @@ describe('chromium feature', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { session: customSession } webPreferences: {
nodeIntegration: true,
session: customSession
}
}) })
w.webContents.on('ipc-message', (event, args) => { w.webContents.on('ipc-message', (event, args) => {
if (args[0] === 'reload') { if (args[0] === 'reload') {
@ -294,6 +294,7 @@ describe('chromium feature', () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences: {
nodeIntegration: true,
partition: 'geolocation-spec' partition: 'geolocation-spec'
} }
}) })
@ -388,26 +389,6 @@ describe('chromium feature', () => {
b = window.open(windowUrl, '', 'nodeIntegration=no,show=no') b = window.open(windowUrl, '', 'nodeIntegration=no,show=no')
}) })
it('disables webviewTag when node integration is disabled on the parent window', (done) => {
let b = null
listener = (event) => {
assert.strictEqual(event.data.isWebViewUndefined, true)
b.close()
done()
}
window.addEventListener('message', listener)
const windowUrl = require('url').format({
pathname: `${fixtures}/pages/window-opener-no-web-view-tag.html`,
protocol: 'file',
query: {
p: `${fixtures}/pages/window-opener-web-view.html`
},
slashes: true
})
b = window.open(windowUrl, '', 'nodeIntegration=no,show=no')
})
// TODO(codebytere): re-enable this test // TODO(codebytere): re-enable this test
xit('disables node integration when it is disabled on the parent window for chrome devtools URLs', (done) => { xit('disables node integration when it is disabled on the parent window for chrome devtools URLs', (done) => {
let b = null let b = null
@ -604,7 +585,12 @@ describe('chromium feature', () => {
describe('window.opener', () => { describe('window.opener', () => {
const url = `file://${fixtures}/pages/window-opener.html` const url = `file://${fixtures}/pages/window-opener.html`
it('is null for main window', (done) => { it('is null for main window', (done) => {
w = new BrowserWindow({ show: false }) w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
w.webContents.once('ipc-message', (event, args) => { w.webContents.once('ipc-message', (event, args) => {
assert.deepStrictEqual(args, ['opener', null]) assert.deepStrictEqual(args, ['opener', null])
done() done()
@ -1008,7 +994,9 @@ describe('chromium feature', () => {
}) })
beforeEach(() => { beforeEach(() => {
contents = webContents.create({}) contents = webContents.create({
nodeIntegration: true
})
}) })
afterEach(() => { afterEach(() => {
@ -1102,8 +1090,10 @@ describe('chromium feature', () => {
const testLocalStorageAfterXSiteRedirect = (testTitle, extraPreferences = {}) => { const testLocalStorageAfterXSiteRedirect = (testTitle, extraPreferences = {}) => {
it(testTitle, (done) => { it(testTitle, (done) => {
const webPreferences = { show: false, ...extraPreferences } w = new BrowserWindow({
w = new BrowserWindow(webPreferences) show: false,
...extraPreferences
})
let redirected = false let redirected = false
w.webContents.on('crashed', () => { w.webContents.on('crashed', () => {
assert.fail('renderer crashed / was killed') assert.fail('renderer crashed / was killed')
@ -1417,7 +1407,11 @@ describe('chromium feature', () => {
beforeEach(async () => { beforeEach(async () => {
w = new BrowserWindow({ w = new BrowserWindow({
show: true show: true,
webPreferences: {
nodeIntegration: true,
webviewTag: true
}
}) })
const webviewReady = emittedOnce(w.webContents, 'did-attach-webview') const webviewReady = emittedOnce(w.webContents, 'did-attach-webview')

View file

@ -14,7 +14,10 @@ let window
app.once('ready', () => { app.once('ready', () => {
window = new BrowserWindow({ window = new BrowserWindow({
show: false show: false,
webPreferences: {
nodeIntegration: true
}
}) })
window.webContents.on('crashed', (event, killed) => { window.webContents.on('crashed', (event, killed) => {

View file

@ -162,7 +162,12 @@ describe('modules support', () => {
let w let w
beforeEach(() => { beforeEach(() => {
w = new BrowserWindow({ show: false }) w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
}) })
afterEach(async () => { afterEach(async () => {

View file

@ -59,7 +59,12 @@ describe('security warnings', () => {
}) })
it('should warn about Node.js integration with remote content', (done) => { it('should warn about Node.js integration with remote content', (done) => {
w = new BrowserWindow({ show: false }) w = new BrowserWindow({
show: false,
webPreferences: {
nodeIntegration: true
}
})
w.webContents.once('console-message', (e, level, message) => { w.webContents.once('console-message', (e, level, message) => {
assert(message.includes('Node.js Integration with Remote Content'), message) assert(message.includes('Node.js Integration with Remote Content'), message)
done() done()
@ -75,7 +80,6 @@ describe('security warnings', () => {
show: false, show: false,
webPreferences: { webPreferences: {
webSecurity: false, webSecurity: false,
nodeIntegration: false,
...webPreferences ...webPreferences
} }
}) })
@ -90,10 +94,7 @@ describe('security warnings', () => {
it('should warn about insecure Content-Security-Policy', (done) => { it('should warn about insecure Content-Security-Policy', (done) => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences
nodeIntegration: false,
...webPreferences
}
}) })
w.webContents.once('console-message', (e, level, message) => { w.webContents.once('console-message', (e, level, message) => {
@ -110,7 +111,6 @@ describe('security warnings', () => {
show: false, show: false,
webPreferences: { webPreferences: {
allowRunningInsecureContent: true, allowRunningInsecureContent: true,
nodeIntegration: false,
...webPreferences ...webPreferences
} }
}) })
@ -127,7 +127,6 @@ describe('security warnings', () => {
show: false, show: false,
webPreferences: { webPreferences: {
experimentalFeatures: true, experimentalFeatures: true,
nodeIntegration: false,
...webPreferences ...webPreferences
} }
}) })
@ -144,7 +143,6 @@ describe('security warnings', () => {
show: false, show: false,
webPreferences: { webPreferences: {
enableBlinkFeatures: ['my-cool-feature'], enableBlinkFeatures: ['my-cool-feature'],
nodeIntegration: false,
...webPreferences ...webPreferences
} }
}) })
@ -159,10 +157,7 @@ describe('security warnings', () => {
it('should warn about allowpopups', (done) => { it('should warn about allowpopups', (done) => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences
nodeIntegration: false,
...webPreferences
}
}) })
w.webContents.once('console-message', (e, level, message) => { w.webContents.once('console-message', (e, level, message) => {
assert(message.includes('allowpopups'), message) assert(message.includes('allowpopups'), message)
@ -175,10 +170,7 @@ describe('security warnings', () => {
it('should warn about insecure resources', (done) => { it('should warn about insecure resources', (done) => {
w = new BrowserWindow({ w = new BrowserWindow({
show: false, show: false,
webPreferences: { webPreferences
nodeIntegration: false,
...webPreferences
}
}) })
w.webContents.once('console-message', (e, level, message) => { w.webContents.once('console-message', (e, level, message) => {
assert(message.includes('Insecure Resources'), message) assert(message.includes('Insecure Resources'), message)

View file

@ -124,7 +124,9 @@ app.on('ready', function () {
width: 800, width: 800,
height: 600, height: 600,
webPreferences: { webPreferences: {
backgroundThrottling: false backgroundThrottling: false,
nodeIntegration: true,
webviewTag: true
} }
}) })
window.loadFile('static/index.html', { window.loadFile('static/index.html', {

View file

@ -65,7 +65,13 @@ describe('<webview> tag', function () {
}) })
it('works without script tag in page', async () => { it('works without script tag in page', async () => {
const w = await openTheWindow({ show: false }) const w = await openTheWindow({
show: false,
webPreferences: {
webviewTag: true,
nodeIntegration: true
}
})
w.loadFile(path.join(fixtures, 'pages', 'webview-no-script.html')) w.loadFile(path.join(fixtures, 'pages', 'webview-no-script.html'))
await emittedOnce(ipcMain, 'pong') await emittedOnce(ipcMain, 'pong')
}) })
@ -75,6 +81,7 @@ describe('<webview> tag', function () {
show: false, show: false,
webPreferences: { webPreferences: {
webviewTag: true, webviewTag: true,
nodeIntegration: true,
contextIsolation: true contextIsolation: true
} }
}) })
@ -82,12 +89,12 @@ describe('<webview> tag', function () {
await emittedOnce(ipcMain, 'pong') await emittedOnce(ipcMain, 'pong')
}) })
it('is disabled when nodeIntegration is disabled', async () => { it('is disabled by default', async () => {
const w = await openTheWindow({ const w = await openTheWindow({
show: false, show: false,
webPreferences: { webPreferences: {
nodeIntegration: false, preload: path.join(fixtures, 'module', 'preload-webview.js'),
preload: path.join(fixtures, 'module', 'preload-webview.js') nodeIntegration: true
} }
}) })
@ -97,22 +104,6 @@ describe('<webview> tag', function () {
expect(type).to.equal('undefined', 'WebView still exists') expect(type).to.equal('undefined', 'WebView still exists')
}) })
it('is enabled when the webviewTag option is enabled and the nodeIntegration option is disabled', async () => {
const w = await openTheWindow({
show: false,
webPreferences: {
nodeIntegration: false,
preload: path.join(fixtures, 'module', 'preload-webview.js'),
webviewTag: true
}
})
w.loadFile(path.join(fixtures, 'pages', 'webview-no-script.html'))
const [, type] = await emittedOnce(ipcMain, 'webview')
expect(type).to.not.equal('undefined', 'WebView is not created')
})
describe('src attribute', () => { describe('src attribute', () => {
it('specifies the page to load', async () => { it('specifies the page to load', async () => {
const message = await startLoadingWebViewAndWaitForMessage(webview, { const message = await startLoadingWebViewAndWaitForMessage(webview, {
@ -1289,7 +1280,13 @@ describe('<webview> tag', function () {
describe('did-attach-webview event', () => { describe('did-attach-webview event', () => {
it('is emitted when a webview has been attached', async () => { it('is emitted when a webview has been attached', async () => {
const w = await openTheWindow({ show: false }) const w = await openTheWindow({
show: false,
webPreferences: {
webviewTag: true,
nodeIntegration: true
}
})
w.loadFile(path.join(fixtures, 'pages', 'webview-did-attach-event.html')) w.loadFile(path.join(fixtures, 'pages', 'webview-did-attach-event.html'))
const [, webContents] = await emittedOnce(w.webContents, 'did-attach-webview') const [, webContents] = await emittedOnce(w.webContents, 'did-attach-webview')
@ -1299,7 +1296,13 @@ describe('<webview> tag', function () {
}) })
it('loads devtools extensions registered on the parent window', async () => { it('loads devtools extensions registered on the parent window', async () => {
const w = await openTheWindow({ show: false }) const w = await openTheWindow({
show: false,
webPreferences: {
webviewTag: true,
nodeIntegration: true
}
})
BrowserWindow.removeDevToolsExtension('foo') BrowserWindow.removeDevToolsExtension('foo')
const extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo') const extensionPath = path.join(__dirname, 'fixtures', 'devtools-extensions', 'foo')
@ -1391,6 +1394,8 @@ describe('<webview> tag', function () {
const w = await openTheWindow({ const w = await openTheWindow({
show: false, show: false,
webPreferences: { webPreferences: {
webviewTag: true,
nodeIntegration: true,
zoomFactor: 1.2 zoomFactor: 1.2
} }
}) })
@ -1405,6 +1410,8 @@ describe('<webview> tag', function () {
return openTheWindow({ return openTheWindow({
show: false, show: false,
webPreferences: { webPreferences: {
webviewTag: true,
nodeIntegration: true,
zoomFactor: 1.2 zoomFactor: 1.2
} }
}).then((w) => { }).then((w) => {
@ -1434,6 +1441,8 @@ describe('<webview> tag', function () {
return openTheWindow({ return openTheWindow({
show: false, show: false,
webPreferences: { webPreferences: {
webviewTag: true,
nodeIntegration: true,
zoomFactor: 1.2 zoomFactor: 1.2
} }
}).then((w) => { }).then((w) => {
@ -1458,6 +1467,8 @@ describe('<webview> tag', function () {
const w = await openTheWindow({ const w = await openTheWindow({
show: false, show: false,
webPreferences: { webPreferences: {
webviewTag: true,
nodeIntegration: true,
zoomFactor: 1.2 zoomFactor: 1.2
} }
}) })