Skip to content

Commit

Permalink
Merge pull request #1063 from bugsnag/nickdowell/catch-json-exceptions
Browse files Browse the repository at this point in the history
Prevent uploads from crashing with bad JSON
  • Loading branch information
nickdowell authored Apr 6, 2021
2 parents 0c04cae + 17c44b0 commit 05b6524
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
12 changes: 10 additions & 2 deletions Bugsnag/Delivery/BSGEventUploadOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,16 @@ - (void)runWithDelegate:(id<BSGEventUploadOperationDelegate>)delegate completion
}
}

NSDictionary *eventPayload = [event toJsonWithRedactedKeys:configuration.redactedKeys];

NSDictionary *eventPayload;
@try {
eventPayload = [event toJsonWithRedactedKeys:configuration.redactedKeys];
} @catch (NSException *exception) {
bsg_log_err(@"Discarding event %@ because an exception was thrown by -toJsonWithRedactedKeys: %@", self.name, exception);
[self deleteEvent];
completionHandler();
return;
}

NSString *apiKey = event.apiKey ?: configuration.apiKey;

NSMutableDictionary *requestPayload = [NSMutableDictionary dictionary];
Expand Down
16 changes: 14 additions & 2 deletions Bugsnag/Payload/BugsnagEvent.m
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,11 @@ - (NSDictionary *)toJsonWithRedactedKeys:(NSSet *)redactedKeys {

// add metadata
NSMutableDictionary *metadata = [[[self metadata] toDictionary] mutableCopy];
event[BSGKeyMetadata] = [self sanitiseMetadata:metadata redactedKeys:redactedKeys];
@try {
event[BSGKeyMetadata] = [self sanitiseMetadata:metadata redactedKeys:redactedKeys];
} @catch (NSException *exception) {
bsg_log_err(@"An exception was thrown while sanitising metadata: %@", exception);
}

event[BSGKeyDevice] = [self.device toDictionary];
event[BSGKeyApp] = [self.app toDict];
Expand Down Expand Up @@ -605,7 +609,15 @@ - (NSDictionary *)toJsonWithRedactedKeys:(NSSet *)redactedKeys {

- (NSMutableDictionary *)sanitiseMetadata:(NSMutableDictionary *)metadata redactedKeys:(NSSet *)redactedKeys {
for (NSString *sectionKey in [metadata allKeys]) {
metadata[sectionKey] = [metadata[sectionKey] mutableCopy];
if ([metadata[sectionKey] isKindOfClass:[NSDictionary class]]) {
metadata[sectionKey] = [metadata[sectionKey] mutableCopy];
} else {
NSString *message = [NSString stringWithFormat:@"Expected an NSDictionary but got %@ %@",
NSStringFromClass([metadata[sectionKey] class]), metadata[sectionKey]];
bsg_log_err(@"%@", message);
// Leave an indication of the error in the payload for diagnosis
metadata[sectionKey] = [@{@"bugsnag.error": message} mutableCopy];
}
NSMutableDictionary *section = metadata[sectionKey];

if (section != nil) { // redact sensitive metadata values
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changelog

### Bug fixes

* Catch exceptions thrown while preparing JSON for upload rather than crashing.
[#1063](https://github.com/bugsnag/bugsnag-cocoa/pull/1063)

* Prevent app hangs being reported if a debugger is attached.
[#1058](https://github.com/bugsnag/bugsnag-cocoa/pull/1058)

Expand Down

0 comments on commit 05b6524

Please sign in to comment.