Fix iOS crash: sanitize forwarded event params before JSON serialization#3
Merged
ianrumac merged 1 commit intoJul 21, 2026
Merged
Conversation
serializeEventInfo copies the native SuperwallEventInfo.params verbatim into the
payload sendToUnity feeds to JSONSerialization.data(withJSONObject:). When params
contain a value that is not a JSON-native type (a Swift enum/struct/URL bridged
as __SwiftValue), JSONSerialization raises an Objective-C
NSInvalidArgumentException ("Invalid type in JSON write (__SwiftValue)"). That is
an NSException, not a Swift Error, so the `try?` in toJsonString cannot catch it
and the app hard-crashes.
Reproduces reliably on iOS at launch: after Superwall.Identify the SDK runs
getEnrichment and tracks an event whose params carry such a value; the delegate
forwards it via handleSuperwallEvent -> sendToUnity and the app terminates.
Route toJsonString through a recursive sanitizer that coerces any non-JSON leaf
to its String(describing:) form, and guard with isValidJSONObject so residual
edge cases (e.g. non-finite Doubles) drop the message instead of crashing. This
protects the entire native->Unity path, not just event forwarding.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
toJsonStringin the iOS bridge can crash the app with an uncaught Objective-CNSExceptionwhen a forwarded event carries a non-JSON-serializable param value.Crash
Root cause
serializeEventInfocopies the nativeSuperwallEventInfo.paramsverbatim into the payload thatsendToUnitypasses toJSONSerialization.data(withJSONObject:). When a param value is not a JSON-native type (a Swift enum/struct/URL bridged as__SwiftValue),JSONSerializationraises an Objective-CNSInvalidArgumentException. That is anNSException, not a SwiftError, so thetry?intoJsonStringdoes not catch it and the app hard-crashes.Reproduction
Deterministic on iOS at launch: after
Superwall.Identify(...), the SDK runsgetEnrichmentand tracks an event whose params carry such a value; the delegate forwards it viahandleSuperwallEvent→sendToUnityand the app terminates. Verified on iPad, iOS 26.5, Unity IL2CPP, SDK 0.2.4.Fix
Route
toJsonStringthrough a recursive sanitizer that coerces any non-JSON leaf to itsString(describing:)form, and guard withisValidJSONObjectso residual edge cases (e.g. non-finiteDoubles) drop the message instead of crashing. This protects the entire native→Unity path, not just event forwarding, and is a no-op for already-valid payloads.Notes
SuperwallOptionsflag to disable/filter event forwarding, and dropping the delegate is not viable for integrators who rely on it for entitlements — so guarding the bridge is the appropriate layer.JSONSerializationin an Objective-C@try/@catch. Sanitizing up front was chosen because it preserves the payload (as strings) instead of dropping the whole message, and keeps everything in Swift.