Skip to content

Commit

Permalink
added reliable attachment type recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
martgil committed Oct 24, 2024
1 parent e25fb01 commit 85cf5f9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
5 changes: 3 additions & 2 deletions extension/js/common/browser/browser-msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Ui } from './ui.js';
import { AuthRes } from '../api/authentication/generic/oauth.js';
import { GlobalStore } from '../platform/store/global-store.js';
import { BgUtils } from '../../service_worker/bgutils.js';
import { ThunderbirdAttachment } from '../core/attachment.js';

export type GoogleAuthWindowResult$result = 'Success' | 'Denied' | 'Error' | 'Closed';
export type ScreenDimensions = { width: number; height: number; availLeft: number; availTop: number };
Expand Down Expand Up @@ -98,7 +99,7 @@ export namespace Bm {
export type PgpBlockRetry = { frameId: string; messageSender: Dest };
export type PgpBlockReady = { frameId: string; messageSender: Dest };
export type ThunderbirdOpenPassphraseDialog = { acctEmail: string; longids: string };
export type ThunderbirdGetDownloadableAttachment = { attachment: messenger.messages.MessageAttachment };
export type ThunderbirdGetDownloadableAttachment = { attachments: messenger.messages.MessageAttachment[] };
export type ThunderbirdInitiateAttachmentDownload = { decryptedFileName: string; decryptedContent: Buf };

export namespace Res {
Expand All @@ -116,7 +117,7 @@ export namespace Bm {
export type ExpirationCacheGet<V> = Promise<V | undefined>;
export type ExpirationCacheSet = Promise<void>;
export type ExpirationCacheDeleteExpired = Promise<void>;
export type ThunderbirdGetDownloadableAttachment = Buf | undefined;
export type ThunderbirdGetDownloadableAttachment = ThunderbirdAttachment[];
export type ThunderbirdGetCurrentUser = string | undefined;
export type ThunderbirdMsgGet = { attachments: messenger.messages.MessageAttachment[]; messagePart: messenger.messages.MessagePart };
export type ThunderbirdOpenPassphraseDialog = Promise<void>;
Expand Down
6 changes: 6 additions & 0 deletions extension/js/common/core/attachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export type AttachmentProperties = {
contentDescription?: string;
contentTransferEncoding?: ContentTransferEncoding;
};
export type ThunderbirdAttachment = {
name: string;
contentType: string;
data: Buf;
treatAs: Attachment$treatAs;
};
export type AttachmentMeta = (AttachmentId | { data: Uint8Array }) & AttachmentProperties;

export type FcAttachmentLinkData = { name: string; type: string; size: number };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'use strict';

import { BrowserMsg } from '../../../common/browser/browser-msg.js';
import { Attachment } from '../../../common/core/attachment.js';
import { Attachment, ThunderbirdAttachment } from '../../../common/core/attachment.js';
import { Buf } from '../../../common/core/buf.js';
import { KeyUtil } from '../../../common/core/crypto/key.js';
import { DecryptError, DecryptErrTypes, MsgUtil, VerifyRes } from '../../../common/core/crypto/pgp/msg-util.js';
Expand Down Expand Up @@ -44,11 +44,9 @@ export class ThunderbirdElementReplacer extends WebmailElementReplacer {
await this.messageVerify(signerKeys);
}
if (emailBodyToParse && attachments.length) {
for (const attachment of attachments) {
const fcAttachment = await BrowserMsg.send.bg.await.thunderbirdGetDownloadableAttachment({ attachment });
if (fcAttachment) {
await this.attachmentUiRenderer(attachment.name, fcAttachment, signerKeys, emailBodyToParse);
}
const fcAttachments = await BrowserMsg.send.bg.await.thunderbirdGetDownloadableAttachment({ attachments });
for (const fcAttachment of fcAttachments) {
await this.attachmentUiRenderer(fcAttachment, signerKeys, emailBodyToParse);
}
}
}
Expand Down Expand Up @@ -112,14 +110,17 @@ export class ThunderbirdElementReplacer extends WebmailElementReplacer {
$('body').html(pgpBlock); // xss-sanitized
};

private attachmentUiRenderer = async (attachmentName: string, fcAttachment: Buf, verificationPubs: string[], plaintext: string) => {
if (attachmentName.endsWith('.pgp')) {
const generatedPgpTemplate = this.generatePgpAttachmentTemplate(attachmentName, fcAttachment);
private attachmentUiRenderer = async (fcAttachment: ThunderbirdAttachment, verificationPubs: string[], plaintext: string) => {
if (fcAttachment.treatAs === 'encryptedFile') {
const generatedPgpTemplate = this.generatePgpAttachmentTemplate(fcAttachment.name, fcAttachment.data);
$('.pgp_attachments_block').append(generatedPgpTemplate); // xss-sanitized
} else if (Attachment.encryptedMsgNames.some(a => attachmentName.includes(a)) && !this.emailBodyFromThunderbirdMail) {
await this.messageDecrypt(verificationPubs, fcAttachment);
} else if (attachmentName.endsWith('.asc')) {
const sigText = new TextDecoder('utf-8').decode(fcAttachment).trim();
} else if (
(fcAttachment.treatAs === 'encryptedMsg' || Attachment.encryptedMsgNames.some(a => fcAttachment.name.includes(a))) &&
!this.emailBodyFromThunderbirdMail
) {
await this.messageDecrypt(verificationPubs, fcAttachment.data);
} else if (fcAttachment.treatAs === 'signature') {
const sigText = new TextDecoder('utf-8').decode(fcAttachment.data).trim();
if (this.resemblesSignedMsg(sigText)) {
await this.messageVerify(verificationPubs, { plaintext, sigText });
}
Expand Down
27 changes: 23 additions & 4 deletions extension/js/service_worker/bg-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,32 @@ export class BgHandlers {
public static thunderbirdGetDownloadableAttachment = async (
r: Bm.ThunderbirdGetDownloadableAttachment
): Promise<Bm.Res.ThunderbirdGetDownloadableAttachment> => {
const processableAttachments: Bm.Res.ThunderbirdGetDownloadableAttachment = [];
const [tab] = await messenger.mailTabs.query({ active: true, currentWindow: true });
if (tab.id) {
const rawAttachment = await messenger.messages.getAttachmentFile(tab.id, r.attachment.partName);
const data = new Uint8Array(await rawAttachment.arrayBuffer());
return new Attachment({ data }).getData();
const fcAttachments: Attachment[] = [];
// convert Thunderbird Attachments to FlowCrypt recognizable Attachments
for (const tbAttachment of r.attachments) {
const rawAttachment = await messenger.messages.getAttachmentFile(tab.id, tbAttachment.partName);
fcAttachments.push(
new Attachment({
data: new Uint8Array(await rawAttachment.arrayBuffer()),
type: tbAttachment.contentType,
name: tbAttachment.name,
length: tbAttachment.size,
})
);
}
for (const fcAttachment of fcAttachments) {
processableAttachments.push({
name: fcAttachment.name,
contentType: fcAttachment.type,
data: fcAttachment.getData(),
treatAs: fcAttachment.treatAs(fcAttachments),
});
}
}
return;
return processableAttachments;
};

public static thunderbirdInitiateAttachmentDownload = async (
Expand Down

0 comments on commit 85cf5f9

Please sign in to comment.