2019-09-03 10:02:22 +03: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 03:04:09 +03:00
|
|
|
import { BrowserWindow, WebPreferences } from 'electron/main';
|
2019-09-03 10:02:22 +03:00
|
|
|
|
2023-01-26 00:01:25 +03:00
|
|
|
import { closeWindow } from './lib/window-helpers';
|
|
|
|
import { emittedUntil } from './lib/events-helpers';
|
2023-02-24 02:53:53 +03:00
|
|
|
import { listen } from './lib/spec-helpers';
|
|
|
|
import { setTimeout } from 'timers/promises';
|
2019-10-18 22:57:34 +03: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 10:02:22 +03:00
|
|
|
|
|
|
|
describe('security warnings', () => {
|
|
|
|
let server: http.Server;
|
|
|
|
let w: BrowserWindow;
|
|
|
|
let useCsp = true;
|
|
|
|
let serverUrl: string;
|
|
|
|
|
2023-02-20 14:30:57 +03:00
|
|
|
before(async () => {
|
2019-09-03 10:02:22 +03:00
|
|
|
// Create HTTP Server
|
|
|
|
server = http.createServer((request, response) => {
|
|
|
|
const uri = url.parse(request.url!).pathname!;
|
2022-08-16 22:23:13 +03:00
|
|
|
let filename = path.join(__dirname, 'fixtures', 'pages', uri);
|
2019-09-03 10:02:22 +03:00
|
|
|
|
|
|
|
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 04:31:25 +03:00
|
|
|
const cspHeaders = [
|
2021-10-26 00:11:24 +03:00
|
|
|
...(useCsp ? ['script-src \'self\' \'unsafe-inline\''] : [])
|
2021-01-25 04:31:25 +03:00
|
|
|
];
|
|
|
|
response.writeHead(200, { 'Content-Security-Policy': cspHeaders });
|
2019-09-03 10:02:22 +03:00
|
|
|
response.write(file, 'binary');
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-02-20 14:30:57 +03:00
|
|
|
|
|
|
|
serverUrl = `http://localhost2:${(await listen(server)).port}`;
|
2019-09-03 10:02:22 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
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-02 00:52:29 +03:00
|
|
|
nodeIntegration: true,
|
|
|
|
contextIsolation: false
|
2019-09-03 10:02:22 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
w.loadURL(`${serverUrl}/base-page-security.html`);
|
2019-10-18 22:57:34 +03:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 10:02:22 +03:00
|
|
|
expect(message).to.include('Node.js Integration with Remote Content');
|
|
|
|
});
|
|
|
|
|
2019-10-18 22:57:34 +03:00
|
|
|
it('should not warn about Node.js integration with remote content from localhost', async () => {
|
2019-09-03 10:02:22 +03:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
|
|
|
webPreferences: {
|
|
|
|
nodeIntegration: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
w.loadURL(`${serverUrl}/base-page-security-onload-message.html`);
|
2019-10-18 22:57:34 +03:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', isLoaded);
|
|
|
|
expect(message).to.not.include('Node.js Integration with Remote Content');
|
2019-09-03 10:02:22 +03: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 22:57:34 +03:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 10:02:22 +03:00
|
|
|
expect(message).to.include('Disabled webSecurity');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should warn about insecure Content-Security-Policy', async () => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
2021-03-10 04:12:40 +03:00
|
|
|
webPreferences
|
2019-09-03 10:02:22 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
useCsp = false;
|
|
|
|
w.loadURL(`${serverUrl}/base-page-security.html`);
|
2019-10-18 22:57:34 +03:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 10:02:22 +03:00
|
|
|
expect(message).to.include('Insecure Content-Security-Policy');
|
|
|
|
});
|
|
|
|
|
2021-10-26 00:11:24 +03:00
|
|
|
it('should not warn about secure Content-Security-Policy', async () => {
|
2021-01-25 04:31:25 +03:00
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
2021-03-10 04:12:40 +03:00
|
|
|
webPreferences
|
2021-01-25 04:31:25 +03:00
|
|
|
});
|
|
|
|
|
2021-10-26 00:11:24 +03:00
|
|
|
useCsp = true;
|
2021-01-25 04:31:25 +03:00
|
|
|
w.loadURL(`${serverUrl}/base-page-security.html`);
|
2021-10-26 00:11:24 +03:00
|
|
|
let didNotWarn = true;
|
|
|
|
w.webContents.on('console-message', () => {
|
|
|
|
didNotWarn = false;
|
|
|
|
});
|
2023-02-24 02:53:53 +03:00
|
|
|
await setTimeout(500);
|
2021-10-26 00:11:24 +03:00
|
|
|
expect(didNotWarn).to.equal(true);
|
2021-01-25 04:31:25 +03:00
|
|
|
});
|
|
|
|
|
2019-09-03 10:02:22 +03: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 22:57:34 +03:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 10:02:22 +03: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 22:57:34 +03:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 10:02:22 +03: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 22:57:34 +03:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 10:02:22 +03: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 22:57:34 +03:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 10:02:22 +03:00
|
|
|
expect(message).to.include('allowpopups');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should warn about insecure resources', async () => {
|
|
|
|
w = new BrowserWindow({
|
|
|
|
show: false,
|
2021-03-10 04:12:40 +03:00
|
|
|
webPreferences
|
2019-09-03 10:02:22 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
w.loadURL(`${serverUrl}/insecure-resources.html`);
|
2019-10-18 22:57:34 +03:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 10:02:22 +03: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 23:37:02 +03:00
|
|
|
const [,, message] = await emittedUntil(w.webContents, 'console-message', messageContainsSecurityWarning);
|
2019-09-03 10:02:22 +03:00
|
|
|
expect(message).to.not.include('insecure-resources.html');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2020-03-20 23:28:31 +03:00
|
|
|
|
2021-03-02 00:52:29 +03:00
|
|
|
generateSpecs('without sandbox', { contextIsolation: false });
|
|
|
|
generateSpecs('with sandbox', { sandbox: true, contextIsolation: false });
|
2019-09-03 10:02:22 +03:00
|
|
|
});
|