diff --git a/Sparkle/SUApplicationInfo.h b/Sparkle/SUApplicationInfo.h index db4dd1ca..9aeeca68 100644 --- a/Sparkle/SUApplicationInfo.h +++ b/Sparkle/SUApplicationInfo.h @@ -10,11 +10,13 @@ NS_ASSUME_NONNULL_BEGIN +@class SUHost; + @interface SUApplicationInfo : NSObject + (BOOL)isBackgroundApplication:(NSApplication *)application; -+ (NSImage *)bestIconForBundle:(NSBundle *)bundle; ++ (NSImage *)bestIconForHost:(SUHost *)host; @end diff --git a/Sparkle/SUApplicationInfo.m b/Sparkle/SUApplicationInfo.m index c8fec0be..eb349af3 100644 --- a/Sparkle/SUApplicationInfo.m +++ b/Sparkle/SUApplicationInfo.m @@ -8,6 +8,7 @@ #import "SUApplicationInfo.h" #import "SUBundleIcon.h" +#import "SUHost.h" @implementation SUApplicationInfo @@ -16,16 +17,16 @@ return (application.activationPolicy == NSApplicationActivationPolicyAccessory); } -+ (NSImage *)bestIconForBundle:(NSBundle *)bundle ++ (NSImage *)bestIconForHost:(SUHost *)host { - NSURL *iconURL = [SUBundleIcon iconURLForBundle:bundle]; + NSURL *iconURL = [SUBundleIcon iconURLForHost:host]; NSImage *icon = (iconURL == nil) ? nil : [[NSImage alloc] initWithContentsOfURL:iconURL]; // Use a default icon if none is defined. if (!icon) { // this asumption may not be correct (eg. even though we're not the main bundle, it could be still be a regular app) // but still better than nothing if no icon was included - BOOL isMainBundle = (bundle == [NSBundle mainBundle]); + BOOL isMainBundle = [host.bundle isEqualTo:[NSBundle mainBundle]]; NSString *fileType = isMainBundle ? (__bridge NSString *)kUTTypeApplication : (__bridge NSString *)kUTTypeBundle; icon = [[NSWorkspace sharedWorkspace] iconForFileType:fileType]; diff --git a/Sparkle/SUAutomaticUpdateAlert.m b/Sparkle/SUAutomaticUpdateAlert.m index 8325eab7..4f066d90 100644 --- a/Sparkle/SUAutomaticUpdateAlert.m +++ b/Sparkle/SUAutomaticUpdateAlert.m @@ -73,7 +73,7 @@ - (NSImage *__nonnull)applicationIcon { - return [SUApplicationInfo bestIconForBundle:self.host.bundle]; + return [SUApplicationInfo bestIconForHost:self.host]; } - (NSString *__nonnull)titleText diff --git a/Sparkle/SUBundleIcon.h b/Sparkle/SUBundleIcon.h index f42be2fd..3e709b5d 100644 --- a/Sparkle/SUBundleIcon.h +++ b/Sparkle/SUBundleIcon.h @@ -10,9 +10,11 @@ NS_ASSUME_NONNULL_BEGIN +@class SUHost; + @interface SUBundleIcon : NSObject -+ (NSURL * _Nullable)iconURLForBundle:(NSBundle *)bundle; ++ (NSURL * _Nullable)iconURLForHost:(SUHost *)host; @end diff --git a/Sparkle/SUBundleIcon.m b/Sparkle/SUBundleIcon.m index a3d86a80..97e22698 100644 --- a/Sparkle/SUBundleIcon.m +++ b/Sparkle/SUBundleIcon.m @@ -7,21 +7,23 @@ // #import "SUBundleIcon.h" +#import "SUHost.h" @implementation SUBundleIcon -+ (NSURL *)iconURLForBundle:(NSBundle *)bundle +// Note: To obtain the most current bundle icon file from the Info dictionary, this should take a SUHost, not a NSBundle ++ (NSURL *)iconURLForHost:(SUHost *)host { - NSString *resource = [bundle objectForInfoDictionaryKey:@"CFBundleIconFile"]; + NSString *resource = [host objectForInfoDictionaryKey:@"CFBundleIconFile"]; if (resource == nil || ![resource isKindOfClass:[NSString class]]) { return nil; } - NSURL *iconURL = [bundle URLForResource:resource withExtension:@"icns"]; + NSURL *iconURL = [host.bundle URLForResource:resource withExtension:@"icns"]; // The resource could already be containing the path extension, so try again without the extra extension if (iconURL == nil) { - iconURL = [bundle URLForResource:resource withExtension:nil]; + iconURL = [host.bundle URLForResource:resource withExtension:nil]; } return iconURL; } diff --git a/Sparkle/SUHost.m b/Sparkle/SUHost.m index 19abc204..659f59b4 100644 --- a/Sparkle/SUHost.m +++ b/Sparkle/SUHost.m @@ -20,6 +20,7 @@ @interface SUHost () @property (strong, readwrite) NSBundle *bundle; +@property (nonatomic, readonly) BOOL isMainBundle; @property (copy) NSString *defaultsDomain; @property (assign) BOOL usesStandardUserDefaults; @@ -28,6 +29,7 @@ @implementation SUHost @synthesize bundle; +@synthesize isMainBundle = _isMainBundle; @synthesize defaultsDomain; @synthesize usesStandardUserDefaults; @@ -40,8 +42,10 @@ if (![self.bundle bundleIdentifier]) { SULog(SULogLevelError, @"Error: the bundle being updated at %@ has no %@! This will cause preference read/write to not work properly.", self.bundle, kCFBundleIdentifierKey); } + + _isMainBundle = [aBundle isEqualTo:[NSBundle mainBundle]]; - self.defaultsDomain = [self.bundle objectForInfoDictionaryKey:SUDefaultsDomainKey]; + self.defaultsDomain = [self objectForInfoDictionaryKey:SUDefaultsDomainKey]; if (!self.defaultsDomain) { self.defaultsDomain = [self.bundle bundleIdentifier]; } @@ -69,7 +73,7 @@ name = [self objectForInfoDictionaryKey:@"SUBundleName"]; if (name && name.length > 0) return name; - name = [self.bundle objectForInfoDictionaryKey:@"CFBundleDisplayName"]; + name = [self objectForInfoDictionaryKey:@"CFBundleDisplayName"]; if (name && name.length > 0) return name; name = [self objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleNameKey]; @@ -80,7 +84,7 @@ - (NSString *__nonnull)version { - NSString *version = [self.bundle objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey]; + NSString *version = [self objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey]; if (!version || [version isEqualToString:@""]) [NSException raise:@"SUNoVersionException" format:@"This host (%@) has no %@! This attribute is required.", [self bundlePath], (__bridge NSString *)kCFBundleVersionKey]; return version; @@ -88,7 +92,7 @@ - (NSString *__nonnull)displayVersion { - NSString *shortVersionString = [self.bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; + NSString *shortVersionString = [self objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; if (shortVersionString) return shortVersionString; else @@ -105,7 +109,7 @@ - (NSString *__nullable)publicDSAKey { // Maybe the key is just a string in the Info.plist. - NSString *key = [self.bundle objectForInfoDictionaryKey:SUPublicDSAKeyKey]; + NSString *key = [self objectForInfoDictionaryKey:SUPublicDSAKeyKey]; if (key) { return key; } @@ -130,7 +134,21 @@ - (id)objectForInfoDictionaryKey:(NSString *)key { - return [self.bundle objectForInfoDictionaryKey:key]; + if (self.isMainBundle) { + // Common fast path - if we're updating the main bundle, that means our updater and host bundle's lifetime is the same + // If the bundle happens to be updated or change, that means our updater process needs to be terminated first to do it safely + // Thus we can rely on the cached Info dictionary + return [self.bundle objectForInfoDictionaryKey:key]; + } else { + // Slow path - if we're updating another bundle, we should read in the most up to date Info dictionary because + // the bundle can be replaced externally or even by us. + // This is the easiest way to read the Info dictionary values *correctly* despite some performance loss. + // A mutable method to reload the Info dictionary at certain points and have it cached at other points is challenging to do correctly. + CFDictionaryRef cfInfoDictionary = CFBundleCopyInfoDictionaryInDirectory((CFURLRef)self.bundle.bundleURL); + NSDictionary *infoDictionary = CFBridgingRelease(cfInfoDictionary); + + return [infoDictionary objectForKey:key]; + } } - (BOOL)boolForInfoDictionaryKey:(NSString *)key diff --git a/Sparkle/SUInstaller.m b/Sparkle/SUInstaller.m index 6abef25b..4ef9ec50 100644 --- a/Sparkle/SUInstaller.m +++ b/Sparkle/SUInstaller.m @@ -150,7 +150,7 @@ NSBundle *bundle = host.bundle; assert(bundle != nil); - NSString *normalizedAppPath = [[[bundle bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", [bundle objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleNameKey], [[bundle bundlePath] pathExtension]]]; + NSString *normalizedAppPath = [[[bundle bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", [host objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleNameKey], [[bundle bundlePath] pathExtension]]]; return normalizedAppPath; } diff --git a/Sparkle/SUPlainInstaller.m b/Sparkle/SUPlainInstaller.m index a845a47e..98ef2c74 100644 --- a/Sparkle/SUPlainInstaller.m +++ b/Sparkle/SUPlainInstaller.m @@ -186,12 +186,15 @@ // Prevent malicious downgrades if (!allowDowngrades) { NSString *hostVersion = [self.host version]; + NSBundle *bundle = [NSBundle bundleWithPath:self.bundlePath]; - NSString *updateVersion = [bundle objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey]; + SUHost *updateHost = [[SUHost alloc] initWithBundle:bundle]; + NSString *updateVersion = [updateHost objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey]; + id comparator = [[SUStandardVersionComparator alloc] init]; if (!updateVersion || [comparator compareVersion:hostVersion toVersion:updateVersion] == NSOrderedDescending) { if (error != NULL) { - NSString *errorMessage = [NSString stringWithFormat:@"For security reasons, updates that downgrade version of the application are not allowed. Refusing to downgrade app from version %@ to %@. Aborting update.", hostVersion, [bundle objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey]]; + NSString *errorMessage = [NSString stringWithFormat:@"For security reasons, updates that downgrade version of the application are not allowed. Refusing to downgrade app from version %@ to %@. Aborting update.", hostVersion, updateVersion]; *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUDowngradeError userInfo:@{ NSLocalizedDescriptionKey: errorMessage }]; } diff --git a/Sparkle/SUStatusController.m b/Sparkle/SUStatusController.m index cdca9ff8..eb5ecbfc 100644 --- a/Sparkle/SUStatusController.m +++ b/Sparkle/SUStatusController.m @@ -66,7 +66,7 @@ - (NSImage *)applicationIcon { - return [SUApplicationInfo bestIconForBundle:self.host.bundle]; + return [SUApplicationInfo bestIconForHost:self.host]; } - (void)beginActionWithTitle:(NSString *)aTitle maxProgressValue:(double)aMaxProgressValue statusText:(NSString *)aStatusText diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 99d4ddba..e0f5cb90 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -314,7 +314,7 @@ // are focused to inform the user since there is no dock icon to notify them. if ([SUApplicationInfo isBackgroundApplication:[NSApplication sharedApplication]]) { [NSApp activateIgnoringOtherApps:YES]; } - [alert setIcon:[SUApplicationInfo bestIconForBundle:self.host.bundle]]; + [alert setIcon:[SUApplicationInfo bestIconForHost:self.host]]; [alert runModal]; if ([[updater delegate] respondsToSelector:@selector(updaterDidShowModalAlert:)]) diff --git a/Sparkle/SUUpdateAlert.m b/Sparkle/SUUpdateAlert.m index d3261b18..e7d79610 100644 --- a/Sparkle/SUUpdateAlert.m +++ b/Sparkle/SUUpdateAlert.m @@ -225,7 +225,7 @@ - (NSImage *)applicationIcon { - return [SUApplicationInfo bestIconForBundle:self.host.bundle]; + return [SUApplicationInfo bestIconForHost:self.host]; } - (NSString *)titleText diff --git a/Sparkle/SUUpdatePermissionPrompt.m b/Sparkle/SUUpdatePermissionPrompt.m index 23ee647e..a6b96378 100644 --- a/Sparkle/SUUpdatePermissionPrompt.m +++ b/Sparkle/SUUpdatePermissionPrompt.m @@ -100,7 +100,7 @@ - (NSImage *)icon { - return [SUApplicationInfo bestIconForBundle:self.host.bundle]; + return [SUApplicationInfo bestIconForHost:self.host]; } - (NSString *)promptDescription