Refractoring Stage 4 Completed IPC hardening
This commit is contained in:
+3
-1
@@ -86,7 +86,8 @@ This is a gradual destination, not a requirement to move everything at once. IPC
|
|||||||
|
|
||||||
- [x] Verify the IPC sender is the expected top-level application document (`desktop/security/ipc-sender.cjs`).
|
- [x] Verify the IPC sender is the expected top-level application document (`desktop/security/ipc-sender.cjs`).
|
||||||
- [x] Restrict navigation to the exact app document URL; reject arbitrary `file:` navigation.
|
- [x] Restrict navigation to the exact app document URL; reject arbitrary `file:` navigation.
|
||||||
- [ ] Validate argument shape, type, size, and authority-sensitive values at the IPC boundary.
|
- [x] Validate channel names, argument counts/types, and nested payload complexity (`desktop/security/ipc-arguments.cjs`).
|
||||||
|
- [ ] Validate channel-specific field limits and authority-sensitive values, including DAV remote object references.
|
||||||
- [ ] Register handlers by feature and keep `preload.cjs` as the narrow renderer API.
|
- [ ] Register handlers by feature and keep `preload.cjs` as the narrow renderer API.
|
||||||
- [ ] Return deliberate DTOs rather than internal store objects.
|
- [ ] Return deliberate DTOs rather than internal store objects.
|
||||||
|
|
||||||
@@ -126,4 +127,5 @@ This is a gradual destination, not a requirement to move everything at once. IPC
|
|||||||
- Step 4 completed with extracted IDLE and snooze services; the full suite passed 92 tests.
|
- Step 4 completed with extracted IDLE and snooze services; the full suite passed 92 tests.
|
||||||
- Step 5 added a trusted top-level sender check to all 54 registered IPC handlers; sender boundary tests pass.
|
- Step 5 added a trusted top-level sender check to all 54 registered IPC handlers; sender boundary tests pass.
|
||||||
- Step 5 also aligned `will-navigate` with the same exact-document policy; `npm test` passes 96 tests.
|
- Step 5 also aligned `will-navigate` with the same exact-document policy; `npm test` passes 96 tests.
|
||||||
|
- Step 5 added request schemas for all 54 registered channels, plus depth/collection/string bounds. Base64 attachment payloads remain uncapped to preserve the existing no-total-size-cap behavior; `npm test` passes 100 tests.
|
||||||
- While work continued, `package.json` and `package-lock.json` reflected a version bump to `0.4.6`; those changes were preserved.
|
- While work continued, `package.json` and `package-lock.json` reflected a version bump to `0.4.6`; those changes were preserved.
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ const { createBackgroundSyncCoordinator } = require('./mail/sync-coordinator.cjs
|
|||||||
const { createIdleWatcherManager } = require('./mail/idle-watcher-manager.cjs');
|
const { createIdleWatcherManager } = require('./mail/idle-watcher-manager.cjs');
|
||||||
const { createSnoozeReminderService } = require('./mail/snooze-reminder-service.cjs');
|
const { createSnoozeReminderService } = require('./mail/snooze-reminder-service.cjs');
|
||||||
const { assertTrustedIpcSender, isTrustedApplicationUrl } = require('./security/ipc-sender.cjs');
|
const { assertTrustedIpcSender, isTrustedApplicationUrl } = require('./security/ipc-sender.cjs');
|
||||||
|
const { validateIpcArguments } = require('./security/ipc-arguments.cjs');
|
||||||
|
|
||||||
const credentialsPath = () => path.join(app.getPath('userData'), 'mail-account.enc');
|
const credentialsPath = () => path.join(app.getPath('userData'), 'mail-account.enc');
|
||||||
const davCredentialsPath = () => path.join(app.getPath('userData'), 'dav-accounts.enc');
|
const davCredentialsPath = () => path.join(app.getPath('userData'), 'dav-accounts.enc');
|
||||||
@@ -76,6 +77,7 @@ let tray = null;
|
|||||||
function registerIpcHandler(channel, handler) {
|
function registerIpcHandler(channel, handler) {
|
||||||
return ipcMainHandle(channel, (event, ...args) => {
|
return ipcMainHandle(channel, (event, ...args) => {
|
||||||
assertTrustedIpcSender(event, applicationHtmlPath);
|
assertTrustedIpcSender(event, applicationHtmlPath);
|
||||||
|
validateIpcArguments(channel, args);
|
||||||
return handler(event, ...args);
|
return handler(event, ...args);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,142 @@
|
|||||||
|
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 };
|
||||||
Generated
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "ep-mail",
|
"name": "ep-mail",
|
||||||
"version": "0.4.7",
|
"version": "0.4.8",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "ep-mail",
|
"name": "ep-mail",
|
||||||
"version": "0.4.7",
|
"version": "0.4.8",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@capacitor/android": "^8.5.2",
|
"@capacitor/android": "^8.5.2",
|
||||||
"@capacitor/core": "^8.5.2",
|
"@capacitor/core": "^8.5.2",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ep-mail",
|
"name": "ep-mail",
|
||||||
"version": "0.4.7",
|
"version": "0.4.8",
|
||||||
"private": true,
|
"private": true,
|
||||||
"description": "Eternity Project mail client",
|
"description": "Eternity Project mail client",
|
||||||
"homepage": "https://git.eternityproject.fi/thuttu/ep-mail-client",
|
"homepage": "https://git.eternityproject.fi/thuttu/ep-mail-client",
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
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}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user