From 414c8affc5d9acca34ee1c2d03b477f66a9d07fc Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 27 Mar 2021 20:35:17 -0700 Subject: [PATCH] Compute app path normalization in progress agent Compute path instead of having installer pass over arbitrary path to agent. --- Autoupdate/AppInstaller.m | 11 +----- Autoupdate/SUInstaller.m | 22 ++---------- Configurations/ConfigCommon.xcconfig | 4 ++- Sparkle.xcodeproj/project.pbxproj | 8 +++++ .../InstallerProgressAppController.m | 29 ++++++++++----- .../SPUInstallerAgentProtocol.h | 2 +- Sparkle/SUNormalization.h | 16 +++++++++ Sparkle/SUNormalization.m | 35 +++++++++++++++++++ 8 files changed, 88 insertions(+), 39 deletions(-) create mode 100644 Sparkle/SUNormalization.h create mode 100644 Sparkle/SUNormalization.m diff --git a/Autoupdate/AppInstaller.m b/Autoupdate/AppInstaller.m index 2513b031..b3172018 100644 --- a/Autoupdate/AppInstaller.m +++ b/Autoupdate/AppInstaller.m @@ -573,17 +573,8 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; [self.communicator handleMessageWithIdentifier:SPUInstallationFinishedStage3 data:[NSData data]]; if (self.shouldRelaunch) { - NSString *pathToRelaunch = nil; - // If the relaunch path is the same as the host bundle path, use the installation path from the installer which may be normalized - // Otherwise use the requested relaunch path in all other cases - if ([self.relaunchPath.pathComponents isEqualToArray:self.host.bundlePath.pathComponents]) { - pathToRelaunch = installationPath; - } else { - pathToRelaunch = self.relaunchPath; - } - // This will also signal to the agent that it will terminate soon - [self.agentConnection.agent relaunchPath:pathToRelaunch]; + [self.agentConnection.agent relaunchApplication]; } [self.installer performCleanup]; diff --git a/Autoupdate/SUInstaller.m b/Autoupdate/SUInstaller.m index 725c6435..553c12b0 100644 --- a/Autoupdate/SUInstaller.m +++ b/Autoupdate/SUInstaller.m @@ -15,6 +15,7 @@ #import "SULog.h" #import "SUErrors.h" #import "SPUInstallationType.h" +#import "SUNormalization.h" #include "AppKitPrevention.h" @@ -152,11 +153,12 @@ } else { NSString *normalizedInstallationPath = nil; if (SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) { - normalizedInstallationPath = [self normalizedInstallationPathForHost:host]; + normalizedInstallationPath = SUNormalizedInstallationPath(host); } // If we have a normalized path, 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. // Otherwise if there's no normalized path (the more likely case), we'll just use the host bundle's path + // Check progress agent app which computes normalized path too according to these rules NSString *installationPath; if (normalizedInstallationPath != nil && ![[NSFileManager defaultManager] fileExistsAtPath:normalizedInstallationPath]) { installationPath = normalizedInstallationPath; @@ -171,22 +173,4 @@ return installer; } -+ (NSString *)normalizedInstallationPathForHost:(SUHost *)host -{ - NSBundle *bundle = host.bundle; - assert(bundle != nil); - - NSString * baseBundleName = [host objectForInfoDictionaryKey:@"SUBundleName"]; - - if (baseBundleName == nil) { - baseBundleName = [host objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleNameKey]; - } - - NSString *normalizedAppPath = [[[bundle bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", baseBundleName, [[bundle bundlePath] pathExtension]]]; - - // Roundtrip string through fileSystemRepresentation to ensure it uses filesystem's Unicode normalization - // rather than arbitrary Unicode form from Info.plist - #1017 - return [NSString stringWithUTF8String:[normalizedAppPath fileSystemRepresentation]]; -} - @end diff --git a/Configurations/ConfigCommon.xcconfig b/Configurations/ConfigCommon.xcconfig index b2157b01..0ae30b1b 100644 --- a/Configurations/ConfigCommon.xcconfig +++ b/Configurations/ConfigCommon.xcconfig @@ -10,7 +10,9 @@ SPARKLE_AUTOMATED_DOWNGRADES = 0 // If your app file on disk is named "MyApp 1.1b4", Sparkle usually updates it // in place, giving you an app named 1.1b4 that is actually 1.2. Turn the -// following on to always reset the name back to "MyApp": +// following on to always reset the name back to "MyApp" +// If you are using this option to change the name of your app, you should +// disable the option again when you no longer need it SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME = 0 SPARKLE_ICON_NAME = AppIcon diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 49d097e6..7860b693 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -168,6 +168,8 @@ 7229E1BA1C97CC4D00CB50D0 /* SPUScheduledUpdateDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = 7229E1B81C97CC4D00CB50D0 /* SPUScheduledUpdateDriver.m */; }; 7229E1BD1C98EFF200CB50D0 /* SPUDownloadDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 7229E1BB1C98EFF200CB50D0 /* SPUDownloadDriver.h */; }; 7229E1BE1C98EFF200CB50D0 /* SPUDownloadDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = 7229E1BC1C98EFF200CB50D0 /* SPUDownloadDriver.m */; }; + 722FB7E5260EE53F00EB571C /* SUNormalization.m in Sources */ = {isa = PBXBuildFile; fileRef = 722FB7E4260EE53F00EB571C /* SUNormalization.m */; }; + 722FB7E6260EE53F00EB571C /* SUNormalization.m in Sources */ = {isa = PBXBuildFile; fileRef = 722FB7E4260EE53F00EB571C /* SUNormalization.m */; }; 72316BD31E0DA8430039EFD9 /* SUUnarchiverNotifier.m in Sources */ = {isa = PBXBuildFile; fileRef = 72316BD21E0DA8430039EFD9 /* SUUnarchiverNotifier.m */; }; 72316BD41E0DB37E0039EFD9 /* SUUnarchiverNotifier.m in Sources */ = {isa = PBXBuildFile; fileRef = 72316BD21E0DA8430039EFD9 /* SUUnarchiverNotifier.m */; }; 723ABD38259A9BA500BDB4FA /* SUUpdateAlert.xib in Resources */ = {isa = PBXBuildFile; fileRef = 723ABD36259A9BA500BDB4FA /* SUUpdateAlert.xib */; }; @@ -1059,6 +1061,8 @@ 7229E1BB1C98EFF200CB50D0 /* SPUDownloadDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPUDownloadDriver.h; sourceTree = ""; }; 7229E1BC1C98EFF200CB50D0 /* SPUDownloadDriver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPUDownloadDriver.m; sourceTree = ""; }; 722FB7CA1DD69897001D40CE /* ConfigSwift.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = ConfigSwift.xcconfig; sourceTree = ""; }; + 722FB7E3260EE53F00EB571C /* SUNormalization.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SUNormalization.h; sourceTree = ""; }; + 722FB7E4260EE53F00EB571C /* SUNormalization.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SUNormalization.m; sourceTree = ""; }; 72316BD11E0DA8430039EFD9 /* SUUnarchiverNotifier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SUUnarchiverNotifier.h; path = Autoupdate/SUUnarchiverNotifier.h; sourceTree = SOURCE_ROOT; }; 72316BD21E0DA8430039EFD9 /* SUUnarchiverNotifier.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SUUnarchiverNotifier.m; path = Autoupdate/SUUnarchiverNotifier.m; sourceTree = SOURCE_ROOT; }; 723ABCD7259A9A9D00BDB4FA /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/Sparkle.strings; sourceTree = ""; }; @@ -1924,6 +1928,8 @@ 7267E5BB1D3D8B2700D1BF90 /* SUGuidedPackageInstaller.m */, 7267E5BC1D3D8B2700D1BF90 /* SUInstaller.h */, 7267E5BD1D3D8B2700D1BF90 /* SUInstaller.m */, + 722FB7E3260EE53F00EB571C /* SUNormalization.h */, + 722FB7E4260EE53F00EB571C /* SUNormalization.m */, 7267E5B91D3D8B0B00D1BF90 /* SUInstallerProtocol.h */, 7267E5BE1D3D8B2700D1BF90 /* SUPackageInstaller.h */, 7267E5BF1D3D8B2700D1BF90 /* SUPackageInstaller.m */, @@ -3400,6 +3406,7 @@ 7267E5FA1D3DAC3600D1BF90 /* StatusInfo.m in Sources */, 720595EF1D700568000572E8 /* SUApplicationInfo.m in Sources */, 7214B8831D456C2400CB5CED /* SUBundleIcon.m in Sources */, + 722FB7E6260EE53F00EB571C /* SUNormalization.m in Sources */, 721C245E1CB757DE005440CB /* SUConstants.m in Sources */, 7267E5EB1D3D90C200D1BF90 /* SUFileManager.m in Sources */, 721C24611CB75C5D005440CB /* SUHost.m in Sources */, @@ -3578,6 +3585,7 @@ 72316BD31E0DA8430039EFD9 /* SUUnarchiverNotifier.m in Sources */, 729924941DF4A45000DBCDF5 /* SUUpdateValidator.m in Sources */, 7267E5AB1D3D8AA800D1BF90 /* TerminationListener.m in Sources */, + 722FB7E5260EE53F00EB571C /* SUNormalization.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Sparkle/InstallerProgress/InstallerProgressAppController.m b/Sparkle/InstallerProgress/InstallerProgressAppController.m index e43affa4..425a63c5 100644 --- a/Sparkle/InstallerProgress/InstallerProgressAppController.m +++ b/Sparkle/InstallerProgress/InstallerProgressAppController.m @@ -17,6 +17,7 @@ #import "StatusInfo.h" #import "SUHost.h" #import "SUErrors.h" +#import "SUNormalization.h" /*! * Terminate the application after a delay from launching the new update to avoid OS activation issues @@ -38,6 +39,7 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; @property (nonatomic) BOOL willTerminate; @property (nonatomic) BOOL applicationInitiallyAlive; @property (nonatomic) NSBundle *applicationBundle; +@property (nonatomic) NSString *normalizedPath; @end @@ -56,6 +58,7 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; @synthesize willTerminate = _willTerminate; @synthesize applicationInitiallyAlive = _applicationInitiallyAlive; @synthesize applicationBundle = _applicationBundle; +@synthesize normalizedPath = _normalizedPath; - (instancetype)initWithApplication:(NSApplication *)application arguments:(NSArray *)arguments delegate:(id)delegate { @@ -215,6 +218,20 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; [self cleanupAndExitWithStatus:EXIT_FAILURE error:[NSError errorWithDomain:SUSparkleErrorDomain code:SUAgentInvalidationError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Error: Encountered invalid path for waiting termination: %@", applicationBundlePath] }]]; } + // Compute normalized path that we may use later for relaunching the application + // We compute normalized path from progress agent instead of trusting or having the installer + // pass it to us + if (SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) { + SUHost *applicationHost = [[SUHost alloc] initWithBundle:applicationBundle]; + + NSString *normalizedPath = SUNormalizedInstallationPath(applicationHost); + // We only use normalized path if it doesn't already exist + // Check the installer which has the same logic + if (![[NSFileManager defaultManager] fileExistsAtPath:normalizedPath]) { + self.normalizedPath = SUNormalizedInstallationPath(applicationHost); + } + } + NSArray *runningApplications = [self runningApplicationsWithBundle:applicationBundle]; // We're just picking the first running application to send.. @@ -256,27 +273,23 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; }); } -- (void)relaunchPath:(NSString *)requestedPathToRelaunch +- (void)relaunchApplication { dispatch_async(dispatch_get_main_queue(), ^{ if (!self.willTerminate && self.applicationBundle != nil && self.applicationInitiallyAlive) { - // If the normalized setting is not enabled, we shouldn't trust the relaunch path input that is being passed to us - // because we already have the application path to relaunch, and we verified that it was running before NSString *pathToRelaunch; - if (SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) { - pathToRelaunch = requestedPathToRelaunch; + if (self.normalizedPath != nil) { + pathToRelaunch = self.normalizedPath; } else { pathToRelaunch = self.applicationBundle.bundlePath; } - // We should at least make sure we're opening a bundle however + // We should at least make sure we're opening a bundle NSBundle *relaunchBundle = [NSBundle bundleWithPath:pathToRelaunch]; if (relaunchBundle == nil) { [self cleanupAndExitWithStatus:EXIT_FAILURE error:[NSError errorWithDomain:SUSparkleErrorDomain code:SUAgentInvalidationError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Error: Encountered invalid path to relaunch: %@", pathToRelaunch] }]]; } - // We only launch applications, but I'm not sure how reliable -launchApplicationAtURL:options:config: is so we're not using it - // Eg: http://www.openradar.me/10952677 if (![[NSWorkspace sharedWorkspace] openFile:pathToRelaunch]) { SULog(SULogLevelError, @"Error: Failed to relaunch bundle at %@", pathToRelaunch); } diff --git a/Sparkle/InstallerProgress/SPUInstallerAgentProtocol.h b/Sparkle/InstallerProgress/SPUInstallerAgentProtocol.h index 255166c7..e5b52ee1 100644 --- a/Sparkle/InstallerProgress/SPUInstallerAgentProtocol.h +++ b/Sparkle/InstallerProgress/SPUInstallerAgentProtocol.h @@ -22,7 +22,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)stopProgress; -- (void)relaunchPath:(NSString *)pathToRelaunch; +- (void)relaunchApplication; @end diff --git a/Sparkle/SUNormalization.h b/Sparkle/SUNormalization.h new file mode 100644 index 00000000..6f67c3ed --- /dev/null +++ b/Sparkle/SUNormalization.h @@ -0,0 +1,16 @@ +// +// SUNormalization.h +// Sparkle +// +// Created by Mayur Pawashe on 3/26/21. +// Copyright © 2021 Sparkle Project. All rights reserved. +// + +#import +#import "SUHost.h" + +NS_ASSUME_NONNULL_BEGIN + +NSString *SUNormalizedInstallationPath(SUHost *host); + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUNormalization.m b/Sparkle/SUNormalization.m new file mode 100644 index 00000000..5193bc56 --- /dev/null +++ b/Sparkle/SUNormalization.m @@ -0,0 +1,35 @@ +// +// SUNormalization.m +// Sparkle +// +// Created by Mayur Pawashe on 3/26/21. +// Copyright © 2021 Sparkle Project. All rights reserved. +// + +#import "SUNormalization.h" + + +#include "AppKitPrevention.h" + +NSString *SUNormalizedInstallationPath(SUHost *host) +{ + NSBundle *bundle = host.bundle; + assert(bundle != nil); + + NSString * baseBundleName = [host objectForInfoDictionaryKey:@"SUBundleName"]; + + if (baseBundleName == nil) { + baseBundleName = [host objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleNameKey]; + } + + NSString *normalizedAppPath = [[[bundle bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", baseBundleName, [[bundle bundlePath] pathExtension]]]; + + // Roundtrip string through fileSystemRepresentation to ensure it uses filesystem's Unicode normalization + // rather than arbitrary Unicode form from Info.plist - #1017 + NSString *unicodeNormalizedPath = [NSString stringWithUTF8String:[normalizedAppPath fileSystemRepresentation]]; + if (unicodeNormalizedPath != nil) { + return unicodeNormalizedPath; + } else { + return normalizedAppPath; + } +}