Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Refactor email decryption to util method (#541)
Leendert de Borst10f9d5e2b171
diff --git a/browser-extensions/chrome/src/app/components/EmailPreview.tsx b/browser-extensions/chrome/src/app/components/EmailPreview.tsxindex 3ef476247..81c6be9e7 100644--- a/browser-extensions/chrome/src/app/components/EmailPreview.tsx+++ b/browser-extensions/chrome/src/app/components/EmailPreview.tsx@@ -2,7 +2,6 @@ import React, { useState, useEffect } from 'react';import { useWebApi } from '../context/WebApiContext';import { useDb } from '../context/DbContext';import EncryptionUtility from '../../shared/EncryptionUtility';-import { Buffer } from 'buffer';import { MailboxEmail } from '../../shared/types/webapi/MailboxEmail';import { Link } from 'react-router-dom';@@ -21,6 +20,9 @@ export const EmailPreview: React.FC<EmailPreviewProps> = ({ email }) => {const webApi = useWebApi();const dbContext = useDb();+ /**+ * Checks if the email is a public domain.+ */const isPublicDomain = async (emailAddress: string): Promise<boolean> => {// Get metadata from storageconst storageResult = await chrome.storage.session.get(['publicEmailDomains']);@@ -65,57 +67,9 @@ export const EmailPreview: React.FC<EmailPreviewProps> = ({ email }) => {if (latestMails) {// Loop through all emails and decrypt them locally- const decryptedEmails: MailboxEmail[] = await Promise.all(- latestMails.map(async (mail: MailboxEmail) => {- try {- // Decrypt email locally using private key- const encryptionKeys = dbContext.sqliteClient!.getAllEncryptionKeys();- const encryptionKey = encryptionKeys.find(- key => key.PublicKey === mail.encryptionKey- );-- if (!encryptionKey) {- throw new Error('Encryption key not found');- }-- // Decrypt symmetric key with asymmetric private key- const symmetricKey = await EncryptionUtility.decryptWithPrivateKey(- mail.encryptedSymmetricKey,- encryptionKey.PrivateKey- );- const symmetricKeyBase64 = Buffer.from(symmetricKey).toString('base64');-- // Create a new object to avoid mutating the original- const decryptedMail = { ...mail };-- // Decrypt all email fields- decryptedMail.subject = await EncryptionUtility.symmetricDecrypt(- mail.subject,- symmetricKeyBase64- );- decryptedMail.fromDisplay = await EncryptionUtility.symmetricDecrypt(- mail.fromDisplay,- symmetricKeyBase64- );- decryptedMail.fromDomain = await EncryptionUtility.symmetricDecrypt(- mail.fromDomain,- symmetricKeyBase64- );- decryptedMail.fromLocal = await EncryptionUtility.symmetricDecrypt(- mail.fromLocal,- symmetricKeyBase64- );- decryptedMail.messagePreview = await EncryptionUtility.symmetricDecrypt(- mail.messagePreview,- symmetricKeyBase64- );-- return decryptedMail;- } catch (error) {- console.error('Error decrypting email:', error);- return mail; // Return original mail if decryption fails- }- })+ const decryptedEmails: MailboxEmail[] = await EncryptionUtility.decryptEmailList(+ latestMails,+ dbContext.sqliteClient!.getAllEncryptionKeys());if (loading && decryptedEmails.length > 0) {diff --git a/browser-extensions/chrome/src/app/pages/CredentialDetails.tsx b/browser-extensions/chrome/src/app/pages/CredentialDetails.tsxindex d9c9176d4..eccd01ffb 100644--- a/browser-extensions/chrome/src/app/pages/CredentialDetails.tsx+++ b/browser-extensions/chrome/src/app/pages/CredentialDetails.tsx@@ -99,19 +99,19 @@ const CredentialDetails: React.FC = () => {title="Open in new window"><svg- className="w-5 h-5"- fill="none"- stroke="currentColor"- viewBox="0 0 24 24"- xmlns="http://www.w3.org/2000/svg"- >- <path- strokeLinecap="round"- strokeLinejoin="round"- strokeWidth={2}- d="M4 8V4m0 0h4M4 4l5 5m11-1V4m0 0h-4m4 0l-5 5M4 16v4m0 0h4m-4 0l5-5m11 5l-5-5m5 5v-4m0 4h-4"- />- </svg>+ className="w-5 h-5"+ fill="none"+ stroke="currentColor"+ viewBox="0 0 24 24"+ xmlns="http://www.w3.org/2000/svg"+ >+ <path+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth={2}+ d="M4 8V4m0 0h4M4 4l5 5m11-1V4m0 0h-4m4 0l-5 5M4 16v4m0 0h4m-4 0l5-5m11 5l-5-5m5 5v-4m0 4h-4"+ />+ </svg></button></div>diff --git a/browser-extensions/chrome/src/app/pages/EmailDetails.tsx b/browser-extensions/chrome/src/app/pages/EmailDetails.tsxindex 2ab0fba94..cf394e1b6 100644--- a/browser-extensions/chrome/src/app/pages/EmailDetails.tsx+++ b/browser-extensions/chrome/src/app/pages/EmailDetails.tsx@@ -6,7 +6,6 @@ import { useWebApi } from '../context/WebApiContext';import LoadingSpinner from '../components/LoadingSpinner';import { useMinDurationLoading } from '../hooks/useMinDurationLoading';import EncryptionUtility from '../../shared/EncryptionUtility';-import { Buffer } from 'buffer';/*** Email details page.@@ -42,35 +41,10 @@ const EmailDetails: React.FC = () => {const response = await webApi.get<Email>(`Email/${id}`);- // Decrypt email locally using private key+ // Decrypt email locally using public/private key pairsconst encryptionKeys = dbContext.sqliteClient.getAllEncryptionKeys();- const encrytionKey = encryptionKeys.find(key => key.PublicKey === response.encryptionKey);-- if (!encrytionKey) {- throw new Error('Encryption key not found');- }-- // Decrypt symmetric key with assymetric private key- const symmetricKey = await EncryptionUtility.decryptWithPrivateKey(- response.encryptedSymmetricKey,- encrytionKey.PrivateKey- );- const symmetricKeyBase64 = Buffer.from(symmetricKey).toString('base64');-- // Decrypt all email fields- response.subject = await EncryptionUtility.symmetricDecrypt(response.subject, symmetricKeyBase64);- response.fromDisplay = await EncryptionUtility.symmetricDecrypt(response.fromDisplay, symmetricKeyBase64);- response.fromDomain = await EncryptionUtility.symmetricDecrypt(response.fromDomain, symmetricKeyBase64);- response.fromLocal = await EncryptionUtility.symmetricDecrypt(response.fromLocal, symmetricKeyBase64);-- if (response.messageHtml) {- response.messageHtml = await EncryptionUtility.symmetricDecrypt(response.messageHtml, symmetricKeyBase64);- }- if (response.messagePlain) {- response.messagePlain = await EncryptionUtility.symmetricDecrypt(response.messagePlain, symmetricKeyBase64);- }-- setEmail(response);+ const decryptedEmail = await EncryptionUtility.decryptEmail(response, encryptionKeys);+ setEmail(decryptedEmail);} catch (err) {setError(err instanceof Error ? err.message : 'An error occurred');} finally {@@ -150,19 +124,19 @@ const EmailDetails: React.FC = () => {title="Open in new window"><svg- className="w-5 h-5"- fill="none"- stroke="currentColor"- viewBox="0 0 24 24"- xmlns="http://www.w3.org/2000/svg"- >- <path- strokeLinecap="round"- strokeLinejoin="round"- strokeWidth={2}- d="M4 8V4m0 0h4M4 4l5 5m11-1V4m0 0h-4m4 0l-5 5M4 16v4m0 0h4m-4 0l5-5m11 5l-5-5m5 5v-4m0 4h-4"- />- </svg>+ className="w-5 h-5"+ fill="none"+ stroke="currentColor"+ viewBox="0 0 24 24"+ xmlns="http://www.w3.org/2000/svg"+ >+ <path+ strokeLinecap="round"+ strokeLinejoin="round"+ strokeWidth={2}+ d="M4 8V4m0 0h4M4 4l5 5m11-1V4m0 0h-4m4 0l-5 5M4 16v4m0 0h4m-4 0l5-5m11 5l-5-5m5 5v-4m0 4h-4"+ />+ </svg></button><buttononClick={handleDelete}diff --git a/browser-extensions/chrome/src/app/pages/EmailsList.tsx b/browser-extensions/chrome/src/app/pages/EmailsList.tsxindex 19f5a654d..2c95d3c5a 100644--- a/browser-extensions/chrome/src/app/pages/EmailsList.tsx+++ b/browser-extensions/chrome/src/app/pages/EmailsList.tsx@@ -6,7 +6,6 @@ import { useWebApi } from '../context/WebApiContext';import LoadingSpinner from '../components/LoadingSpinner';import { useMinDurationLoading } from '../hooks/useMinDurationLoading';import EncryptionUtility from '../../shared/EncryptionUtility';-import { Buffer } from 'buffer';import ReloadButton from '../components/ReloadButton';import { Link } from 'react-router-dom';/**@@ -54,24 +53,8 @@ const EmailsList: React.FC = () => {// Decrypt emails locally using private key associated with the email address.const encryptionKeys = dbContext.sqliteClient.getAllEncryptionKeys();- const decryptedEmails = await Promise.all(data.mails.map(async email => {- const encrytionKey = encryptionKeys.find(key => key.PublicKey === email.encryptionKey);- if (!encrytionKey) {- throw new Error(`Encryption key not found for email: ${email.fromDisplay}`);- }-- // Decrypt symmetric key with assymetric private key.- const symmetricKey = await EncryptionUtility.decryptWithPrivateKey(email.encryptedSymmetricKey, encrytionKey.PrivateKey);- const symmetricKeyBase64 = Buffer.from(symmetricKey).toString('base64');-- // Decrypt email with decrypted symmetric key.- email.subject = await EncryptionUtility.symmetricDecrypt(email.subject, symmetricKeyBase64);- email.fromDisplay = await EncryptionUtility.symmetricDecrypt(email.fromDisplay, symmetricKeyBase64);- email.fromDomain = await EncryptionUtility.symmetricDecrypt(email.fromDomain, symmetricKeyBase64);- email.fromLocal = await EncryptionUtility.symmetricDecrypt(email.fromLocal, symmetricKeyBase64);- email.messagePreview = await EncryptionUtility.symmetricDecrypt(email.messagePreview, symmetricKeyBase64);- return email;- }));+ // Decrypt emails locally using public/private key pairs.+ const decryptedEmails = await EncryptionUtility.decryptEmailList(data.mails, encryptionKeys);setEmails(decryptedEmails);} catch (error) {diff --git a/browser-extensions/chrome/src/shared/EncryptionUtility.tsx b/browser-extensions/chrome/src/shared/EncryptionUtility.tsxindex d914abe91..b83b7ff9f 100644--- a/browser-extensions/chrome/src/shared/EncryptionUtility.tsx+++ b/browser-extensions/chrome/src/shared/EncryptionUtility.tsx@@ -1,4 +1,8 @@import argon2 from 'argon2-browser/dist/argon2-bundled.min.js';+import { Email } from './types/webapi/Email';+import { EncryptionKey } from './types/EncryptionKey';+import { MailboxEmail } from './types/webapi/MailboxEmail';+import { Buffer } from 'buffer';/*** Utility class for encryption operations including:@@ -158,7 +162,7 @@ class EncryptionUtility {encodedPlaintext);- return btoa(String.fromCharCode.apply(null, new Uint8Array(cipherBuffer)));+ return btoa(String.fromCharCode.apply(null, Array.from(new Uint8Array(cipherBuffer))));}/**@@ -193,6 +197,91 @@ class EncryptionUtility {throw new Error(`Failed to decrypt: ${error.message}`);}}++ /**+ * Decrypts an individual email based on the provided public/private key pairs.+ */+ public static async decryptEmail(+ email: Email,+ encryptionKeys: EncryptionKey[]+ ): Promise<Email> {+ try {+ const encryptionKey = encryptionKeys.find(key => key.PublicKey === email.encryptionKey);++ if (!encryptionKey) {+ throw new Error('Encryption key not found');+ }++ // Decrypt symmetric key with asymmetric private key+ const symmetricKey = await EncryptionUtility.decryptWithPrivateKey(+ email.encryptedSymmetricKey,+ encryptionKey.PrivateKey+ );+ const symmetricKeyBase64 = Buffer.from(symmetricKey).toString('base64');++ // Create a new object to avoid mutating the original+ const decryptedEmail = { ...email };++ // Decrypt all email fields+ decryptedEmail.subject = await EncryptionUtility.symmetricDecrypt(email.subject, symmetricKeyBase64);+ decryptedEmail.fromDisplay = await EncryptionUtility.symmetricDecrypt(email.fromDisplay, symmetricKeyBase64);+ decryptedEmail.fromDomain = await EncryptionUtility.symmetricDecrypt(email.fromDomain, symmetricKeyBase64);+ decryptedEmail.fromLocal = await EncryptionUtility.symmetricDecrypt(email.fromLocal, symmetricKeyBase64);++ if (email.messageHtml) {+ decryptedEmail.messageHtml = await EncryptionUtility.symmetricDecrypt(email.messageHtml, symmetricKeyBase64);+ }+ if (email.messagePlain) {+ decryptedEmail.messagePlain = await EncryptionUtility.symmetricDecrypt(email.messagePlain, symmetricKeyBase64);+ }++ return decryptedEmail;+ } catch (err) {+ throw new Error(err instanceof Error ? err.message : 'Failed to decrypt email');+ }+ }++ /**+ * Decrypts a list of emails based on the provided public/private key pairs.+ */+ public static async decryptEmailList(+ emails: MailboxEmail[],+ encryptionKeys: EncryptionKey[]+ ): Promise<MailboxEmail[]> {+ return Promise.all(emails.map(async email => {+ try {+ const encryptionKey = encryptionKeys.find(key => key.PublicKey === email.encryptionKey);++ if (!encryptionKey) {+ throw new Error('Encryption key not found');+ }++ // Decrypt symmetric key with asymmetric private key+ const symmetricKey = await EncryptionUtility.decryptWithPrivateKey(+ email.encryptedSymmetricKey,+ encryptionKey.PrivateKey+ );+ const symmetricKeyBase64 = Buffer.from(symmetricKey).toString('base64');++ // Create a new object to avoid mutating the original+ const decryptedEmail = { ...email };++ // Decrypt all email fields+ decryptedEmail.subject = await EncryptionUtility.symmetricDecrypt(email.subject, symmetricKeyBase64);+ decryptedEmail.fromDisplay = await EncryptionUtility.symmetricDecrypt(email.fromDisplay, symmetricKeyBase64);+ decryptedEmail.fromDomain = await EncryptionUtility.symmetricDecrypt(email.fromDomain, symmetricKeyBase64);+ decryptedEmail.fromLocal = await EncryptionUtility.symmetricDecrypt(email.fromLocal, symmetricKeyBase64);++ if (email.messagePreview) {+ decryptedEmail.messagePreview = await EncryptionUtility.symmetricDecrypt(email.messagePreview, symmetricKeyBase64);+ }++ return decryptedEmail;+ } catch (err) {+ throw new Error(err instanceof Error ? err.message : 'Failed to decrypt email');+ }+ }));+ }}export default EncryptionUtility;