Skip to content

Commit b7fec9e

Browse files
Use secure encoding for messaging's pending topics (#4826)
* also remove deprecate warning when archive the object
1 parent 921184e commit b7fec9e

File tree

4 files changed

+35
-27
lines changed

4 files changed

+35
-27
lines changed

Firebase/Messaging/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- [added] Firebase Pod support for watchOS: `pod 'Firebase/Messaging'` in addition to `pod 'FirebaseMessaging'`. (#4807)
33
- [fixed] Fix FIRMessagingExtensionHelper crash in unit tests when `attachment == nil`. (#4689)
44
- [fixed] Fix FIRMessagingRmqManager crash when database is removed. This only happens when device has a corrupted database file. (#4771)
5+
- [fixed] Use secure encoding for messaging's pending topics objects. (#3686)
56

67
# 2020-01 -- v 4.2.0
78
- [added] Added watchOS support for Firebase Messaging. This enables FCM push notification function on watch only app or independent watch app. (#4016)

Firebase/Messaging/FIRMessagingPendingTopicsList.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ NS_ASSUME_NONNULL_BEGIN
3030
* topics is unique, as it doesn't make sense to apply the same action to the same topic
3131
* repeatedly; the result would be the same as the first time.
3232
*/
33-
@interface FIRMessagingTopicBatch : NSObject <NSCoding>
33+
@interface FIRMessagingTopicBatch : NSObject <NSSecureCoding>
3434

3535
@property(nonatomic, readonly, assign) FIRMessagingTopicAction action;
3636
@property(nonatomic, readonly, copy) NSMutableSet <NSString *> *topics;
@@ -101,7 +101,7 @@ NS_ASSUME_NONNULL_BEGIN
101101
*
102102
* @see FIRMessagingPendingTopicsListDelegate
103103
*/
104-
@interface FIRMessagingPendingTopicsList : NSObject <NSCoding>
104+
@interface FIRMessagingPendingTopicsList : NSObject <NSSecureCoding>
105105

106106
@property(nonatomic, weak) NSObject <FIRMessagingPendingTopicsListDelegate> *delegate;
107107

Firebase/Messaging/FIRMessagingPendingTopicsList.m

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,18 @@ - (instancetype)initWithAction:(FIRMessagingTopicAction)action {
4848
return self;
4949
}
5050

51-
#pragma mark NSCoding
51+
#pragma mark NSSecureCoding
52+
53+
+ (BOOL)supportsSecureCoding {
54+
return YES;
55+
}
5256

5357
- (void)encodeWithCoder:(NSCoder *)aCoder {
5458
[aCoder encodeInteger:self.action forKey:kPendingTopicBatchActionKey];
5559
[aCoder encodeObject:self.topics forKey:kPendingTopicBatchTopicsKey];
5660
}
5761

5862
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
59-
6063
// Ensure that our integer -> enum casting is safe
6164
NSInteger actionRawValue = [aDecoder decodeIntegerForKey:kPendingTopicBatchActionKey];
6265
FIRMessagingTopicAction action = FIRMessagingTopicActionSubscribe;
@@ -65,10 +68,7 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder {
6568
}
6669

6770
if (self = [self initWithAction:action]) {
68-
NSSet *topics = [aDecoder decodeObjectForKey:kPendingTopicBatchTopicsKey];
69-
if ([topics isKindOfClass:[NSSet class]]) {
70-
_topics = [topics mutableCopy];
71-
}
71+
_topics = [aDecoder decodeObjectOfClass:[NSSet class] forKey:kPendingTopicBatchTopicsKey];
7272
_topicHandlers = [NSMutableDictionary dictionary];
7373
}
7474
return self;
@@ -109,20 +109,22 @@ + (void)pruneTopicBatches:(NSMutableArray <FIRMessagingTopicBatch *> *)topicBatc
109109
}
110110
}
111111

112-
#pragma mark NSCoding
112+
#pragma mark NSSecureCoding
113+
114+
+ (BOOL)supportsSecureCoding {
115+
return YES;
116+
}
113117

114118
- (void)encodeWithCoder:(NSCoder *)aCoder {
115119
[aCoder encodeObject:[NSDate date] forKey:kPendingTopicsTimestampEncodingKey];
116120
[aCoder encodeObject:self.topicBatches forKey:kPendingBatchesEncodingKey];
117121
}
118122

119123
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
120-
121124
if (self = [self init]) {
122-
_archiveDate = [aDecoder decodeObjectForKey:kPendingTopicsTimestampEncodingKey];
123-
NSArray *archivedBatches = [aDecoder decodeObjectForKey:kPendingBatchesEncodingKey];
124-
if (archivedBatches) {
125-
_topicBatches = [archivedBatches mutableCopy];
125+
_archiveDate = [aDecoder decodeObjectOfClass:[NSDate class] forKey:kPendingTopicsTimestampEncodingKey];
126+
_topicBatches = [aDecoder decodeObjectOfClass:[NSMutableArray<FIRMessagingTopicBatch *> class] forKey:kPendingBatchesEncodingKey];
127+
if (_topicBatches) {
126128
[FIRMessagingPendingTopicsList pruneTopicBatches:_topicBatches];
127129
}
128130
_topicsInFlight = [NSMutableSet set];

Firebase/Messaging/FIRMessagingPubSub.m

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,14 @@ - (BOOL)pendingTopicsListCanRequestTopicUpdates:(FIRMessagingPendingTopicsList *
183183

184184
- (void)archivePendingTopicsList:(FIRMessagingPendingTopicsList *)topicsList {
185185
GULUserDefaults *defaults = [GULUserDefaults standardUserDefaults];
186-
#pragma clang diagnostic push
187-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
188-
NSData *pendingData = [NSKeyedArchiver archivedDataWithRootObject:topicsList];
189-
#pragma clang diagnostic pop
186+
187+
NSData *pendingData;
188+
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)) {
189+
NSError *error;
190+
pendingData = [NSKeyedArchiver archivedDataWithRootObject:topicsList requiringSecureCoding:YES error:&error];
191+
} else {
192+
pendingData = [NSKeyedArchiver archivedDataWithRootObject:topicsList];
193+
}
190194
[defaults setObject:pendingData forKey:kPendingSubscriptionsListKey];
191195
[defaults synchronize];
192196
}
@@ -195,16 +199,17 @@ - (void)restorePendingTopicsList {
195199
GULUserDefaults *defaults = [GULUserDefaults standardUserDefaults];
196200
NSData *pendingData = [defaults objectForKey:kPendingSubscriptionsListKey];
197201
FIRMessagingPendingTopicsList *subscriptions;
198-
@try {
199-
if (pendingData) {
200-
#pragma clang diagnostic push
201-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
202-
subscriptions = [NSKeyedUnarchiver unarchiveObjectWithData:pendingData];
203-
#pragma clang diagnostic pop
202+
if (pendingData) {
203+
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)) {
204+
NSError *error;
205+
subscriptions = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSData class] fromData:pendingData error:&error];
206+
} else {
207+
@try {
208+
subscriptions = [NSKeyedUnarchiver unarchiveObjectWithData:pendingData];
209+
} @catch (NSException *exception) {
210+
// Nothing we can do, just continue as if we don't have pending subscriptions
211+
}
204212
}
205-
} @catch (NSException *exception) {
206-
// Nothing we can do, just continue as if we don't have pending subscriptions
207-
} @finally {
208213
if (subscriptions) {
209214
self.pendingTopicUpdates = subscriptions;
210215
} else {

0 commit comments

Comments
 (0)