From 4cc17020938212c063a09d5fdb236144378add6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Wed, 22 Jul 2015 10:35:03 +0100 Subject: [PATCH] Use bundleForClass instead of finding bundle by ID It should prevent confusion when multiple copies of Sparkle are available Fixes #571 --- Sparkle/SUBasicUpdateDriver.m | 7 ++++--- Sparkle/SUHost.h | 1 + Sparkle/SUHost.m | 12 +++++++++--- Sparkle/SUPlainInstaller.m | 7 ++++--- Sparkle/SUPlainInstallerInternals.h | 4 ++-- Sparkle/SUPlainInstallerInternals.m | 12 ++++++------ Sparkle/SUUpdater.m | 2 +- 7 files changed, 27 insertions(+), 18 deletions(-) diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 71c4151f..e4300633 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -419,7 +419,7 @@ [updaterDelegate updater:self.updater willInstallUpdate:self.updateItem]; } - NSBundle *sparkleBundle = [NSBundle bundleWithIdentifier:SUBundleIdentifier]; + NSBundle *sparkleBundle = self.host.sparkleBundle; // Copy the relauncher into a temporary directory so we can get to it after the new version's installed. // Only the paranoid survive: if there's already a stray copy of relaunch there, we would have problems. @@ -429,8 +429,9 @@ // Only the paranoid survive: if there's already a stray copy of relaunch there, we would have problems. NSError *error = nil; [[NSFileManager defaultManager] createDirectoryAtPath:[targetPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:@{} error:&error]; - - if ([SUPlainInstaller copyPathWithAuthentication:relaunchPathToCopy overPath:targetPath temporaryName:nil error:&error]) { + + BOOL appendVersion = [[sparkleBundle infoDictionary][SUAppendVersionNumberKey] boolValue]; + if ([SUPlainInstaller copyPathWithAuthentication:relaunchPathToCopy overPath:targetPath temporaryName:nil appendVersion:appendVersion error:&error]) { self.relaunchPath = targetPath; } else { [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SURelaunchError userInfo:@{ diff --git a/Sparkle/SUHost.h b/Sparkle/SUHost.h index 92925e7d..6fc82ee6 100644 --- a/Sparkle/SUHost.h +++ b/Sparkle/SUHost.h @@ -14,6 +14,7 @@ @interface SUHost : NSObject @property (strong, readonly) NSBundle *bundle; +@property (strong, readonly) NSBundle *sparkleBundle; + (NSString *)systemVersionString; diff --git a/Sparkle/SUHost.m b/Sparkle/SUHost.m index b9d622e8..62033380 100644 --- a/Sparkle/SUHost.m +++ b/Sparkle/SUHost.m @@ -25,7 +25,7 @@ typedef struct { @interface SUHost () -@property (strong, readwrite) NSBundle *bundle; +@property (strong, readwrite) NSBundle *bundle, *sparkleBundle; @property (copy) NSString *defaultsDomain; @property (assign) BOOL usesStandardUserDefaults; @@ -33,7 +33,7 @@ typedef struct { @implementation SUHost -@synthesize bundle; +@synthesize bundle, sparkleBundle; @synthesize defaultsDomain; @synthesize usesStandardUserDefaults; @@ -47,6 +47,12 @@ typedef struct { SULog(@"Error: the bundle being updated at %@ has no %@! This will cause preference read/write to not work properly.", self.bundle, kCFBundleIdentifierKey); } + self.sparkleBundle = [NSBundle bundleForClass:[self class]]; + if (!self.sparkleBundle) { + SULog(@"Error: SUHost can't find Sparkle.framework it belongs to"); + return nil; + } + self.defaultsDomain = [self.bundle objectForInfoDictionaryKey:SUDefaultsDomainKey]; if (!self.defaultsDomain) { self.defaultsDomain = [self.bundle bundleIdentifier]; @@ -91,7 +97,7 @@ typedef struct { - (NSString *)installationPath { - if ([[[NSBundle bundleWithIdentifier:SUBundleIdentifier] infoDictionary][SUNormalizeInstalledApplicationNameKey] boolValue]) { + if ([[self.sparkleBundle infoDictionary][SUNormalizeInstalledApplicationNameKey] boolValue]) { // We'll install to "#{CFBundleName}.app", but only if that path doesn't already exist. If we're "Foo 4.2.app," and there's a "Foo.app" in this directory, we don't want to overwrite it! But if there's no "Foo.app," we'll take that name. NSString *normalizedAppPath = [[[self.bundle bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", [self.bundle objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleNameKey], [[self.bundle bundlePath] pathExtension]]]; if (![[NSFileManager defaultManager] fileExistsAtPath:[[[self.bundle bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", [self.bundle objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleNameKey], [[self.bundle bundlePath] pathExtension]]]]) { diff --git a/Sparkle/SUPlainInstaller.m b/Sparkle/SUPlainInstaller.m index cea72914..03fe061d 100644 --- a/Sparkle/SUPlainInstaller.m +++ b/Sparkle/SUPlainInstaller.m @@ -19,7 +19,7 @@ SUParameterAssert(host); // Prevent malicious downgrades - if (![[[NSBundle bundleWithIdentifier:SUBundleIdentifier] infoDictionary][SUEnableAutomatedDowngradesKey] boolValue]) { + if (![[host.sparkleBundle infoDictionary][SUEnableAutomatedDowngradesKey] boolValue]) { if ([comparator compareVersion:[host version] toVersion:[[NSBundle bundleWithPath:path] objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey]] == NSOrderedDescending) { NSString *errorMessage = [NSString stringWithFormat:@"Sparkle Updater: Possible attack in progress! Attempting to \"upgrade\" from %@ to %@. Aborting update.", [host version], [[NSBundle bundleWithPath:path] objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey]]; NSError *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUDowngradeError userInfo:@{ NSLocalizedDescriptionKey: errorMessage }]; @@ -32,8 +32,9 @@ NSError *error = nil; NSString *oldPath = [host bundlePath]; NSString *tempName = [self temporaryNameForPath:[host installationPath]]; + BOOL appendVersion = [[host.sparkleBundle infoDictionary][SUAppendVersionNumberKey] boolValue]; - BOOL result = [self copyPathWithAuthentication:path overPath:installationPath temporaryName:tempName error:&error]; + BOOL result = [self copyPathWithAuthentication:path overPath:installationPath temporaryName:tempName appendVersion:appendVersion error:&error]; if (result) { if ([SUCodeSigningVerifier applicationAtPathIsCodeSigned:installationPath]) { @@ -45,7 +46,7 @@ BOOL haveOld = [[NSFileManager defaultManager] fileExistsAtPath:oldPath]; BOOL differentFromNew = ![oldPath isEqualToString:installationPath]; if (haveOld && differentFromNew) { - [self _movePathToTrash:oldPath]; // On success, trash old copy if there's still one due to renaming. + [self _movePathToTrash:oldPath appendVersion:appendVersion]; // On success, trash old copy if there's still one due to renaming. } } diff --git a/Sparkle/SUPlainInstallerInternals.h b/Sparkle/SUPlainInstallerInternals.h index f05cae91..cc6778b4 100644 --- a/Sparkle/SUPlainInstallerInternals.h +++ b/Sparkle/SUPlainInstallerInternals.h @@ -15,8 +15,8 @@ @interface SUPlainInstaller (Internals) + (NSString *)temporaryNameForPath:(NSString *)path; -+ (BOOL)copyPathWithAuthentication:(NSString *)src overPath:(NSString *)dst temporaryName:(NSString *)tmp error:(NSError **)error; -+ (void)_movePathToTrash:(NSString *)path; ++ (BOOL)copyPathWithAuthentication:(NSString *)src overPath:(NSString *)dst temporaryName:(NSString *)tmp appendVersion:(BOOL)a error:(NSError **)error; ++ (void)_movePathToTrash:(NSString *)path appendVersion:(BOOL)a; + (BOOL)_removeFileAtPath:(NSString *)path error:(NSError **)error; + (BOOL)_removeFileAtPathWithForcedAuthentication:(NSString *)src error:(NSError **)error; @end diff --git a/Sparkle/SUPlainInstallerInternals.m b/Sparkle/SUPlainInstallerInternals.m index 75ae5a5c..3f126875 100644 --- a/Sparkle/SUPlainInstallerInternals.m +++ b/Sparkle/SUPlainInstallerInternals.m @@ -107,7 +107,7 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza return [tempDir lastPathComponent]; } -+ (NSString *)_temporaryCopyNameForPath:(NSString *)path didFindTrash:(BOOL *)outDidFindTrash ++ (NSString *)_temporaryCopyNameForPath:(NSString *)path appendVersion:(BOOL)appendVersion didFindTrash:(BOOL *)outDidFindTrash { // *** MUST BE SAFE TO CALL ON NON-MAIN THREAD! NSString *tempDir = nil; @@ -140,7 +140,7 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza // Let's try to read the version number so the filename will be more meaningful NSString *prefix; - if ([[[NSBundle bundleWithIdentifier:SUBundleIdentifier] infoDictionary][SUAppendVersionNumberKey] boolValue]) { + if (appendVersion) { NSString *postFix = nil; NSString *version = nil; if ((version = [[NSBundle bundleWithPath: path] objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey]) && ![version isEqualToString:@""]) @@ -443,14 +443,14 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza return success; } -+ (void)_movePathToTrash:(NSString *)path ++ (void)_movePathToTrash:(NSString *)path appendVersion:(BOOL)appendVersion { //SULog(@"Moving %@ to the trash.", path); NSInteger tag = 0; if (![[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:[path stringByDeletingLastPathComponent] destination:@"" files:@[[path lastPathComponent]] tag:&tag]) { BOOL didFindTrash = NO; - NSString *trashPath = [self _temporaryCopyNameForPath:path didFindTrash:&didFindTrash]; + NSString *trashPath = [self _temporaryCopyNameForPath:path appendVersion:appendVersion didFindTrash:&didFindTrash]; if( didFindTrash ) { NSError *err = nil; @@ -464,12 +464,12 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza } } -+ (BOOL)copyPathWithAuthentication:(NSString *)src overPath:(NSString *)dst temporaryName:(NSString *)__unused tmp error:(NSError *__autoreleasing *)error ++ (BOOL)copyPathWithAuthentication:(NSString *)src overPath:(NSString *)dst temporaryName:(NSString *)__unused tmp appendVersion:(BOOL)appendVersion error:(NSError *__autoreleasing *)error { FSRef srcRef, dstRef, dstDirRef, tmpDirRef; OSStatus err; BOOL hadFileAtDest = NO, didFindTrash = NO; - NSString *tmpPath = [self _temporaryCopyNameForPath:dst didFindTrash:&didFindTrash]; + NSString *tmpPath = [self _temporaryCopyNameForPath:dst appendVersion:appendVersion didFindTrash:&didFindTrash]; // Make FSRef for destination: err = FSPathMakeRefWithOptions((const UInt8 *)[dst fileSystemRepresentation], kFSPathMakeRefDoNotFollowLeafSymlink, &dstRef, NULL); diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index ca3b3606..2235f32f 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -465,7 +465,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults return customUserAgentString; } - NSString *version = [[NSBundle bundleWithIdentifier:SUBundleIdentifier] objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey]; + NSString *version = [self.host.sparkleBundle objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey]; NSString *userAgent = [NSString stringWithFormat:@"%@/%@ Sparkle/%@", [self.host name], [self.host displayVersion], version ? version : @"?"]; NSData *cleanedAgent = [userAgent dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; return [[NSString alloc] initWithData:cleanedAgent encoding:NSASCIIStringEncoding];