From 3b892524a6fd75091efe46772efa2c2cc9418299 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 17 Apr 2016 22:48:17 -0400 Subject: [PATCH] Rewrite ATS check by distinguishing if Sparkle is in XPC service or not If it's not in a XPC service, then the main bundle decides the rules. --- Configurations/set-ats-exceptions.sh | 2 +- Sparkle/SUUpdater.m | 55 +++++++++++++++++++++++++--- Sparkle/SUXPCServiceInfo.h | 2 + Sparkle/SUXPCServiceInfo.m | 11 ++++-- 4 files changed, 61 insertions(+), 9 deletions(-) diff --git a/Configurations/set-ats-exceptions.sh b/Configurations/set-ats-exceptions.sh index 9b5fa1b9..79391535 100755 --- a/Configurations/set-ats-exceptions.sh +++ b/Configurations/set-ats-exceptions.sh @@ -7,6 +7,6 @@ case "${SPARKLE_ALLOW_ARBITRARY_HTTP_LOADS}" in /usr/libexec/PlistBuddy -c "Set :NSAppTransportSecurity:NSAllowsArbitraryLoads YES" "${INFOPLIST}" ;; "NO"|"0") -/usr/libexec/PlistBuddy -c "Set :NSAppTransportSecurity:NSAllowsArbitraryLoads NO" "${INFOPLIST}" +/usr/libexec/PlistBuddy -c "Delete :NSAppTransportSecurity" "${INFOPLIST}" ;; esac diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index 502a5acf..555f66de 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -25,6 +25,7 @@ #import "SUAppcastItem.h" #import "SUInstallationInfo.h" #import "SUErrors.h" +#import "SUXPCServiceInfo.h" #ifdef _APPKITDEFINES_H #error This is a "core" class and should NOT import AppKit @@ -118,6 +119,27 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification return YES; } +- (BOOL)checkATSIssueForBundleURL:(NSURL *)bundleURL getBundleExists:(BOOL *)bundleExists +{ + if (bundleURL == nil) { + if (bundleExists != NULL) { + *bundleExists = NO; + } + return NO; + } + + NSBundle *bundle = [NSBundle bundleWithURL:bundleURL]; + if (bundleExists != NULL) { + *bundleExists = (bundle != nil); + } + + if (bundle == nil) { + return NO; + } + + return ([bundle objectForInfoDictionaryKey:@"NSAppTransportSecurity"] == nil); +} + - (BOOL)checkIfConfiguredProperly:(NSError * __autoreleasing *)error { if (self.sparkleBundle == nil) { @@ -155,12 +177,35 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification } BOOL servingOverHttps = [[[feedURL scheme] lowercaseString] isEqualToString:@"https"]; - BOOL allowsArbitraryHTTPLoads = SPARKLE_ALLOW_ARBITRARY_HTTP_LOADS; - if (!allowsArbitraryHTTPLoads && !servingOverHttps) { - if (error != NULL) { - *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInsecureFeedURLError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"You must change the feed URL (%@) to use HTTPS or use a build of Sparkle with App Transport Security disabled.\n\nFor more information:\nhttps://sparkle-project.org/documentation/app-transport-security/", [feedURL absoluteString]] }]; + if (!servingOverHttps) { + BOOL foundXPCAppcastDownloaderService = NO; + BOOL foundATSAppcastIssue = [self checkATSIssueForBundleURL:SUXPCServiceURL(@APPCAST_DOWNLOADER_PRODUCT_NAME) getBundleExists:&foundXPCAppcastDownloaderService]; + + BOOL foundXPCUpdateDownloaderService = NO; + BOOL foundATSUpdateIssue = NO; + if (!foundATSAppcastIssue) { + foundATSUpdateIssue = [self checkATSIssueForBundleURL:SUXPCServiceURL(@UPDATE_DOWNLOADER_PRODUCT_NAME) getBundleExists:&foundXPCUpdateDownloaderService]; + } + + NSBundle *mainBundle = [NSBundle mainBundle]; + BOOL foundATSMainBundleIssue = NO; + if (!foundATSAppcastIssue && !foundATSUpdateIssue && (!foundXPCAppcastDownloaderService || !foundXPCUpdateDownloaderService)) { + BOOL foundATSIssue = ([mainBundle objectForInfoDictionaryKey:@"NSAppTransportSecurity"] == nil); + BOOL updatingMainBundle = [self.host.bundle isEqualTo:mainBundle]; + + if (updatingMainBundle) { + // The only way we'll know for sure if there is an issue is if the main bundle is the same as the one we're updating + // We don't want to generate false positives.. + foundATSMainBundleIssue = foundATSIssue; + } + } + + if (foundATSAppcastIssue || foundATSUpdateIssue || foundATSMainBundleIssue) { + if (error != NULL) { + *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInsecureFeedURLError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"You must change the feed URL (%@) to use HTTPS or use a build of Sparkle with App Transport Security disabled.\n\nFor more information:\nhttps://sparkle-project.org/documentation/app-transport-security/", [feedURL absoluteString]] }]; + } + return NO; } - return NO; } return YES; diff --git a/Sparkle/SUXPCServiceInfo.h b/Sparkle/SUXPCServiceInfo.h index 13a0b9ee..f3c4451f 100644 --- a/Sparkle/SUXPCServiceInfo.h +++ b/Sparkle/SUXPCServiceInfo.h @@ -12,4 +12,6 @@ NS_ASSUME_NONNULL_BEGIN BOOL SUXPCServiceExists(NSString *bundleName); +NSURL * _Nullable SUXPCServiceURL(NSString *bundleName); + NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUXPCServiceInfo.m b/Sparkle/SUXPCServiceInfo.m index 657d6e2b..f1b4dab0 100644 --- a/Sparkle/SUXPCServiceInfo.m +++ b/Sparkle/SUXPCServiceInfo.m @@ -9,15 +9,20 @@ #import "SUXPCServiceInfo.h" BOOL SUXPCServiceExists(NSString *bundleName) +{ + NSURL *xpcBundleURL = SUXPCServiceURL(bundleName); + return (xpcBundleURL != nil && [xpcBundleURL checkResourceIsReachableAndReturnError:NULL]); +} + +NSURL * _Nullable SUXPCServiceURL(NSString *bundleName) { NSBundle *mainBundle = [NSBundle mainBundle]; NSURL *executableURL = mainBundle.executableURL; if (executableURL == nil) { - return NO; + return nil; } NSURL *xpcBundleURL = [[[executableURL.URLByDeletingLastPathComponent.URLByDeletingLastPathComponent URLByAppendingPathComponent:@"XPCServices"] URLByAppendingPathComponent:bundleName] URLByAppendingPathExtension:@"xpc"]; - BOOL serviceExists = [xpcBundleURL checkResourceIsReachableAndReturnError:NULL]; - return serviceExists; + return xpcBundleURL; }