Skip to content

Commit

Permalink
Handle nan values and other non-serializable data in events from the …
Browse files Browse the repository at this point in the history
…WebView tracker (#909)
  • Loading branch information
matus-tomlein authored Nov 20, 2024
1 parent bf14959 commit 8f404b5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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

0 comments on commit 8f404b5

Please sign in to comment.