diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index d0ca721f..9aa3b8c1 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -141,6 +141,7 @@ 720E217B1D0D00BF003A311C /* SPUUpdaterCycle.h in Headers */ = {isa = PBXBuildFile; fileRef = 720E21791D0D00BF003A311C /* SPUUpdaterCycle.h */; }; 720E217C1D0D00BF003A311C /* SPUUpdaterCycle.m in Sources */ = {isa = PBXBuildFile; fileRef = 720E217A1D0D00BF003A311C /* SPUUpdaterCycle.m */; }; 7210C7681B9A9A1500EB90AC /* SUUnarchiverTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7210C7671B9A9A1500EB90AC /* SUUnarchiverTest.swift */; }; + 7212F16330A0311500BC9C7D /* testappcast_bundleIdentifiers.xml in Resources */ = {isa = PBXBuildFile; fileRef = 7210219630A0311500CA3DBD /* testappcast_bundleIdentifiers.xml */; }; 72162B081C82C9600013C1C5 /* SULocalizations.h in Headers */ = {isa = PBXBuildFile; fileRef = 72162B071C82C9600013C1C5 /* SULocalizations.h */; }; 721BC20E1D1CDE55002BC71E /* SPULocalCacheDirectory.h in Headers */ = {isa = PBXBuildFile; fileRef = 721BC20C1D1CDE55002BC71E /* SPULocalCacheDirectory.h */; }; 721BC20F1D1CDE55002BC71E /* SPULocalCacheDirectory.m in Sources */ = {isa = PBXBuildFile; fileRef = 721BC20D1D1CDE55002BC71E /* SPULocalCacheDirectory.m */; }; @@ -1039,6 +1040,7 @@ 720DC50527A62CDC00DFF3EC /* testappcast_minimumAutoupdateVersionSkipping2.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = testappcast_minimumAutoupdateVersionSkipping2.xml; sourceTree = ""; }; 720E21791D0D00BF003A311C /* SPUUpdaterCycle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPUUpdaterCycle.h; sourceTree = ""; }; 720E217A1D0D00BF003A311C /* SPUUpdaterCycle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPUUpdaterCycle.m; sourceTree = ""; }; + 7210219630A0311500CA3DBD /* testappcast_bundleIdentifiers.xml */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = testappcast_bundleIdentifiers.xml; sourceTree = ""; }; 7210C7671B9A9A1500EB90AC /* SUUnarchiverTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SUUnarchiverTest.swift; sourceTree = ""; }; 7214B8851D45AD9A00CB5CED /* SPUInstallationType.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SPUInstallationType.h; sourceTree = ""; }; 72162B071C82C9600013C1C5 /* SULocalizations.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SULocalizations.h; sourceTree = ""; }; @@ -1825,6 +1827,7 @@ 725EA4B13027D2AF0041BFA6 /* test-package-update.xml */, 72C56E212F04C28B0005A484 /* testreleasenotes.html */, 5A5DD40324958AFF0045EB3E /* SUUpdateValidatorTest */, + 7210219630A0311500CA3DBD /* testappcast_bundleIdentifiers.xml */, ); path = Resources; sourceTree = ""; @@ -3117,6 +3120,7 @@ 72CCDEBE27421FD500B53718 /* SparkleTestCodeSignApp_bad_header.zip in Resources */, 726FC0362C1E787A00177986 /* SparkleTestCodeSignApp.aar in Resources */, 722545B626805FF80036465C /* testappcast_info_updates.xml in Resources */, + 7212F16330A0311500BC9C7D /* testappcast_bundleIdentifiers.xml in Resources */, 723414172EBFB482009727E0 /* testappcast_arm64HardwareRequirement.xml in Resources */, 7202DC9A269ABD3500737EC4 /* testappcast_phasedRollout.xml in Resources */, 72C6F2DB303BE56100D39EF2 /* SparkleTestCodeSignApp.license.no-text.dmg in Resources */, diff --git a/Sparkle/SPUUpdater.m b/Sparkle/SPUUpdater.m index 480ce027..3fb88f13 100644 --- a/Sparkle/SPUUpdater.m +++ b/Sparkle/SPUUpdater.m @@ -73,6 +73,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification BOOL _loggedATSWarning; BOOL _loggedNoSecureKeyWarning; BOOL _loggedUpdateSecurityPolicyWarning; + BOOL _loggedFeedBundleIdentifierWarning; BOOL _updatingMainBundle; } @@ -379,14 +380,26 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification return NO; } + } else if (_host.requiresFeedBundleIdentifier) { + if (error != NULL) { + *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInvalidUpdaterError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"For security reasons, %@ needs to also be enabled if %@ is enabled for %@. Visit Sparkle's documentation for more information: https://sparkle-project.org/documentation/customization/", SURequireSignedFeedKey, SURequireFeedBundleIdentifierKey, hostName] }]; + } + + return NO; } - + if (_updatingMainBundle) { if (!_loggedUpdateSecurityPolicyWarning && mainBundleHost.hasUpdateSecurityPolicy) { SULog(SULogLevelDefault, @"Warning: %@ has a custom NSUpdateSecurityPolicy in its Info.plist. This may cause issues when installing updates. Please consider removing this key for your builds using Sparkle if you do not really require a custom update security policy.", hostName); _loggedUpdateSecurityPolicyWarning = YES; } + + if (!_loggedFeedBundleIdentifierWarning && requiresSignedFeed && ![_host boolForInfoDictionaryKey:SURequireFeedBundleIdentifierKey]) { + SULog(SULogLevelDefault, @"Warning: %@ is enabled but %@ is not enabled for %@. For increased security, please consider enabling %@ and adding a %@ element to your signed feed. Visit Sparkle's documentation for more information: https://sparkle-project.org/documentation/#3-segue-for-security-concerns.", SURequireSignedFeedKey, SURequireFeedBundleIdentifierKey, hostName, SURequireFeedBundleIdentifierKey, SUAppcastElementBundleIdentifier); + + _loggedFeedBundleIdentifierWarning = YES; + } } return YES; diff --git a/Sparkle/SUAppcast+Private.h b/Sparkle/SUAppcast+Private.h index 65363aee..717bbfcc 100644 --- a/Sparkle/SUAppcast+Private.h +++ b/Sparkle/SUAppcast+Private.h @@ -30,6 +30,8 @@ NS_ASSUME_NONNULL_BEGIN - (SUAppcast *)copyByFilteringItems:(BOOL (^)(SUAppcastItem *))filterBlock; +@property (readonly, nonatomic, copy) NSSet *bundleIdentifiers; + @end NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUAppcast.m b/Sparkle/SUAppcast.m index c9a0c760..37449534 100644 --- a/Sparkle/SUAppcast.m +++ b/Sparkle/SUAppcast.m @@ -21,10 +21,17 @@ #include "AppKitPrevention.h" +@interface SUAppcast () + +@property (readonly, nonatomic, copy) NSSet *bundleIdentifiers; + +@end + @implementation SUAppcast @synthesize items = _items; @synthesize signingValidationStatus = _signingValidationStatus; +@synthesize bundleIdentifiers = _bundleIdentifiers; - (nullable instancetype)initWithXMLData:(NSData *)xmlData relativeToURL:(NSURL * _Nullable)relativeURL stateResolver:(SPUAppcastItemStateResolver *)stateResolver signingValidationStatus:(SPUAppcastSigningValidationStatus)signingValidationStatus error:(NSError * __autoreleasing *)error { @@ -86,6 +93,22 @@ return nil; } + NSArray *channelNodes = [document nodesForXPath:@"/rss/channel/*" error:errorp]; + if (nil == channelNodes) { + return nil; + } + + NSMutableSet *bundleIdentifiers = [NSMutableSet set]; + for (NSXMLNode *channelNode in channelNodes) { + if ([[self sparkleNamespacedNameOfNode:channelNode] isEqualToString:SUAppcastElementBundleIdentifier]) { + NSString *bundleIdentifier = [channelNode.stringValue stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet]; + if (bundleIdentifier.length > 0) { + [bundleIdentifiers addObject:bundleIdentifier]; + } + } + } + _bundleIdentifiers = [bundleIdentifiers copy]; + NSArray *xmlItems = [document nodesForXPath:@"/rss/channel/item" error:errorp]; if (nil == xmlItems) { return nil; @@ -288,6 +311,7 @@ other->_items = newItems; other->_signingValidationStatus = _signingValidationStatus; + other->_bundleIdentifiers = _bundleIdentifiers; return other; } diff --git a/Sparkle/SUAppcastDriver.m b/Sparkle/SUAppcastDriver.m index b207a1e3..4b4bdd22 100644 --- a/Sparkle/SUAppcastDriver.m +++ b/Sparkle/SUAppcastDriver.m @@ -232,7 +232,18 @@ return; } - + + if (appcastSigningValidationStatus == SPUAppcastSigningValidationStatusSucceeded && _host.requiresFeedBundleIdentifier) { + NSString *hostBundleIdentifier = _host.bundle.bundleIdentifier; + if (hostBundleIdentifier == nil || ![loadedAppcast.bundleIdentifiers containsObject:hostBundleIdentifier]) { + NSError *bundleIdentifierError = [NSError errorWithDomain:SUSparkleErrorDomain code:SUAppcastParseError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"The update feed is rejected because %@ is enabled and no %@ element in the feed matches the bundle identifier %@.", SURequireFeedBundleIdentifierKey, SUAppcastElementBundleIdentifier, hostBundleIdentifier] }]; + + [delegate didFailToFetchAppcastWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUAppcastParseError userInfo:@{ NSLocalizedDescriptionKey: SULocalizedStringFromTableInBundle(@"An error occurred while parsing the update feed.", SPARKLE_TABLE, SUSparkleBundle(), nil), NSUnderlyingErrorKey: bundleIdentifierError }]]; + + return; + } + } + // Now process the loaded appcast and find a suitable update item [delegate didFinishLoadingAppcast:loadedAppcast]; diff --git a/Sparkle/SUConstants.h b/Sparkle/SUConstants.h index b911c1a5..4a430be0 100644 --- a/Sparkle/SUConstants.h +++ b/Sparkle/SUConstants.h @@ -47,6 +47,7 @@ extern NSString *const SUPublicDSAKeyKey; extern NSString *const SUPublicDSAKeyFileKey; extern NSString *const SUPublicEDKeyKey; extern NSString *const SURequireSignedFeedKey; +extern NSString *const SURequireFeedBundleIdentifierKey; extern NSString *const SUVerifyUpdateBeforeExtractionKey; extern NSString *const SUDisableFeedCacheValidationKey; extern NSString *const SUAutomaticallyUpdateKey; @@ -83,6 +84,7 @@ extern NSString *const SUAppcastAttributeLength; extern NSString *const SUAppcastElementVersion; extern NSString *const SUAppcastElementShortVersionString; +extern NSString *const SUAppcastElementBundleIdentifier; extern NSString *const SUAppcastElementCriticalUpdate; extern NSString *const SUAppcastElementDeltas; extern NSString *const SUAppcastElementMinimumAutoupdateVersion; diff --git a/Sparkle/SUConstants.m b/Sparkle/SUConstants.m index 6ed4c74b..052f626b 100644 --- a/Sparkle/SUConstants.m +++ b/Sparkle/SUConstants.m @@ -34,6 +34,7 @@ NSString *const SUPublicDSAKeyKey = @"SUPublicDSAKey"; NSString *const SUPublicDSAKeyFileKey = @"SUPublicDSAKeyFile"; NSString *const SUPublicEDKeyKey = @"SUPublicEDKey"; NSString *const SURequireSignedFeedKey = @"SURequireSignedFeed"; +NSString *const SURequireFeedBundleIdentifierKey = @"SURequireFeedBundleIdentifier"; NSString *const SUVerifyUpdateBeforeExtractionKey = @"SUVerifyUpdateBeforeExtraction"; NSString *const SUDisableFeedCacheValidationKey = @"SUDisableFeedCacheValidation"; NSString *const SUAutomaticallyUpdateKey = @"SUAutomaticallyUpdate"; @@ -72,6 +73,7 @@ NSString *const SUAppcastAttributeLength = @"sparkle:length"; NSString *const SUAppcastElementVersion = SUAppcastAttributeVersion; NSString *const SUAppcastElementShortVersionString = SUAppcastAttributeShortVersionString; +NSString *const SUAppcastElementBundleIdentifier = @"sparkle:bundleIdentifier"; NSString *const SUAppcastElementCriticalUpdate = @"sparkle:criticalUpdate"; NSString *const SUAppcastElementDeltas = @"sparkle:deltas"; NSString *const SUAppcastElementMinimumAutoupdateVersion = @"sparkle:minimumAutoupdateVersion"; diff --git a/Sparkle/SUHost.h b/Sparkle/SUHost.h index 13477928..43e5b725 100644 --- a/Sparkle/SUHost.h +++ b/Sparkle/SUHost.h @@ -44,6 +44,7 @@ SUHostDefinitionAttribute @property (nonatomic, readonly) BOOL hasUpdateSecurityPolicy; @property (nonatomic, readonly) BOOL requiresSignedAppcast; +@property (nonatomic, readonly) BOOL requiresFeedBundleIdentifier; - (nullable id)objectForInfoDictionaryKey:(NSString *)key ofClass:(Class)aClass; - (nullable NSNumber *)boolNumberForInfoDictionaryKey:(NSString *)key; diff --git a/Sparkle/SUHost.m b/Sparkle/SUHost.m index 5147ef0f..162489fa 100644 --- a/Sparkle/SUHost.m +++ b/Sparkle/SUHost.m @@ -268,6 +268,11 @@ static void *SUHostObservableContext = &SUHostObservableContext; return [self boolForInfoDictionaryKey:SURequireSignedFeedKey]; } +- (BOOL)requiresFeedBundleIdentifier +{ + return [self boolForInfoDictionaryKey:SURequireFeedBundleIdentifierKey]; +} + - (SUPublicKeys *)publicKeys { return [[SUPublicKeys alloc] initWithEd:[self publicEDKey] diff --git a/TestApplication/TestApplication-Info.plist b/TestApplication/TestApplication-Info.plist index 2bc0b476..17f6ad8d 100644 --- a/TestApplication/TestApplication-Info.plist +++ b/TestApplication/TestApplication-Info.plist @@ -36,6 +36,8 @@ SURequireSignedFeed + SURequireFeedBundleIdentifier + SUVerifyUpdateBeforeExtraction _SUEnableDebugUpdateCheckIntervals diff --git a/TestApplication/sparkletestcast.xml b/TestApplication/sparkletestcast.xml index 38790fa6..b0cad735 100644 --- a/TestApplication/sparkletestcast.xml +++ b/TestApplication/sparkletestcast.xml @@ -7,6 +7,7 @@ IMPORTANT: This file must be re-signed if modifications are made to this file! T Sparkle Test App Changelog Most recent changes with links to updates. en + org.sparkle-project.SparkleTestApp Version 3.1 3.1 diff --git a/Tests/Resources/testappcast_bundleIdentifiers.xml b/Tests/Resources/testappcast_bundleIdentifiers.xml new file mode 100644 index 00000000..1639f305 --- /dev/null +++ b/Tests/Resources/testappcast_bundleIdentifiers.xml @@ -0,0 +1,15 @@ + + + + Test + com.example.first + + com.example.second + + + + Version 2.0 + + + + diff --git a/Tests/SUAppcastTest.swift b/Tests/SUAppcastTest.swift index 1e428a41..866f72b3 100644 --- a/Tests/SUAppcastTest.swift +++ b/Tests/SUAppcastTest.swift @@ -1208,4 +1208,25 @@ class SUAppcastTest: XCTestCase { XCTFail("Appcast creation should have passed, dropping only the rejected package item") } } + + func testParseAppcastBundleIdentifiers() throws { + let versionComparator = SUStandardVersionComparator.default + let stateResolver = SPUAppcastItemStateResolver(hostVersion: "1.0", applicationVersionComparator: versionComparator, standardVersionComparator: versionComparator) + + let testURL = Bundle(for: SUAppcastTest.self).url(forResource: "testappcast_bundleIdentifiers", withExtension: "xml")! + let testData = try Data(contentsOf: testURL) + + let appcast = try SUAppcast(xmlData: testData, relativeTo: nil, stateResolver: stateResolver, signingValidationStatus: .succeeded) + XCTAssertEqual(appcast.bundleIdentifiers, ["com.example.first", "com.example.second"]) + XCTAssertEqual(appcast.items.count, 1) + + let filteredAppcast = appcast.copy(byFilteringItems: { _ in false }) + XCTAssertEqual(filteredAppcast.bundleIdentifiers, ["com.example.first", "com.example.second"]) + + let testURLWithoutBundleIdentifiers = Bundle(for: SUAppcastTest.self).url(forResource: "testappcast", withExtension: "xml")! + let testDataWithoutBundleIdentifiers = try Data(contentsOf: testURLWithoutBundleIdentifiers) + + let appcastWithoutBundleIdentifiers = try SUAppcast(xmlData: testDataWithoutBundleIdentifiers, relativeTo: nil, stateResolver: stateResolver, signingValidationStatus: .succeeded) + XCTAssertEqual(appcastWithoutBundleIdentifiers.bundleIdentifiers, []) + } } diff --git a/generate_appcast/ArchiveItem.swift b/generate_appcast/ArchiveItem.swift index 833a2028..738d1b9d 100644 --- a/generate_appcast/ArchiveItem.swift +++ b/generate_appcast/ArchiveItem.swift @@ -78,6 +78,7 @@ class ArchiveItem: CustomStringConvertible { let publicEdKey: Data? let supportsDSA: Bool let requiresSignedAppcast: Bool + let bundleIdentifier: String? let archiveFileAttributes: [FileAttributeKey: Any] var deltas: [DeltaUpdate] @@ -174,11 +175,12 @@ class ArchiveItem: CustomStringConvertible { } } - init(version: String, shortVersion: String?, feedURL: URL?, requiresSignedAppcast: Bool, minimumSystemVersion: String?, hardwareRequirements: String?, frameworkVersion: String?, sparkleExecutableFileSize: Int?, sparkleLocales: String?, publicEdKey: String?, supportsDSA: Bool, appPath: URL, archivePath: URL) throws { + init(version: String, shortVersion: String?, feedURL: URL?, requiresSignedAppcast: Bool, bundleIdentifier: String?, minimumSystemVersion: String?, hardwareRequirements: String?, frameworkVersion: String?, sparkleExecutableFileSize: Int?, sparkleLocales: String?, publicEdKey: String?, supportsDSA: Bool, appPath: URL, archivePath: URL) throws { self.version = version self._shortVersion = shortVersion self.feedURL = feedURL self.requiresSignedAppcast = requiresSignedAppcast + self.bundleIdentifier = bundleIdentifier self.minimumSystemVersion = minimumSystemVersion ?? "12.0" self.hardwareRequirements = hardwareRequirements self.frameworkVersion = frameworkVersion @@ -369,6 +371,7 @@ class ArchiveItem: CustomStringConvertible { shortVersion: shortVersion, feedURL: feedURL, requiresSignedAppcast: requiresSignedAppcast, + bundleIdentifier: infoPlist[kCFBundleIdentifierKey as String] as? String, minimumSystemVersion: minimumSystemVersion, hardwareRequirements: hardwareRequirements, frameworkVersion: frameworkVersion, diff --git a/generate_appcast/FeedXML.swift b/generate_appcast/FeedXML.swift index 745e1e6b..df7f6f67 100644 --- a/generate_appcast/FeedXML.swift +++ b/generate_appcast/FeedXML.swift @@ -311,7 +311,20 @@ func writeAppcast(appcastDestPath: URL, keys: PrivateKeys, disableEmbeddedSignWa requiresSignedAppcast = requiresSignedAppcastFromArchives } - + + if requiresSignedAppcast && keys.publicEdKey != nil && keys.privateEdKey != nil { + let existingBundleIdentifiers = Set(channel.elements(forName: SUAppcastElementBundleIdentifier).compactMap({ $0.stringValue?.trimmingCharacters(in: .whitespacesAndNewlines) })) + let archiveBundleIdentifiers = Set(appcast.versionsInFeed.compactMap({ appcast.archives[$0]?.bundleIdentifier })) + + var bundleIdentifierIndex = try channel.nodes(forXPath: "item").first?.index ?? channel.childCount + for bundleIdentifier in archiveBundleIdentifiers.subtracting(existingBundleIdentifiers).sorted() { + let bundleIdentifierElement = XMLElement.element(withName: SUAppcastElementBundleIdentifier, uri: sparkleNS) as! XMLElement + bundleIdentifierElement.setChildren([text(bundleIdentifier)]) + channel.insertChild(bundleIdentifierElement, at: bundleIdentifierIndex) + bundleIdentifierIndex += 1 + } + } + // Keep track of already signed release note files so we avoid // re-signing the same file in case the developer is using symlinks // Path -> [(EdSignature, Length)]