Skip to content

Commit e43e368

Browse files
authored
fix(nav3): Prevent NaN and infinite arguments from dropping SentryEvents and SentryTransactions (#6713)
Commit fixes an edge-case bug in our Nav3 integration that could cause SentryEvents or Transactions to be dropped SDK-side if back stack entry arguments contained non-finite numeric values. Now we convert NaN and positive or negative infinity values in back stack entry arguments to strings instead of passing them to the transport layer's JSON serializer unchanged. Motivation and Context Our Nav3 integration attaches nav arguments to SentryEvents and SentryTransactions. JSON can't represent NaN or infinity, so an argument containing one of those values can cause serialization of the entire event or transaction to fail. Converting these values to strings preserves the argument’s value while allowing the containing event or transaction to be sent.
1 parent 4ab0ce0 commit e43e368

4 files changed

Lines changed: 81 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
### Fixes
1111

12+
- Nav3: Serialize `NaN` and infinite navigation arguments as strings so they cannot prevent the containing error report or transaction from being serialized ([#6713](https://github.com/getsentry/sentry-java/pull/6713))
1213
- Kotlin now sees `Attachment.getBytes()`, `Device.getArchs()`, `Spring.getActiveProfiles()` and `JsonSerializationUtils.bytesFrom(...)` as nullable, because `@Nullable` now annotates the returned array instead of its elements ([#6558](https://github.com/getsentry/sentry-java/pull/6558))
1314
- Shake-to-report no longer leaks the first Activity that resumes after `Sentry.init` ([#6686](https://github.com/getsentry/sentry-java/pull/6686))
1415
- Android app start: Reopening the app within a minute of process start is now reported as a warm start timed from the reopened screen, instead of reusing the cold start's timing. In standalone app start mode, this no longer sends the cold start a second time, as a warm start ([#6687](https://github.com/getsentry/sentry-java/pull/6687))

‎sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/BackStackConverter.kt‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,11 @@ internal class BackStackConverter<T : Any>(
215215
private fun sanitizeScalar(value: Any?): Any? =
216216
when (value) {
217217
null,
218-
is Number,
219218
is Boolean -> value
219+
is Number ->
220+
if (value.isNonFinite()) {
221+
value.toString().also { consumeCharacters(it.length) }
222+
} else value
220223
is String -> value.also { consumeCharacters(it.length) }
221224
is CharSequence,
222225
is Char -> value.toString().also { consumeCharacters(it.length) }
@@ -466,3 +469,7 @@ internal enum class ArgumentDropReason(
466469
const val ARGUMENT_DROP_REASON_KEY = "dropped_by_sentry"
467470
}
468471
}
472+
473+
/** Whether this is a NaN or infinite Double or Float. */
474+
private fun Number.isNonFinite(): Boolean =
475+
(this is Double && !isFinite()) || (this is Float && !isFinite())

‎sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/BackStackEntryMapper.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public class SentryBackStackEntry(
9292
* - [CharSequence]
9393
* - [Char]
9494
* - [Boolean]
95-
* - any [Number]
95+
* - any [Number] (`NaN` and infinite values are sent as strings)
9696
* - enums (via [Enum.name])
9797
* - `null`
9898
*
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package io.sentry.compose.navigation3
2+
3+
import com.google.common.truth.Truth.assertThat
4+
import io.sentry.Breadcrumb
5+
import io.sentry.JsonSerializer
6+
import io.sentry.NoOpLogger
7+
import io.sentry.SentryEvent
8+
import io.sentry.SentryOptions
9+
import java.io.StringWriter
10+
import org.junit.Test
11+
12+
class ArgumentNumberSerializationTest {
13+
@Test
14+
fun `non-finite numbers produce valid navigation argument JSON`() {
15+
val mapper =
16+
BackStackEntryMapper<Any> {
17+
SentryBackStackEntry(
18+
"Home",
19+
mapOf(
20+
"price" to Double.NaN,
21+
"limits" to floatArrayOf(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY),
22+
),
23+
)
24+
}
25+
val converter =
26+
BackStackConverter(ForwardingBackStackEntryMapper { mapper }, NoOpLogger.getInstance())
27+
val entry =
28+
converter.convert(listOf(Any()), BackStackConverter.RetentionPolicy.KEEP_FIRST).single()
29+
val json = StringWriter()
30+
JsonSerializer(SentryOptions()).serialize(entry.serialize(), json)
31+
assertThat(json.toString())
32+
.isEqualTo(
33+
"""{"entry":"/Home","entry_arguments":{"price":"NaN","limits":["Infinity","-Infinity"]}}""",
34+
)
35+
}
36+
37+
@Test
38+
fun `non-finite number text spends the character budget`() {
39+
val sanitizer =
40+
BackStackConverter.ArgumentSanitizer(
41+
NoOpLogger.getInstance(),
42+
BackStackConverter.WarningState(),
43+
)
44+
val values = mapOf("x" to "a".repeat(4_096 - 3), "n" to Double.NaN)
45+
assertThat(sanitizer.sanitizeEntry(values).dropReason)
46+
.isEqualTo(ArgumentDropReason.CHARACTER_LIMIT)
47+
}
48+
49+
@Test
50+
fun `non-finite navigation arguments keep the containing SentryEvent serializable`() {
51+
val mapper =
52+
BackStackEntryMapper<Any> {
53+
SentryBackStackEntry("Home", mapOf("price" to Double.NaN))
54+
}
55+
val converter =
56+
BackStackConverter(ForwardingBackStackEntryMapper { mapper }, NoOpLogger.getInstance())
57+
val entry =
58+
converter.convert(listOf(Any()), BackStackConverter.RetentionPolicy.KEEP_FIRST).single()
59+
val event =
60+
SentryEvent().apply {
61+
contexts["navigation"] = mapOf("backstack" to listOf(entry.serialize()))
62+
breadcrumbs = listOf(Breadcrumb().apply { data["to_arguments"] = entry.arguments })
63+
}
64+
val json = StringWriter()
65+
66+
JsonSerializer(SentryOptions()).serialize(event, json)
67+
68+
assertThat(json.toString()).contains(""""entry_arguments":{"price":"NaN"}""")
69+
assertThat(json.toString()).contains(""""to_arguments":{"price":"NaN"}""")
70+
}
71+
}

0 commit comments

Comments
 (0)