Skip to content

Commit

Permalink
Disregard payloads that fail conversion from plist to json backing (#925
Browse files Browse the repository at this point in the history
)
  • Loading branch information
maciesielka authored Jul 22, 2020
1 parent abc43e6 commit 34d00ff
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
17 changes: 12 additions & 5 deletions Analytics/Internal/SEGFileStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions AnalyticsTests/FileStorageTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 34d00ff

Please sign in to comment.