diff --git a/Bugsnag/KSCrash/Source/KSCrash/Recording/Tools/BSG_KSJSONCodecObjC.m b/Bugsnag/KSCrash/Source/KSCrash/Recording/Tools/BSG_KSJSONCodecObjC.m index b18e19572..b687d6e30 100644 --- a/Bugsnag/KSCrash/Source/KSCrash/Recording/Tools/BSG_KSJSONCodecObjC.m +++ b/Bugsnag/KSCrash/Source/KSCrash/Recording/Tools/BSG_KSJSONCodecObjC.m @@ -447,10 +447,12 @@ int bsg_ksjsoncodecobjc_i_encodeObject(BSG_KSJSONCodec *codec, id object, keys = [keys sortedArrayUsingSelector:@selector(compare:)]; } for (id key in keys) { - if ((result = bsg_ksjsoncodecobjc_i_encodeObject( - codec, [object valueForKey:key], key, context)) != - BSG_KSJSON_OK) { - return result; + if([key isKindOfClass:[NSString class]]) { + if ((result = bsg_ksjsoncodecobjc_i_encodeObject( + codec, [object valueForKey:key], key, context)) != + BSG_KSJSON_OK) { + return result; + } } } return bsg_ksjsonendContainer(context); diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f8b1f944..125c8c63f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ Changelog ### Bug fixes +* Guard against non-string metadata map keys + [#790](https://bugsnag.atlassian.net/browse/PLAT-4685) + * Quiet some Analyzer false positives [#789](https://github.com/bugsnag/bugsnag-cocoa/pull/789) diff --git a/Tests/BugsnagClientTests.m b/Tests/BugsnagClientTests.m index b92be7594..e26b1e515 100644 --- a/Tests/BugsnagClientTests.m +++ b/Tests/BugsnagClientTests.m @@ -278,6 +278,27 @@ - (void)testLargeBreadcrumbSize { XCTAssertEqualObjects(largeMetadata, crumb.metadata); } +- (void)testMetadataInvalidKey { + BugsnagConfiguration *configuration = [[BugsnagConfiguration alloc] initWithApiKey:DUMMY_APIKEY_32CHAR_1]; + configuration.enabledBreadcrumbTypes = BSGEnabledBreadcrumbTypeNone; + BugsnagClient *client = [[BugsnagClient alloc] initWithConfiguration:configuration]; + [client start]; + + NSMutableArray *breadcrumbs = client.breadcrumbs.breadcrumbs; + XCTAssertEqual(0, [breadcrumbs count]); + + id badMetadata = @{ + @"test": @"string key is fine", + @85 : @"numeric key would break JSON" + }; + + [client leaveBreadcrumbWithMessage:@"test msg" metadata:badMetadata andType:BSGBreadcrumbTypeUser]; + + XCTAssertEqual(1, [breadcrumbs count]); + + [client notifyError:[NSError errorWithDomain:@"test" code:0 userInfo:badMetadata]]; +} + - (NSDictionary *)generateLargeMetadata { NSMutableDictionary *dict = [NSMutableDictionary new];