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('mail:open-external', [`https://example.test/${'x'.repeat(2048)}`]), /too large/); assert.throws(() => validateIpcArguments('pgp:export-public', ['A'.repeat(65)]), /too large/); 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('validates channel-specific field shapes, limits, and enums', () => { assert.doesNotThrow(() => validateIpcArguments('mail:connect', [{ email: 'user@example.test', password: 'secret', displayName: 'User', imapHost: 'imap.example.test', smtpHost: 'smtp.example.test', rules: [{ senderContains: 'alerts', action: 'flag', value: '' }] }])); assert.throws(() => validateIpcArguments('mail:connect', [{ rules: Array(51).fill({ action: 'flag' }) }]), /Invalid argument 1/); assert.throws(() => validateIpcArguments('mail:update-message', [{ uid: '4', action: 'star' }]), /uid/); assert.throws(() => validateIpcArguments('mail:update-message', [{ uid: 4, action: 'delete-anything' }]), /action/); assert.throws(() => validateIpcArguments('pgp:add-key-server', [{ name: 'Keys', url: 'https://keys.example.test', protocol: 'ftp' }]), /protocol/); assert.throws(() => validateIpcArguments('dav:save-event', [{ title: 'Event', _dav: { etag: 42 } }]), /_dav.etag/); }); test('prevents renderer-supplied raw SMTP messages while accepting standard compose fields', () => { assert.doesNotThrow(() => validateIpcArguments('mail:send', [{ to: 'friend@example.test', cc: '', subject: 'Hello', text: 'Body', attachments: [{ filename: 'note.txt', contentBase64: 'aGVsbG8=' }], pgp: { encrypt: true, sign: false, passphrase: null } }])); assert.doesNotThrow(() => validateIpcArguments('pgp:decrypt-message', [{ folder: 'Inbox', uid: 4, passphrase: null }])); assert.throws(() => validateIpcArguments('mail:send', [{ rawBase64: 'YWJj', envelope: { to: ['victim@example.test'] } }]), /Unsupported message field/); assert.throws(() => validateIpcArguments('mail:send', [{ pgp: { encrypt: 'true' } }]), /pgp.encrypt/); assert.throws(() => validateIpcArguments('mail:send', [{ attachments: [{ contentBase64: 123 }] }]), /contentBase64/); }); test('every registered main-process IPC handler has a request schema', () => { const mainPath = path.resolve(__dirname, '..', 'desktop', 'main.cjs'); const ipcDirectory = path.resolve(__dirname, '..', 'desktop', 'ipc'); const sourceFiles = [mainPath, ...fs.readdirSync(ipcDirectory) .filter(name => name.endsWith('.cjs')) .map(name => path.join(ipcDirectory, name))]; const channels = sourceFiles.flatMap(file => { const source = fs.readFileSync(file, 'utf8'); return [...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}`); } });