Skip to content

feat(nav3): [Part 1] Define an Arguments Protocol when converting back stack entry properties into Sentry arguments - #6705

Open
0xadam-brown wants to merge 2 commits into
feat/nav3-serial-name-mappers-basefrom
feat/nav3-serial-name-mappers-8
Open

0xadam-brown wants to merge 2 commits into
feat/nav3-serial-name-mappers-basefrom
feat/nav3-serial-name-mappers-8

Conversation

@0xadam-brown

@0xadam-brown 0xadam-brown commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

PR Stack (Nav3 SerialNameAndProperties Mapper)

This stack follows the SerialName stack (#6780) and is based on its last PR, #6783.


📜 Description

PR provides infrastructure for serializing back stack entry properties as nav arguments, subject to size and structural limits. It also applies Sentry's sensitive denylist to back stack properties, as described in the "Callouts" below.

(Note: Part 3 will introduce the SerialNameAndProperties mapper that actually exposes this functionality to users; it and the updated sentry-docs are very explicit about the various issues surrounding sensitive data or PII that serializing entry properties can raise.)

💡 Motivation and Context

With Nav3, host apps manage their own nav back stack, which they implement as a list of arbitrarily typed entries. In order to be displayed in Sentry, each of those entries has to be mapped to a Sentry name (e.g., ProductDetailScreen) and zero or more arguments (e.g., productId="123abc"). There are two ways for host apps to provide us with those mappings:

  1. via a custom BackStackEntryMapper; or
  2. via one of our own built-in mappers introduced in this PR sequence and the previous one (feat(nav3): Add the built-in SerialName BackStackEntryMapper #6780).

A new ArgumentSink and ArgumentSource type create the seam that lets us support differentiated processing on the source side (the host app's logic vs built-in logic) but uniform processing on the sink side (argument pruning, protecting against constraint violation, and tree materialization).

Here are the major moving parts of the new protocol:

argument-protocol

addresses: JAVA-787 (link)

⚠️ Callouts

1. We filter sensitive data but not PII from back stack entry properties

This PR:

  1. continues to pass along custom mapper output as-is to the Sentry backend, but
  2. filters built-in mapper properties through Sentry's sensitive denylist.

Sentry's sensitive data policy applies to "key-value data" SDKs send to Sentry on host app's behalf, including "HTTP headers, cookies, and URL query params". This PR extends the rule to "key-value data" of serialized back stack entry properties.

Note that I have not tried to filter personal data or PII. A few reasons for that:

  • we don't do so for Nav2 (in fact, Nav2 doesn't filter sensitive data either, which is likely an oversight)
  • other SDKs don't do so
  • our coverage would be patchy / we'd have to figure out a policy for which keys we'd filter and which not
  • the number of false positives could be large
  • host apps are able to filter via a custom BackStackEntryMapper, or by applying their own filters directly to SerialNameAndProperties

Because we don't remove any personal data or PII, the sendDefaultPii and data-collection options don't apply.

In case you're curious, here's more info contrasting the behavior implemented here for Nav3 vs what we currently do for Nav2:

nav2 vs nav3 sensitive data and PII treatment

Chime in if you have thoughts.

.
⚠️ ⚠️ ⚠️ DEAR READER: YOU CAN SKIP THE REMAINDER OF THIS PR DESCRIPTION. IT ADDRESSES THE ISSUES WE DISCUSSED IN OUR CONVO ABOUT USING KOTLINX.SERIALIZATION WITH NAV3. ⚠️ ⚠️ ⚠️

(Some of the claims that follow are out-of-date, eg, this PR stack no longer introduces a weekly CI check. But they're minor, so I haven't bothered to update them.)
.

2. We use an experimental kotlinx.serialization API

We need to run the app’s serializers in order to extract nav data from back stack entries. We use an experimental kotlinx.serialization API to do so (AbstractEncoder). AbstractEncoder lets us turn serializer output directly into Sentry arguments, filter sensitive keys before retaining their values, and stop encoding when an argument limit is exceeded. It also provides the common scalar-handling behavior that we would otherwise have to implement ourselves.

There are, however, alternatives to AbstractEncoder. See the deep dive section below for more details.

🤿 Implementation Deep Dive (and Alternatives)

Setting up the problem

Kotlin serialization works by having a compiler plugin generate byte code for Serializable classes. That generated byte code calls into kotlinx-serialization-core APIs at runtime, some of which are internal. Host apps determine the version of kotlinx-serialization-core that’s used (as the Sentry SDK declares a compileOnly dep).

So we have to worry about two issues:

  1. Plugin-generated byte code calling kotlinx-serialization-core APIs that are missing or have changed semantics.
  2. SDK code calling the same.

This PR raises both possibilities.

Fwiw, my clanker isn’t worried about (1). The module ships no @Serializable classes of its own, so the only plugin-generated code that runs is the host app's, which the app compiles against the -core version it ships.

W/r/t (2), both built-in mappers invoke kotlinx-serialization-core APIs in order to generate nav data based on the host app’s back stack. The APIs called by the simpler map (SerialName) are stable and non-problematic. The APIs called by the more complicated one (SerialNameAndProperties) are experimental.

The current approach: Extend AbstractEncoder

Reading properties from a @Serializable class is tricker than accessing that class's serial name, which is what makes the SerialNameAndProperties mapper more involved than SerialName.

In order to read property values, kotlinx.serialization requires the use of an Encoder. kotlinx-serialization-core doesn’t supply its own encoder, so I've introduced an AbstractEncoder implementation. That API is experimental, but our use is similar to what androidx does in its SavedStateEncoder. SerialNameAndProperties gives the host app our AbstractEncoder implementation and has the host app’s own serializer walk each nav back stack entry. We then generate Sentry data from each of the reported entries.

To ensure our use of the experimental AbstractEncoder is safe, the SerialNameAndProperties mapper (Part 3) checks the encoder when it's created: it encodes a known value with kotlinx.serialization's built-in serializers and compares the output with its known encoding. If the check fails or the output doesn't match, entries keep their names, but every entry's arguments are dropped with encoding_failed, since recording wrong arguments is worse than recording none.

The big win in the current approach is the ability to support built-in maps for both SAGP use (forthcoming) and manually. The latter matters because back stack entries can contain sensitive data, and manual support means host apps can special-case sensitive entries via a custom BackStackEntryMapper and fall back for everything else to SerialNameAndProperties. The performance of the current approach is also on the better side of our available options.

The downsides are that we have to depend on an experimental API, guard against drift, and drop every entry's arguments (while keeping names) if the host app's version of kotlinx-serialization-core isn't compatible. There are also some downsides w/r/t our current Kotlin dependency management, discussed in the next section.

Impacts on our Kotlin dependency management

[1] The weekly CI job ties our SDK-wide Kotlin version to kotlinx.serialization.

Our test fixtures and the R8 check app are compiled by our compiler against the latest kotlinx.serialization. The plugin checks the runtime jar's Require-Kotlin-Version, and a compiler can only read metadata from its own version and the next one.

If kotlinx.serialization moves more than one Kotlin version ahead of us, the weekly bump PR stops compiling. We'd then have to upgrade Kotlin repo-wide or pause the bumps. This affects CI only, not what we ship, but it sets a minimum pace for the repo's Kotlin upgrades.

[2] Newer kotlinx-serialization-core versions may require updating our SerialNameAndProperties implementation

We should make sure we always support AbstractEncoder APIs from whatever our current kotinx-serialization-core floor is up to the latest -core version, including implementing against multiple versions' APIs. Otherwise, apps that use unsupported -core versions would get no arguments for any destination.

Note: Nav3 upgrades actually help us here. If a newer version of Nav3 requires a newer kotlinx-serialization-core version, then our practical minimum in apps rises, which makes it easier to raise our floor -core version.

Alternatives

Some other options we could consider if we don't like the downsides of the AbstractEncoder approach.

Options that use an Encoder Pros Cons
1. Only support SerialName • No properties encoder, so no encoder compatibility check.
• No dependency on an experimental API.
• No property capture: apps that want arguments must write a custom mapper for all of their back stack entries.
2. Use kotlinx-serialization-json, either as an implementation dependency (bad) or compileOnly (good). With compileOnly, apps without JSON still get destination names, but their entries won't record any arguments. • We could drop AbstractEncoder and the encoder compatibility check, and capture properties via JSON's stable public API instead. • Roughly double the allocations of the current approach: JSON builds its own tree, then we convert it to ours.
• Numbers round-trip through strings, which costs extra parsing and can lose fidelity for some numeric types.
• Size limits are checked too late: oversized entries are fully encoded before being rejected, and cyclic entries cause a stack overflow that crashes the app.
• Higher class-loading cost on first use if the app doesn't already use JSON.
• Like all Encoder approaches, can only support @Serializable back stack entries.

Any option that uses an Encoder (including our current approach) requires the following, although the details of what each involves differ based on the option:

  • R8 emulator matrix tests
  • A weekly CI check against the latest kotlinx-serialization-core version

What differs among the Encoder-based options is whether we need an encoder compatibility check and whether we make use of an experimental API (AbstractEncoder). There are also options that don't involve an Encoder...

Options that don't use an Encoder Pros Cons
3. Use reflection: (more pain than it's worth). • No dependency on kotlinx.serialization.
• Drops the use of AbstractEncoder, the encoder compatibility check, and reliance on kotlinx.serialization's consumer rules.
• Could cover back stack entries that aren't @Serializable.
• We'd need consumer rules that keep property/field names for all apps using our built-in mappers; otherwise R8 obfuscates them.
• Without those rules, sensitive-data filtering breaks (e.g., authToken becomes a), so secrets could be recorded.
• Wouldn't match serialization semantics out of the box (e.g., kotlinx's @Transient, @SerialName on properties, delegated/synthetic fields, value classes).
• We'd have to figure out how to follow only relevant references in our property walk.
• Worse performance than serializers.
4. Have SAGP do the mapping: SAGP reads properties by their real names at compile time. • No runtime dependency on kotlinx.serialization.
• Drops the use of AbstractEncoder, the encoder compatibility check, and reliance on kotlinx.serialization's consumer rules.
• Compiled by the app's own toolchain; survives R8 like any other code.
• Fast, and avoids reflection.
• Sensitive-data filtering can use real property names.
• Could cover back stack entries that aren't @Serializable.
• Only available to apps that use SAGP.
• Unclear whether apps can still special-case sensitive back stack entries and fall back to generated mapping for the rest (e.g., via source generation, or an SDK shim that discovers the generated mapper at runtime).
• Significant new tooling. Needs to: (i) find entry classes across modules; (ii) read Kotlin property metadata in the plugin; (iii) access private properties, probably by injecting a method into each back stack entry class; (iv) support incremental builds.
• Adds build-time cost.
• The sentry-java Kotlin upgrade concerns listed above go away, but are replaced by whatever concerns SAGP's version matrix creates (Kotlin x AGP x SAGP)

💚 How did you test it?

  • Unit tests
  • R8 tests
  • New sample app facilities + manual clanker tests

📝 Checklist

  • I added GH Issue ID & Linear ID
  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.
  • Public API changes reviewed by another Mobile SDK team member or implemented according to the develop docs spec.

🔮 Next steps

Continue with Part 2: Add a lazily encoding argument source.

⚠️ Merge this PR using a merge commit (not squash). Only the collection branch is squash-merged into main.

#skip-changelog

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor
Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 7e35dad

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit 7e35dad.

Main concerns: data sent to Sentry, reach, host performance.

Rules that block low also fired:

  • R1: code was truncated, omitted or a reduced context was used: sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxPropertiesEncoderLimitsTest.kt, sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxPropertiesEncoderTest.kt, sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/KotlinxTestEntries.kt, sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/backstack/kotlinx/SensitiveKeyFilterTest.kt, sentry-android-navigation3/src/test/kotlin/io/sentry/compose/navigation3/internal/common/ArgumentBudgetTest.kt and 5 more
  • R4: a public API record changed: sentry/api/sentry.api

This label is a hint for reviewers, not an approval.

@sentry

sentry Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
SDK Size io.sentry.tests.size 8.65.1 (1) release

⚙️ sentry-android Build Distribution Settings

@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 20, 2026
@0xadam-brown 0xadam-brown changed the title feat(nav3): [Nav3 Mappers 8] Define bounded property encoding feat(nav3): [Part 8] Define bounded property encoding Nov 20, 2026
@0xadam-brown 0xadam-brown changed the title feat(nav3): [Part 8] Define bounded property encoding feat(nav3): [Part 8] Define size and sensitive-data policies used when converting back stack entry properties into Sentry arguments Nov 20, 2026
@0xadam-brown
0xadam-brown marked this pull request as ready for review November 20, 2026 15:43
@0xadam-brown
0xadam-brown requested review from a team and adinauer as code owners November 20, 2026 15:43
@0xadam-brown
0xadam-brown requested review from markushi and romtsn and removed request for a team November 20, 2026 15:43
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 20, 2026
@0xadam-brown
0xadam-brown force-pushed the feat/nav3-serial-name-mappers-8 branch from 122ad95 to f4a7b52 Compare November 24, 2026 10:21
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 24, 2026
@0xadam-brown 0xadam-brown changed the title feat(nav3): [Part 6] Define size and sensitive-data policies used when converting back stack entry properties into Sentry arguments feat(nav3): [Part 3] Define size and sensitive-data policies used when converting back stack entry properties into Sentry arguments Nov 24, 2026
@0xadam-brown
0xadam-brown force-pushed the feat/nav3-serial-name-mappers-7 branch from 801de33 to 6937bd5 Compare November 25, 2026 15:30
@0xadam-brown
0xadam-brown marked this pull request as draft November 25, 2026 15:37
@github-actions github-actions Bot removed the risk: medium PR risk score: medium label Nov 25, 2026
@0xadam-brown
0xadam-brown force-pushed the feat/nav3-serial-name-mappers-7 branch 2 times, most recently from c8bea42 to 1765464 Compare November 27, 2026 15:27
@0xadam-brown
0xadam-brown changed the base branch from feat/nav3-serial-name-mappers-7 to feat/nav3-serial-name-mappers-base November 27, 2026 15:36
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 27, 2026
Bring back the ArgumentSink seam so that a custom mapper's map and the
SerialNameAndProperties encoder that follows write their arguments through
the same TreeArgumentWriter, which enforces every size and structural
limit in one place.

ArgumentSanitizer now adapts a custom mapper's map with HostArgumentSource
instead of recursing through it directly. Behavior is unchanged: the
existing ArgumentSanitizerTest passes, apart from its warnings moving to
DropArguments. Duplicate keys are rejected in key(), before a value is
spent or computed, which keeps the order of checks the direct recursion
had.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@0xadam-brown
0xadam-brown force-pushed the feat/nav3-serial-name-mappers-8 branch from f4a7b52 to d6095b5 Compare December 1, 2026 01:31
@0xadam-brown
0xadam-brown force-pushed the feat/nav3-serial-name-mappers-base branch from 0441799 to 4be993a Compare December 1, 2026 01:31
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Dec 1, 2026
@0xadam-brown 0xadam-brown changed the title feat(nav3): [Part 3] Define size and sensitive-data policies used when converting back stack entry properties into Sentry arguments feat(nav3): [Part 1] Define size and sensitive-data policies used when converting back stack entry properties into Sentry arguments Dec 1, 2026
@0xadam-brown
0xadam-brown marked this pull request as ready for review December 1, 2026 01:49
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Dec 1, 2026
Add KotlinxPropertiesEncoder, which encodes a @serializable back stack
entry's properties as maps, lists, and scalars, for the upcoming
SerialNameAndProperties mapper. It writes through the same ArgumentSink as
a custom mapper's map, so the same size and structural limits apply, and
it also limits how deeply serializers nest.

Values under property names or map keys on Sentry's sensitive denylist
are replaced with [Filtered] without running their serializers. To share
the denylist, HttpUtils.containsSensitiveDataKey becomes public.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@0xadam-brown 0xadam-brown changed the title feat(nav3): [Part 1] Define size and sensitive-data policies used when converting back stack entry properties into Sentry arguments feat(nav3): [Part 1] Define an Arguments Protocol when converting back stack entry properties into Sentry arguments Dec 1, 2026
@0xadam-brown
0xadam-brown force-pushed the feat/nav3-serial-name-mappers-8 branch from d6095b5 to 7e35dad Compare December 1, 2026 10:01
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Dec 1, 2026
* Preserves the original drop reason even if intervening code (e.g., a host app serializer)
* catches it and throws its own exception instead.
*/
val failure: DropArgumentsException?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if instead of asking all the callers to check this, we instead return something like a sealed class, Written, Dropped or Failed?

@runningcode runningcode left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just one comment, see it below.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium sanity-check PR needs a lightweight review for obvious issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants