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

[PLAT-4685] Guard against non-string keys when encoding metadata to JSON #790

Merged
merged 2 commits into from
Sep 4, 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
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
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

* 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)

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