From c6a41d31ce46a7fda34b878660eefbcba9648fd1 Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Fri, 8 Apr 2022 10:01:54 -0400 Subject: [PATCH] fix(mail): avoid exception when accessing signed/encrypted body parts --- SoObjects/Mailer/SOGoMailObject.m | 53 +++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/SoObjects/Mailer/SOGoMailObject.m b/SoObjects/Mailer/SOGoMailObject.m index 78ca794af..2906ee64b 100644 --- a/SoObjects/Mailer/SOGoMailObject.m +++ b/SoObjects/Mailer/SOGoMailObject.m @@ -1,5 +1,5 @@ /* - Copyright (C) 2007-2017 Inverse inc. + Copyright (C) 2007-2022 Inverse inc. Copyright (C) 2004-2005 SKYRIX Software AG This file is part of SOGo. @@ -1162,7 +1162,7 @@ static BOOL debugSoParts = NO; NSArray *parts; Class clazz; - int part; + int partIndex; if ([self isEncrypted]) { @@ -1178,11 +1178,24 @@ static BOOL debugSoParts = NO; id part; m = [[self content] messageFromEncryptedDataAndCertificate: certificate]; + part = nil; + + partIndex = [_key intValue] - 1; + parts = [[m body] parts]; + if (partIndex > -1 && partIndex < [parts count]) + part = [parts objectAtIndex: partIndex]; + if (part) + { + mimeType = [[part contentType] stringValue]; + clazz = [SOGoMailBodyPart bodyPartClassForMimeType: mimeType + inContext: _ctx]; + } + else + { + [self logWithFormat: @"Lookup of body part %@ failed for encrypted message (%i parts found)", _key, [parts count]]; + clazz = Nil; + } - part = [[[m body] parts] objectAtIndex: ([_key intValue]-1)]; - mimeType = [[part contentType] stringValue]; - clazz = [SOGoMailBodyPart bodyPartClassForMimeType: mimeType - inContext: _ctx]; return [clazz objectWithName:_key inContainer: self]; } } @@ -1192,11 +1205,25 @@ static BOOL debugSoParts = NO; id part; m = [[self content] messageFromOpaqueSignedData]; + part = nil; + + partIndex = [_key intValue] - 1; + parts = [[m body] parts]; + + if (partIndex > -1 && partIndex < [parts count]) + part = [parts objectAtIndex: partIndex]; + if (part) + { + mimeType = [[part contentType] stringValue]; + clazz = [SOGoMailBodyPart bodyPartClassForMimeType: mimeType + inContext: _ctx]; + } + else + { + [self logWithFormat: @"Lookup of body part %@ failed for signed message (%i parts found)", _key, [parts count]]; + clazz = Nil; + } - part = [[[m body] parts] objectAtIndex: ([_key intValue]-1)]; - mimeType = [[part contentType] stringValue]; - clazz = [SOGoMailBodyPart bodyPartClassForMimeType: mimeType - inContext: _ctx]; return [clazz objectWithName:_key inContainer: self]; } @@ -1211,9 +1238,9 @@ static BOOL debugSoParts = NO; } else { - part = [_key intValue] - 1; - if (part > -1 && part < [parts count]) - partDesc = [parts objectAtIndex: part]; + partIndex = [_key intValue] - 1; + if (partIndex > -1 && partIndex < [parts count]) + partDesc = [parts objectAtIndex: partIndex]; else partDesc = nil; }