Skip to content

Commit e13909a

Browse files
authored
fix(nav3): Reject colliding navigation argument keys (#6714)
Drop an entry’s arguments when distinct map keys stringify identically, rather than silently reporting an incomplete map.
1 parent e43e368 commit e13909a

4 files changed

Lines changed: 55 additions & 3 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: Drop an entry’s arguments with `duplicate_key` when distinct map keys stringify to the same key, instead of silently overwriting a value ([#6714](https://github.com/getsentry/sentry-java/pull/6714))
1213
- 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))
1314
- 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))
1415
- Shake-to-report no longer leaks the first Activity that resumes after `Sentry.init` ([#6686](https://github.com/getsentry/sentry-java/pull/6686))

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,17 @@ internal class BackStackConverter<T : Any>(
179179
for ((key, childValue) in value) {
180180
val keyString = key.toString()
181181
consumeCharacters(keyString.length)
182+
if (sanitized.containsKey(keyString)) {
183+
// Types only b/c key strings may contain host data. Collisions are rare, so we re-scan
184+
// instead of tracking original keys for every map.
185+
val otherKey = value.keys.firstOrNull { it !== key && it.toString() == keyString }
186+
throw DropSubtree(
187+
"A Nav3 map key (${key?.let { it::class.simpleName }}) stringifies to the same " +
188+
"string as another key (${otherKey?.let { it::class.simpleName }}). " +
189+
"Dropping this entry's arguments.",
190+
ArgumentDropReason.DUPLICATE_KEY,
191+
)
192+
}
182193
sanitized[keyString] = sanitizeValue(childValue, depth + 1)
183194
}
184195
return sanitized
@@ -460,6 +471,7 @@ internal enum class ArgumentDropReason(
460471
) {
461472

462473
CHARACTER_LIMIT("max_character_limit_exceeded", exhaustsUpdateBudget = true),
474+
DUPLICATE_KEY("duplicate_key"),
463475
INVALID_STRUCTURE("invalid_structure"),
464476
MAPPING_FAILED("mapping_failed"),
465477
MAX_COUNT("max_argument_count_exceeded", exhaustsUpdateBudget = true),

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,12 @@ public class SentryBackStackEntry(
107107
*
108108
* **Arguments fall back to `toString()` or nothing**
109109
*
110-
* All non-supported argument types are stringified via `toString()`.
110+
* All non-supported argument types are stringified via `toString()`, as are map keys.
111111
*
112112
* No arguments will be recorded for a back stack entry if [map] throws or returns `null`, or if its
113-
* arguments are cyclic, too deeply nested, or too large. If the SDK drops an entry's arguments, it
114-
* adds a `dropped_by_sentry` field and corresponding reason in the Sentry UI.
113+
* arguments are cyclic, too deeply nested, too large, or contain map keys that stringify to the
114+
* same string. If the SDK drops an entry's arguments, it adds a `dropped_by_sentry` field and
115+
* corresponding reason (e.g., `duplicate_key`) in the Sentry UI.
115116
*
116117
* **Using kotlinx.serialization**
117118
*
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.sentry.compose.navigation3
2+
3+
import com.google.common.truth.Truth.assertThat
4+
import io.sentry.ILogger
5+
import io.sentry.SentryLevel.WARNING
6+
import org.junit.Test
7+
import org.mockito.kotlin.eq
8+
import org.mockito.kotlin.mock
9+
import org.mockito.kotlin.verify
10+
11+
class ArgumentDuplicateKeysTest {
12+
13+
private val logger = mock<ILogger>()
14+
15+
@Test
16+
fun `stringified key collisions drop only that entry`() {
17+
val sanitizer = BackStackConverter.ArgumentSanitizer(logger, BackStackConverter.WarningState())
18+
19+
val result =
20+
sanitizer.sanitizeEntry(
21+
mapOf("nested" to linkedMapOf<Any, Any>(1 to "first", "1" to "second")),
22+
)
23+
assertThat(result.values).isEmpty()
24+
assertThat(result.dropReason).isEqualTo(ArgumentDropReason.DUPLICATE_KEY)
25+
verify(logger)
26+
.log(
27+
eq(WARNING),
28+
eq(
29+
"A Nav3 map key (String) stringifies to the same string as another key (Int). " +
30+
"Dropping this entry's arguments.",
31+
),
32+
)
33+
34+
val next = sanitizer.sanitizeEntry(mapOf("id" to 123))
35+
assertThat(next.dropReason).isNull()
36+
assertThat(next.values).containsExactly("id", 123)
37+
}
38+
}

0 commit comments

Comments
 (0)