From 6248959dab465717b548ae3e236cc02bcdbcb96c Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Fri, 20 Nov 2026 15:39:45 +0100 Subject: [PATCH 1/2] fix(nav3): Serialize NaN and infinity as strings Keep navigation arguments JSON-compatible so non-finite numbers cannot prevent the containing report from being serialized. Refs JAVA-787 --- .../compose/navigation3/BackStackConverter.kt | 9 ++- .../navigation3/BackStackEntryMapper.kt | 2 +- .../ArgumentNumberSerializationTest.kt | 71 +++++++++++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/ArgumentNumberSerializationTest.kt diff --git a/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/BackStackConverter.kt b/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/BackStackConverter.kt index 4bd0bd24e57..57b0c708de7 100644 --- a/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/BackStackConverter.kt +++ b/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/BackStackConverter.kt @@ -215,8 +215,11 @@ internal class BackStackConverter( private fun sanitizeScalar(value: Any?): Any? = when (value) { null, - is Number, is Boolean -> value + is Number -> + if (value.isNonFinite()) { + value.toString().also { consumeCharacters(it.length) } + } else value is String -> value.also { consumeCharacters(it.length) } is CharSequence, is Char -> value.toString().also { consumeCharacters(it.length) } @@ -466,3 +469,7 @@ internal enum class ArgumentDropReason( const val ARGUMENT_DROP_REASON_KEY = "dropped_by_sentry" } } + +/** Whether this is a NaN or infinite Double or Float. */ +private fun Number.isNonFinite(): Boolean = + (this is Double && !isFinite()) || (this is Float && !isFinite()) diff --git a/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/BackStackEntryMapper.kt b/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/BackStackEntryMapper.kt index 924f8c49728..12ca7c72257 100644 --- a/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/BackStackEntryMapper.kt +++ b/sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/BackStackEntryMapper.kt @@ -92,7 +92,7 @@ public class SentryBackStackEntry( * - [CharSequence] * - [Char] * - [Boolean] - * - any [Number] + * - any [Number] (`NaN` and infinite values are sent as strings) * - enums (via [Enum.name]) * - `null` * diff --git a/sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/ArgumentNumberSerializationTest.kt b/sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/ArgumentNumberSerializationTest.kt new file mode 100644 index 00000000000..1190e63603e --- /dev/null +++ b/sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/ArgumentNumberSerializationTest.kt @@ -0,0 +1,71 @@ +package io.sentry.compose.navigation3 + +import com.google.common.truth.Truth.assertThat +import io.sentry.Breadcrumb +import io.sentry.JsonSerializer +import io.sentry.NoOpLogger +import io.sentry.SentryEvent +import io.sentry.SentryOptions +import java.io.StringWriter +import org.junit.Test + +class ArgumentNumberSerializationTest { + @Test + fun `non-finite numbers produce valid navigation argument JSON`() { + val mapper = + BackStackEntryMapper { + SentryBackStackEntry( + "Home", + mapOf( + "price" to Double.NaN, + "limits" to floatArrayOf(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY), + ), + ) + } + val converter = + BackStackConverter(ForwardingBackStackEntryMapper { mapper }, NoOpLogger.getInstance()) + val entry = + converter.convert(listOf(Any()), BackStackConverter.RetentionPolicy.KEEP_FIRST).single() + val json = StringWriter() + JsonSerializer(SentryOptions()).serialize(entry.serialize(), json) + assertThat(json.toString()) + .isEqualTo( + """{"entry":"/Home","entry_arguments":{"price":"NaN","limits":["Infinity","-Infinity"]}}""", + ) + } + + @Test + fun `non-finite number text spends the character budget`() { + val sanitizer = + BackStackConverter.ArgumentSanitizer( + NoOpLogger.getInstance(), + BackStackConverter.WarningState(), + ) + val values = mapOf("x" to "a".repeat(4_096 - 3), "n" to Double.NaN) + assertThat(sanitizer.sanitizeEntry(values).dropReason) + .isEqualTo(ArgumentDropReason.CHARACTER_LIMIT) + } + + @Test + fun `non-finite navigation arguments keep the containing SentryEvent serializable`() { + val mapper = + BackStackEntryMapper { + SentryBackStackEntry("Home", mapOf("price" to Double.NaN)) + } + val converter = + BackStackConverter(ForwardingBackStackEntryMapper { mapper }, NoOpLogger.getInstance()) + val entry = + converter.convert(listOf(Any()), BackStackConverter.RetentionPolicy.KEEP_FIRST).single() + val event = + SentryEvent().apply { + contexts["navigation"] = mapOf("backstack" to listOf(entry.serialize())) + breadcrumbs = listOf(Breadcrumb().apply { data["to_arguments"] = entry.arguments }) + } + val json = StringWriter() + + JsonSerializer(SentryOptions()).serialize(event, json) + + assertThat(json.toString()).contains(""""entry_arguments":{"price":"NaN"}""") + assertThat(json.toString()).contains(""""to_arguments":{"price":"NaN"}""") + } +} From f360490c4ebf9522379324f04ad9eab8ec9c6780 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Fri, 20 Nov 2026 15:42:15 +0100 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9472ec4027c..9eb582d3bab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### Fixes +- 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)) - 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)) - Shake-to-report no longer leaks the first Activity that resumes after `Sentry.init` ([#6686](https://github.com/getsentry/sentry-java/pull/6686))