From 4d57402367fd9443bb47f42498a89f4b17221984 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 3 Nov 2017 15:03:17 -0400 Subject: [PATCH 1/2] Ensure attachments always have a valid content type. // FREEBIE --- .../src/Messages/Attachments/TSAttachment.m | 24 ++++++++++++++++++- .../Messages/Interactions/TSOutgoingMessage.m | 2 ++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/SignalServiceKit/src/Messages/Attachments/TSAttachment.m b/SignalServiceKit/src/Messages/Attachments/TSAttachment.m index 4bea23d36..5fb730562 100644 --- a/SignalServiceKit/src/Messages/Attachments/TSAttachment.m +++ b/SignalServiceKit/src/Messages/Attachments/TSAttachment.m @@ -31,6 +31,11 @@ NSUInteger const TSAttachmentSchemaVersion = 4; // This will fail with legacy iOS clients which don't upload attachment size. DDLogWarn(@"%@ Missing byteCount for attachment with serverId: %lld", self.tag, serverId); } + if (contentType.length < 1) { + DDLogWarn(@"%@ incoming attachment has invalid content type", self.tag); + + contentType = @"application/octet-stream"; + } OWSAssert(contentType.length > 0); self = [super init]; @@ -55,6 +60,11 @@ NSUInteger const TSAttachmentSchemaVersion = 4; byteCount:(UInt32)byteCount sourceFilename:(nullable NSString *)sourceFilename { + if (contentType.length < 1) { + DDLogWarn(@"%@ outgoing attachment has invalid content type", self.tag); + + contentType = @"application/octet-stream"; + } OWSAssert(contentType.length > 0); OWSAssert(byteCount > 0); @@ -93,8 +103,14 @@ NSUInteger const TSAttachmentSchemaVersion = 4; _serverId = pointer.serverId; _encryptionKey = pointer.encryptionKey; _byteCount = pointer.byteCount; - _contentType = pointer.contentType; _sourceFilename = pointer.sourceFilename; + NSString *contentType = pointer.contentType; + if (contentType.length < 1) { + DDLogWarn(@"%@ incoming attachment has invalid content type", self.tag); + + contentType = @"application/octet-stream"; + } + _contentType = contentType; _attachmentSchemaVersion = TSAttachmentSchemaVersion; @@ -119,6 +135,12 @@ NSUInteger const TSAttachmentSchemaVersion = 4; OWSAssert(!_sourceFilename || [_sourceFilename isKindOfClass:[NSString class]]); } + if (_contentType.length < 1) { + DDLogWarn(@"%@ legacy attachment has invalid content type", self.tag); + + _contentType = @"application/octet-stream"; + } + return self; } diff --git a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m index 0c783c7d2..0c6d81ee4 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m +++ b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m @@ -541,6 +541,8 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec OWSSignalServiceProtosAttachmentPointerBuilder *builder = [OWSSignalServiceProtosAttachmentPointerBuilder new]; [builder setId:attachmentStream.serverId]; + NSString *contentType = attachmentStream.contentType; + OWSAssert(attachmentStream.contentType.length > 0); [builder setContentType:attachmentStream.contentType]; [builder setFileName:filename]; [builder setSize:attachmentStream.byteCount]; From 8b6265f1b56c9397efade9f6f5e48cd10e2c3be6 Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 3 Nov 2017 15:24:22 -0400 Subject: [PATCH 2/2] Respond to CR. // FREEBIE --- SignalServiceKit/src/Messages/Attachments/TSAttachment.m | 8 ++++---- .../src/Messages/Interactions/TSOutgoingMessage.m | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/SignalServiceKit/src/Messages/Attachments/TSAttachment.m b/SignalServiceKit/src/Messages/Attachments/TSAttachment.m index 5fb730562..c2d5c1565 100644 --- a/SignalServiceKit/src/Messages/Attachments/TSAttachment.m +++ b/SignalServiceKit/src/Messages/Attachments/TSAttachment.m @@ -34,7 +34,7 @@ NSUInteger const TSAttachmentSchemaVersion = 4; if (contentType.length < 1) { DDLogWarn(@"%@ incoming attachment has invalid content type", self.tag); - contentType = @"application/octet-stream"; + contentType = OWSMimeTypeApplicationOctetStream; } OWSAssert(contentType.length > 0); @@ -63,7 +63,7 @@ NSUInteger const TSAttachmentSchemaVersion = 4; if (contentType.length < 1) { DDLogWarn(@"%@ outgoing attachment has invalid content type", self.tag); - contentType = @"application/octet-stream"; + contentType = OWSMimeTypeApplicationOctetStream; } OWSAssert(contentType.length > 0); OWSAssert(byteCount > 0); @@ -108,7 +108,7 @@ NSUInteger const TSAttachmentSchemaVersion = 4; if (contentType.length < 1) { DDLogWarn(@"%@ incoming attachment has invalid content type", self.tag); - contentType = @"application/octet-stream"; + contentType = OWSMimeTypeApplicationOctetStream; } _contentType = contentType; @@ -138,7 +138,7 @@ NSUInteger const TSAttachmentSchemaVersion = 4; if (_contentType.length < 1) { DDLogWarn(@"%@ legacy attachment has invalid content type", self.tag); - _contentType = @"application/octet-stream"; + _contentType = OWSMimeTypeApplicationOctetStream; } return self; diff --git a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m index 0c6d81ee4..2bc575a28 100644 --- a/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m +++ b/SignalServiceKit/src/Messages/Interactions/TSOutgoingMessage.m @@ -541,7 +541,6 @@ NSString *const kTSOutgoingMessageSentRecipientAll = @"kTSOutgoingMessageSentRec OWSSignalServiceProtosAttachmentPointerBuilder *builder = [OWSSignalServiceProtosAttachmentPointerBuilder new]; [builder setId:attachmentStream.serverId]; - NSString *contentType = attachmentStream.contentType; OWSAssert(attachmentStream.contentType.length > 0); [builder setContentType:attachmentStream.contentType]; [builder setFileName:filename];