32 lines
944 B
JavaScript
32 lines
944 B
JavaScript
const path = require('node:path');
|
|
const { fileURLToPath } = require('node:url');
|
|
|
|
function isTrustedApplicationUrl(rawUrl, expectedFile) {
|
|
let actualFile;
|
|
try {
|
|
const url = new URL(rawUrl);
|
|
if (url.protocol !== 'file:' || url.search || url.hash) {
|
|
return false;
|
|
}
|
|
actualFile = fileURLToPath(url);
|
|
} catch {
|
|
return false;
|
|
}
|
|
|
|
const expected = path.resolve(expectedFile);
|
|
const actual = path.resolve(actualFile);
|
|
return process.platform === 'win32'
|
|
? actual.toLowerCase() === expected.toLowerCase()
|
|
: actual === expected;
|
|
}
|
|
|
|
function assertTrustedIpcSender(event, expectedFile) {
|
|
const frame = event?.senderFrame;
|
|
if (!frame || !event?.sender || frame !== event.sender.mainFrame ||
|
|
!isTrustedApplicationUrl(frame.url, expectedFile)) {
|
|
throw new Error('This request must come from the application window.');
|
|
}
|
|
}
|
|
|
|
module.exports = { assertTrustedIpcSender, isTrustedApplicationUrl };
|