Fix silent error handling in message encryption

Previously, if encryption failed for any recipient, the error was only
logged to console and the code continued, resulting in a message being
sent that some recipients couldn't decrypt. This creates a security and
reliability issue.

Changes:
- Collect all failed encryption attempts instead of silently logging
- Throw an error if any recipient encryption fails
- Provide detailed error message listing number of failures
- Implement all-or-nothing approach to prevent partial message delivery

This ensures senders are notified immediately if message encryption
fails for any recipient, preventing silent security failures.
This commit is contained in:
Claude 2026-01-19 06:31:02 +00:00
parent 3218287c7e
commit 41b03b88d5
No known key found for this signature in database

View file

@ -175,6 +175,7 @@ export async function encryptMessage(message, recipientPublicKeys) {
// Encrypt AES key for each recipient with their RSA public key // Encrypt AES key for each recipient with their RSA public key
const encryptedKeys = {}; const encryptedKeys = {};
const failedRecipients = [];
for (const recipientKeyB64 of recipientPublicKeys) { for (const recipientKeyB64 of recipientPublicKeys) {
try { try {
@ -201,10 +202,22 @@ export async function encryptMessage(message, recipientPublicKeys) {
encryptedKeys[recipientKeyB64] = arrayBufferToBase64(encryptedKey); encryptedKeys[recipientKeyB64] = arrayBufferToBase64(encryptedKey);
} catch (error) { } catch (error) {
console.error('Failed to encrypt for recipient:', error); failedRecipients.push({
publicKey: recipientKeyB64.substring(0, 20) + '...', // Truncate for error message
error: error.message
});
} }
} }
// Throw error if any encryptions failed to prevent partial message delivery
if (failedRecipients.length > 0) {
throw new Error(
`Failed to encrypt message for ${failedRecipients.length} recipient(s): ${
failedRecipients.map(r => r.error).join(', ')
}`
);
}
return { return {
ciphertext: arrayBufferToBase64(encryptedMessage), ciphertext: arrayBufferToBase64(encryptedMessage),
iv: arrayBufferToBase64(iv), iv: arrayBufferToBase64(iv),