Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disregard payloads that fail conversion from plist to json backing #925

Merged
merged 1 commit into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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