2018-02-03 14:50:12 +00:00
|
|
|
const assert = require('assert')
|
|
|
|
const http = require('http')
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const url = require('url')
|
|
|
|
|
|
|
|
const {remote} = require('electron')
|
|
|
|
const {BrowserWindow} = remote
|
|
|
|
|
|
|
|
const {closeWindow} = require('./window-helpers')
|
|
|
|
|
|
|
|
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) => {
|
|
|
|
w = new BrowserWindow({ show: false })
|
2018-05-01 05:28:55 +00:00
|
|
|
w.webContents.once('console-message', (e, level, message) => {
|
|
|
|
assert(message.includes('Node.js Integration with Remote Content'), message)
|
2018-02-03 14:50:12 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should warn about disabled webSecurity', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webSecurity: false,
|
|
|
|
nodeIntegration: false
|
|
|
|
}
|
|
|
|
})
|
2018-05-01 05:28:55 +00:00
|
|
|
w.webContents.once('console-message', (e, level, message) => {
|
|
|
|
assert(message.includes('Disabled webSecurity'), message)
|
2018-02-03 14:50:12 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should warn about insecure Content-Security-Policy', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2018-05-01 05:28:55 +00:00
|
|
|
w.webContents.once('console-message', (e, level, message) => {
|
|
|
|
assert(message.includes('Insecure Content-Security-Policy'), message)
|
2018-02-03 14:50:12 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
useCsp = false
|
|
|
|
w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should warn about allowRunningInsecureContent', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
allowRunningInsecureContent: true,
|
|
|
|
nodeIntegration: false
|
|
|
|
}
|
|
|
|
})
|
2018-05-01 05:28:55 +00:00
|
|
|
w.webContents.once('console-message', (e, level, message) => {
|
|
|
|
assert(message.includes('allowRunningInsecureContent'), message)
|
2018-02-03 14:50:12 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should warn about experimentalFeatures', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
experimentalFeatures: true,
|
|
|
|
nodeIntegration: false
|
|
|
|
}
|
|
|
|
})
|
2018-05-01 05:28:55 +00:00
|
|
|
w.webContents.once('console-message', (e, level, message) => {
|
|
|
|
assert(message.includes('experimentalFeatures'), message)
|
2018-02-03 14:50:12 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should warn about blinkFeatures', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
blinkFeatures: ['my-cool-feature'],
|
|
|
|
nodeIntegration: false
|
|
|
|
}
|
|
|
|
})
|
2018-05-01 05:28:55 +00:00
|
|
|
w.webContents.once('console-message', (e, level, message) => {
|
|
|
|
assert(message.includes('blinkFeatures'), message)
|
2018-02-03 14:50:12 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
w.loadURL(`http://127.0.0.1:8881/base-page-security.html`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should warn about allowpopups', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: false
|
|
|
|
}
|
|
|
|
})
|
2018-05-01 05:28:55 +00:00
|
|
|
w.webContents.once('console-message', (e, level, message) => {
|
|
|
|
assert(message.includes('allowpopups'), message)
|
2018-02-03 14:50:12 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
w.loadURL(`http://127.0.0.1:8881/webview-allowpopups.html`)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should warn about insecure resources', (done) => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: true,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: false
|
|
|
|
}
|
|
|
|
})
|
2018-05-01 05:28:55 +00:00
|
|
|
w.webContents.once('console-message', (e, level, message) => {
|
|
|
|
assert(message.includes('Insecure Resources'), message)
|
2018-02-03 14:50:12 +00:00
|
|
|
done()
|
|
|
|
})
|
|
|
|
|
|
|
|
w.loadURL(`http://127.0.0.1:8881/insecure-resources.html`)
|
|
|
|
w.webContents.openDevTools()
|
|
|
|
})
|
|
|
|
})
|