2019-09-03 10:02:22 +03:00
|
|
|
import { expect } from 'chai';
|
2023-06-15 17:42:27 +03:00
|
|
|
import * as http from 'node:http';
|
2023-11-17 12:44:03 +03:00
|
|
|
import * as fs from 'node:fs/promises';
|
2023-06-15 17:42:27 +03:00
|
|
|
import * as path from 'node:path';
|
2019-09-03 10:02:22 +03:00
|
|
|
|
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';
|
2023-06-15 17:42:27 +03:00
|
|
|
import { setTimeout } from 'node:timers/promises';
|
2019-10-18 22:57:34 +03:00
|
|
|
|
|
|
|
const messageContainsSecurityWarning = (event: Event, level: number, message: string) => {
|
2023-07-24 13:32:54 +03:00
|
|
|
return message.includes('Electron Security Warning');
|
2019-10-18 22:57:34 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
2023-11-17 12:44:03 +03:00
|
|
|
server = http.createServer(async (request, response) => {
|
2024-09-26 10:12:11 +03:00
|
|
|
const uri = new URL(request.url!, `http://${request.headers.host}`).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
|
|
|
|
2023-11-17 12:44:03 +03:00
|
|
|
try {
|
|
|
|
const stats = await fs.stat(filename);
|
2019-09-03 10:02:22 +03:00
|
|
|
if (stats.isDirectory()) {
|
|
|
|
filename += '/index.html';
|
|
|
|
}
|
|
|
|
|
2023-11-17 12:44:03 +03:00
|
|
|
const file = await fs.readFile(filename, 'binary');
|
|
|
|
const cspHeaders = [
|
|
|
|
...(useCsp ? ['script-src \'self\' \'unsafe-inline\''] : [])
|
|
|
|
];
|
|
|
|
response.writeHead(200, { 'Content-Security-Policy': cspHeaders });
|
|
|
|
response.write(file, 'binary');
|
|
|
|
} catch {
|
|
|
|
response.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
|
|
}
|
2019-09-03 10:02:22 +03:00
|
|
|
|
2023-11-17 12:44:03 +03:00
|
|
|
response.end();
|
2019-09-03 10:02:22 +03:00
|
|
|
});
|
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
|
|
|
});
|