From 9c806d59df0b39e5059cda21c7553865298fb9b8 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 25 Jan 2019 15:33:32 -0500 Subject: [PATCH] Safely ignore invalid link preview images. --- .../ConversationView/ConversationViewItem.m | 15 +++++++++++++-- Signal/src/views/LinkPreviewView.swift | 6 ++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m index 539788ee5..fa13720a0 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m @@ -669,10 +669,21 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType) if (self.hasBodyText && attachment == nil && message.linkPreview) { self.linkPreview = message.linkPreview; if (message.linkPreview.imageAttachmentId.length > 0) { - self.linkPreviewAttachment = + TSAttachment *_Nullable linkPreviewAttachment = [TSAttachment fetchObjectWithUniqueID:message.linkPreview.imageAttachmentId transaction:transaction]; - if (!self.linkPreviewAttachment) { + if (!linkPreviewAttachment) { OWSFailDebug(@"Could not load link preview image attachment."); + } else if (!linkPreviewAttachment.isImage) { + OWSFailDebug(@"Link preview attachment isn't an image."); + } else if ([linkPreviewAttachment isKindOfClass:[TSAttachmentStream class]]) { + TSAttachmentStream *attachmentStream = (TSAttachmentStream *)linkPreviewAttachment; + if (!attachmentStream.isValidImage) { + OWSFailDebug(@"Link preview image attachment isn't valid."); + } else { + self.linkPreviewAttachment = linkPreviewAttachment; + } + } else { + self.linkPreviewAttachment = linkPreviewAttachment; } } } diff --git a/Signal/src/views/LinkPreviewView.swift b/Signal/src/views/LinkPreviewView.swift index f5395683c..2f1448ca0 100644 --- a/Signal/src/views/LinkPreviewView.swift +++ b/Signal/src/views/LinkPreviewView.swift @@ -188,7 +188,8 @@ public class LinkPreviewSent: NSObject, LinkPreviewState { guard let attachmentStream = imageAttachment as? TSAttachmentStream else { return .loading } - guard attachmentStream.isValidImage else { + guard attachmentStream.isImage, + attachmentStream.isValidImage else { return .invalid } return .loaded @@ -201,7 +202,8 @@ public class LinkPreviewSent: NSObject, LinkPreviewState { owsFailDebug("Could not load image.") return nil } - guard attachmentStream.isValidImage else { + guard attachmentStream.isImage, + attachmentStream.isValidImage else { return nil } guard let imageFilepath = attachmentStream.originalFilePath else {