Skip to content

Commit

Permalink
fix: Sanitize SentryMechanism.data on serialize (#947)
Browse files Browse the repository at this point in the history
When serializing SentryMechanism, the SDK didn't sanitize the data dictionary.
This is fixed now.
  • Loading branch information
philipphofmann authored Feb 12, 2021
1 parent 9e67662 commit 9c04875
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## unreleased

- fix: Sanitize SentryMechanism.data on serialize #947
- feat: Add error to SentryEvent #944
- fix: Mark SentryEvent.message as Nullable #943
- fix: Stacktrace inApp marking on Simulators #942
Expand Down
3 changes: 2 additions & 1 deletion Sources/Sentry/SentryMechanism.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "SentryMechanism.h"
#import "NSDictionary+SentrySanitize.h"

NS_ASSUME_NONNULL_BEGIN

Expand All @@ -20,7 +21,7 @@ - (instancetype)initWithType:(NSString *)type
[serializedData setValue:self.handled forKey:@"handled"];
[serializedData setValue:self.desc forKey:@"description"];
[serializedData setValue:self.meta forKey:@"meta"];
[serializedData setValue:self.data forKey:@"data"];
[serializedData setValue:[self.data sentry_sanitize] forKey:@"data"];
[serializedData setValue:self.helpLink forKey:@"help_link"];

return serializedData;
Expand Down
12 changes: 10 additions & 2 deletions Tests/SentryTests/Protocol/SentryMechanismTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ class SentryMechanismTests: XCTestCase {

// Changing the original doesn't modify the serialized
mechanism.data?["other"] = "object"
mechanism.meta?["other"] = "object"
mechanism.meta?["data"] = "object"

let expected = TestData.mechanism
XCTAssertEqual(expected.type, actual["type"] as! String)
XCTAssertEqual(1, (actual["data"] as! [String: Any]).count)
XCTAssertEqual(expected.desc, actual["description"] as? String)
XCTAssertEqual(expected.handled, actual["handled"] as? NSNumber)
XCTAssertEqual(expected.helpLink, actual["help_link"] as? String)
XCTAssertEqual(1, (actual["meta"] as! [String: Any]).count)

guard let something = (actual["data"] as? [String: Any])?["something"] as? [String: Any] else {
XCTFail("Serialized SentryMechanism doesn't contain something.")
return
}

let currentDateProvider = TestCurrentDateProvider()
let date = currentDateProvider.date() as NSDate
XCTAssertEqual(date.sentry_toIso8601String(), something["date"] as? String)
}
}
3 changes: 2 additions & 1 deletion Tests/SentryTests/Protocol/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ class TestData {
}

static var mechanism: Mechanism {
let currentDateProvider = TestCurrentDateProvider()
let mechanism = Mechanism(type: "type")
mechanism.data = ["data": ["any": "some"]]
mechanism.data = ["something": ["date": currentDateProvider.date()]]
mechanism.desc = "desc"
mechanism.handled = true
mechanism.helpLink = "https://www.sentry.io"
Expand Down

0 comments on commit 9c04875

Please sign in to comment.