143 lines
5.2 KiB
JavaScript
143 lines
5.2 KiB
JavaScript
const MAX_DEPTH = 20;
|
|
const MAX_VALUES = 25_000;
|
|
const MAX_COLLECTION_ITEMS = 5_000;
|
|
const MAX_STRING_LENGTH = 1_000_000;
|
|
const MAX_ARMORED_KEY_LENGTH = 200_000;
|
|
|
|
const schemas = {
|
|
'app:get-version': [],
|
|
'mail:get-account': [],
|
|
'mail:list-accounts': [],
|
|
'mail:select-account': ['string'],
|
|
'mail:remove-account': ['string'],
|
|
'mail:update-account': ['string', 'object'],
|
|
'mail:connect': ['object'],
|
|
'mail:sync-folder': ['string', 'number?', 'object?'],
|
|
'mail:list-folders': [],
|
|
'mail:create-folder': ['object'],
|
|
'mail:list-drafts': [],
|
|
'mail:save-draft': ['object'],
|
|
'mail:delete-draft': ['string'],
|
|
'mail:set-snooze': ['object'],
|
|
'mail:update-message': ['object'],
|
|
'mail:save-attachment': ['object'],
|
|
'mail:preview-attachment': ['object'],
|
|
'mail:send': ['object'],
|
|
'mail:open-external': ['string'],
|
|
'mail:sync-inbox': [],
|
|
'mail:disconnect': ['string?'],
|
|
'pgp:list': [],
|
|
'pgp:generate': ['object'],
|
|
'pgp:import': ['object'],
|
|
'pgp:export-public': ['string'],
|
|
'pgp:export-private': ['string', 'string'],
|
|
'pgp:change-passphrase': ['object'],
|
|
'pgp:export-revocation': ['string', 'string'],
|
|
'pgp:delete': ['string', 'string'],
|
|
'pgp:decrypt-message': ['object'],
|
|
'pgp:check-send': ['object'],
|
|
'pgp:contacts': [],
|
|
'pgp:add-contact': ['object'],
|
|
'pgp:set-trust': ['string', 'string'],
|
|
'pgp:remove-contact': ['string'],
|
|
'pgp:export-contact': ['string'],
|
|
'pgp:get-settings': [],
|
|
'pgp:set-settings': ['object'],
|
|
'pgp:key-servers': [],
|
|
'pgp:set-key-server-enabled': ['object'],
|
|
'pgp:add-key-server': ['object'],
|
|
'pgp:remove-key-server': ['string'],
|
|
'pgp:find-key': ['object'],
|
|
'pgp:publish-key': ['object'],
|
|
'pgp:forget-passphrases': [],
|
|
'dav:list': [],
|
|
'dav:connect': ['object'],
|
|
'dav:activate': ['object'],
|
|
'dav:remove': ['object'],
|
|
'dav:sync': ['enum:caldav,carddav'],
|
|
'dav:save-event': ['object'],
|
|
'dav:delete-event': ['object'],
|
|
'dav:save-contact': ['object'],
|
|
'dav:delete-contact': ['object']
|
|
};
|
|
|
|
function isRecord(value) {
|
|
if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
|
|
const prototype = Object.getPrototypeOf(value);
|
|
return prototype === Object.prototype || prototype === null;
|
|
}
|
|
|
|
function validateTree(value, channel) {
|
|
let values = 0;
|
|
const ancestors = new WeakSet();
|
|
|
|
function visit(current, depth, key = '') {
|
|
values += 1;
|
|
if (values > MAX_VALUES) throw new Error(`Too many values in IPC request for ${channel}.`);
|
|
if (depth > MAX_DEPTH) throw new Error(`IPC request for ${channel} is nested too deeply.`);
|
|
if (current === null || current === undefined || typeof current === 'boolean') return;
|
|
if (typeof current === 'number') {
|
|
if (!Number.isFinite(current)) throw new Error(`Invalid number in IPC request for ${channel}.`);
|
|
return;
|
|
}
|
|
if (typeof current === 'string') {
|
|
const limit = key === 'armoredKey' ? MAX_ARMORED_KEY_LENGTH :
|
|
['contentBase64', 'rawBase64'].includes(key) ? Infinity : MAX_STRING_LENGTH;
|
|
if (current.length > limit) throw new Error(`String value is too large in IPC request for ${channel}.`);
|
|
return;
|
|
}
|
|
if (typeof current !== 'object' || (!Array.isArray(current) && !isRecord(current))) {
|
|
throw new Error(`Unsupported value in IPC request for ${channel}.`);
|
|
}
|
|
if (ancestors.has(current)) throw new Error(`Cyclic value in IPC request for ${channel}.`);
|
|
ancestors.add(current);
|
|
if (Array.isArray(current)) {
|
|
if (current.length > MAX_COLLECTION_ITEMS) throw new Error(`Too many items in IPC request for ${channel}.`);
|
|
for (const item of current) visit(item, depth + 1);
|
|
} else {
|
|
const entries = Object.entries(current);
|
|
if (entries.length > MAX_COLLECTION_ITEMS) throw new Error(`Too many fields in IPC request for ${channel}.`);
|
|
for (const [childKey, child] of entries) {
|
|
if (['__proto__', 'prototype', 'constructor'].includes(childKey)) {
|
|
throw new Error(`Unsafe field in IPC request for ${channel}.`);
|
|
}
|
|
visit(child, depth + 1, childKey);
|
|
}
|
|
}
|
|
ancestors.delete(current);
|
|
}
|
|
|
|
visit(value, 0);
|
|
}
|
|
|
|
function validateIpcArguments(channel, args) {
|
|
const schema = schemas[channel];
|
|
if (!schema) throw new Error(`Unknown IPC channel: ${channel}.`);
|
|
if (!Array.isArray(args)) throw new TypeError('IPC arguments must be an array.');
|
|
const required = schema.filter(type => !type.endsWith('?')).length;
|
|
if (args.length < required || args.length > schema.length) {
|
|
throw new Error(`Invalid argument count for IPC channel ${channel}.`);
|
|
}
|
|
for (let index = 0; index < args.length; index += 1) {
|
|
const expected = schema[index].replace(/\?$/, '');
|
|
const value = args[index];
|
|
if (value === undefined && schema[index].endsWith('?')) continue;
|
|
const isEnum = expected.startsWith('enum:');
|
|
const typeIsValid = expected === 'object'
|
|
? isRecord(value)
|
|
: isEnum
|
|
? expected.slice('enum:'.length).split(',').includes(value)
|
|
: typeof value === expected;
|
|
if (!typeIsValid) {
|
|
throw new Error(`Invalid ${expected} argument ${index + 1} for IPC channel ${channel}.`);
|
|
}
|
|
validateTree(value, channel);
|
|
}
|
|
}
|
|
|
|
function hasIpcSchema(channel) {
|
|
return Object.hasOwn(schemas, channel);
|
|
}
|
|
|
|
module.exports = { hasIpcSchema, validateIpcArguments };
|