Skip to content
Draft
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

### Fixes

- Nav3: `NaN` and infinite numbers in back stack entry arguments are now sent as the strings `"NaN"`, `"Infinity"`, and `"-Infinity"`, instead of causing the event they're attached to be dropped ([#6700](https://github.com/getsentry/sentry-java/pull/6700))

- 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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public class SentryBackStackEntry(
* - [CharSequence]
* - [Char]
* - [Boolean]
* - any [Number]
* - any [Number] (`NaN` and infinite values are sent as strings)
* - enums (via [Enum.name])
* - `null`
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,11 @@ internal class BackStackConverter<T : Any>(
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) }
Expand Down Expand Up @@ -433,3 +436,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())
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.sentry.compose.navigation3.internal.backstack

import com.google.common.truth.Truth.assertThat
import io.sentry.JsonSerializer
import io.sentry.NoOpLogger
import io.sentry.SentryOptions
import io.sentry.compose.navigation3.BackStackEntryMapper
import io.sentry.compose.navigation3.SentryBackStackEntry
import java.io.StringWriter
import org.junit.Test

class ArgumentNumberSerializationTest {
@Test
fun `non-finite numbers produce valid event JSON`() {
val mapper =
BackStackEntryMapper<Any> {
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(ArgumentConstraints.MAX_ARGUMENT_CHARACTERS - 3), "n" to Double.NaN)
assertThat(sanitizer.sanitizeEntry(values).dropReason)
.isEqualTo(ArgumentDropReason.CHARACTER_LIMIT)
}
}
Loading