Skip to content

fix(profiling): Fix accidentally correct Android profile chunk parsing#6183

Open
0xadam-brown wants to merge 1 commit into
masterfrom
fix/v2-android-trace-version
Open

fix(profiling): Fix accidentally correct Android profile chunk parsing#6183
0xadam-brown wants to merge 1 commit into
masterfrom
fix/v2-android-trace-version

Conversation

@0xadam-brown

@0xadam-brown 0xadam-brown commented Jul 6, 2026

Copy link
Copy Markdown
Member

Overview

Fixes logic for handling Android profile chunks that pass through AndroidOrV2ProfileChunk::parse (viz., Android trace profiles and Android ANR profiles). Prior to this commit, the ::parse implementation worked only because it had been tailored to the quirks of existing Android trace + ANR profiles. But that special-casing will break once the updates to ANR billing under JAVA-548 are complete.

This commit anticipates JAVA-548 by:

  1. routing Android trace profile chunks by their payload shape, and allowing all ANR profiles to (rightly) be parsed as V2ProfileChunk's; and

  2. parsing + serializing Android trace chunks with a new 2.android-trace version so downstream consumers can distinguish them from sample v2 chunks.

Note: we now reject v1 and versionless Android trace profile chunks. Our Android SDK implementation suggests they were always unwanted; this commit makes that explicit. (But see the comment here.)

JAVA-634

Legal Boilerplate

Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.

Comment thread relay-profiling/src/profile_chunk.rs Outdated
// Destination type: V2ProfileChunk

// First handle Android trace profiles...
(_, true) => AndroidProfileChunk::parse(data)

This comment was marked as duplicate.

@0xadam-brown
0xadam-brown requested a review from markushi July 6, 2026 20:22
@0xadam-brown 0xadam-brown changed the title fix(profiling): Fix accidentally correct Android profile_chunk parsing fix(profiling): Fix accidentally correct Android profile chunk parsing Jul 6, 2026
Fixes logic for parsing Android profile chunks that pass through AndroidOrV2ProfileChunk::parse (viz., Android trace profiles and Android ANR profiles). Prior to this commit, the ::parse implementation worked only because it had been tailored to the quirks of existing Android trace + ANR profiles. But that special-casing will break once the updates to ANR billing under [JAVA-548](https://linear.app/getsentry/issue/JAVA-548/bill-anr-profiles-as-ui-profile-hours-instead-of-continuous-profile0) are complete.

Commit anticipates JAVA-548 by:

 1. routing Android trace profile chunks by their payload shape, and allowing all ANR profiles to (rightly) be parsed as V2ProfileChunk's; and

 2. parsing + serializing Android trace chunks with a new 2.android-trace version so downstream consumers can distinguish them from sample-format v2 chunks.

Note: we now reject v1 and versionless Android trace profile chunks. They were always unwanted (and never existed in our reference Android SDK implementation); this commit makes that explicit.

Co-Authored-By: Codex <noreply@openai.com>
@0xadam-brown
0xadam-brown force-pushed the fix/v2-android-trace-version branch from c60d4b0 to 17415a2 Compare July 24, 2026 14:25
sample::Version::V2 => V2ProfileChunk::parse(data).map(Box::new).map(Self::V2),
sample::Version::V2AndroidTrace
| sample::Version::V1
| sample::Version::Unknown => Err(ProfileError::PlatformNotSupported),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

⚠️ Note that we previously mapped any chunks with the "android" platform to AndroidProfileChunk. Now we do so only if is_android_trace_profile is true.

My research here indicates the new (and narrower) approach works well with Sentry's own SDK implementations:

Relay profile types - boxed

But what about non-Sentry implementations? Do we want to support anyone who might be relying on Relay's existing approach of parsing anything with an "android" platform as AndroidProfileChunk? We could easily do so by including a if (platform == android) => AndroidProfileChunk::parse(data) fallback after the (sample::Version::V2, _) check.

I've omitted the fallback because it requires extra explanation to understand what's going on + it'd leave a live code path "just in case", and I assumed we probably don't want to support speculative use cases. (I've added a callout in the CHANGELOG instead.)

Chime in if you think otherwise and I can easily include that fallback....


Note: all values that currently pass through AndroidOrV2ProfileChunk::parse are covered by the red box in the table above. (The "cocoa" platform is representative of all non-Android platforms.) W/r/t any previous Sentry SDK implementations, my LLM confirmed that this PR can handle all historical profile types that i) would currently be routed through AndroidOrV2ProfileChunk::parse and ii) should be mapped to AndroidProfileChunk.

@0xadam-brown
0xadam-brown marked this pull request as ready for review July 24, 2026 15:03
@0xadam-brown
0xadam-brown requested a review from a team as a code owner July 24, 2026 15:03
0xadam-brown added a commit to getsentry/sentry-java that referenced this pull request Jul 24, 2026
Commit updates the platform used with ANR profiles from Java to Android so that we can properly bill ANR profiling under UI Profile Hours rather than Continuous Profile Hours.

Depends on the updates made in [Relay #6183](getsentry/relay#6183), [getsentry #118849](getsentry/sentry#118849), [vroomrs #93](getsentry/vroomrs#93), and [vroom #672](getsentry/vroom#672).
Comment on lines 133 to +160
serde_path_to_error::deserialize(d)
}?;

match (minimal.platform.as_str(), minimal.version) {
// This has always been parsed with higher priority than `v2`, so this was kept as-is
// when refactoring, but from the looks of it, this may cause issues with v2 profiles
// which happen to be sent from android.
("android", _) => AndroidProfileChunk::parse(data)
// Android SDKs produce two profile_chunk types that pass through this method: trace
// profiles and Application-Not-Responding (ANR) profiles. They come in multiple
// varieties, each of which needs to be accounted for.

// Android trace profiles:
// ---------------
// Version: 2 (incorrect), 2.android-trace (corrected)
// Platform: android
// Content field: sampled_profile (i.e., Android Runtime's event-based format, aka
// "traces")
// Destination type: AndroidProfileChunk

// Android ANR profiles:
// ---------------
// Version: 2
// Platform: java (incorrect), android (corrected)
// Content field: profile (i.e., standardized stacks/frames/samples format)
// Destination type: V2ProfileChunk

// We also need to handle non-Android profile chunks.

// Non-Android profiles:
// ---------------
// Version: 2
// Platform: cocoa, javascript, etc.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

unbounded JSON deserialization depth in AndroidOrV2ProfileChunk::parse

Attacker-controlled profile chunk bytes are deserialized with serde_json without a caller-side recursion depth limit. The default max_profile_size (50 MiB) bounds payload bytes but not JSON nesting depth; a payload with millions of nested objects or arrays causes an uncatchable stack-overflow abort in serde_json's recursive visitor.

Evidence
  • relay-server/src/processing/profile_chunks/process.rs:89 calls AndroidOrV2ProfileChunk::parse(&item.payload()) with attacker-controlled profile chunk bytes.
  • relay-profiling/src/profile_chunk.rs:132: first deserialization into MinimalProfile uses serde_json::Deserializer::from_slice with no depth limit; the sampled_profile: Option<serde::de::IgnoredAny> field triggers recursive traversal of arbitrarily nested JSON.
  • relay-profiling/src/profile_chunk.rs:171 and relay-profiling/src/profile_chunk.rs:176: the same data is re-parsed by AndroidProfileChunk::parse and V2ProfileChunk::parse, both also using unbounded serde_json::Deserializer::from_slice; unknown nested fields are skipped via deserialize_ignored_any, again recursing per nesting level.
  • relay-config/src/config.rs:728 sets max_profile_size to ByteSize::mebibytes(50) by default. This byte cap is enforced before parsing (relay-server/src/utils/sizes.rs), but it bounds the wrong dimension: 50 MiB allows roughly 7 million nesting levels with compact nested objects ({"a":...}), far exceeding the default 8 MiB native stack.
  • No recursion depth limit, custom deserializer guard, or serde_json depth configuration is visible anywhere in the three deserialization paths.

Identified by Warden · wrdn-dos-review · RZD-T7M

payload.as_object_mut().unwrap().remove("version");
let data = serde_json::to_vec(&payload).unwrap();

let err = AndroidOrV2ProfileChunk::parse(&data).unwrap_err();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Android profile chunk parsing delegates to unbounded third-party binary parser

Untrusted profile chunk data routed through AndroidProfileChunk::parse reaches android_trace_log::parse with no caller-side element-count, depth, or internal-allocation bound on the decoded binary format.

Evidence
  • expand_json_item in relay-server/src/processing/profile_chunks/process.rs:89 calls AndroidOrV2ProfileChunk::parse(&item.payload()) on envelope items of type ProfileChunk.
  • AndroidOrV2ProfileChunk::parse at relay-profiling/src/profile_chunk.rs:171 routes to AndroidProfileChunk::parse(data) when is_android_trace_profile is true (including the newly added version == V2AndroidTrace condition from this PR).
  • AndroidProfileChunk::parse at relay-profiling/src/android/chunk.rs:96 base64-decodes the sampled_profile field and calls android_trace_log::parse(&profile_bytes) at chunk.rs:100.
  • No caller-side check limits the decoded byte length, declared element counts, recursion depth, or total allocations performed by the third-party binary parser before the call.
  • Envelope-level max_profile_size() (default 50 MiB) caps the total JSON payload size but does not prevent the binary parser from allocating disproportionate memory based on format-declared counts within that bounded input.

Identified by Warden · wrdn-dos-review · ZWW-DCC

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant