From b4b38838507c27c46a57aa09629dc24a4b6a4c10 Mon Sep 17 00:00:00 2001 From: Josh Perez Date: Thu, 5 Oct 2023 12:16:50 -0400 Subject: [PATCH] Do not drop edit messages if we get an invalid attachment --- ts/util/handleEditMessage.ts | 26 ++++++++++++++++++------ ts/util/queueAttachmentDownloads.ts | 31 +++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/ts/util/handleEditMessage.ts b/ts/util/handleEditMessage.ts index 84ac2cbb6..2f9f8b2f4 100644 --- a/ts/util/handleEditMessage.ts +++ b/ts/util/handleEditMessage.ts @@ -26,6 +26,20 @@ import { modifyTargetMessage } from './modifyTargetMessage'; const RECURSION_LIMIT = 15; +function getAttachmentSignatureSafe( + attachment: AttachmentType +): string | undefined { + try { + return getAttachmentSignature(attachment); + } catch { + log.warn( + 'handleEditMessage: attachment was missing digest', + attachment.blurHash + ); + return undefined; + } +} + export async function handleEditMessage( mainMessage: MessageAttributesType, editAttributes: Pick< @@ -129,7 +143,7 @@ export async function handleEditMessage( const quoteSignatures: Map = new Map(); mainMessage.attachments?.forEach(attachment => { - const signature = getAttachmentSignature(attachment); + const signature = getAttachmentSignatureSafe(attachment); if (signature) { attachmentSignatures.set(signature, attachment); } @@ -138,7 +152,7 @@ export async function handleEditMessage( if (!preview.image) { return; } - const signature = getAttachmentSignature(preview.image); + const signature = getAttachmentSignatureSafe(preview.image); if (signature) { previewSignatures.set(signature, preview); } @@ -148,7 +162,7 @@ export async function handleEditMessage( if (!attachment.thumbnail) { continue; } - const signature = getAttachmentSignature(attachment.thumbnail); + const signature = getAttachmentSignatureSafe(attachment.thumbnail); if (signature) { quoteSignatures.set(signature, attachment); } @@ -158,7 +172,7 @@ export async function handleEditMessage( let newAttachments = 0; const nextEditedMessageAttachments = upgradedEditedMessageData.attachments?.map(attachment => { - const signature = getAttachmentSignature(attachment); + const signature = getAttachmentSignatureSafe(attachment); const existingAttachment = signature ? attachmentSignatures.get(signature) : undefined; @@ -178,7 +192,7 @@ export async function handleEditMessage( return preview; } - const signature = getAttachmentSignature(preview.image); + const signature = getAttachmentSignatureSafe(preview.image); const existingPreview = signature ? previewSignatures.get(signature) : undefined; @@ -208,7 +222,7 @@ export async function handleEditMessage( if (!attachment.thumbnail) { return attachment; } - const signature = getAttachmentSignature(attachment.thumbnail); + const signature = getAttachmentSignatureSafe(attachment.thumbnail); const existingThumbnail = signature ? quoteSignatures.get(signature) : undefined; diff --git a/ts/util/queueAttachmentDownloads.ts b/ts/util/queueAttachmentDownloads.ts index 66cd588de..ec1f40570 100644 --- a/ts/util/queueAttachmentDownloads.ts +++ b/ts/util/queueAttachmentDownloads.ts @@ -40,6 +40,20 @@ export type MessageAttachmentsDownloadedType = { sticker?: StickerType; }; +function getAttachmentSignatureSafe( + attachment: AttachmentType +): string | undefined { + try { + return getAttachmentSignature(attachment); + } catch { + log.warn( + 'queueAttachmentDownloads: attachment was missing digest', + attachment.blurHash + ); + return undefined; + } +} + // Receive logic // NOTE: If you're changing any logic in this function that deals with the // count then you'll also have to modify ./hasAttachmentsDownloads @@ -273,7 +287,7 @@ async function queueNormalAttachments( // then not be added to the AttachmentDownloads job. const attachmentSignatures: Map = new Map(); otherAttachments?.forEach(attachment => { - const signature = getAttachmentSignature(attachment); + const signature = getAttachmentSignatureSafe(attachment); if (signature) { attachmentSignatures.set(signature, attachment); } @@ -291,7 +305,7 @@ async function queueNormalAttachments( return attachment; } - const signature = getAttachmentSignature(attachment); + const signature = getAttachmentSignatureSafe(attachment); const existingAttachment = signature ? attachmentSignatures.get(signature) : undefined; @@ -332,7 +346,12 @@ function getLinkPreviewSignature(preview: LinkPreviewType): string | undefined { return; } - return `<${url}>${getAttachmentSignature(image)}`; + const signature = getAttachmentSignatureSafe(image); + if (!signature) { + return; + } + + return `<${url}>${signature}`; } async function queuePreviews( @@ -406,7 +425,11 @@ function getQuoteThumbnailSignature( if (!thumbnail) { return undefined; } - return `<${quote.id}>${getAttachmentSignature(thumbnail)}`; + const signature = getAttachmentSignatureSafe(thumbnail); + if (!signature) { + return; + } + return `<${quote.id}>${signature}`; } async function queueQuoteAttachments(