Skip to content

Commit

Permalink
[PLAT-4685] Guard against non-string keys when encoding metadata to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
kstenerud committed Sep 3, 2020
1 parent 75ac232 commit dc1b1c3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 21 additions & 0 deletions Tests/BugsnagClientTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down

0 comments on commit dc1b1c3

Please sign in to comment.