From e7f85bb40fbd85c6ec25f223f0d8ae16128bff3f Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 1 Apr 2016 18:56:20 -0400 Subject: [PATCH] Add testing writability as part of SUInstallerLauncher This is necessary for detecting if automatic updates are possible in a sandboxed environment --- InstallerLauncher/SUInstallerLauncher.h | 2 +- InstallerLauncher/SUInstallerLauncher.m | 5 ++ .../SUInstallerLauncherProtocol.h | 6 +- InstallerLauncher/main.m | 2 +- Sparkle.xcodeproj/project.pbxproj | 8 +++ Sparkle/SUInstallerDriver.m | 2 +- Sparkle/SUUpdater.h | 2 - Sparkle/SUUpdater.m | 72 +++++++++++-------- Sparkle/SUUpdaterPermission.h | 20 ++++++ Sparkle/SUUpdaterPermission.m | 46 ++++++++++++ 10 files changed, 129 insertions(+), 36 deletions(-) create mode 100644 Sparkle/SUUpdaterPermission.h create mode 100644 Sparkle/SUUpdaterPermission.m diff --git a/InstallerLauncher/SUInstallerLauncher.h b/InstallerLauncher/SUInstallerLauncher.h index e9e637cc..4379e6a6 100644 --- a/InstallerLauncher/SUInstallerLauncher.h +++ b/InstallerLauncher/SUInstallerLauncher.h @@ -11,5 +11,5 @@ // Non-sandboxed XPC service used for launching our installer // This is necessary for sandboxed applications -@interface SUInstallerLauncher : NSObject +@interface SUInstallerLauncher : NSObject @end diff --git a/InstallerLauncher/SUInstallerLauncher.m b/InstallerLauncher/SUInstallerLauncher.m index e4e53d33..2a66af53 100644 --- a/InstallerLauncher/SUInstallerLauncher.m +++ b/InstallerLauncher/SUInstallerLauncher.m @@ -35,4 +35,9 @@ }); } +- (void)testWritabilityAtPath:(NSString *)path completion:(void (^)(BOOL isWritable))completionHandler +{ + completionHandler([[NSFileManager defaultManager] isWritableFileAtPath:path]); +} + @end diff --git a/InstallerLauncher/SUInstallerLauncherProtocol.h b/InstallerLauncher/SUInstallerLauncherProtocol.h index d6ff28fd..a62f6df2 100644 --- a/InstallerLauncher/SUInstallerLauncherProtocol.h +++ b/InstallerLauncher/SUInstallerLauncherProtocol.h @@ -1,5 +1,5 @@ // -// InstallerLauncherProtocol.h +// SUInstallerLauncherProtocol.h // InstallerLauncher // // Created by Mayur Pawashe on 4/1/16. @@ -8,8 +8,10 @@ #import -@protocol InstallerLauncherProtocol +@protocol SUInstallerLauncherProtocol - (void)launchInstallerAtPath:(NSString *)installerPath withArguments:(NSArray *)arguments completion:(void (^)(BOOL success))completionHandler; + +- (void)testWritabilityAtPath:(NSString *)path completion:(void (^)(BOOL isWritable))completionHandler; @end diff --git a/InstallerLauncher/main.m b/InstallerLauncher/main.m index 8baf157f..6f1ac846 100644 --- a/InstallerLauncher/main.m +++ b/InstallerLauncher/main.m @@ -19,7 +19,7 @@ // Configure the connection. // First, set the interface that the exported object implements. - newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(InstallerLauncherProtocol)]; + newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SUInstallerLauncherProtocol)]; // Next, set the object that the connection exports. All messages sent on the connection to this service will be sent to the exported object to handle. The connection retains the exported object. SUInstallerLauncher *exportedObject = [SUInstallerLauncher new]; diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 8963c2bb..40c5cce4 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -174,6 +174,8 @@ 726E07BF1CAF0C6C001A286B /* SUConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = 61299A5F09CA6EB100B7442F /* SUConstants.m */; }; 726E07C01CAF15B7001A286B /* SULog.m in Sources */ = {isa = PBXBuildFile; fileRef = 55C14F05136EF6DB00649790 /* SULog.m */; }; 726E07C21CAF1E79001A286B /* ConfigInstallerLauncher.xcconfig in Resources */ = {isa = PBXBuildFile; fileRef = 726E07C11CAF1E79001A286B /* ConfigInstallerLauncher.xcconfig */; }; + 726E07E91CAF30C0001A286B /* SUUpdaterPermission.h in Headers */ = {isa = PBXBuildFile; fileRef = 726E07E71CAF30C0001A286B /* SUUpdaterPermission.h */; }; + 726E07EA1CAF30C0001A286B /* SUUpdaterPermission.m in Sources */ = {isa = PBXBuildFile; fileRef = 726E07E81CAF30C0001A286B /* SUUpdaterPermission.m */; }; 726E4A0E1C86881600C57C6A /* SUStandardUserDriverUIDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 726E4A0D1C86881600C57C6A /* SUStandardUserDriverUIDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; }; 726E4A101C86B5A200C57C6A /* SUStandardUserDriverRemoteDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 726E4A0F1C86B5A200C57C6A /* SUStandardUserDriverRemoteDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; }; 726E4A1B1C86C88F00C57C6A /* TestAppHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 726E4A1A1C86C88F00C57C6A /* TestAppHelper.m */; }; @@ -710,6 +712,8 @@ 726E07B31CAF08D6001A286B /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 726E07B51CAF08D6001A286B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 726E07C11CAF1E79001A286B /* ConfigInstallerLauncher.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = ConfigInstallerLauncher.xcconfig; sourceTree = ""; }; + 726E07E71CAF30C0001A286B /* SUUpdaterPermission.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUUpdaterPermission.h; sourceTree = ""; }; + 726E07E81CAF30C0001A286B /* SUUpdaterPermission.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUUpdaterPermission.m; sourceTree = ""; }; 726E4A0D1C86881600C57C6A /* SUStandardUserDriverUIDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUStandardUserDriverUIDelegate.h; sourceTree = ""; }; 726E4A0F1C86B5A200C57C6A /* SUStandardUserDriverRemoteDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUStandardUserDriverRemoteDelegate.h; sourceTree = ""; }; 726E4A111C86C44A00C57C6A /* Sparkle-Test-App.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = "Sparkle-Test-App.entitlements"; sourceTree = ""; }; @@ -1269,6 +1273,8 @@ 61CFB2C20E38496B007A1735 /* Drivers */, 61B5F8E309C4CE3C00B25A18 /* SUUpdater.h */, 61B5F8E409C4CE3C00B25A18 /* SUUpdater.m */, + 726E07E71CAF30C0001A286B /* SUUpdaterPermission.h */, + 726E07E81CAF30C0001A286B /* SUUpdaterPermission.m */, 726E078B1CA891E9001A286B /* SUUpdaterSettings.h */, 726E078C1CA891E9001A286B /* SUUpdaterSettings.m */, 72A6F9891C94E2D6005F404C /* SUUpdaterDelegate.h */, @@ -1440,6 +1446,7 @@ 726E07601CA4E9A5001A286B /* SUInstallationInputData.h in Headers */, 725602D51C83551C00DAA70E /* SUApplicationInfo.h in Headers */, 767B61AC1972D488004E0C3C /* SUGuidedPackageInstaller.h in Headers */, + 726E07E91CAF30C0001A286B /* SUUpdaterPermission.h in Headers */, 618FA5010DAE88B40026945C /* SUInstaller.h in Headers */, 55C14F06136EF6DB00649790 /* SULog.h in Headers */, 726F2CE51BC9C33D001971A4 /* SUOperatingSystem.h in Headers */, @@ -2070,6 +2077,7 @@ 729746411C9119F300FC134E /* SULocalMessagePort.m in Sources */, 726E07611CA4E9A5001A286B /* SUInstallationInputData.m in Sources */, 7275F9C21B5F1F2900B1D19E /* SUFileManager.m in Sources */, + 726E07EA1CAF30C0001A286B /* SUUpdaterPermission.m in Sources */, 725CB95B1C7121830064365A /* SUStandardUserDriver.m in Sources */, 61EF67560E25B58D00F754E0 /* SUHost.m in Sources */, 55C14F07136EF6DB00649790 /* SULog.m in Sources */, diff --git a/Sparkle/SUInstallerDriver.m b/Sparkle/SUInstallerDriver.m index f8bcc32b..45424a42 100644 --- a/Sparkle/SUInstallerDriver.m +++ b/Sparkle/SUInstallerDriver.m @@ -388,7 +388,7 @@ } NSXPCConnection *launcherConnection = [[NSXPCConnection alloc] initWithServiceName:@"org.sparkle-project.InstallerLauncher"]; - launcherConnection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(InstallerLauncherProtocol)]; + launcherConnection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SUInstallerLauncherProtocol)]; [launcherConnection resume]; [launcherConnection.remoteObjectProxy launchInstallerAtPath:relaunchToolPath withArguments:@[hostBundleIdentifier] completion:^(BOOL success) { diff --git a/Sparkle/SUUpdater.h b/Sparkle/SUUpdater.h index 837d5aed..e56ee065 100644 --- a/Sparkle/SUUpdater.h +++ b/Sparkle/SUUpdater.h @@ -67,8 +67,6 @@ SU_EXPORT @interface SUUpdater : NSObject @property (nonatomic) BOOL sendsSystemProfile; -@property (nonatomic, readonly) BOOL allowsAutomaticUpdates; - @property (nonatomic) BOOL automaticallyDownloadsUpdates; /*! diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index f88bc480..5736bb50 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -23,6 +23,7 @@ #import "SUAutomaticUpdateDriver.h" #import "SUProbeInstallStatus.h" #import "SUAppcastItem.h" +#import "SUUpdaterPermission.h" #ifdef _APPKITDEFINES_H #error This is a "core" class and should NOT import AppKit @@ -43,6 +44,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification @property (strong) id driver; @property (strong) SUHost *host; @property (nonatomic, readonly) SUUpdaterSettings *updaterSettings; +@property (nonatomic, readonly) SUUpdaterPermission *updaterPermission; @end @@ -55,6 +57,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification @synthesize driver; @synthesize host; @synthesize updaterSettings = _updaterSettings; +@synthesize updaterPermission = _updaterPermission; @synthesize sparkleBundle; - (instancetype)initWithHostBundle:(NSBundle *)bundle userDriver:(id )userDriver delegate:(id )theDelegate @@ -72,6 +75,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification host = [[SUHost alloc] initWithBundle:bundle]; _updaterSettings = [[SUUpdaterSettings alloc] initWithHostBundle:bundle]; + _updaterPermission = [[SUUpdaterPermission alloc] init]; _userDriver = userDriver; @@ -300,31 +304,33 @@ static void SUCheckForUpdatesInBgReachabilityCheck(__weak SUUpdater *updater, id // Wouldn't want to annoy users on dial-up by establishing a connection every // hour or so: - id updateDriver; - if ([self automaticallyDownloadsUpdates] && [self allowsAutomaticUpdates] && ![SUProbeInstallStatus probeInstallerInProgressForHost:self.host]) { - updateDriver = - [[SUAutomaticUpdateDriver alloc] - initWithHost:self.host - sparkleBundle:self.sparkleBundle - updater:self - updaterDelegate:self.delegate]; - } else { - updateDriver = - [[SUScheduledUpdateDriver alloc] - initWithHost:self.host - allowsAutomaticUpdates:[self allowsAutomaticUpdates] - sparkleBundle:self.sparkleBundle - updater:self - userDriver:self.userDriver - updaterDelegate:self.delegate]; - } - - // We don't want the reachability check to act on the driver if the updater is going near death - __weak SUUpdater *updater = self; - NSURL *feedURL = [updater feedURL]; - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - SUCheckForUpdatesInBgReachabilityCheck(updater, updateDriver, feedURL); - }); + [self allowsAutomaticUpdatesWithCompletion:^(BOOL allowsAutomaticUpdates) { + id updateDriver; + if ([self automaticallyDownloadsUpdates] && allowsAutomaticUpdates && ![SUProbeInstallStatus probeInstallerInProgressForHost:self.host]) { + updateDriver = + [[SUAutomaticUpdateDriver alloc] + initWithHost:self.host + sparkleBundle:self.sparkleBundle + updater:self + updaterDelegate:self.delegate]; + } else { + updateDriver = + [[SUScheduledUpdateDriver alloc] + initWithHost:self.host + allowsAutomaticUpdates:allowsAutomaticUpdates + sparkleBundle:self.sparkleBundle + updater:self + userDriver:self.userDriver + updaterDelegate:self.delegate]; + } + + // We don't want the reachability check to act on the driver if the updater is going near death + __weak SUUpdater *updater = self; + NSURL *feedURL = [updater feedURL]; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + SUCheckForUpdatesInBgReachabilityCheck(updater, updateDriver, feedURL); + }); + }]; } - (void)checkForUpdates @@ -335,8 +341,10 @@ static void SUCheckForUpdatesInBgReachabilityCheck(__weak SUUpdater *updater, id return; } - id theUpdateDriver = [[SUUserInitiatedUpdateDriver alloc] initWithHost:self.host allowsAutomaticUpdates:[self allowsAutomaticUpdates] sparkleBundle:self.sparkleBundle updater:self userDriver:self.userDriver updaterDelegate:self.delegate]; - [self checkForUpdatesWithDriver:theUpdateDriver]; + [self allowsAutomaticUpdatesWithCompletion:^(BOOL allowsAutomaticUpdates) { + id theUpdateDriver = [[SUUserInitiatedUpdateDriver alloc] initWithHost:self.host allowsAutomaticUpdates:allowsAutomaticUpdates sparkleBundle:self.sparkleBundle updater:self userDriver:self.userDriver updaterDelegate:self.delegate]; + [self checkForUpdatesWithDriver:theUpdateDriver]; + }]; } - (void)checkForUpdateInformation @@ -443,10 +451,16 @@ static void SUCheckForUpdatesInBgReachabilityCheck(__weak SUUpdater *updater, id return [self.updaterSettings automaticallyDownloadsUpdates]; } -- (BOOL)allowsAutomaticUpdates +- (void)allowsAutomaticUpdatesWithCompletion:(void (^)(BOOL))completionHandler { NSNumber *developerAllowsAutomaticUpdates = [self.host objectForInfoDictionaryKey:SUAllowsAutomaticUpdatesKey]; - return (developerAllowsAutomaticUpdates == nil || developerAllowsAutomaticUpdates.boolValue) && [[NSFileManager defaultManager] isWritableFileAtPath:self.host.bundlePath]; + if (developerAllowsAutomaticUpdates != nil && !developerAllowsAutomaticUpdates.boolValue) { + completionHandler(NO); + } else { + [self.updaterPermission testUpdateWritabilityAtPath:self.host.bundlePath completion:^(BOOL isWritable) { + completionHandler(isWritable); + }]; + } } - (void)setFeedURL:(NSURL *)feedURL diff --git a/Sparkle/SUUpdaterPermission.h b/Sparkle/SUUpdaterPermission.h new file mode 100644 index 00000000..8fefa10a --- /dev/null +++ b/Sparkle/SUUpdaterPermission.h @@ -0,0 +1,20 @@ +// +// SUUpdaterPermission.h +// Sparkle +// +// Created by Mayur Pawashe on 4/1/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface SUUpdaterPermission : NSObject + +// Must be called from main queue +- (void)testUpdateWritabilityAtPath:(NSString *)path completion:(void (^)(BOOL))completionHandler; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUUpdaterPermission.m b/Sparkle/SUUpdaterPermission.m new file mode 100644 index 00000000..91a881c9 --- /dev/null +++ b/Sparkle/SUUpdaterPermission.m @@ -0,0 +1,46 @@ +// +// SUUpdaterPermission.m +// Sparkle +// +// Created by Mayur Pawashe on 4/1/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUUpdaterPermission.h" +#import "SUInstallerLauncherProtocol.h" + +@interface SUUpdaterPermission () + +@property (nonatomic) BOOL allowsPermission; +@property (nonatomic) BOOL checkedPermission; + +@end + +@implementation SUUpdaterPermission + +@synthesize allowsPermission = _allowsPermission; +@synthesize checkedPermission = _checkedPermission; + +- (void)testUpdateWritabilityAtPath:(NSString *)path completion:(void (^)(BOOL))completionHandler +{ + if (!self.checkedPermission) { + NSXPCConnection *launcherConnection = [[NSXPCConnection alloc] initWithServiceName:@"org.sparkle-project.InstallerLauncher"]; + launcherConnection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SUInstallerLauncherProtocol)]; + [launcherConnection resume]; + + __weak SUUpdaterPermission *weakSelf = self; + [launcherConnection.remoteObjectProxy testWritabilityAtPath:path completion:^(BOOL isWritable) { + [launcherConnection invalidate]; + + dispatch_async(dispatch_get_main_queue(), ^{ + weakSelf.checkedPermission = YES; + weakSelf.allowsPermission = isWritable; + completionHandler(isWritable); + }); + }]; + } else { + completionHandler(self.allowsPermission); + } +} + +@end