Skip to content

Commit 340b104

Browse files
committed
fix(nav3): Verify the probe's encoded output to catch silent argument corruption
The property-encoding compatibility probe ran the encoder but ignored its output, so a binary-incompatible kotlinx.serialization version that links and runs yet mis-encodes under R8 (observed renaming {id=1} to {value=1} and dropping a route as a duplicate key) passed the probe, and the mapper then recorded wrong arguments. The probe now compares the encoded ProbeClass fixture to its known-good output and disables property encoding when they differ, keeping names. The mismatch signal carries only a fixed message, never route data.
1 parent 79e4583 commit 340b104

3 files changed

Lines changed: 81 additions & 12 deletions

File tree

‎sentry-android-navigation3/src/main/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxCompatibilityProbe.kt‎

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@ internal interface KotlinxCompatibilityProbe {
4545
fun nameLookupLinkageError(onInconclusive: (Throwable?) -> Unit): LinkageError?
4646

4747
/**
48-
* Encodes [ProbeClass] using the same path as [KotlinxBackStackEntryMapper] and checks that it
49-
* links and runs.
48+
* Encodes [ProbeClass] using the same path as [KotlinxBackStackEntryMapper], checks that it links
49+
* and runs, and verifies its output matches the known fixture.
5050
*
51-
* Returns the error that makes the properties encoder unusable, or `null` if encoding succeeds.
52-
* Unlike [nameLookupLinkageError], any failure is conclusive: an incompatible
51+
* Returns the error that makes the properties encoder unusable, or `null` if encoding succeeds
52+
* and matches. Unlike [nameLookupLinkageError], any failure is conclusive: an incompatible
5353
* kotlinx.serialization version can fail the encoder without a [LinkageError] (for instance, an
54-
* API-incompatible encoder can throw a [NullPointerException] under R8), and the caller disables
55-
* property encoding on any failure here rather than risk recording wrong arguments.
54+
* API-incompatible encoder can throw a [NullPointerException] under R8) or mis-encode without
55+
* throwing at all, and the caller disables property encoding on any failure here rather than risk
56+
* recording wrong arguments.
5657
*/
5758
fun encoderIncompatibility(): Throwable?
5859

@@ -87,18 +88,25 @@ internal interface KotlinxCompatibilityProbe {
8788

8889
override fun encoderIncompatibility(): Throwable? =
8990
try {
91+
val writer = TreeArgumentWriter(ArgumentBudget())
9092
@Suppress("UNCHECKED_CAST")
9193
KotlinxPropertiesEncoder.encode(
9294
ProbeClass.serializer() as KSerializer<Any>,
9395
ProbeClass(),
94-
TreeArgumentWriter(ArgumentBudget()),
96+
writer,
9597
)
96-
null
98+
// An incompatible kotlinx.serialization version can link and run yet mis-encode properties
99+
// under R8 without throwing. Verifying the fixture's output disables such a silently
100+
// corrupting encoder instead of letting wrong arguments reach Sentry.
101+
if (writer.argumentsOrThrow() == EXPECTED_PROBE_ENCODING) {
102+
null
103+
} else {
104+
EncoderProducedUnexpectedOutput()
105+
}
97106
} catch (@Suppress("TooGenericExceptionCaught") t: Throwable) {
98-
// Broad because an incompatible kotlinx.serialization version can fail the encoder in any
99-
// way, not only with a LinkageError. Any failure disables property encoding and keeps
100-
// names,
101-
// as recording wrong arguments would be worse than recording none.
107+
// Broad because an incompatible kotlinx.serialization version can fail the encoder in many
108+
// ways, not only with a LinkageError. Any failure disables property encoding but keeps
109+
// names, as recording wrong arguments would be worse than recording none.
102110
ExceptionUtils.rethrowIfFatal(t)
103111
t
104112
}
@@ -148,5 +156,23 @@ internal interface KotlinxCompatibilityProbe {
148156
* back stack entries. This lookup reads the object's singleton instance.
149157
*/
150158
@Serializable internal object ProbeObject
159+
160+
/**
161+
* [ProbeClass]'s expected encoding. A kotlinx.serialization version that encodes it to anything
162+
* else mis-encodes properties and must not be used. None of the keys are on the sensitive
163+
* denylist, so none are filtered.
164+
*/
165+
private val EXPECTED_PROBE_ENCODING: Map<String, Any?> =
166+
mapOf("name" to "probe", "values" to listOf(1), "counts" to mapOf("count" to 1))
151167
}
152168
}
169+
170+
/**
171+
* Signals that the properties encoder ran but produced output that didn't match the SDK's own
172+
* `ProbeClass` fixture, so the app's kotlinx.serialization version mis-encodes properties. Carries
173+
* only a fixed message, never route data: the fixture is the SDK's own.
174+
*/
175+
internal class EncoderProducedUnexpectedOutput :
176+
RuntimeException(
177+
"The kotlinx.serialization properties encoder produced unexpected output for a known fixture.",
178+
)

‎sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxBackStackEntryMapperProbeTest.kt‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,34 @@ class KotlinxBackStackEntryMapperProbeTest {
266266
assertThat(message.firstValue).contains(INCOMPATIBLE_KOTLINX_ADVICE)
267267
}
268268

269+
@Test
270+
fun `keeps the name and records only the encoding failure when the encoder produces unexpected output`() {
271+
val probe = FakeCompatibilityProbe(encoderError = EncoderProducedUnexpectedOutput())
272+
val fixture = KotlinxMapperFixture.create(includeProperties = true, probe = probe)
273+
val mapper = fixture.mapper
274+
275+
val mapped = mapper.map(Detail("1"))
276+
277+
assertThat(mapped)
278+
.isEqualTo(SentryBackStackEntry("Detail", droppedBy(ArgumentDropReason.ENCODING_FAILED)))
279+
}
280+
281+
@Test
282+
fun `logs a warning with the mismatch error and compatibility advice when the encoder produces unexpected output`() {
283+
val error = EncoderProducedUnexpectedOutput()
284+
val probe = FakeCompatibilityProbe(encoderError = error)
285+
val fixture = KotlinxMapperFixture.create(includeProperties = true, probe = probe)
286+
287+
val message = argumentCaptor<String>()
288+
verify(fixture.logger, times(1)).log(eq(SentryLevel.WARNING), message.capture(), eq(error))
289+
assertThat(message.firstValue)
290+
.startsWith(
291+
"Nav3 SerialNameAndProperties is incompatible with this app's kotlinx.serialization " +
292+
"version.",
293+
)
294+
assertThat(message.firstValue).contains(INCOMPATIBLE_KOTLINX_ADVICE)
295+
}
296+
269297
private companion object {
270298
const val NAME_LOOKUP_CAPABILITY = "name lookups"
271299

‎sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxCompatibilityProbeTest.kt‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,26 @@ class KotlinxCompatibilityProbeTest {
2727

2828
@Test
2929
fun `encoderIncompatibility() - returns null for a compatible kotlinx serialization version`() {
30+
// A compatible version encodes the probe fixture to its expected output, so the probe's output
31+
// verification matches and it reports no incompatibility.
3032
val error = KotlinxCompatibilityProbe.encoderIncompatibility()
3133

3234
assertThat(error).isNull()
3335
}
3436

37+
@Test
38+
fun `EncoderProducedUnexpectedOutput is a RuntimeException with a message that leaks no route data`() {
39+
val error = EncoderProducedUnexpectedOutput()
40+
41+
assertThat(error).isInstanceOf(RuntimeException::class.java)
42+
assertThat(error)
43+
.hasMessageThat()
44+
.isEqualTo(
45+
"The kotlinx.serialization properties encoder produced unexpected output for a known " +
46+
"fixture.",
47+
)
48+
}
49+
3550
// Probe types
3651

3752
@Test

0 commit comments

Comments
 (0)