Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Make browser extension gracefully degrade for email view with upcoming 0.31+ API (#2346)
Leendert de Borst1973a318e809
diff --git a/apps/browser-extension/src/entrypoints/popup/pages/emails/EmailDetails.tsx b/apps/browser-extension/src/entrypoints/popup/pages/emails/EmailDetails.tsxindex 3ea7601f7..e661284ea 100644--- a/apps/browser-extension/src/entrypoints/popup/pages/emails/EmailDetails.tsx+++ b/apps/browser-extension/src/entrypoints/popup/pages/emails/EmailDetails.tsx@@ -77,11 +77,16 @@ const EmailDetails: React.FC = (): React.ReactElement => {const decryptedEmail = await EncryptionUtility.decryptEmail(response, encryptionKeys);setEmail(decryptedEmail);- // Set initial view mode based on available content+ /*+ * Set initial view mode based on available content. Emails received by newer server+ * versions only carry the raw source, so fall back to that when no rendered body exists.+ */if (decryptedEmail.messageHtml) {setViewMode('html');} else if (decryptedEmail.messagePlain) {setViewMode('plain');+ } else if (decryptedEmail.messageSource) {+ setViewMode('source');}} catch (err) {setError(err instanceof Error ? err.message : 'An error occurred');@@ -127,6 +132,12 @@ const EmailDetails: React.FC = (): React.ReactElement => {return modes;}, [email]);+ /*+ * Emails stored by newer server versions only contain the raw source. Until this client can+ * render those itself we show the source verbatim plus a notice to update.+ */+ const isSourceOnly = Boolean(email?.messageSource) && !email?.messageHtml && !email?.messagePlain;+const formatLabels = useMemo<Record<'html' | 'plain' | 'source', string>>(() => ({html: t('emails.formatHtml'),plain: t('emails.formatPlain'),@@ -336,6 +347,12 @@ const EmailDetails: React.FC = (): React.ReactElement => {)}</div>+ {isSourceOnly && (+ <div className="mt-4 p-3 rounded-lg border border-amber-300 bg-amber-50 text-sm text-amber-800 dark:border-amber-700 dark:bg-amber-900/30 dark:text-amber-200">+ {t('emails.updateClientForFormattedView')}+ </div>+ )}+{/* Email Body — always rendered on a white background with dark text so contrast doesn't break in dark mode. */}<div className="bg-white mt-4">{viewMode === 'html' && email.messageHtml ? (diff --git a/apps/browser-extension/src/i18n/locales/en.json b/apps/browser-extension/src/i18n/locales/en.jsonindex 727c0dcfc..55d1b6894 100644--- a/apps/browser-extension/src/i18n/locales/en.json+++ b/apps/browser-extension/src/i18n/locales/en.json@@ -366,6 +366,7 @@"noEmails": "No emails found","noEmailsDescription": "You have not received any emails at your private email addresses yet. When you receive a new email, it will appear here.","offlineMessage": "You are offline. Please connect to the internet to load your emails.",+ "updateClientForFormattedView": "This email requires a newer app version to display properly. Please update your app.","loadMore": "Load More ({{count}} remaining)","dateFormat": {"justNow": "just now",diff --git a/apps/browser-extension/src/utils/EncryptionUtility.ts b/apps/browser-extension/src/utils/EncryptionUtility.tsindex 085cd5e51..117e3a999 100644--- a/apps/browser-extension/src/utils/EncryptionUtility.ts+++ b/apps/browser-extension/src/utils/EncryptionUtility.ts@@ -152,6 +152,41 @@ export class EncryptionUtility {return new Uint8Array(decrypted);}+ /**+ * Decrypts a base64 AES-GCM payload that may be gzip compressed after decryption.+ */+ public static async symmetricDecryptMaybeCompressed(base64Ciphertext: string, base64Key: string): Promise<string> {+ if (!base64Ciphertext) {+ return base64Ciphertext;+ }++ const encryptedBytes = Uint8Array.from(atob(base64Ciphertext), c => c.charCodeAt(0));+ const decryptedBytes = await EncryptionUtility.symmetricDecryptBytes(encryptedBytes, base64Key);++ return await EncryptionUtility.decodeMaybeGzipped(decryptedBytes);+ }++ /**+ * Decodes bytes as UTF-8, attempting to gunzip them first when they carry the gzip magic header.+ */+ private static async decodeMaybeGzipped(bytes: Uint8Array): Promise<string> {+ const decoder = new TextDecoder();++ // Gzip magic number (0x1f 0x8b); anything else is treated as plain UTF-8.+ if (bytes.length < 2 || bytes[0] !== 0x1f || bytes[1] !== 0x8b) {+ return decoder.decode(bytes);+ }++ if (typeof DecompressionStream === 'undefined') {+ throw new Error('Gzip decompression is not supported by this browser');+ }++ const stream = new Blob([bytes as BlobPart]).stream().pipeThrough(new DecompressionStream('gzip'));+ const decompressed = await new Response(stream).arrayBuffer();++ return decoder.decode(decompressed);+ }+/*** Generates a new RSA key pair for asymmetric encryption*/@@ -337,7 +372,8 @@ export class EncryptionUtility {decryptedEmail.messagePlain = await EncryptionUtility.symmetricDecrypt(email.messagePlain, symmetricKeyBase64);}if (email.messageSource) {- decryptedEmail.messageSource = await EncryptionUtility.symmetricDecrypt(email.messageSource, symmetricKeyBase64);+ // The raw source is optionally gzip compressed before encryption in v0.31+ APIs.+ decryptedEmail.messageSource = await EncryptionUtility.symmetricDecryptMaybeCompressed(email.messageSource, symmetricKeyBase64);}return decryptedEmail;diff --git a/apps/browser-extension/src/utils/__tests__/EncryptionUtility.crypto.test.ts b/apps/browser-extension/src/utils/__tests__/EncryptionUtility.crypto.test.tsindex a3f608839..a522aef58 100644--- a/apps/browser-extension/src/utils/__tests__/EncryptionUtility.crypto.test.ts+++ b/apps/browser-extension/src/utils/__tests__/EncryptionUtility.crypto.test.ts@@ -86,6 +86,15 @@ async function encryptAttachmentBytes(plaintext: Uint8Array, rawSymmetricKey: streturn encryptedBytes;}+/**+ * Gzip compresses a string.+ */+async function gzip(plaintext: string): Promise<Uint8Array> {+ const stream = new Blob([new TextEncoder().encode(plaintext) as BlobPart]).stream()+ .pipeThrough(new CompressionStream('gzip'));+ return new Uint8Array(await new Response(stream).arrayBuffer());+}+/*** Counts non-extractable RSA private-key imports.*/@@ -224,3 +233,58 @@ describe('email RSA private key cache', () => {}});});++describe('decryptEmail message source', () => {+ const rawSource = 'Subject: Hello\r\nContent-Type: text/plain\r\n\r\nRaw RFC 822 body.';++ /**+ * Builds an email whose message source holds the supplied already-encoded bytes.+ */+ async function createEmailWithSource(+ sourceBytes: Uint8Array,+ encryptionKey: EncryptionKey,+ rawSymmetricKey: string+ ): Promise<Email> {+ const email = await createEmail(encryptionKey, rawSymmetricKey);+ const encryptedBytes = await encryptAttachmentBytes(sourceBytes, rawSymmetricKey);+ email.messageSource = Buffer.from(encryptedBytes).toString('base64');+ return email;+ }++ /**+ * Creates an RSA encryption key pair for the test.+ */+ async function createEncryptionKey(): Promise<EncryptionKey> {+ const keyPair = await EncryptionUtility.generateRsaKeyPair();+ return {+ Id: 'key-a',+ PublicKey: keyPair.publicKey,+ PrivateKey: keyPair.privateKey,+ IsPrimary: true,+ };+ }++ it('decrypts an uncompressed message source', async () => {+ const encryptionKey = await createEncryptionKey();+ const rawSymmetricKey = '0123456789abcdef0123456789abcdef';+ const email = await createEmailWithSource(+ new TextEncoder().encode(rawSource),+ encryptionKey,+ rawSymmetricKey+ );++ const decrypted = await EncryptionUtility.decryptEmail(email, [encryptionKey]);++ expect(decrypted.messageSource).toBe(rawSource);+ });++ it('gunzips a compressed message source', async () => {+ const encryptionKey = await createEncryptionKey();+ const rawSymmetricKey = '0123456789abcdef0123456789abcdef';+ const email = await createEmailWithSource(await gzip(rawSource), encryptionKey, rawSymmetricKey);++ const decrypted = await EncryptionUtility.decryptEmail(email, [encryptionKey]);++ expect(decrypted.messageSource).toBe(rawSource);+ });+});