From 4b39b46b6a1649868e14315d8d8eadbc6b8838a9 Mon Sep 17 00:00:00 2001 From: audric Date: Fri, 14 Jan 2022 11:03:22 +1100 Subject: [PATCH] replace invalid unicode in filename --- ts/interactions/conversationInteractions.ts | 1 - ts/receiver/attachments.ts | 4 +-- .../crypto/DecryptedAttachmentsManager.ts | 2 +- ts/session/utils/AttachmentsDownload.ts | 1 + ts/types/MessageAttachment.ts | 22 ++++++------- ts/types/attachments/migrations.ts | 32 ++++++++----------- ts/util/attachmentsUtil.ts | 9 ++++-- 7 files changed, 34 insertions(+), 37 deletions(-) diff --git a/ts/interactions/conversationInteractions.ts b/ts/interactions/conversationInteractions.ts index cbf967aeb..f4f328601 100644 --- a/ts/interactions/conversationInteractions.ts +++ b/ts/interactions/conversationInteractions.ts @@ -381,7 +381,6 @@ export async function uploadOurAvatar(newAvatarDecrypted?: ArrayBuffer) { isRaw: true, data: decryptedAvatarData, contentType: MIME.IMAGE_UNKNOWN, // contentType is mostly used to generate previews and screenshot. We do not care for those in this case. - // url: fileUrl, }); // Replace our temporary image with the attachment pointer from the server: ourConvo.set('avatar', null); diff --git a/ts/receiver/attachments.ts b/ts/receiver/attachments.ts index 6ec667ef0..bd929d729 100644 --- a/ts/receiver/attachments.ts +++ b/ts/receiver/attachments.ts @@ -152,7 +152,7 @@ async function processNormalAttachments( if (message.isTrustedForAttachmentDownload()) { const openGroupV2Details = (isOpenGroupV2 && convo.toOpenGroupV2()) || undefined; const attachments = await Promise.all( - normalAttachments.map(async (attachment: any, index: any) => { + normalAttachments.map(async (attachment: any, index: number) => { return AttachmentDownloads.addJob(attachment, { messageId: message.id, type: 'attachment', @@ -177,7 +177,7 @@ async function processPreviews(message: MessageModel, convo: ConversationModel): const openGroupV2Details = (isOpenGroupV2 && convo.toOpenGroupV2()) || undefined; const preview = await Promise.all( - (message.get('preview') || []).map(async (item: any, index: any) => { + (message.get('preview') || []).map(async (item: any, index: number) => { if (!item.image) { return item; } diff --git a/ts/session/crypto/DecryptedAttachmentsManager.ts b/ts/session/crypto/DecryptedAttachmentsManager.ts index 4816cc1a2..9db0746c8 100644 --- a/ts/session/crypto/DecryptedAttachmentsManager.ts +++ b/ts/session/crypto/DecryptedAttachmentsManager.ts @@ -100,7 +100,7 @@ export const getDecryptedMediaUrl = async ( forceRetain: isAvatar, }); } - window.log.info(' file decrypted :', url); + window.log.info(' file decrypted :', url, ' as ', obj); urlToDecryptingPromise.delete(url); resolve(obj); return; diff --git a/ts/session/utils/AttachmentsDownload.ts b/ts/session/utils/AttachmentsDownload.ts index 386c88538..6e643577d 100644 --- a/ts/session/utils/AttachmentsDownload.ts +++ b/ts/session/utils/AttachmentsDownload.ts @@ -208,6 +208,7 @@ async function _runJob(job: any) { } const upgradedAttachment = await processNewAttachment({ ...downloaded, + fileName: attachment.fileName, contentType: attachment.contentType, }); found = await getMessageById(messageId); diff --git a/ts/types/MessageAttachment.ts b/ts/types/MessageAttachment.ts index 2b678e83a..555cbc729 100644 --- a/ts/types/MessageAttachment.ts +++ b/ts/types/MessageAttachment.ts @@ -12,6 +12,7 @@ import { captureDimensionsAndScreenshot, deleteData, loadData, + replaceUnicodeV2, } from './attachments/migrations'; // tslint:disable: prefer-object-spread @@ -160,31 +161,28 @@ export const loadQuoteData = async (quote: any) => { }; export const processNewAttachment = async (attachment: { + fileName?: string; contentType: string; data: ArrayBuffer; digest?: string; path?: string; isRaw?: boolean; }) => { + const fileName = attachment.fileName ? replaceUnicodeV2(attachment.fileName) : ''; // this operation might change the size (as we might print the content to a canvas and get the data back) const rotatedData = await autoOrientJPEGAttachment(attachment); - const rotatedAttachment = { - ...attachment, - contentType: rotatedData.contentType, - data: rotatedData.data, - digest: attachment.digest as string | undefined, - }; + const onDiskAttachmentPath = await migrateDataToFileSystem(rotatedData.data); + + const attachmentWithoutData = omit({ ...attachment, fileName, path: onDiskAttachmentPath }, [ + 'data', + ]); if (rotatedData.shouldDeleteDigest) { - delete rotatedAttachment.digest; + delete attachmentWithoutData.digest; } - - const onDiskAttachmentPath = await migrateDataToFileSystem(rotatedAttachment.data); - - const attachmentWithoutData = omit({ ...attachment, path: onDiskAttachmentPath }, ['data']); const finalAttachment = await captureDimensionsAndScreenshot(attachmentWithoutData); - return { ...finalAttachment, size: rotatedAttachment.data.byteLength }; + return { ...finalAttachment, fileName, size: rotatedData.data.byteLength }; }; export const readAttachmentData = async (relativePath: string): Promise => { diff --git a/ts/types/attachments/migrations.ts b/ts/types/attachments/migrations.ts index a34f45e7a..c533754ba 100644 --- a/ts/types/attachments/migrations.ts +++ b/ts/types/attachments/migrations.ts @@ -112,25 +112,20 @@ export const _replaceUnicodeOrderOverridesSync = (attachment: any) => { // const replaceUnicodeOrderOverrides = async (attachment: any) => // _replaceUnicodeOrderOverridesSync(attachment); -// // \u202A-\u202E is LRE, RLE, PDF, LRO, RLO -// // \u2066-\u2069 is LRI, RLI, FSI, PDI -// // \u200E is LRM -// // \u200F is RLM -// // \u061C is ALM -// const V2_UNWANTED_UNICODE = /[\u202A-\u202E\u2066-\u2069\u200E\u200F\u061C]/g; - -// const replaceUnicodeV2 = async (attachment: any) => { -// if (!isString(attachment.fileName)) { -// return attachment; -// } - -// const fileName = attachment.fileName.replace(V2_UNWANTED_UNICODE, UNICODE_REPLACEMENT_CHARACTER); +// \u202A-\u202E is LRE, RLE, PDF, LRO, RLO +// \u2066-\u2069 is LRI, RLI, FSI, PDI +// \u200E is LRM +// \u200F is RLM +// \u061C is ALM +const V2_UNWANTED_UNICODE = /[\u202A-\u202E\u2066-\u2069\u200E\u200F\u061C]/g; + +export const replaceUnicodeV2 = (fileName: string) => { + if (!isString(fileName)) { + throw new Error('replaceUnicodeV2 should not be called without a filename'); + } -// return { -// ...attachment, -// fileName, -// }; -// }; + return fileName.replace(V2_UNWANTED_UNICODE, UNICODE_REPLACEMENT_CHARACTER); +}; // const removeSchemaVersion = ({ attachment }: any) => { // if (!isValid(attachment)) { @@ -196,6 +191,7 @@ export const captureDimensionsAndScreenshot = async ( ): Promise