pinterest / PINCache

Fast, non-deadlocking parallel object cache for iOS, tvOS and OS X
Apache License 2.0
2.66k stars 361 forks source link

Secure code by using NSSecureCoding instead of NSCoding #314

Open guraghav opened 2 years ago

guraghav commented 2 years ago

Hello,

Created this issue to address the vulnerability with using NSCoding. The NSCoding does not verify the type of object upon deserialization and therefore is vulnerable to object substitution attacks.

To mitigate this vulnerability, Apple introduced the NSSecureCoding protocol along with the following secure methods of NSKeyedArchiver and NSKeyedUnarchiver, which are robust against this type of attack:

// Secure NSKeyedUnarchiver methods
- (instancetype)initForReadingFromData:(NSData *)data error:(NSError **)error;
+ (id)unarchivedObjectOfClass:(Class)cls fromData:(NSData *)data error:(NSError **)error;
+ (id)unarchivedObjectOfClasses:(NSSet<Class> *)classes fromData:(NSData *)data error:(NSError **)error;

// Secure NSKeyedArchiver methods
- (instancetype)initRequiringSecureCoding:(BOOL)requiresSecureCoding;
+ (NSData *)archivedDataWithRootObject:(id)object requiringSecureCoding:(BOOL)requiresSecureCoding error:(NSError **)error;

Apple provides more information in the WWDC20 session, 'Securing Your App'.

Various Places where NSCoding is being used: https://github.com/pinterest/PINCache/search?q=NSCoding