Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<team>.*`. 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)
}
}

Expand Down
51 changes: 51 additions & 0 deletions Tests/XToolTests/KeychainAccessGroupsTests.swift
Original file line number Diff line number Diff line change
@@ -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",
])
}