Created
February 22, 2026 08:14
-
-
Save nythepegasus/ebbd30f72242d0985430182278d36449 to your computer and use it in GitHub Desktop.
iOS Security entitlements helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // original code was shared to me by @stossy11 | |
| // cleaned up by nythepegasus | |
| typealias SecTaskRef = OpaquePointer | |
| @_silgen_name("SecTaskCopyValueForEntitlement") | |
| func SecTaskCopyValueForEntitlement( | |
| _ task: SecTaskRef, | |
| _ entitlement: NSString, | |
| _ error: NSErrorPointer | |
| ) -> CFTypeRef? | |
| @_silgen_name("SecTaskCopyTeamIdentifier") | |
| func SecTaskCopyTeamIdentifier( | |
| _ task: SecTaskRef, | |
| _ error: NSErrorPointer | |
| ) -> NSString? | |
| @_silgen_name("SecTaskCreateFromSelf") | |
| func SecTaskCreateFromSelf( | |
| _ allocator: CFAllocator? | |
| ) -> SecTaskRef? | |
| @_silgen_name("CFRelease") | |
| func CFRelease(_ cf: CFTypeRef) | |
| @_silgen_name("SecTaskCopyValuesForEntitlements") | |
| func SecTaskCopyValuesForEntitlements( | |
| _ task: SecTaskRef, | |
| _ entitlements: CFArray, | |
| _ error: UnsafeMutablePointer<Unmanaged<CFError>?>? | |
| ) -> CFDictionary? | |
| func withSecTask(_ block: (SecTaskRef) -> Void) { | |
| guard let task = SecTaskCreateFromSelf(nil) else { return } | |
| block(task) | |
| CFRelease(unsafeBitCast(task, to: CFTypeRef.self)) | |
| } | |
| func withSecEntitlements(for entitlements: [String], _ block: (CFDictionary) -> Void) { | |
| withSecTask { task in | |
| if let entitlements = SecTaskCopyValuesForEntitlements(task, entitlements as CFArray, nil) { | |
| block(entitlements) | |
| } | |
| } | |
| } | |
| func withSecEntitlement(for entitlement: String, _ block: (CFTypeRef) -> Void) { | |
| withSecTask { task in | |
| if let entitlement = SecTaskCopyValueForEntitlement(task, entitlement as NSString, nil) { | |
| block(entitlement) | |
| } | |
| } | |
| } | |
| func checkAppEntitlements(_ ents: [String]) -> [String: Any] { | |
| var ret: [String: Any] = [:] | |
| withSecEntitlements(for: ents) { entitlements in | |
| ret = (entitlements as NSDictionary) as? [String: Any] ?? [:] | |
| } | |
| return ret | |
| } | |
| func checkAppEntitlement(_ ent: String) -> Bool { | |
| var ret: Bool = false | |
| withSecEntitlement(for: ent) { entitlement in | |
| if let num = entitlement as? NSNumber { | |
| ret = num.boolValue | |
| } else if let bool = entitlement as? Bool { | |
| ret = bool | |
| } | |
| } | |
| return ret | |
| } | |
| func getEntitlement(_ ent: String) -> String { | |
| var ret: String = "" | |
| withSecEntitlement(for: ent) { entitlement in | |
| if let entitlement = entitlement as? String { | |
| ret = entitlement | |
| } else if let entitlement = entitlement as? NSString as? String { | |
| ret = entitlement | |
| } | |
| } | |
| return ret | |
| } | |
| func getAppGroups() -> [String] { | |
| var ret: [String] = [] | |
| withSecEntitlement(for: "com.apple.security.application-groups") { groups in | |
| if let groups = groups as? [String] { | |
| ret = groups | |
| } else if let groups = groups as? NSArray as? [String] { | |
| ret = groups | |
| } else if CFGetTypeID(groups) == CFArrayGetTypeID() { | |
| ret = groups as! CFArray as NSArray as? [String] ?? [] | |
| } | |
| } | |
| return ret | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment