diff --git a/Sources/XKit/DeveloperServices/App IDs/Entitlements/EntitlementWithTeamOrBundleID.swift b/Sources/XKit/DeveloperServices/App IDs/Entitlements/EntitlementWithTeamOrBundleID.swift index 1efdb224..7d1d96a4 100644 --- a/Sources/XKit/DeveloperServices/App IDs/Entitlements/EntitlementWithTeamOrBundleID.swift +++ b/Sources/XKit/DeveloperServices/App IDs/Entitlements/EntitlementWithTeamOrBundleID.swift @@ -38,8 +38,29 @@ extension Entitlements { let teamOrBundleIDSet = Set(teamOrBundleIDTypes.map(ObjectIdentifier.init)) try updateEntitlements { entitlements in + // an app shares its keychain with its extensions through a group they all list, so + // keep the groups the build asked for when they belong to the team: the development + // profile grants `.*`. A wildcard is not a group an app can name. + let teamPrefix = "\(teamID.rawValue)." + let sharedGroups = entitlements + .compactMap { ($0 as? KeychainAccessGroupsEntitlement)?.rawValue } + .joined() + .filter { $0.hasPrefix(teamPrefix) && !$0.contains("*") } + entitlements.removeAll { teamOrBundleIDSet.contains(ObjectIdentifier(type(of: $0))) } entitlements += teamOrBundleIDTypes.map { $0.init(teamID: teamID, bundleID: bundleID) } + + guard !sharedGroups.isEmpty, + let index = entitlements.firstIndex(where: { $0 is KeychainAccessGroupsEntitlement }), + let own = entitlements[index] as? KeychainAccessGroupsEntitlement + else { return } + // the app's own group stays first: items added without a group go to the first one, + // so a shared group never becomes the default. + var groups = own.rawValue + for group in sharedGroups where !groups.contains(group) { + groups.append(group) + } + entitlements[index] = KeychainAccessGroupsEntitlement(rawValue: groups) } } diff --git a/Tests/XToolTests/KeychainAccessGroupsTests.swift b/Tests/XToolTests/KeychainAccessGroupsTests.swift new file mode 100644 index 00000000..7f1d8bbd --- /dev/null +++ b/Tests/XToolTests/KeychainAccessGroupsTests.swift @@ -0,0 +1,51 @@ +import Foundation +import Testing +@testable import XKit + +private func keychainGroups( + requesting requested: [String]?, + teamID: String = "TEAM123456", + bundleID: String = "XTL-1.com.example.app" +) throws -> [String]? { + var entitlements = try Entitlements( + entitlements: requested.map { [KeychainAccessGroupsEntitlement(rawValue: $0)] } ?? [] + ) + try entitlements.update(teamID: .init(rawValue: teamID), bundleID: bundleID) + return try entitlements.entitlements() + .compactMap { $0 as? KeychainAccessGroupsEntitlement } + .first? + .rawValue +} + +@Test func keychainGroupsDefaultToTheAppsOwnGroup() throws { + #expect(try keychainGroups(requesting: nil) == ["TEAM123456.XTL-1.com.example.app"]) + #expect(try keychainGroups(requesting: []) == ["TEAM123456.XTL-1.com.example.app"]) +} + +@Test func keychainGroupsKeepTheTeamsSharedGroups() throws { + // the shared group is listed first here: the app's own group must still come out first. + let groups = try keychainGroups(requesting: [ + "TEAM123456.com.example.shared", + "TEAM123456.XTL-1.com.example.app", + "TEAM123456.com.example.other", + ]) + #expect(groups == [ + "TEAM123456.XTL-1.com.example.app", + "TEAM123456.com.example.shared", + "TEAM123456.com.example.other", + ]) +} + +@Test func keychainGroupsDropWhatTheTeamCannotName() throws { + let groups = try keychainGroups(requesting: [ + "OTHERTEAM1.com.example.shared", + "TEAM123456.*", + "$(AppIdentifierPrefix)com.example.shared", + "TEAM123456.com.example.shared", + "TEAM123456.com.example.shared", + ]) + #expect(groups == [ + "TEAM123456.XTL-1.com.example.app", + "TEAM123456.com.example.shared", + ]) +}