2019-09-03 07:02:22 +00:00
|
|
|
import { expect } from 'chai';
|
|
|
|
import * as http from 'http';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as url from 'url';
|
|
|
|
|
2020-04-07 00:04:09 +00:00
|
|
|
import { BrowserWindow, WebPreferences } from 'electron/main';
|
2019-09-03 07:02:22 +00:00
|
|
|
|
|
|
|
import { closeWindow } from './window-helpers';
|
2019-11-01 20:37:02 +00:00
|
|
|
import { AddressInfo } from 'net';
|
|
|
|
import { emittedUntil } from './events-helpers';
|
2021-10-25 21:11:24 +00:00
|
|
|
import { delay } from './spec-helpers';
|
2019-10-18 19:57:34 +00:00
|
|
|
|
|
|
|
const messageContainsSecurityWarning = (event: Event, level: number, message: string) => {
|
|
|
|
return message.indexOf('Electron Security Warning') > -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
const isLoaded = (event: Event, level: number, message: string) => {
|
|
|
|
return (message === 'loaded');
|
|
|
|
};
|
2019-09-03 07:02:22 +00:00
|
|
|
|
|
|
|
describe('security warnings', () => {
|
|
|
|
let server: http.Server;
|
|
|
|
let w: BrowserWindow;
|
|
|
|
let useCsp = true;
|
|
|
|
let serverUrl: string;
|
|
|
|
|
|
|
|
before((done) => {
|
|
|
|
// Create HTTP Server
|
|
|
|
server = http.createServer((request, response) => {
|
|
|
|
const uri = url.parse(request.url!).pathname!;
|
|
|
|
let filename = path.join(__dirname, '..', 'spec', '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;
|
|
|
|
}
|
|
|
|
|
2021-01-25 01:31:25 +00:00
|
|
|
const cspHeaders = [
|
2021-10-25 21:11:24 +00:00
|
|
|
...(useCsp ? ['script-src \'self\' \'unsafe-inline\''] : [])
|
2021-01-25 01:31:25 +00:00
|
|
|
];
|
|
|
|
response.writeHead(200, { 'Content-Security-Policy': cspHeaders });
|
2019-09-03 07:02:22 +00:00
|
|
|
response.write(file, 'binary');
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}).listen(0, '127.0.0.1', () => {
|
2021-09-21 06:47:54 +00:00
|
|
|
serverUrl = `http://localhost2:${(server.address() as AddressInfo).port}`;
|
2019-09-03 07:02:22 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
// Close server
|
|
|
|
server.close();
|
|
|
|
server = null as unknown as any;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
useCsp = true;
|
|
|
|
await closeWindow(w);
|
|
|
|
w = null as unknown as any;
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should warn about Node.js integration with remote content', async () => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
2021-03-01 21:52:29 +00:00
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
2019-09-03 07:02:22 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
w.loadURL(`${serverUrl}/base-page-security.html`);
|
2019-10-18 19:57:34 +00:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 07:02:22 +00:00
|
|
|
expect(message).to.include('Node.js Integration with Remote Content');
|
|
|
|
});
|
|
|
|
|
2019-10-18 19:57:34 +00:00
|
|
|
it('should not warn about Node.js integration with remote content from localhost', async () => {
|
2019-09-03 07:02:22 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
w.loadURL(`${serverUrl}/base-page-security-onload-message.html`);
|
2019-10-18 19:57:34 +00:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', isLoaded);
|
|
|
|
expect(message).to.not.include('Node.js Integration with Remote Content');
|
2019-09-03 07:02:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const generateSpecs = (description: string, webPreferences: WebPreferences) => {
|
|
|
|
describe(description, () => {
|
|
|
|
it('should warn about disabled webSecurity', async () => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
webSecurity: false,
|
|
|
|
...webPreferences
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
w.loadURL(`${serverUrl}/base-page-security.html`);
|
2019-10-18 19:57:34 +00:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 07:02:22 +00:00
|
|
|
expect(message).to.include('Disabled webSecurity');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should warn about insecure Content-Security-Policy', async () => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
2021-03-10 01:12:40 +00:00
|
|
|
webPreferences
|
2019-09-03 07:02:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
useCsp = false;
|
|
|
|
w.loadURL(`${serverUrl}/base-page-security.html`);
|
2019-10-18 19:57:34 +00:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 07:02:22 +00:00
|
|
|
expect(message).to.include('Insecure Content-Security-Policy');
|
|
|
|
});
|
|
|
|
|
2021-10-25 21:11:24 +00:00
|
|
|
it('should not warn about secure Content-Security-Policy', async () => {
|
2021-01-25 01:31:25 +00:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
2021-03-10 01:12:40 +00:00
|
|
|
webPreferences
|
2021-01-25 01:31:25 +00:00
|
|
|
});
|
|
|
|
|
2021-10-25 21:11:24 +00:00
|
|
|
useCsp = true;
|
2021-01-25 01:31:25 +00:00
|
|
|
w.loadURL(`${serverUrl}/base-page-security.html`);
|
2021-10-25 21:11:24 +00:00
|
|
|
let didNotWarn = true;
|
|
|
|
w.webContents.on('console-message', () => {
|
|
|
|
didNotWarn = false;
|
|
|
|
});
|
|
|
|
await delay(500);
|
|
|
|
expect(didNotWarn).to.equal(true);
|
2021-01-25 01:31:25 +00:00
|
|
|
});
|
|
|
|
|
2019-09-03 07:02:22 +00:00
|
|
|
it('should warn about allowRunningInsecureContent', async () => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
allowRunningInsecureContent: true,
|
|
|
|
...webPreferences
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
w.loadURL(`${serverUrl}/base-page-security.html`);
|
2019-10-18 19:57:34 +00:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 07:02:22 +00:00
|
|
|
expect(message).to.include('allowRunningInsecureContent');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should warn about experimentalFeatures', async () => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
experimentalFeatures: true,
|
|
|
|
...webPreferences
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
w.loadURL(`${serverUrl}/base-page-security.html`);
|
2019-10-18 19:57:34 +00:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 07:02:22 +00:00
|
|
|
expect(message).to.include('experimentalFeatures');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should warn about enableBlinkFeatures', async () => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
enableBlinkFeatures: 'my-cool-feature',
|
|
|
|
...webPreferences
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
w.loadURL(`${serverUrl}/base-page-security.html`);
|
2019-10-18 19:57:34 +00:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 07:02:22 +00:00
|
|
|
expect(message).to.include('enableBlinkFeatures');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should warn about allowpopups', async () => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences
|
|
|
|
});
|
|
|
|
|
|
|
|
w.loadURL(`${serverUrl}/webview-allowpopups.html`);
|
2019-10-18 19:57:34 +00:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 07:02:22 +00:00
|
|
|
expect(message).to.include('allowpopups');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should warn about insecure resources', async () => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
2021-03-10 01:12:40 +00:00
|
|
|
webPreferences
|
2019-09-03 07:02:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
w.loadURL(`${serverUrl}/insecure-resources.html`);
|
2019-10-18 19:57:34 +00:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 07:02:22 +00:00
|
|
|
expect(message).to.include('Insecure Resources');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not warn about loading insecure-resources.html from localhost', async () => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences
|
|
|
|
});
|
|
|
|
|
|
|
|
w.loadURL(`${serverUrl}/insecure-resources.html`);
|
2019-11-01 20:37:02 +00:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 07:02:22 +00:00
|
|
|
expect(message).to.not.include('insecure-resources.html');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2020-03-20 20:28:31 +00:00
|
|
|
|
2021-03-01 21:52:29 +00:00
|
|
|
generateSpecs('without sandbox', { contextIsolation: false });
|
|
|
|
generateSpecs('with sandbox', { sandbox: true, contextIsolation: false });
|
2019-09-03 07:02:22 +00:00
|
|
|
});
|