From 0b6b6fe4ca20ccb9ad506231f8913e1d48b453eb Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 7 Jan 2017 23:52:56 -0500 Subject: [PATCH 1/3] Improve -[SUHost objectForInfoDictionaryKey:] We use the cached host bundle if the host bundle is equal to the main bundle. Otherwise we read in the latest Info dictionary every time. The cached case is the most common case. The odd case (updating other bundles) is less common. Thus we have better performance for the common case, and we retain correct code in both cases. Also we invoke our own implementation of -objectForInfoDictionaryKey: rather than NSBundle's implementation in a few places. --- Sparkle/SUHost.m | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/Sparkle/SUHost.m b/Sparkle/SUHost.m index 8470ae22..bd9bc78a 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(@"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 From fc9b2b38fa4ecbd69a23e13fe8ceb16123632e03 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 8 Jan 2017 15:36:30 -0500 Subject: [PATCH 2/3] Make SUBundleIcon & SUApplicationInfo take SUHost, not NSBundle When we retrieve the "CFBundleIconFile" key from the Info dictionary, we should rely on SUHost's method of obtaining that value now. --- Sparkle/SUApplicationInfo.h | 4 +++- Sparkle/SUApplicationInfo.m | 7 ++++--- Sparkle/SUAutomaticUpdateAlert.m | 2 +- Sparkle/SUBundleIcon.h | 4 +++- Sparkle/SUBundleIcon.m | 10 ++++++---- Sparkle/SUStatusController.m | 2 +- Sparkle/SUUIBasedUpdateDriver.m | 2 +- Sparkle/SUUpdateAlert.m | 2 +- Sparkle/SUUpdatePermissionPrompt.m | 2 +- 9 files changed, 21 insertions(+), 14 deletions(-) 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 f49dae6e..5e1ae37e 100644 --- a/Sparkle/SUAutomaticUpdateAlert.m +++ b/Sparkle/SUAutomaticUpdateAlert.m @@ -61,7 +61,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/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 efda7f45..e44f00de 100644 --- a/Sparkle/SUUpdateAlert.m +++ b/Sparkle/SUUpdateAlert.m @@ -221,7 +221,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 From 7ef468e8ea66a7b11cb67d46bf7cbecf32dd2372 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 8 Jan 2017 15:47:44 -0500 Subject: [PATCH 3/3] Use -[SUHost objectForInfoDictionaryKey:] in couple more places --- Sparkle/SUInstaller.m | 2 +- Sparkle/SUPlainInstaller.m | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Sparkle/SUInstaller.m b/Sparkle/SUInstaller.m index 3ef0c5ff..2a3c5b3e 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 586ce47f..26b27256 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 }]; }