@@ -212,13 +212,35 @@ - (NSProgress *)signAppAtURL:(NSURL *)appURL provisioningProfiles:(NSArray<ALTPr
212212 NSURL *profileURL = [app.fileURL URLByAppendingPathComponent: @" embedded.mobileprovision" ];
213213 [profile.data writeToURL: profileURL atomically: YES ];
214214
215+ NSString *additionalEntitlements = nil ;
216+
217+ NSRange commentStartRange = [app.entitlementsString rangeOfString: @" <!---><!-->" ];
218+ NSRange commentEndRange = [app.entitlementsString rangeOfString: @" <!-- -->" ];
219+ if (commentStartRange.location != NSNotFound && commentEndRange.location != NSNotFound && commentEndRange.location > commentStartRange.location )
220+ {
221+ // Most likely using private (commented out) entitlements to exploit Psychic Paper https://github.com/Siguza/psychicpaper
222+ // Assume they know what they are doing and extract private entitlements to merge with profile's.
223+
224+ NSRange commentRange = NSMakeRange (commentStartRange.location , (commentEndRange.location + commentEndRange.length ) - commentStartRange.location );
225+ NSString *commentedEntitlements = [app.entitlementsString substringWithRange: commentRange];
226+
227+ additionalEntitlements = commentedEntitlements;
228+ }
229+
215230 NSData *entitlementsData = [NSPropertyListSerialization dataWithPropertyList: profile.entitlements format: NSPropertyListXMLFormat_v1_0 options: 0 error: &error];
216231 if (entitlementsData == nil )
217232 {
218233 return error;
219234 }
220235
221- NSString *entitlements = [[NSString alloc ] initWithData: entitlementsData encoding: NSUTF8StringEncoding];
236+ NSMutableString *entitlements = [[NSMutableString alloc ] initWithData: entitlementsData encoding: NSUTF8StringEncoding];
237+ if (additionalEntitlements != nil )
238+ {
239+ // Insert additional entitlements after first occurence of <dict>.
240+ NSRange entitlementsStartRange = [entitlements rangeOfString: @" <dict>" ];
241+ [entitlements insertString: additionalEntitlements atIndex: entitlementsStartRange.location + entitlementsStartRange.length];
242+ }
243+
222244 entitlementsByFileURL[app.fileURL] = entitlements;
223245
224246 return nil ;
0 commit comments