From 2474f45770a2a574739e550d5d04f0336e6cd7e6 Mon Sep 17 00:00:00 2001
From: martgil <46025304+martgil@users.noreply.github.com>
Date: Wed, 18 Nov 2026 16:56:54 +0800
Subject: [PATCH] fix(attachment-preview): require successful decrypt before
showing content
---
.../chrome/elements/attachment_preview.ts | 10 ++---
.../js/common/core/crypto/pgp/msg-util.ts | 4 +-
extension/js/common/message-renderer.ts | 3 +-
test/source/mock/fes/fes-constants.ts | 6 +--
.../strategies/send-message-strategy.ts | 8 ++--
test/source/tests/setup.ts | 2 +-
test/source/tests/unit-node.ts | 38 ++++++++++++++++++-
7 files changed, 53 insertions(+), 18 deletions(-)
diff --git a/extension/chrome/elements/attachment_preview.ts b/extension/chrome/elements/attachment_preview.ts
index 8feb553ce0c..63ba78f8ae1 100644
--- a/extension/chrome/elements/attachment_preview.ts
+++ b/extension/chrome/elements/attachment_preview.ts
@@ -7,7 +7,7 @@ import { Attachment } from '../../js/common/core/attachment.js';
import { AttachmentDownloadView } from './attachment.js';
import { BrowserMsg } from '../../js/common/browser/browser-msg.js';
import { KeyStore } from '../../js/common/platform/store/key-store.js';
-import { MsgUtil, DecryptError, DecryptErrTypes, DecryptSuccess, DecryptionError } from '../../js/common/core/crypto/pgp/msg-util.js';
+import { MsgUtil, DecryptErrTypes, DecryptionError } from '../../js/common/core/crypto/pgp/msg-util.js';
import { View } from '../../js/common/view.js';
import { Xss } from '../../js/common/platform/xss.js';
import { Ui } from '../../js/common/browser/ui.js';
@@ -107,17 +107,17 @@ View.run(
encryptedData: this.attachment.getData(),
verificationPubs: [], // todo: #4158 signature verification of attachments
});
- if ((result as DecryptSuccess).content) {
+ if (result.success) {
return result.content;
- } else if ((result as DecryptError).error.type === DecryptErrTypes.needPassphrase) {
+ } else if (result.error.type === DecryptErrTypes.needPassphrase) {
BrowserMsg.send.passphraseDialog(this.parentTabId, {
type: 'attachment',
- longids: (result as DecryptError).longids.needPassphrase,
+ longids: result.longids.needPassphrase,
initiatorFrameId: this.initiatorFrameId,
});
return;
}
- throw new DecryptionError(result as DecryptError);
+ throw new DecryptionError(result);
};
private renderPdf = async (data: Uint8Array, password?: string) => {
diff --git a/extension/js/common/core/crypto/pgp/msg-util.ts b/extension/js/common/core/crypto/pgp/msg-util.ts
index ddea796b4f4..a3ff8967635 100644
--- a/extension/js/common/core/crypto/pgp/msg-util.ts
+++ b/extension/js/common/core/crypto/pgp/msg-util.ts
@@ -83,7 +83,7 @@ export type DecryptError = {
success: false;
error: DecryptError$error;
longids: DecryptError$longids;
- content?: Buf;
+ unauthenticatedContent?: Buf; // present only for noMdc, must never be treated as successful decrypt content
isEncrypted?: boolean;
};
@@ -262,7 +262,7 @@ export class MsgUtil {
' The sender should update their outdated software.\n\nDisplay the message at your own risk.';
return {
success: false,
- content,
+ unauthenticatedContent: content,
error: { type: DecryptErrTypes.noMdc, message: noMdc },
longids,
isEncrypted,
diff --git a/extension/js/common/message-renderer.ts b/extension/js/common/message-renderer.ts
index 96ad1290ea6..c059d45de84 100644
--- a/extension/js/common/message-renderer.ts
+++ b/extension/js/common/message-renderer.ts
@@ -884,8 +884,7 @@ export class MessageRenderer {
} else if (result.error.type === DecryptErrTypes.wrongPwd || result.error.type === DecryptErrTypes.usePassword) {
renderModule.renderErr(Lang.pgpBlock.pwdMsgAskSenderUsePubkey, undefined);
} else if (result.error.type === DecryptErrTypes.noMdc) {
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- renderModule.renderErr(Xss.escape(result.error.message), result.content!.toUtfStr()); // missing mdc - only render the result after user confirmation
+ renderModule.renderErr(Xss.escape(result.error.message), result.unauthenticatedContent?.toUtfStr()); // missing mdc - only render the result after user confirmation
} else if (result.error) {
renderModule.renderErr(
`${Lang.pgpBlock.cantOpen}\n\n${Xss.escape(result.error.type)}: ${Xss.escape(result.error.message)}`,
diff --git a/test/source/mock/fes/fes-constants.ts b/test/source/mock/fes/fes-constants.ts
index 2431fb1b2f8..c72f317e67d 100644
--- a/test/source/mock/fes/fes-constants.ts
+++ b/test/source/mock/fes/fes-constants.ts
@@ -54,7 +54,7 @@ export const processMessageFromUser = async (body: string, fesUrl: string) => {
})) as DecryptSuccess;
expect(decrypted.success).to.equal(true);
expect(decrypted.signature?.signerLongids.length).to.equal(1);
- const decryptedMimeMsg = decrypted.content?.toUtfStr();
+ const decryptedMimeMsg = decrypted.success ? decrypted.content.toUtfStr() : undefined;
expect(decryptedMimeMsg).to.contain(
'Content-Type: text/plain\r\n' + 'Content-Transfer-Encoding: quoted-printable\r\n\r\n' + 'PWD encrypted message with FES - ID TOKEN'
);
@@ -93,7 +93,7 @@ export const processMessageFromUser2 = async (body: string, fesUrl: string) => {
verificationPubs: [],
});
expect(decrypted.success).to.equal(true);
- const decryptedMimeMsg = decrypted.content?.toUtfStr();
+ const decryptedMimeMsg = decrypted.success ? decrypted.content.toUtfStr() : undefined;
// small.txt
expect(decryptedMimeMsg).to.contain('Content-Type: text/plain; name=small.txt\r\n' + 'Content-Disposition: attachment; filename=small.txt');
expect(decryptedMimeMsg).to.contain('Content-Transfer-Encoding: base64\r\n\r\n' + 'c21hbGwgdGV4dCBmaWxlCm5vdCBtdWNoIGhlcmUKdGhpcyB3b3JrZWQK');
@@ -142,7 +142,7 @@ export const processMessageFromUser3 = async (body: string, fesUrl: string) => {
verificationPubs: [],
});
expect(decrypted.success).to.equal(true);
- const decryptedMimeMsg = decrypted.content?.toUtfStr();
+ const decryptedMimeMsg = decrypted.success ? decrypted.content.toUtfStr() : undefined;
const cryptupDataRegex = /cryptup-data="([^"]*)"/;
const cryptupDataMatch = decryptedMimeMsg?.match(cryptupDataRegex);
expect(cryptupDataMatch).to.not.be.undefined;
diff --git a/test/source/mock/google/strategies/send-message-strategy.ts b/test/source/mock/google/strategies/send-message-strategy.ts
index 30c3de54741..e0f33dd14e5 100644
--- a/test/source/mock/google/strategies/send-message-strategy.ts
+++ b/test/source/mock/google/strategies/send-message-strategy.ts
@@ -100,7 +100,7 @@ class PwdAndPubkeyEncryptedMessagesWithFlowCryptComApiTestStrategy implements IT
const encryptedData = mimeMsg.text!;
const decrypted = await MsgUtil.decryptMessage({ kisWithPp, encryptedData, verificationPubs: [] });
expect(decrypted.success).to.be.true;
- expect(decrypted.content!.toUtfStr()).to.contain('PWD and pubkey encrypted messages with flowcrypt.com/shared-tenant-fes');
+ expect(decrypted.success ? decrypted.content.toUtfStr() : '').to.contain('PWD and pubkey encrypted messages with flowcrypt.com/shared-tenant-fes');
expect(mimeMsg.bcc).to.be.an.undefined;
expect(mimeMsg.to).to.be.an.undefined;
expect((mimeMsg.headers.get('reply-to') as AddressObject).text).to.equal('"First Last" , test@email.com');
@@ -172,7 +172,7 @@ class PwdEncryptedMessageWithFesPubkeyRecipientInBccTestStrategy implements ITes
const encryptedData = Buf.fromUtfStr(mimeMsg.text!);
const decrypted = await MsgUtil.decryptMessage({ kisWithPp, encryptedData, verificationPubs: [] });
expect(decrypted.success).to.be.true;
- expect(decrypted.content!.toUtfStr()).to.equal('PWD encrypted message with FES - pubkey recipient in bcc');
+ expect(decrypted.success ? decrypted.content.toUtfStr() : '').to.equal('PWD encrypted message with FES - pubkey recipient in bcc');
expect((mimeMsg.bcc as AddressObject).text).to.equal('flowcrypt.compatibility@gmail.com');
expect(mimeMsg.cc).to.be.an.undefined;
expect(mimeMsg.to).to.be.an.undefined;
@@ -233,7 +233,7 @@ class PwdEncryptedMessageWithFesReplyRenderingTestStrategy implements ITestMsgSt
const encryptedData = mimeMsg.text!;
const decrypted = await MsgUtil.decryptMessage({ kisWithPp, encryptedData, verificationPubs: [] });
expect(decrypted.success).to.be.true;
- expect(decrypted.content!.toUtfStr()).to.include('> some dummy text');
+ expect(decrypted.success ? decrypted.content.toUtfStr() : '').to.include('> some dummy text');
expect((mimeMsg.to as AddressObject).text).to.equal('flowcrypt.compatibility@gmail.com, mock.only.pubkey@flowcrypt.com');
expect(mimeMsg.cc).to.be.an.undefined;
expect(mimeMsg.bcc).to.be.an.undefined;
@@ -335,7 +335,7 @@ class SignatureRemovalTestStrategy implements ITestMsgStrategy {
const decrypted = await MsgUtil.decryptMessage({ kisWithPp, encryptedData, verificationPubs: [] });
expect(decrypted.success).to.be.true;
// We expect the body to contain the message text but NOT the signature
- const body = decrypted.content?.toUtfStr() || '';
+ const body = decrypted.success ? decrypted.content.toUtfStr() : '';
if (!body.includes('Message without signature')) {
throw new HttpClientErr(`Error: Msg Text doesn't contain expected body. Current: '${body}'`);
}
diff --git a/test/source/tests/setup.ts b/test/source/tests/setup.ts
index 8c158466694..da86655a3e8 100644
--- a/test/source/tests/setup.ts
+++ b/test/source/tests/setup.ts
@@ -2500,7 +2500,7 @@ AN8G3r5Htj8olot+jm9mIa5XLXWzMNUZgg==
encryptedData: encryptedAttachmentContent,
verificationPubs: [pubkey],
});
- const armoredPrivateKey = decryptedAttachmentContent.content!.toUtfStr();
+ const armoredPrivateKey = decryptedAttachmentContent.success ? decryptedAttachmentContent.content.toUtfStr() : '';
const key = await KeyUtil.parse(armoredPrivateKey);
expect(key.isPrivate).to.equal(true);
expect(key.fullyDecrypted).to.equal(true);
diff --git a/test/source/tests/unit-node.ts b/test/source/tests/unit-node.ts
index 0c77f6e2b89..e8d293ab77c 100644
--- a/test/source/tests/unit-node.ts
+++ b/test/source/tests/unit-node.ts
@@ -11,7 +11,7 @@ import { KeyUtil, KeyInfoWithIdentityAndOptionalPp, Key } from '../core/crypto/k
import { UnreportableError } from '../platform/error-report.js';
import { Buf } from '../core/buf';
import { OpenPGPKey } from '../core/crypto/pgp/openpgp-key';
-import { DecryptError, DecryptSuccess, MsgUtil } from '../core/crypto/pgp/msg-util';
+import { DecryptError, DecryptErrTypes, DecryptResult, DecryptSuccess, MsgUtil } from '../core/crypto/pgp/msg-util';
import { opgp } from '../core/crypto/pgp/openpgpjs-custom';
import { Attachment } from '../core/attachment.js';
import { GoogleData, GmailMsg } from '../mock/google/google-data';
@@ -1199,6 +1199,42 @@ jLwe8W9IMt765T5x5oux9MmPDXF05xHfm4qfH/BMO3a802x5u2gJjJjuknrFdgXY
t.pass();
});
+ test('[unit][MsgUtil.decryptMessage] no-MDC plaintext is not exposed as decrypt content', async t => {
+ const encryptedData = decodeURIComponent(testConstants.encryptedMessageMissingMdcUriEncoded);
+ const compatibilityKey1 = Config.key('flowcrypt.compatibility.1pp1');
+ const kisWithPp = [
+ {
+ ...(await KeyUtil.keyInfoObj(await KeyUtil.parse(compatibilityKey1.armored!))),
+ passphrase: compatibilityKey1.passphrase,
+ },
+ ];
+ const result = await MsgUtil.decryptMessage({ kisWithPp, encryptedData, verificationPubs: [] });
+ expect(result.success).to.equal(false);
+ // the cast used by the old preview gate must never yield plaintext on a failed integrity check
+ expect((result as DecryptSuccess).content).to.equal(undefined);
+ if (!result.success) {
+ expect(result.error.type).to.equal(DecryptErrTypes.noMdc);
+ expect(result.unauthenticatedContent?.toUtfStr()).to.include('Shall not decrypt automatically');
+ }
+ t.pass();
+ });
+
+ test('[unit][DecryptResult] other failed decryptions never expose content, success does', async t => {
+ const encryptedData = decodeURIComponent(testConstants.encryptedMessageMissingMdcUriEncoded);
+ const failures = await Promise.all([
+ MsgUtil.decryptMessage({ kisWithPp: [], encryptedData: 'not a pgp message', verificationPubs: [] }),
+ MsgUtil.decryptMessage({ kisWithPp: [], encryptedData, verificationPubs: [] }),
+ ]);
+ for (const failure of failures) {
+ expect(failure.success).to.equal(false);
+ expect('content' in failure).to.equal(false);
+ }
+ const content = Buf.fromUtfStr('content');
+ const succeeded: DecryptResult = { success: true, content, isEncrypted: true, isCleartext: false };
+ expect(succeeded.content).to.equal(content);
+ t.pass();
+ });
+
test('[unit][MsgUtil.decryptMessage] decrypts a pubkey-encrypted OpenPGP message', async t => {
const data = await GoogleData.withInitializedData('flowcrypt.compatibility@gmail.com');