fix(profiling): Fix accidentally correct Android profile chunk parsing#6183
fix(profiling): Fix accidentally correct Android profile chunk parsing#61830xadam-brown wants to merge 1 commit into
Conversation
| // Destination type: V2ProfileChunk | ||
|
|
||
| // First handle Android trace profiles... | ||
| (_, true) => AndroidProfileChunk::parse(data) |
This comment was marked as duplicate.
This comment was marked as duplicate.
Sorry, something went wrong.
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>
c60d4b0 to
17415a2
Compare
| sample::Version::V2 => V2ProfileChunk::parse(data).map(Box::new).map(Self::V2), | ||
| sample::Version::V2AndroidTrace | ||
| | sample::Version::V1 | ||
| | sample::Version::Unknown => Err(ProfileError::PlatformNotSupported), |
There was a problem hiding this comment.
is_android_trace_profile is true.
My research here indicates the new (and narrower) approach works well with Sentry's own SDK implementations:
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.
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).
| 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. |
There was a problem hiding this comment.
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:89callsAndroidOrV2ProfileChunk::parse(&item.payload())with attacker-controlled profile chunk bytes.relay-profiling/src/profile_chunk.rs:132: first deserialization intoMinimalProfileusesserde_json::Deserializer::from_slicewith no depth limit; thesampled_profile: Option<serde::de::IgnoredAny>field triggers recursive traversal of arbitrarily nested JSON.relay-profiling/src/profile_chunk.rs:171andrelay-profiling/src/profile_chunk.rs:176: the samedatais re-parsed byAndroidProfileChunk::parseandV2ProfileChunk::parse, both also using unboundedserde_json::Deserializer::from_slice; unknown nested fields are skipped viadeserialize_ignored_any, again recursing per nesting level.relay-config/src/config.rs:728setsmax_profile_sizetoByteSize::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_jsondepth 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(); |
There was a problem hiding this comment.
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_iteminrelay-server/src/processing/profile_chunks/process.rs:89callsAndroidOrV2ProfileChunk::parse(&item.payload())on envelope items of typeProfileChunk.AndroidOrV2ProfileChunk::parseatrelay-profiling/src/profile_chunk.rs:171routes toAndroidProfileChunk::parse(data)whenis_android_trace_profileis true (including the newly addedversion == V2AndroidTracecondition from this PR).AndroidProfileChunk::parseatrelay-profiling/src/android/chunk.rs:96base64-decodes thesampled_profilefield and callsandroid_trace_log::parse(&profile_bytes)atchunk.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
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:
routing Android trace profile chunks by their payload shape, and allowing all ANR profiles to (rightly) be parsed as V2ProfileChunk's; and
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.