Files

45 lines
1.9 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const path = require('node:path');
const { pathToFileURL } = require('node:url');
const { assertTrustedIpcSender, isTrustedApplicationUrl } = require('../desktop/security/ipc-sender.cjs');
function eventFor(url, { subframe = false } = {}) {
const mainFrame = { url: subframe ? pathToFileURL(path.resolve('index.html')).href : url };
const senderFrame = subframe ? { url } : mainFrame;
return { sender: { mainFrame }, senderFrame };
}
test('accepts the trusted top-level application document', () => {
const expected = path.resolve('index.html');
assert.doesNotThrow(() => assertTrustedIpcSender(
eventFor(pathToFileURL(expected).href), expected
));
});
test('rejects remote, different-file, query, and hash documents', () => {
const expected = path.resolve('index.html');
for (const url of [
'https://example.com/index.html',
pathToFileURL(path.resolve('other.html')).href,
`${pathToFileURL(expected).href}?debug=1`,
`${pathToFileURL(expected).href}#fragment`
]) {
assert.throws(() => assertTrustedIpcSender(eventFor(url), expected), /application window/);
}
});
test('rejects subframes and missing sender-frame information', () => {
const expected = path.resolve('index.html');
const topUrl = pathToFileURL(expected).href;
assert.throws(() => assertTrustedIpcSender(eventFor(topUrl, { subframe: true }), expected), /application window/);
assert.throws(() => assertTrustedIpcSender({ sender: { mainFrame: {} } }, expected), /application window/);
});
test('trusted-navigation policy allows only the app document URL', () => {
const expected = path.resolve('index.html');
assert.equal(isTrustedApplicationUrl(pathToFileURL(expected).href, expected), true);
assert.equal(isTrustedApplicationUrl('https://example.com/', expected), false);
assert.equal(isTrustedApplicationUrl(`${pathToFileURL(expected).href}#route`, expected), false);
});