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 f526c4f23..0967093d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ Changelog ========= +## TBD + +### Bug fixes + +* Guard against non-string metadata map keys + [#790](https://bugsnag.atlassian.net/browse/PLAT-4685) + ## 6.1.3 (2020-08-17) ### Bug fixes 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];