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