Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions extension/chrome/elements/attachment_preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions extension/js/common/core/crypto/pgp/msg-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions extension/js/common/message-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<em>${Xss.escape(result.error.type)}: ${Xss.escape(result.error.message)}</em>`,
Expand Down
6 changes: 3 additions & 3 deletions test/source/mock/fes/fes-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions test/source/mock/google/strategies/send-message-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" <flowcrypt.compatibility@gmail.com>, test@email.com');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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}'`);
}
Expand Down
2 changes: 1 addition & 1 deletion test/source/tests/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 37 additions & 1 deletion test/source/tests/unit-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');

Expand Down
Loading