From 34d00fffa438f7a8419740fb1b3dbfd92ea408f7 Mon Sep 17 00:00:00 2001 From: Mike Ciesielka Date: Wed, 22 Jul 2020 12:28:41 -0400 Subject: [PATCH] Disregard payloads that fail conversion from plist to json backing (#925) --- Analytics/Internal/SEGFileStorage.m | 17 ++++++++++++----- AnalyticsTests/FileStorageTest.swift | 13 +++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Analytics/Internal/SEGFileStorage.m b/Analytics/Internal/SEGFileStorage.m index ed534dbab..6392f4f55 100644 --- a/Analytics/Internal/SEGFileStorage.m +++ b/Analytics/Internal/SEGFileStorage.m @@ -160,11 +160,18 @@ - (id _Nullable)jsonForKey:(NSString *)key BOOL needsConversion = NO; result = [self jsonFromData:data needsConversion:&needsConversion]; if (needsConversion) { - [self setJSON:result forKey:key]; - // maybe a little repetitive, but we want to recreate the same path it would - // take if it weren't being converted. - data = [self dataForKey:key]; - result = [self jsonFromData:data needsConversion:&needsConversion]; + @try { + [self setJSON:result forKey:key]; + // maybe a little repetitive, but we want to recreate the same path it would + // take if it weren't being converted. + data = [self dataForKey:key]; + result = [self jsonFromData:data needsConversion:&needsConversion]; + } @catch (NSException *e) { + SEGLog(@"Unable to convert data from plist object to json; Exception: %@, data: %@", e, data); + + [self removeKey:key]; + result = nil; + } } } return result; diff --git a/AnalyticsTests/FileStorageTest.swift b/AnalyticsTests/FileStorageTest.swift index 563cbc38a..9353144fb 100644 --- a/AnalyticsTests/FileStorageTest.swift +++ b/AnalyticsTests/FileStorageTest.swift @@ -114,6 +114,19 @@ class FileStorageTest : XCTestCase { let dictOut = storage.dictionary(forKey: key) XCTAssertEqual(dictOut as? [String: String], dictIn) } + + func testShouldRemoveDictionaryForInvalidPlistConversion() { + let key = "invalid.plist" + let dictIn: [String: Any] = [ + "timestamp": TimeInterval.nan // `.nan` fails JSONSerialization + ] + + let url = storage.url(forKey: key) + (dictIn as NSDictionary).write(to: url, atomically: true) + let dictOut = storage.dictionary(forKey: key) + XCTAssertNil(dictOut) + XCTAssertNil(try? url.checkResourceIsReachable()) + } func testShouldWorkWithCrypto() { let url = FileStorage.applicationSupportDirectoryURL()