From fa36cb300b46266307e1b9ddba293e37ac37b387 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 14 May 2016 23:27:59 -0400 Subject: [PATCH] Add exception handling to secure coding archivers/unarchivers --- Sparkle/SUInstallerDriver.m | 4 ++++ Sparkle/SUSecureCoding.h | 2 +- Sparkle/SUSecureCoding.m | 28 +++++++++++++++++++--------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Sparkle/SUInstallerDriver.m b/Sparkle/SUInstallerDriver.m index 83ed7000..f283f4f7 100644 --- a/Sparkle/SUInstallerDriver.m +++ b/Sparkle/SUInstallerDriver.m @@ -250,6 +250,10 @@ SUInstallationInputData *installationData = [[SUInstallationInputData alloc] initWithRelaunchPath:pathToRelaunch progressToolPath:progressToolPath hostBundlePath:self.host.bundlePath updateDirectoryPath:self.temporaryDirectory downloadName:self.downloadName dsaSignature:dsaSignature decryptionPassword:decryptionPassword]; NSData *archivedData = SUArchiveRootObjectSecurely(installationData); + if (archivedData == nil) { + [self.delegate installerIsRequestingAbortInstallWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:@{ NSLocalizedDescriptionKey:SULocalizedString(@"An error occurred while encoding the installer parameters. Please try again later.", nil) }]]; + return; + } __weak SUInstallerDriver *weakSelf = self; [self setUpRemotePortWithCompletion:^(BOOL setupSuccess) { diff --git a/Sparkle/SUSecureCoding.h b/Sparkle/SUSecureCoding.h index 41548aba..0b7c6184 100644 --- a/Sparkle/SUSecureCoding.h +++ b/Sparkle/SUSecureCoding.h @@ -10,7 +10,7 @@ NS_ASSUME_NONNULL_BEGIN -NSData *SUArchiveRootObjectSecurely(id rootObject); +NSData * _Nullable SUArchiveRootObjectSecurely(id rootObject); id _Nullable SUUnarchiveRootObjectSecurely(NSData *data, Class klass); diff --git a/Sparkle/SUSecureCoding.m b/Sparkle/SUSecureCoding.m index 865fed72..1c5290a5 100644 --- a/Sparkle/SUSecureCoding.m +++ b/Sparkle/SUSecureCoding.m @@ -10,16 +10,21 @@ static NSString *SURootObjectArchiveKey = @"SURootObjectArchive"; -NSData *SUArchiveRootObjectSecurely(id rootObject) +NSData * _Nullable SUArchiveRootObjectSecurely(id rootObject) { NSMutableData *data = [NSMutableData data]; NSKeyedArchiver *keyedArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; keyedArchiver.requiresSecureCoding = YES; - [keyedArchiver encodeObject:rootObject forKey:SURootObjectArchiveKey]; - [keyedArchiver finishEncoding]; - - return [data copy]; + @try { + [keyedArchiver encodeObject:rootObject forKey:SURootObjectArchiveKey]; + [keyedArchiver finishEncoding]; + return [data copy]; + } @catch (NSException *exception) { + NSLog(@"Exception while securely archiving object: %@", exception); + [keyedArchiver finishEncoding]; + return nil; + } } id _Nullable SUUnarchiveRootObjectSecurely(NSData *data, Class klass) @@ -27,8 +32,13 @@ id _Nullable SUUnarchiveRootObjectSecurely(NSData *data, Class k NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; unarchiver.requiresSecureCoding = YES; - id rootObject = [unarchiver decodeObjectOfClass:klass forKey:SURootObjectArchiveKey]; - [unarchiver finishDecoding]; - - return rootObject; + @try { + id rootObject = [unarchiver decodeObjectOfClass:klass forKey:SURootObjectArchiveKey]; + [unarchiver finishDecoding]; + return rootObject; + } @catch (NSException *exception) { + NSLog(@"Exception while securely unarchiving object: %@", exception); + [unarchiver finishDecoding]; + return nil; + } }