From db6ef214abc5f3068d7e9182f42d41fab7c06a1e Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 18 Apr 2021 11:44:30 -0700 Subject: [PATCH] Update authorization prompt when updating an app We have a slightly different prompt for an app updating itself vs an app/tool updating another app. Also put prompt in the localization strings. --- InstallerLauncher/SUInstallerLauncher.m | 19 ++++++++++--- .../SUInstallerLauncherProtocol.h | 2 +- Sparkle/Base.lproj/Sparkle.strings | 6 +++++ Sparkle/SPUInstallerDriver.m | 27 +++++++++++++++++-- 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/InstallerLauncher/SUInstallerLauncher.m b/InstallerLauncher/SUInstallerLauncher.m index 782302e5..c4449780 100644 --- a/InstallerLauncher/SUInstallerLauncher.m +++ b/InstallerLauncher/SUInstallerLauncher.m @@ -91,7 +91,7 @@ return (submittedJob == true); } -- (SUInstallerLauncherStatus)submitInstallerAtPath:(NSString *)installerPath withHostBundle:(NSBundle *)hostBundle authorizationPrompt:(NSString *)authorizationPrompt inSystemDomain:(BOOL)systemDomain +- (SUInstallerLauncherStatus)submitInstallerAtPath:(NSString *)installerPath withHostBundle:(NSBundle *)hostBundle updaterIdentifier:(NSString *)updaterIdentifier authorizationPrompt:(NSString *)authorizationPrompt inSystemDomain:(BOOL)systemDomain { SUFileManager *fileManager = [[SUFileManager alloc] init]; @@ -121,8 +121,19 @@ // https://developer.apple.com/library/mac/technotes/tn2095/_index.html#//apple_ref/doc/uid/DTS10003110-CH1-SECTION7 // We can set a custom right name for authenticating as an administrator // Using this right rather than using something like kSMRightModifySystemDaemons allows us to present a better worded prompt + // Note the right name is cached, so if we want to change the authorization + // prompt, we may need to change the right name. I have found no good way around this :| + NSString *sparkleAuthTag = @"sparkle2-auth"; // this needs to change if auth wording changes + NSString *rightNameString; + if ([hostBundleIdentifier isEqualToString:updaterIdentifier]) { + // Application bundle is likely updating itself + rightNameString = [NSString stringWithFormat:@"%@.%@", hostBundleIdentifier, sparkleAuthTag]; + } else { + // Updater is likely updating a bundle that is not itself + rightNameString = [NSString stringWithFormat:@"%@.%@.%@", updaterIdentifier, hostBundleIdentifier, sparkleAuthTag]; + } - const char *rightName = [[NSString stringWithFormat:@"%@.sparkle-auth", hostBundleIdentifier] UTF8String]; + const char *rightName = rightNameString.UTF8String; assert(rightName != NULL); OSStatus getRightResult = AuthorizationRightGet(rightName, NULL); @@ -323,7 +334,7 @@ static BOOL SPUNeedsSystemAuthorizationAccess(NSString *path, NSString *installa // Note: do not pass untrusted information such as paths to the installer and progress agent tools, when we can find them ourselves here -- (void)launchInstallerWithHostBundlePath:(NSString *)hostBundlePath authorizationPrompt:(NSString *)authorizationPrompt installationType:(NSString *)installationType allowingDriverInteraction:(BOOL)allowingDriverInteraction allowingUpdaterInteraction:(BOOL)allowingUpdaterInteraction completion:(void (^)(SUInstallerLauncherStatus, BOOL))completionHandler +- (void)launchInstallerWithHostBundlePath:(NSString *)hostBundlePath updaterIdentifier:(NSString *)updaterIdentifier authorizationPrompt:(NSString *)authorizationPrompt installationType:(NSString *)installationType allowingDriverInteraction:(BOOL)allowingDriverInteraction allowingUpdaterInteraction:(BOOL)allowingUpdaterInteraction completion:(void (^)(SUInstallerLauncherStatus, BOOL))completionHandler { dispatch_async(dispatch_get_main_queue(), ^{ NSBundle *hostBundle = [NSBundle bundleWithPath:hostBundlePath]; @@ -397,7 +408,7 @@ static BOOL SPUNeedsSystemAuthorizationAccess(NSString *path, NSString *installa return; } - SUInstallerLauncherStatus installerStatus = [self submitInstallerAtPath:installerPath withHostBundle:hostBundle authorizationPrompt:authorizationPrompt inSystemDomain:needsSystemAuthorization]; + SUInstallerLauncherStatus installerStatus = [self submitInstallerAtPath:installerPath withHostBundle:hostBundle updaterIdentifier:updaterIdentifier authorizationPrompt:authorizationPrompt inSystemDomain:needsSystemAuthorization]; BOOL submittedProgressTool = NO; if (installerStatus == SUInstallerLauncherSuccess) { diff --git a/InstallerLauncher/SUInstallerLauncherProtocol.h b/InstallerLauncher/SUInstallerLauncherProtocol.h index c03e01a7..282f76b4 100644 --- a/InstallerLauncher/SUInstallerLauncherProtocol.h +++ b/InstallerLauncher/SUInstallerLauncherProtocol.h @@ -11,7 +11,7 @@ @protocol SUInstallerLauncherProtocol -- (void)launchInstallerWithHostBundlePath:(NSString *)hostBundlePath authorizationPrompt:(NSString *)authorizationPrompt installationType:(NSString *)installationType allowingDriverInteraction:(BOOL)allowingDriverInteraction allowingUpdaterInteraction:(BOOL)allowingUpdaterInteraction completion:(void (^)(SUInstallerLauncherStatus, BOOL))completionHandler; +- (void)launchInstallerWithHostBundlePath:(NSString *)hostBundlePath updaterIdentifier:(NSString *)updaterBundleIdentifier authorizationPrompt:(NSString *)authorizationPrompt installationType:(NSString *)installationType allowingDriverInteraction:(BOOL)allowingDriverInteraction allowingUpdaterInteraction:(BOOL)allowingUpdaterInteraction completion:(void (^)(SUInstallerLauncherStatus, BOOL))completionHandler; - (void)checkIfApplicationInstallationRequiresAuthorizationWithHostBundlePath:(NSString *)hostBundlePath reply:(void(^)(BOOL requiresAuthorization))reply; diff --git a/Sparkle/Base.lproj/Sparkle.strings b/Sparkle/Base.lproj/Sparkle.strings index 48acd4d4..061814ad 100644 --- a/Sparkle/Base.lproj/Sparkle.strings +++ b/Sparkle/Base.lproj/Sparkle.strings @@ -101,3 +101,9 @@ /* Status message shown when the user checks for updates but is already current or the feed doesn't contain any updates. */ "You're up-to-date!" = "You’re up to date!"; + +/* Authorization message shown when app wants permission to update itself. */ +"%1$@ wants permission to update." = "%1$@ wants permission to update."; + +/* Authorization message shown when app wants permission to update another app / bundle. */ +"%1$@ wants permission to update %2$@." = "%1$@ wants permission to update %2$@."; diff --git a/Sparkle/SPUInstallerDriver.m b/Sparkle/SPUInstallerDriver.m index 92797933..044f66e1 100644 --- a/Sparkle/SPUInstallerDriver.m +++ b/Sparkle/SPUInstallerDriver.m @@ -436,8 +436,31 @@ assert(installationType != nil); // The installer launcher could be in a XPC service, so we don't want to do localization in there - NSString *authorizationPrompt = [NSString stringWithFormat:SULocalizedString(@"%1$@ wants to update.", nil), self.host.name]; - [installerLauncher launchInstallerWithHostBundlePath:hostBundlePath authorizationPrompt:authorizationPrompt installationType:installationType allowingDriverInteraction:driverAllowsInteraction allowingUpdaterInteraction:!preventsInstallerInteraction completion:^(SUInstallerLauncherStatus result, BOOL systemDomain) { + // Make sure the authorization prompt reflects whether or not the updater is updating itself, or + // if the updater is updating another application. We use SUHost.name property, so an application + // or Sparkle helper application can override its name with SUBundleName key + + SUHost *mainBundleHost = [[SUHost alloc] initWithBundle:[NSBundle mainBundle]]; + NSString *mainBundleName = mainBundleHost.name; + NSString *hostName = self.host.name; + + // Changing this authorization prompt is a little complicated because the + // Auth database retains and caches the right we use, and there isn't a good way + // of updating the prompt. See code in SUInstallerLauncher.m + NSString *authorizationPrompt; + if ([mainBundleName isEqualToString:hostName]) { + authorizationPrompt = [NSString stringWithFormat:SULocalizedString(@"%1$@ wants permission to update.", nil), hostName]; + } else { + authorizationPrompt = [NSString stringWithFormat:SULocalizedString(@"%1$@ wants permission to update %2$@.", nil), mainBundleName, hostName]; + } + + NSString *mainBundleIdentifier; + { + NSString *bundleIdentifier = mainBundleHost.bundle.bundleIdentifier; + mainBundleIdentifier = (bundleIdentifier == nil) ? mainBundleName : bundleIdentifier; + } + + [installerLauncher launchInstallerWithHostBundlePath:hostBundlePath updaterIdentifier:mainBundleIdentifier authorizationPrompt:authorizationPrompt installationType:installationType allowingDriverInteraction:driverAllowsInteraction allowingUpdaterInteraction:!preventsInstallerInteraction completion:^(SUInstallerLauncherStatus result, BOOL systemDomain) { dispatch_async(dispatch_get_main_queue(), ^{ retrievedLaunchStatus = YES; [launcherConnection invalidate];