From 45737250a7ece216b67216c239942a20cf6b2df7 Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Mon, 30 Mar 2020 15:18:10 -0700 Subject: [PATCH] Fixes potentially incorrect bundle identifier when resigning/refreshing AltStore --- .../FetchProvisioningProfilesOperation.swift | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/AltStore/Operations/FetchProvisioningProfilesOperation.swift b/AltStore/Operations/FetchProvisioningProfilesOperation.swift index 469a5c23..9c8a9f0e 100644 --- a/AltStore/Operations/FetchProvisioningProfilesOperation.swift +++ b/AltStore/Operations/FetchProvisioningProfilesOperation.swift @@ -117,12 +117,10 @@ extension FetchProvisioningProfilesOperation { DatabaseManager.shared.persistentContainer.performBackgroundTask { (context) in - let preferredBundleID: String + let preferredBundleID: String? // Check if we have already installed this app with this team before. - let predicate = NSPredicate(format: "%K == %@ AND %K == %@", - #keyPath(InstalledApp.bundleIdentifier), app.bundleIdentifier, - #keyPath(InstalledApp.team.identifier), team.identifier) + let predicate = NSPredicate(format: "%K == %@", #keyPath(InstalledApp.bundleIdentifier), app.bundleIdentifier) if let installedApp = InstalledApp.first(satisfying: predicate, in: context) { #if DEBUG @@ -139,14 +137,36 @@ extension FetchProvisioningProfilesOperation #else - // This app is already installed, so use the same resigned bundle identifier as before. - // This way, if we change the identifier format (again), AltStore will continue to use - // the old bundle identifier to prevent it from installing as a new app. - preferredBundleID = installedApp.resignedBundleIdentifier + // Teams match if installedApp.team has same identifier as team, + // or if installedApp.team is nil but resignedBundleIdentifier contains the team's identifier. + let teamsMatch = installedApp.team?.identifier == team.identifier || (installedApp.team == nil && installedApp.resignedBundleIdentifier.contains(team.identifier)) + + if teamsMatch + { + // This app is already installed with the same team, so use the same resigned bundle identifier as before. + // This way, if we change the identifier format (again), AltStore will continue to use + // the old bundle identifier to prevent it from installing as a new app. + preferredBundleID = installedApp.resignedBundleIdentifier + } + else + { + preferredBundleID = nil + } #endif } else + { + preferredBundleID = nil + } + + let bundleID: String + + if let preferredBundleID = preferredBundleID + { + bundleID = preferredBundleID + } + else { // This app isn't already installed, so create the resigned bundle identifier ourselves. // Or, if the app _is_ installed but with a different team, we need to create a new @@ -157,11 +177,11 @@ extension FetchProvisioningProfilesOperation if app.bundleIdentifier == StoreApp.altstoreAppID || app.bundleIdentifier == StoreApp.alternativeAltStoreAppID { // Use legacy bundle ID format for AltStore. - preferredBundleID = "com.\(team.identifier).\(app.bundleIdentifier)" + bundleID = "com.\(team.identifier).\(app.bundleIdentifier)" } else { - preferredBundleID = app.bundleIdentifier.replacingOccurrences(of: parentBundleID, with: updatedParentBundleID) + bundleID = app.bundleIdentifier.replacingOccurrences(of: parentBundleID, with: updatedParentBundleID) } } @@ -177,7 +197,7 @@ extension FetchProvisioningProfilesOperation } // Register - self.registerAppID(for: app, name: preferredName, bundleIdentifier: preferredBundleID, team: team, session: session) { (result) in + self.registerAppID(for: app, name: preferredName, bundleIdentifier: bundleID, team: team, session: session) { (result) in switch result { case .failure(let error): completionHandler(.failure(error))