Skip to content

Commit 12e0939

Browse files
authored
Fix Invalid JSON Crash (#323)
* Fix Invalid JSON Crash • Fixes a rare crash during registration, suspected to be caused by invalid JSON in tags (first issue #322 ) • Adds JSON validation to all requests • Fixes unit tests, which were failing around 30% of the time due to concurrency issues • Fixes nil device_model crash (second issue in #322 ) • Adds a test to ensure invalid JSON won't cause a crash
1 parent 47851fa commit 12e0939

File tree

3 files changed

+491
-398
lines changed

3 files changed

+491
-398
lines changed

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,9 +624,9 @@ + (void)sendTagsWithJsonString:(NSString*)jsonString {
624624

625625
NSData* data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
626626
NSDictionary* keyValuePairs = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
627-
if (jsonError == nil)
627+
if (jsonError == nil) {
628628
[self sendTags:keyValuePairs];
629-
else {
629+
} else {
630630
onesignal_Log(ONE_S_LL_WARN,[NSString stringWithFormat: @"sendTags JSON Parse Error: %@", jsonError]);
631631
onesignal_Log(ONE_S_LL_WARN,[NSString stringWithFormat: @"sendTags JSON Parse Error, JSON: %@", jsonString]);
632632
}
@@ -638,6 +638,18 @@ + (void)sendTags:(NSDictionary*)keyValuePair {
638638

639639
+ (void)sendTags:(NSDictionary*)keyValuePair onSuccess:(OSResultSuccessBlock)successBlock onFailure:(OSFailureBlock)failureBlock {
640640

641+
if (![NSJSONSerialization isValidJSONObject:keyValuePair]) {
642+
onesignal_Log(ONE_S_LL_WARN, [NSString stringWithFormat:@"sendTags JSON Invalid: The following key/value pairs you attempted to send as tags are not valid JSON: %@", keyValuePair]);
643+
return;
644+
}
645+
646+
for (NSString *key in [keyValuePair allKeys]) {
647+
if ([keyValuePair[key] isKindOfClass:[NSDictionary class]]) {
648+
onesignal_Log(ONE_S_LL_WARN, @"sendTags Tags JSON must not contain nested objects");
649+
return;
650+
}
651+
}
652+
641653
if (tagsToSend == nil)
642654
tagsToSend = [keyValuePair mutableCopy];
643655
else
@@ -983,7 +995,6 @@ + (void)registerUserInternal {
983995

984996
let dataDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:
985997
app_id, @"app_id",
986-
deviceModel, @"device_model",
987998
[[UIDevice currentDevice] systemVersion], @"device_os",
988999
[NSNumber numberWithInt:(int)[[NSTimeZone localTimeZone] secondsFromGMT]], @"timezone",
9891000
[NSNumber numberWithInt:0], @"device_type",
@@ -992,6 +1003,9 @@ + (void)registerUserInternal {
9921003
self.currentSubscriptionState.pushToken, @"identifier", // identifier MUST be at the end as it could be nil.
9931004
nil];
9941005

1006+
if (deviceModel)
1007+
dataDic[@"device_model"] = deviceModel;
1008+
9951009
if (build)
9961010
dataDic[@"game_version"] = build;
9971011

iOS_SDK/OneSignalSDK/Source/OneSignalRequest.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#import "OneSignalRequest.h"
2929
#import "OneSignalHelper.h"
30+
#import "OneSignal.h"
3031
#import "Requests.h"
3132

3233
#define API_VERSION @"api/v1/"
@@ -73,6 +74,12 @@ -(void)attachBodyToRequest:(NSMutableURLRequest *)request withParameters:(NSDict
7374
if (!self.parameters)
7475
return;
7576

77+
//to prevent a crash, print error and return before attempting to serialize to JSON data
78+
if (![NSJSONSerialization isValidJSONObject:self.parameters]) {
79+
[OneSignal onesignal_Log:ONE_S_LL_WARN message:[NSString stringWithFormat:@"OneSignal Attempted to make a request with an invalid JSON body: %@", self.parameters]];
80+
return;
81+
}
82+
7683
NSError *error;
7784
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&error];
7885

0 commit comments

Comments
 (0)