6 #import <Foundation/Foundation.h>
7 #import <objc/objc-class.h>
8 #import "SecXPCHelper.h"
10 @implementation SecXPCHelper : NSObject
12 + (NSSet<Class> *)safeErrorPrimitiveClasses
14 static NSMutableSet<Class> *errorClasses = nil;
15 static dispatch_once_t onceToken;
16 dispatch_once(&onceToken, ^{
17 errorClasses = [NSMutableSet set];
27 for (unsigned n = 0; n < sizeof(classes) / sizeof(classes[0]); n++) {
28 Class class = objc_getClass(classes[n]);
30 [errorClasses addObject:class];
38 + (NSSet<Class> *)safeErrorCollectionClasses
40 static NSMutableSet<Class> *errorClasses = nil;
41 static dispatch_once_t onceToken;
42 dispatch_once(&onceToken, ^{
43 errorClasses = [NSMutableSet set];
52 for (unsigned n = 0; n < sizeof(classes) / sizeof(classes[0]); n++) {
53 Class class = objc_getClass(classes[n]);
55 [errorClasses addObject:class];
63 + (NSSet<Class> *)safeErrorClasses
65 static NSMutableSet<Class> *errorClasses = nil;
66 static dispatch_once_t onceToken;
67 dispatch_once(&onceToken, ^{
68 errorClasses = [NSMutableSet set];
69 for (Class class in [SecXPCHelper safeErrorPrimitiveClasses]) {
70 [errorClasses addObject:class];
72 for (Class class in [SecXPCHelper safeErrorCollectionClasses]) {
73 [errorClasses addObject:class];
80 + (NSDictionary *)cleanDictionaryForXPC:(NSDictionary *)dict
86 NSMutableDictionary *mutableDictionary = [dict mutableCopy];
87 for (id key in mutableDictionary.allKeys) {
88 id object = mutableDictionary[key];
89 mutableDictionary[key] = [SecXPCHelper cleanObjectForXPC:object];
91 return mutableDictionary;
94 + (id)cleanObjectForXPC:(id)object
100 // Check for primitive classes first, and return them as is
101 for (Class class in [SecXPCHelper safeErrorPrimitiveClasses]) {
102 if ([object isKindOfClass:class]) {
107 // Else, check for known collection classes. We only handle those collection classes we whitelist as
108 // safe, as per the result of `[SecXPCHelper safeErrorCollectionClasses]`. For each collection class,
109 // we also handle the contents uniquely, since not all collections share the same APIs.
110 for (Class class in [SecXPCHelper safeErrorCollectionClasses]) {
111 if ([object isKindOfClass:class]) {
112 if ([object isKindOfClass:[NSError class]]) {
113 NSError *errorObject = (NSError *)object;
114 return [NSError errorWithDomain:errorObject.domain code:errorObject.code userInfo:[SecXPCHelper cleanDictionaryForXPC:errorObject.userInfo]];
115 } if ([object isKindOfClass:[NSDictionary class]]) {
116 return [SecXPCHelper cleanDictionaryForXPC:(NSDictionary *)object];
117 } else if ([object isKindOfClass:[NSArray class]]) {
118 NSArray *arrayObject = (NSArray *)object;
119 NSMutableArray* cleanArray = [NSMutableArray arrayWithCapacity:arrayObject.count];
120 for (id x in arrayObject) {
121 [cleanArray addObject:[SecXPCHelper cleanObjectForXPC:x]];
124 } else if ([object isKindOfClass:[NSSet class]]) {
125 NSSet *setObject = (NSSet *)object;
126 NSMutableSet *cleanSet = [NSMutableSet setWithCapacity:setObject.count];
127 for (id x in setObject) {
128 [cleanSet addObject:[SecXPCHelper cleanObjectForXPC:x]];
131 } else if ([object isKindOfClass:[NSOrderedSet class]]) {
132 NSOrderedSet *setObject = (NSOrderedSet *)object;
133 NSMutableOrderedSet *cleanSet = [NSMutableOrderedSet orderedSetWithCapacity:setObject.count];
134 for (id x in setObject) {
135 [cleanSet addObject:[SecXPCHelper cleanObjectForXPC:x]];
142 // If all else fails, just return the object's class description
143 return NSStringFromClass([object class]);
146 + (NSError *)cleanseErrorForXPC:(NSError * _Nullable)error
152 NSDictionary<NSErrorUserInfoKey, id> *userInfo = [SecXPCHelper cleanDictionaryForXPC:error.userInfo];
153 return [NSError errorWithDomain:error.domain code:error.code userInfo:userInfo];
156 static NSString *kArchiveKeyError = @"error";
158 + (NSError *)errorFromEncodedData:(NSData *)data
160 NSKeyedUnarchiver *unarchiver = nil;
161 NSError *error = nil;
163 unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:NULL];
164 if (unarchiver != nil) {
165 error = [unarchiver decodeObjectOfClass:[NSError class] forKey:kArchiveKeyError];
171 + (NSData *)encodedDataFromError:(NSError *)error
173 NSKeyedArchiver *archiver = nil;
176 archiver = [[NSKeyedArchiver alloc] initRequiringSecureCoding:YES];
177 [archiver encodeObject:error forKey:kArchiveKeyError];
178 data = archiver.encodedData;