Fix PGP security review items and add architecture docs
- Harden key replacement in the Electron main process with a confirmation dialog showing the old and new fingerprints before accepting a replacement. - Enforce plaintext-send checks in the main process before allowing a message to be sent without encryption when recipient keys exist. - Reject the insecure Linux safeStorage basic_text backend before writing mail credentials or OpenPGP keys. - Update the security review findings to reflect the fixes and mark the related TODO item as complete. - Add a project architecture overview and link it from the main README.
This commit is contained in:
+61
-2
@@ -7,6 +7,7 @@ const { simpleParser } = require('mailparser');
|
||||
const { randomUUID } = require('node:crypto');
|
||||
const { createDAVClient } = require('tsdav');
|
||||
const ICAL = require('ical.js');
|
||||
const openpgp = require('openpgp');
|
||||
|
||||
const credentialsPath = () => path.join(app.getPath('userData'), 'mail-account.enc');
|
||||
const davCredentialsPath = () => path.join(app.getPath('userData'), 'dav-accounts.enc');
|
||||
@@ -200,6 +201,7 @@ const pgpKeysPath = () => path.join(app.getPath('userData'), 'pgp-keys.enc');
|
||||
const pgpKeys = createKeyStore({
|
||||
async load() {
|
||||
try {
|
||||
assertSecureStorage();
|
||||
const { result } = await safeStorage.decryptStringAsync(await fs.readFile(pgpKeysPath()));
|
||||
return JSON.parse(result);
|
||||
} catch (error) {
|
||||
@@ -208,6 +210,7 @@ const pgpKeys = createKeyStore({
|
||||
}
|
||||
},
|
||||
async save(data) {
|
||||
assertSecureStorage();
|
||||
if (!await safeStorage.isAsyncEncryptionAvailable()) throw new Error('Secure key storage is unavailable.');
|
||||
await fs.mkdir(path.dirname(pgpKeysPath()), { recursive: true });
|
||||
await fs.writeFile(`${pgpKeysPath()}.tmp`, await safeStorage.encryptStringAsync(JSON.stringify(data)), { mode: 0o600 });
|
||||
@@ -220,6 +223,7 @@ const pgpMail = createPgpMail({ keyStore: pgpKeys, cache: pgpCache });
|
||||
|
||||
async function readDavStore() {
|
||||
try {
|
||||
assertSecureStorage();
|
||||
const encrypted = await fs.readFile(davCredentialsPath());
|
||||
const { result } = await safeStorage.decryptStringAsync(encrypted);
|
||||
return JSON.parse(result);
|
||||
@@ -230,6 +234,7 @@ async function readDavStore() {
|
||||
}
|
||||
|
||||
async function saveDavStore(store) {
|
||||
assertSecureStorage();
|
||||
if (!await safeStorage.isAsyncEncryptionAvailable()) throw new Error('Secure credential storage is unavailable.');
|
||||
const encrypted = await safeStorage.encryptStringAsync(JSON.stringify(store));
|
||||
await fs.mkdir(path.dirname(davCredentialsPath()), { recursive: true });
|
||||
@@ -340,9 +345,19 @@ function smtpTransport(account) {
|
||||
});
|
||||
}
|
||||
|
||||
function assertSecureStorage() {
|
||||
if (process.platform === 'linux' && typeof safeStorage.getSelectedStorageBackend === 'function') {
|
||||
const backend = safeStorage.getSelectedStorageBackend();
|
||||
if (backend === 'basic_text') {
|
||||
throw new Error('This Linux session is using the insecure basic_text password backend. Configure a secure secret store such as gnome-libsecret or kwallet before saving mail credentials or OpenPGP keys.');
|
||||
}
|
||||
}
|
||||
if (!safeStorage.isEncryptionAvailable()) throw new Error('OS credential storage is unavailable.');
|
||||
}
|
||||
|
||||
async function readAccountStore() {
|
||||
try {
|
||||
if (!safeStorage.isEncryptionAvailable()) throw new Error('OS credential storage is unavailable.');
|
||||
assertSecureStorage();
|
||||
const encrypted = await fs.readFile(credentialsPath());
|
||||
const { result } = await safeStorage.decryptStringAsync(encrypted);
|
||||
const stored = JSON.parse(result);
|
||||
@@ -361,6 +376,7 @@ async function readAccountStore() {
|
||||
}
|
||||
|
||||
async function saveAccountStore(store) {
|
||||
assertSecureStorage();
|
||||
if (!await safeStorage.isAsyncEncryptionAvailable()) {
|
||||
throw new Error('Secure credential storage is unavailable on this computer. Set up its system keyring, then try again.');
|
||||
}
|
||||
@@ -1381,6 +1397,32 @@ ipcMain.handle('pgp:add-contact', async (event, input) => {
|
||||
if ((await fs.stat(result.filePaths[0])).size > 200000) throw new Error('That key file is too large.');
|
||||
armoredKey = await fs.readFile(result.filePaths[0], 'utf8');
|
||||
}
|
||||
const key = await openpgp.readKey({ armoredKey }).catch(() => {
|
||||
throw new Error('That is not a valid OpenPGP public key.');
|
||||
});
|
||||
const fingerprint = key.getFingerprint().toUpperCase();
|
||||
const emails = [...new Set(key.getUserIDs().map(id => (/<([^>]+)>/.exec(id)?.[1] || (id.includes('@') ? id : '')).trim().toLowerCase()).filter(Boolean))];
|
||||
const existing = await pgpKeys.listContacts();
|
||||
const overlaps = existing.filter(item => (item.emails || []).some(email => emails.includes(String(email).toLowerCase())));
|
||||
if (overlaps.length) {
|
||||
const detail = overlaps.map(item => {
|
||||
const name = item.name || 'Unknown contact';
|
||||
const addresses = (item.emails || []).join(', ') || 'no address';
|
||||
const fp = String(item.fingerprint || '').replace(/(.{4})/g, '$1 ').trim();
|
||||
return `${name} (${addresses})\nFingerprint: ${fp}`;
|
||||
}).join('\n\n');
|
||||
const { response } = await dialog.showMessageBox(BrowserWindow.fromWebContents(event.sender), {
|
||||
type: 'warning',
|
||||
buttons: ['Cancel', 'Replace key'],
|
||||
defaultId: 0,
|
||||
cancelId: 0,
|
||||
title: 'Confirm key replacement',
|
||||
message: 'A different public key already exists for this email address.',
|
||||
detail: `New key:\n${fingerprint.replace(/(.{4})/g, '$1 ').trim()}\n\nExisting key(s):\n${detail}\n\nOnly continue if you verified the new fingerprint with the owner.`,
|
||||
noLink: true
|
||||
});
|
||||
if (response !== 1) throw new Error('Key replacement cancelled.');
|
||||
}
|
||||
return pgpKeys.addContact({ armoredKey, source: input?.source === 'message' ? 'message' : 'import' });
|
||||
});
|
||||
ipcMain.handle('pgp:set-trust', async (event, fingerprint, trust) => {
|
||||
@@ -1496,12 +1538,29 @@ async function sendMailNow(account, message) {
|
||||
return { ok: true, savedToSent };
|
||||
}
|
||||
|
||||
ipcMain.handle('mail:send', async (_event, message) => {
|
||||
ipcMain.handle('mail:send', async (event, message) => {
|
||||
const account = await readAccount();
|
||||
if (!account) throw new Error('Add a mail account before sending.');
|
||||
const pgpOptions = message?.pgp && (message.pgp.encrypt || message.pgp.sign) ? message.pgp : null;
|
||||
const queuedMessage = { ...message, messageId: message?.messageId || `<${randomUUID()}@ep-mail.local>` };
|
||||
delete queuedMessage.pgp;
|
||||
if (!pgpOptions?.encrypt) {
|
||||
const recipients = [queuedMessage.to, queuedMessage.cc].filter(Boolean);
|
||||
const status = await pgpMail.recipientStatus(recipients, { sign: Boolean(pgpOptions?.sign), from: account.email });
|
||||
if (status.recipients.length && !status.missing.length) {
|
||||
const { response } = await dialog.showMessageBox(BrowserWindow.fromWebContents(event.sender), {
|
||||
type: 'warning',
|
||||
buttons: ['Cancel', 'Send anyway'],
|
||||
defaultId: 0,
|
||||
cancelId: 0,
|
||||
title: 'Send without encryption?',
|
||||
message: 'Public keys are available for all recipients.',
|
||||
detail: 'This message is about to be sent without encryption. Continue only if you explicitly want to send it in plaintext.',
|
||||
noLink: true
|
||||
});
|
||||
if (response !== 1) throw new Error('Plaintext send cancelled.');
|
||||
}
|
||||
}
|
||||
if (pgpOptions) {
|
||||
// Encrypt before sending or queueing so plaintext and passphrases are never stored in the outbox.
|
||||
const signature = String(account.signature || '').slice(0, 5000);
|
||||
|
||||
Reference in New Issue
Block a user