2019-05-20 17:04:18 +00:00
|
|
|
const chai = require('chai')
|
|
|
|
const dirtyChai = require('dirty-chai')
|
|
|
|
|
2018-02-03 14:50:12 +00:00
|
|
|
const http = require('http')
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const url = require('url')
|
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { remote } = require('electron')
|
|
|
|
const { BrowserWindow } = remote
|
2018-02-03 14:50:12 +00:00
|
|
|
|
2018-09-13 16:10:51 +00:00
|
|
|
const { closeWindow } = require('./window-helpers')
|
2018-02-03 14:50:12 +00:00
|
|
|
|
2019-05-20 17:04:18 +00:00
|
|
|
const { expect } = chai
|
|
|
|
chai.use(dirtyChai)
|
|
|
|
|
2018-02-03 14:50:12 +00:00
|
|
|
describe('security warnings', () => {
|
|
|
|
let server
|
|
|
|
let w = null
|
|
|
|
let useCsp = true
|
|
|
|
|
2018-05-01 05:28:55 +00:00
|
|
|
before((done) => {
|
2018-02-03 14:50:12 +00:00
|
|
|
// Create HTTP Server
|
|
|
|
server = http.createServer((request, response) => {
|
|
|
|
const uri = url.parse(request.url).pathname
|
|
|
|
let filename = path.join(__dirname, './fixtures/pages', uri)
|
|
|
|
|
|
|
|
fs.stat(filename, (error, stats) => {
|
|
|
|
if (error) {
|
|
|
|
response.writeHead(404, { 'Content-Type': 'text/plain' })
|
|
|
|
response.end()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stats.isDirectory()) {
|
|
|
|
filename += '/index.html'
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.readFile(filename, 'binary', (err, file) => {
|
|
|
|
if (err) {
|
|
|
|
response.writeHead(404, { 'Content-Type': 'text/plain' })
|
|
|
|
response.end()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const cspHeaders = { 'Content-Security-Policy': `script-src 'self' 'unsafe-inline'` }
|
|
|
|
response.writeHead(200, useCsp ? cspHeaders : undefined)
|
|
|
|
response.write(file, 'binary')
|
|
|
|
response.end()
|
|
|
|
})
|
|
|
|
})
|
2018-05-01 05:28:55 +00:00
|
|
|
}).listen(8881, () => done())
|
2018-02-03 14:50:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
// Close server
|
|
|
|
server.close()
|
|
|
|
server = null
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
useCsp = true
|
2018-05-01 00:49:45 +00:00
|
|
|
return closeWindow(w).then(() => { w = null })
|
2018-02-03 14:50:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should warn about Node.js integration with remote content', (done) => {
|
2019-01-07 19:19:27 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
|
|
|
})
|
2018-05-01 05:28:55 +00:00
|
|
|
w.webContents.once('console-message', (e, level, message) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(message).to.include('Node.js Integration with Remote Content')
|
2018-02-03 14:50:12 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
|
|
|
|
})
|
|
|
|
|
2018-10-13 17:50:07 +00:00
|
|
|
const generateSpecs = (description, webPreferences) => {
|
2018-10-04 22:24:14 +00:00
|
|
|
describe(description, () => {
|
|
|
|
it('should warn about disabled webSecurity', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webSecurity: false,
|
2018-10-13 17:50:07 +00:00
|
|
|
...webPreferences
|
2018-10-04 22:24:14 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
w.webContents.once('console-message', (e, level, message) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(message).include('Disabled webSecurity')
|
2018-10-04 22:24:14 +00:00
|
|
|
done()
|
|
|
|
})
|
2018-02-03 14:50:12 +00:00
|
|
|
|
2018-10-04 22:24:14 +00:00
|
|
|
w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
|
|
|
|
})
|
2018-02-03 14:50:12 +00:00
|
|
|
|
2018-10-04 22:24:14 +00:00
|
|
|
it('should warn about insecure Content-Security-Policy', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
2019-01-07 19:19:27 +00:00
|
|
|
webPreferences
|
2018-10-04 22:24:14 +00:00
|
|
|
})
|
2018-10-03 19:36:12 +00:00
|
|
|
|
2018-10-04 22:24:14 +00:00
|
|
|
w.webContents.once('console-message', (e, level, message) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(message).to.include('Insecure Content-Security-Policy')
|
2018-10-04 22:24:14 +00:00
|
|
|
done()
|
|
|
|
})
|
2018-10-03 19:36:12 +00:00
|
|
|
|
2018-10-04 22:24:14 +00:00
|
|
|
useCsp = false
|
|
|
|
w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
|
|
|
|
})
|
2018-02-03 14:50:12 +00:00
|
|
|
|
2018-10-04 22:24:14 +00:00
|
|
|
it('should warn about allowRunningInsecureContent', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
allowRunningInsecureContent: true,
|
2018-10-13 17:50:07 +00:00
|
|
|
...webPreferences
|
2018-10-04 22:24:14 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
w.webContents.once('console-message', (e, level, message) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(message).to.include('allowRunningInsecureContent')
|
2018-10-04 22:24:14 +00:00
|
|
|
done()
|
|
|
|
})
|
2018-02-03 14:50:12 +00:00
|
|
|
|
2018-10-04 22:24:14 +00:00
|
|
|
w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
|
|
|
|
})
|
2018-10-03 19:36:12 +00:00
|
|
|
|
2018-10-04 22:24:14 +00:00
|
|
|
it('should warn about experimentalFeatures', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
experimentalFeatures: true,
|
2018-10-13 17:50:07 +00:00
|
|
|
...webPreferences
|
2018-10-04 22:24:14 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
w.webContents.once('console-message', (e, level, message) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(message).to.include('experimentalFeatures')
|
2018-10-04 22:24:14 +00:00
|
|
|
done()
|
|
|
|
})
|
2018-10-03 19:36:12 +00:00
|
|
|
|
2018-10-04 22:24:14 +00:00
|
|
|
w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
|
|
|
|
})
|
2018-02-03 14:50:12 +00:00
|
|
|
|
2018-10-04 22:24:14 +00:00
|
|
|
it('should warn about enableBlinkFeatures', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
enableBlinkFeatures: ['my-cool-feature'],
|
2018-10-13 17:50:07 +00:00
|
|
|
...webPreferences
|
2018-10-04 22:24:14 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
w.webContents.once('console-message', (e, level, message) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(message).to.include('enableBlinkFeatures')
|
2018-10-04 22:24:14 +00:00
|
|
|
done()
|
|
|
|
})
|
2018-02-03 14:50:12 +00:00
|
|
|
|
2018-10-04 22:24:14 +00:00
|
|
|
w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
|
|
|
|
})
|
2018-10-03 19:36:12 +00:00
|
|
|
|
2018-10-04 22:24:14 +00:00
|
|
|
it('should warn about allowpopups', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
2019-01-07 19:19:27 +00:00
|
|
|
webPreferences
|
2018-10-04 22:24:14 +00:00
|
|
|
})
|
|
|
|
w.webContents.once('console-message', (e, level, message) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(message).to.include('allowpopups')
|
2018-10-04 22:24:14 +00:00
|
|
|
done()
|
|
|
|
})
|
2018-10-03 19:36:12 +00:00
|
|
|
|
2018-10-04 22:24:14 +00:00
|
|
|
w.loadURL(`http://127.0.0.1:8881/webview-allowpopups.html`)
|
|
|
|
})
|
2018-02-03 14:50:12 +00:00
|
|
|
|
2018-10-04 22:24:14 +00:00
|
|
|
it('should warn about insecure resources', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
2019-01-07 19:19:27 +00:00
|
|
|
webPreferences
|
2018-10-04 22:24:14 +00:00
|
|
|
})
|
|
|
|
w.webContents.once('console-message', (e, level, message) => {
|
2019-05-20 17:04:18 +00:00
|
|
|
expect(message).to.include('Insecure Resources')
|
2018-10-04 22:24:14 +00:00
|
|
|
done()
|
|
|
|
})
|
2018-10-03 19:36:12 +00:00
|
|
|
|
2018-10-04 22:24:14 +00:00
|
|
|
w.loadURL(`http://127.0.0.1:8881/insecure-resources.html`)
|
|
|
|
w.webContents.openDevTools()
|
|
|
|
})
|
2018-10-03 19:36:12 +00:00
|
|
|
})
|
2018-10-04 22:24:14 +00:00
|
|
|
}
|
2018-10-03 19:36:12 +00:00
|
|
|
|
2018-10-13 17:50:07 +00:00
|
|
|
generateSpecs('without sandbox', {})
|
|
|
|
generateSpecs('with sandbox', { sandbox: true })
|
|
|
|
generateSpecs('with remote module disabled', { enableRemoteModule: false })
|
2018-02-03 14:50:12 +00:00
|
|
|
})
|