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

Handle nan values and other non-serializable data in events from the WebView tracker #909

Merged
merged 1 commit into from
Nov 20, 2024
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
5 changes: 5 additions & 0 deletions Sources/Core/Tracker/WebViewMessageHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class WebViewMessageHandler: NSObject, WKScriptMessageHandler {
let context = body["context"] as? [[AnyHashable : Any]] ?? []
let trackers = body["trackers"] as? [String] ?? []

if !JSONSerialization.isValidJSONObject(event) || !JSONSerialization.isValidJSONObject(context) {
logError(message: "WebView: Received event payload is not serializable to JSON, skipping.")
return
}

if command == "trackSelfDescribingEvent" {
trackSelfDescribing(event, withContext: context, andTrackers: trackers)
} else if command == "trackStructEvent" {
Expand Down
36 changes: 36 additions & 0 deletions Tests/TestWebViewMessageHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,41 @@ class TestWebViewMessageHandler: XCTestCase {
let context = payload?["co"] as? String
XCTAssert(context?.contains("{\"a\":\"b\"}") ?? false)
}

func testHandlesNonJSONSerializableDataInEvent() {
let message = MockWKScriptMessage(
body: [
"command": "trackSelfDescribingEvent",
"event": [
"schema": "http://schema.com",
"data": [
"key": Double.nan
]
]
])
webViewMessageHandler?.receivedMesssage(message) // shouldn't crash
}

func testHandlesNonJSONSerializableDataInContext() {
let message = MockWKScriptMessage(
body: [
"command": "trackSelfDescribingEvent",
"event": [
"schema": "http://schema.com",
"data": [
"key": "val"
]
],
"context": [
[
"schema": "http://context-schema.com",
"data": [
"a": Double.nan
]
]
]
])
webViewMessageHandler?.receivedMesssage(message) // shouldn't crash
}
}
#endif
Loading