From e729feed5578433507b4fb083a6073dc5843d5d7 Mon Sep 17 00:00:00 2001 From: martgil <46025304+martgil@users.noreply.github.com> Date: Fri, 27 Nov 2026 12:43:20 +0800 Subject: [PATCH] fix: avoid encryption downgrade for signed messages with encrypted parts --- .../compose-modules/compose-quote-module.ts | 2 +- extension/js/common/core/mime.ts | 5 +++ test/source/tests/unit-node.ts | 41 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/extension/chrome/elements/compose-modules/compose-quote-module.ts b/extension/chrome/elements/compose-modules/compose-quote-module.ts index 686650b512c..bd0d3990c47 100644 --- a/extension/chrome/elements/compose-modules/compose-quote-module.ts +++ b/extension/chrome/elements/compose-modules/compose-quote-module.ts @@ -166,7 +166,7 @@ export class ComposeQuoteModule extends ViewModule { return { headers, text: decryptedAndFormatedContent.join('\n'), - isOnlySigned: !!(decoded.rawSignedContent || (message.blocks.length > 0 && message.blocks[0].type === 'signedMsg')), + isOnlySigned: Mime.isOnlySigned(decoded.rawSignedContent, message.blocks), decryptedFiles, }; } catch (e) { diff --git a/extension/js/common/core/mime.ts b/extension/js/common/core/mime.ts index 699d231cdb7..beabe532886 100644 --- a/extension/js/common/core/mime.ts +++ b/extension/js/common/core/mime.ts @@ -91,6 +91,11 @@ export class Mime { return !(text && !/^(\r)?(\n)?$/.test(text)); } + public static isOnlySigned(rawSignedContent: string | undefined, blocks: MsgBlock[]): boolean { + const hasEncryptedContent = blocks.some(block => ['encryptedMsg', 'encryptedAttachment'].includes(block.type)); + return !hasEncryptedContent && !!(rawSignedContent || (blocks.length > 0 && blocks[0].type === 'signedMsg')); + } + public static processAttachments(bodyBlocks: MsgBlock[], decoded: MimeContent): MimeProccesedMsg { const attachmentBlocks: MsgBlock[] = []; const signatureAttachments: Attachment[] = []; diff --git a/test/source/tests/unit-node.ts b/test/source/tests/unit-node.ts index e8d293ab77c..4003a4a1b5c 100644 --- a/test/source/tests/unit-node.ts +++ b/test/source/tests/unit-node.ts @@ -4,6 +4,7 @@ import test from 'ava'; import { MsgBlock } from '../core/msg-block'; import { MsgBlockParser } from '../core/msg-block-parser'; +import { Mime } from '../core/mime'; import { Config, TestVariant } from '../util'; import { use, expect } from 'chai'; import chaiAsPromised from 'chai-as-promised'; @@ -40,6 +41,46 @@ export const equals = (a: string | Uint8Array, b: string | Uint8Array) => { export const defineUnitNodeTests = (testVariant: TestVariant) => { if (testVariant !== 'CONSUMER-LIVE-GMAIL') { + test(`[unit][Mime.isOnlySigned] signed-only message stays signed-only`, t => { + t.true(Mime.isOnlySigned('signed content', [new MsgBlock('signedMsg', 'signed content', true)])); + }); + + test(`[unit][Mime.isOnlySigned] outer signed wrapper with encrypted body is not signed-only`, t => { + t.false(Mime.isOnlySigned('signed content', [new MsgBlock('encryptedMsg', 'encrypted content', true)])); + }); + + test(`[unit][Mime.isOnlySigned] outer signed wrapper with encrypted attachment is not signed-only`, t => { + t.false( + Mime.isOnlySigned('signed content', [MsgBlock.fromAttachment('encryptedAttachment', '', { data: new Uint8Array(0), name: 'statement.pdf.pgp' })]) + ); + }); + + test(`[unit][Mime.isOnlySigned] leading signedMsg block with later encrypted body is not signed-only`, t => { + t.false(Mime.isOnlySigned(undefined, [new MsgBlock('signedMsg', 'signed content', true), new MsgBlock('encryptedMsg', 'encrypted content', true)])); + }); + + test(`[unit][Mime.isOnlySigned] leading signedMsg block with later encrypted attachment is not signed-only`, t => { + t.false( + Mime.isOnlySigned(undefined, [ + new MsgBlock('signedMsg', 'signed content', true), + MsgBlock.fromAttachment('encryptedAttachment', '', { data: new Uint8Array(0), name: 'statement.pdf.pgp' }), + ]) + ); + }); + + test(`[unit][Mime.isOnlySigned] plain attachment does not disqualify signed-only`, t => { + t.true( + Mime.isOnlySigned('signed content', [ + new MsgBlock('signedMsg', 'signed content', true), + MsgBlock.fromAttachment('plainAttachment', '', { data: new Uint8Array(0), name: 'notes.txt' }), + ]) + ); + }); + + test(`[unit][Mime.isOnlySigned] unsigned message is not signed-only`, t => { + t.false(Mime.isOnlySigned(undefined, [new MsgBlock('plainText', 'hello', true)])); + }); + test(`[unit][emailKeyIndex] gives distinct keys to emails that differ only in punctuation`, async t => { t.not(emailKeyIndex('a-b@corp.test', 'keys'), emailKeyIndex('ab@corp.test', 'keys')); t.is(emailKeyIndex('a-b@corp.test', 'keys'), emailKeyIndex('A-B@corp.test', 'keys'));