Skip to content

Commit ba4bf76

Browse files
authored
Change unarchiveObjectWithFile in FIAM to conform to the secure coding practices (#9834)
* Changed unarchiveObjectWithFile with unarchivedObjectOfClass, added supportsSecureCoding (#9816) * Addressed review feedback * Run style script * Changed unarchivedObjectOfClass to unarchivedObjectOfClasses * Added to changelog, changed NSArray to NSMutableArray
1 parent b638192 commit ba4bf76

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

FirebaseInAppMessaging/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Unreleased
2+
- [changed] Replaced unarchiveObjectWithFile with unarchivedObjectOfClass to conform to secure coding practices, and implemented NSSecureCoding (#9816).
3+
14
# 8.12.0
25
- [fixed] In-App Messaging's test message does not include appData in response. This SDK fix will work once the backend is also updated (#9126).
36

FirebaseInAppMessaging/Sources/Analytics/FIRIAMClearcutLogStorage.m

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,23 @@ - (void)loadFromCachePath:(NSString *)cacheFilePath {
166166
NSString *filePath = cacheFilePath == nil ? [self.class determineCacheFilePath] : cacheFilePath;
167167

168168
NSTimeInterval start = [self.timeFetcher currentTimestampInSeconds];
169+
id fetchedClearcutRetryRecords;
170+
NSData *data = [NSData dataWithContentsOfFile:filePath];
171+
if (data) {
172+
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
173+
fetchedClearcutRetryRecords = [NSKeyedUnarchiver
174+
unarchivedObjectOfClasses:[NSSet setWithObjects:[FIRIAMClearcutLogRecord class],
175+
[NSMutableArray class], nil]
176+
fromData:data
177+
error:nil];
178+
} else {
179+
// Fallback on earlier versions
169180
#pragma clang diagnostic push
170181
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
171-
id fetchedClearcutRetryRecords = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
182+
fetchedClearcutRetryRecords = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
172183
#pragma clang diagnostic pop
184+
}
185+
}
173186
if (fetchedClearcutRetryRecords) {
174187
@synchronized(self) {
175188
self.records = (NSMutableArray<FIRIAMClearcutLogRecord *> *)fetchedClearcutRetryRecords;

FirebaseInAppMessaging/Sources/Flows/FIRIAMActivityLogger.m

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ @implementation FIRIAMActivityRecord
3030
static NSString *const kTimeStampArchiveKey = @"timestamp";
3131
static NSString *const kDetailArchiveKey = @"detail";
3232

33+
+ (BOOL)supportsSecureCoding {
34+
return YES;
35+
}
36+
3337
- (id)initWithCoder:(NSCoder *)decoder {
3438
self = [super init];
3539
if (self != nil) {
3640
_activityType = [decoder decodeIntegerForKey:kActiveTypeArchiveKey];
37-
_timestamp = [decoder decodeObjectForKey:kTimeStampArchiveKey];
41+
_timestamp = [decoder decodeObjectOfClass:[NSDate class] forKey:kTimeStampArchiveKey];
3842
_success = [decoder decodeBoolForKey:kIsSuccessArchiveKey];
39-
_detail = [decoder decodeObjectForKey:kDetailArchiveKey];
43+
_detail = [decoder decodeObjectOfClass:[NSString class] forKey:kDetailArchiveKey];
4044
}
4145
return self;
4246
}
@@ -150,10 +154,23 @@ + (NSString *)determineCacheFilePath {
150154

151155
- (void)loadFromCachePath:(NSString *)cacheFilePath {
152156
NSString *filePath = cacheFilePath == nil ? [self.class determineCacheFilePath] : cacheFilePath;
157+
id fetchedActivityRecords;
158+
NSData *data = [NSData dataWithContentsOfFile:filePath];
159+
if (data) {
160+
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
161+
fetchedActivityRecords = [NSKeyedUnarchiver
162+
unarchivedObjectOfClasses:[NSSet setWithObjects:[FIRIAMActivityRecord class],
163+
[NSMutableArray class], nil]
164+
fromData:data
165+
error:nil];
166+
} else {
167+
// Fallback on earlier versions
153168
#pragma clang diagnostic push
154169
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
155-
id fetchedActivityRecords = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
170+
fetchedActivityRecords = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
156171
#pragma clang diagnostic pop
172+
}
173+
}
157174
if (fetchedActivityRecords) {
158175
@synchronized(self) {
159176
self.activityRecords = (NSMutableArray<FIRIAMActivityRecord *> *)fetchedActivityRecords;

FirebaseInAppMessaging/Sources/Private/Flows/FIRIAMActivityLogger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ typedef NS_ENUM(NSInteger, FIRIAMActivityType) {
2929
};
3030

3131
NS_ASSUME_NONNULL_BEGIN
32-
@interface FIRIAMActivityRecord : NSObject <NSCoding>
32+
@interface FIRIAMActivityRecord : NSObject <NSSecureCoding>
3333
@property(nonatomic, nonnull, readonly) NSDate *timestamp;
3434
@property(nonatomic, readonly) FIRIAMActivityType activityType;
3535
@property(nonatomic, readonly) BOOL success;

0 commit comments

Comments
 (0)