const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const { hasIpcSchema, validateIpcArguments } = require('../desktop/security/ipc-arguments.cjs'); test('validates supported IPC channels and their positional argument types', () => { assert.doesNotThrow(() => validateIpcArguments('mail:connect', [{ email: 'a@example.com' }])); assert.doesNotThrow(() => validateIpcArguments('mail:sync-folder', ['Inbox', 0, { hasAttachments: true }])); assert.doesNotThrow(() => validateIpcArguments('mail:disconnect', [])); assert.doesNotThrow(() => validateIpcArguments('mail:send', [{ to: 'a@example.com', attachments: [] }])); assert.throws(() => validateIpcArguments('mail:connect', ['not-an-object']), /Invalid object argument/); assert.throws(() => validateIpcArguments('mail:sync-folder', ['Inbox', 'zero']), /Invalid number argument/); assert.throws(() => validateIpcArguments('mail:open-external', [null]), /Invalid string argument/); assert.throws(() => validateIpcArguments('pgp:set-trust', ['fingerprint']), /Invalid argument count/); assert.throws(() => validateIpcArguments('mail:list-accounts', ['unexpected']), /Invalid argument count/); assert.doesNotThrow(() => validateIpcArguments('dav:sync', ['carddav'])); assert.throws(() => validateIpcArguments('dav:sync', ['contacts']), /Invalid enum/); assert.throws(() => validateIpcArguments('unknown:channel', []), /Unknown IPC channel/); }); test('bounds nested payload complexity without limiting attachment byte strings', () => { assert.doesNotThrow(() => validateIpcArguments('mail:send', [{ to: 'a@example.com', attachments: [{ filename: 'large.bin', contentBase64: 'A'.repeat(1_000_001) }] }])); assert.throws(() => validateIpcArguments('pgp:add-contact', [{ armoredKey: 'A'.repeat(200_001) }]), /too large/); assert.throws(() => validateIpcArguments('mail:save-draft', [{ text: 'x'.repeat(1_000_001) }]), /too large/); let nested = { payload: 1 }; for (let index = 0; index < 21; index += 1) nested = { child: nested }; assert.throws(() => validateIpcArguments('mail:send', [nested]), /nested too deeply/); assert.throws(() => validateIpcArguments('mail:save-draft', [{ values: Array(5001).fill('x') }]), /Too many items/); }); test('rejects non-finite numbers, cycles, and prototype-sensitive fields', () => { assert.throws(() => validateIpcArguments('mail:set-snooze', [{ uid: Infinity }]), /Invalid number/); const cyclic = {}; cyclic.self = cyclic; assert.throws(() => validateIpcArguments('mail:save-draft', [cyclic]), /Cyclic value/); const unsafe = Object.create(null); unsafe.constructor = 'unexpected'; assert.throws(() => validateIpcArguments('mail:save-draft', [unsafe]), /Unsafe field/); }); test('every registered main-process IPC handler has a request schema', () => { const mainPath = path.resolve(__dirname, '..', 'desktop', 'main.cjs'); const source = fs.readFileSync(mainPath, 'utf8'); const channels = [...source.matchAll(/registerIpcHandler\('([^']+)'/g)].map(match => match[1]); assert.ok(channels.length > 0); for (const channel of channels) { assert.equal(hasIpcSchema(channel), true, `missing IPC request schema: ${channel}`); } });