Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
84eb00e
correctly handle attachment references in the stores
tobias-wilfert Mar 19, 2026
0d39672
assert`stored_key` is none before it is set
tobias-wilfert Mar 20, 2026
15c8294
add happy path test
tobias-wilfert Mar 20, 2026
12da644
fix test (broke due to me adding `relay-new-error-processing`)
tobias-wilfert Mar 20, 2026
e246ebe
add validation logic
tobias-wilfert Mar 20, 2026
a3db734
add test for validation logic and update ratelimit test
tobias-wilfert Mar 20, 2026
51f3560
appease clippy
tobias-wilfert Mar 20, 2026
119e4df
Merge branch 'master' into tobias-wilfert/feat/envelope-placeholder
tobias-wilfert Mar 23, 2026
7b90e97
use `application/vnd.sentry.attachment-ref+json`
tobias-wilfert Mar 23, 2026
efaa253
use `verify` in store to get key
tobias-wilfert Mar 23, 2026
6fcb34d
use post + patch in test helper
tobias-wilfert Mar 23, 2026
fab22b8
add changelog entry
tobias-wilfert Mar 23, 2026
dec60e7
Update relay-server/src/services/objectstore.rs
tobias-wilfert Mar 24, 2026
6612cb3
Update relay-server/src/services/outcome.rs
tobias-wilfert Mar 24, 2026
0dd5dd5
Update relay-server/src/processing/attachments/process.rs
tobias-wilfert Mar 24, 2026
56171c9
Update relay-server/src/processing/utils/attachments.rs
tobias-wilfert Mar 24, 2026
caea42d
Merge branch 'master' into tobias-wilfert/feat/envelope-placeholder
tobias-wilfert Mar 30, 2026
8033ed8
Merge branch 'tobias-wilfert/feat/envelope-placeholder' of github.com…
tobias-wilfert Mar 30, 2026
688ebaa
add org id
tobias-wilfert Mar 30, 2026
1345053
add correct import
tobias-wilfert Mar 30, 2026
ab7163b
move `#[cfg(feature = "processing")]` into validate function
tobias-wilfert Mar 30, 2026
d2d48f6
extract common logic for `produce_attachment`
tobias-wilfert Mar 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- Calculate and track accepted bytes per individual trace metric item via `TraceMetricByte` data category. ([#5744](https://github.com/getsentry/relay/pull/5744), [#5767](https://github.com/getsentry/relay/pull/5767))
- Use new processor architecture to process standalone profiles. ([#5741](https://github.com/getsentry/relay/pull/5741))
- TUS: Disallow creation with upload. ([#5734](https://github.com/getsentry/relay/pull/5734))
- Add logic to verify attachment placeholders and correctly store them. ([#5747](https://github.com/getsentry/relay/pull/5747))
- Remove continuous-profiling-beta feature flags. ([#5762](https://github.com/getsentry/relay/pull/5762))
- Playstation: Do not upload attachments if quota is 0. ([#5770](https://github.com/getsentry/relay/pull/5770))
- Add payload byte size to trace metrics. ([#5764](https://github.com/getsentry/relay/pull/5764))
Expand Down
5 changes: 3 additions & 2 deletions relay-server/src/envelope/attachment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;

use serde::Serialize;
use serde::{Deserialize, Serialize};

use crate::envelope::ContentType;

Expand Down Expand Up @@ -78,8 +78,9 @@ impl fmt::Display for AttachmentType {
/// Represents the payload of an [attachment placeholder item](
/// https://develop.sentry.dev/sdk/telemetry/attachments/#attachment-placeholder-item).
#[cfg_attr(not(sentry), expect(unused))]
#[derive(Serialize)]
#[derive(Serialize, Deserialize)]
pub struct AttachmentPlaceholder<'a> {
#[serde(borrow)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Placeholder deserialization rejects non-enum content types

Medium Severity

The content_type field in AttachmentPlaceholder is Option<ContentType>, but ContentType is an enum that only supports a small set of known MIME types (e.g., text/plain, application/json, application/octet-stream). When serde_json::from_slice encounters an unrecognized content type string like "image/png" or "application/pdf", deserialization of the entire placeholder fails, causing the attachment to be rejected as invalid. Regular attachments accept any content type via raw_content_type() as a free-form string. Using Option<String> here would be consistent with how regular attachments handle content types.

Additional Locations (2)
Fix in Cursor Fix in Web

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is true, but it's an issue that may have been introduced by #5671. So I wouldn't change it in this PR.

pub location: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
pub content_type: Option<ContentType>,
Expand Down
2 changes: 1 addition & 1 deletion relay-server/src/envelope/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ impl Item {
}

/// Returns `true` if this item is an attachment placeholder.
fn is_attachment_ref(&self) -> bool {
pub fn is_attachment_ref(&self) -> bool {
self.ty() == &ItemType::Attachment
&& self.content_type() == Some(ContentType::AttachmentRef)
}
Expand Down
3 changes: 2 additions & 1 deletion relay-server/src/processing/attachments/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl processing::Processor for AttachmentProcessor {

async fn process(
&self,
attachments: Managed<Self::Input>,
mut attachments: Managed<Self::Input>,
ctx: processing::Context<'_>,
) -> Result<processing::Output<Self::Output>, Rejected<Self::Error>> {
let client_name = crate::utils::client_name_tag(attachments.headers.meta().client_name());
Expand Down Expand Up @@ -125,6 +125,7 @@ impl processing::Processor for AttachmentProcessor {
}
}

process::validate_attachments(&mut attachments, ctx);
let mut attachments = self.limiter.enforce_quotas(attachments, ctx).await?;
process::scrub(&mut attachments, ctx)?;

Expand Down
24 changes: 24 additions & 0 deletions relay-server/src/processing/attachments/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,27 @@ pub fn scrub(
Ok::<_, Error>(())
})
}

/// Validates the attachments and drop any invalid ones.
///
/// An attachment might be a placeholder, in which case its signature needs to be verified.
pub fn validate_attachments(
attachments: &mut Managed<SerializedAttachments>,
ctx: processing::Context<'_>,
) {
if !ctx.is_processing() {
return;
}

attachments.modify(|attachments, records| {
attachments.attachments.retain_mut(|attachment| {
match utils::attachments::validate(attachment, ctx.config) {
Ok(()) => true,
Err(err) => {
records.reject_err(err, &*attachment);
false
}
}
});
});
}
2 changes: 2 additions & 0 deletions relay-server/src/processing/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ impl processing::Processor for ErrorsProcessor {
) -> Result<Output<Self::Output>, Rejected<Self::Error>> {
let mut error = process::expand(error, ctx)?;

process::validate_attachments(&mut error, ctx);

process::process(&mut error)?;

process::finalize(&mut error, ctx)?;
Expand Down
21 changes: 21 additions & 0 deletions relay-server/src/processing/errors/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,24 @@ pub fn scrub(error: &mut Managed<ExpandedError>, ctx: Context<'_>) -> Result<(),
Ok::<_, Error>(())
})
}

/// Validates the attachments and drop any invalid ones.
///
/// An attachment might be a placeholder, in which case it needs to be validated.
pub fn validate_attachments(error: &mut Managed<ExpandedError>, ctx: Context<'_>) {
if !ctx.is_processing() {
return;
}

error.modify(|error, records| {
error.attachments.retain_mut(|attachment| {
match processing::utils::attachments::validate(attachment, ctx.config) {
Ok(()) => true,
Err(err) => {
records.reject_err(err, &*attachment);
false
}
}
});
});
}
3 changes: 3 additions & 0 deletions relay-server/src/processing/transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ impl Processor for TransactionProcessor {
relay_log::trace!("Expand transaction");
let mut tx = process::expand(tx)?;

relay_log::trace!("Validate attachments");
process::validate_attachments(&mut tx, ctx);

relay_log::trace!("Prepare transaction data");
process::prepare_data(&mut tx, &mut ctx, &mut metrics)?;

Expand Down
21 changes: 21 additions & 0 deletions relay-server/src/processing/transactions/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,24 @@ impl Counted for IndexedSpans {
smallvec![(DataCategory::SpanIndexed, self.0)]
}
}

/// Validates the attachments and drop any invalid ones.
///
/// An attachment might be a placeholder, in which case it needs to be validated.
pub fn validate_attachments(transaction: &mut Managed<Box<ExpandedTransaction>>, ctx: Context<'_>) {
if !ctx.is_processing() {
return;
}

transaction.modify(|transaction, records| {
transaction.attachments.retain_mut(|attachment| {
match utils::attachments::validate(attachment, ctx.config) {
Ok(()) => true,
Err(err) => {
records.reject_err(err, &*attachment);
false
}
}
});
});
}
Comment on lines +452 to +468
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I feel like it should be possible to generalize this function, something like

fn validate_attachments<T, F>(parent: T, getter: F, ctx: Context<'_>)
where
    T: Managed,
    F: FnOnce(&mut T) -> &mut impl RetainMut<Item>

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.

If it is ok I would do that in a follow up once we have the RetainMut.

36 changes: 36 additions & 0 deletions relay-server/src/processing/utils/attachments.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,52 @@
use std::error::Error;
use std::time::Instant;

use relay_config::Config;
use relay_pii::{PiiAttachmentsProcessor, SelectorPathItem, SelectorSpec};
use relay_statsd::metric;

#[cfg(feature = "processing")]
use crate::envelope::AttachmentPlaceholder;
use crate::envelope::{AttachmentType, ContentType, Item, ItemType};
use crate::managed::RecordKeeper;
use crate::services::processor::ProcessingError;
use crate::statsd::RelayTimers;

use crate::services::projects::project::ProjectInfo;
use relay_dynamic_config::Feature;

#[cfg_attr(not(feature = "processing"), expect(unused_variables))]
pub fn validate(item: &Item, config: &Config) -> Result<(), ProcessingError> {
#[cfg(not(feature = "processing"))]
return Ok(());

#[cfg(feature = "processing")]
{
if !item.is_attachment_ref() {
return Ok(());
}

let payload = item.payload();
let payload: AttachmentPlaceholder =
serde_json::from_slice(&payload).map_err(|_| ProcessingError::InvalidAttachmentRef)?;
let signed_location =
crate::services::upload::SignedLocation::try_from_str(payload.location)
.ok_or(ProcessingError::InvalidAttachmentRef)?;
// NOTE: Using the received timestamp here breaks tests without a pop-relay.
let location = signed_location
.verify(chrono::Utc::now(), config)
.map_err(|_| ProcessingError::InvalidAttachmentRef)?;
let signed_length = location
.length
.ok_or(ProcessingError::InvalidAttachmentRef)?;

match item.attachment_body_size() == signed_length {
true => Ok(()),
false => Err(ProcessingError::InvalidAttachmentRef),
}
}
}

/// Apply data privacy rules to attachments in the envelope.
///
/// This only applies the new PII rules that explicitly select `ValueType::Binary` or one of the
Expand Down
17 changes: 12 additions & 5 deletions relay-server/src/services/objectstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use relay_system::{
use sentry_protos::snuba::v1::TraceItem;

use crate::constants::DEFAULT_ATTACHMENT_RETENTION;
use crate::envelope::ItemType;
use crate::envelope::{Item, ItemType};
use crate::managed::{
Counted, Managed, ManagedEnvelope, ManagedResult, OutcomeError, Quantities, Rejected,
};
Expand Down Expand Up @@ -319,8 +319,7 @@ impl ObjectstoreServiceInner {
}
Ok(session) => {
for attachment in attachments {
// we are not storing zero-size attachments in objectstore
if attachment.is_empty() {
if Self::should_skip_upload(attachment) {
continue;
}
let result = self
Expand Down Expand Up @@ -351,8 +350,7 @@ impl ObjectstoreServiceInner {
/// This mutates the attachment item in-place, setting the `stored_key` field to the key in the
/// objectstore.
async fn handle_event_attachment(&self, mut attachment: Managed<StoreAttachment>) {
// we are not storing zero-size attachments in objectstore
if attachment.attachment.is_empty() {
if Self::should_skip_upload(&attachment.attachment) {
self.store.send(attachment);
return;
}
Expand Down Expand Up @@ -545,4 +543,13 @@ impl ObjectstoreServiceInner {

Ok(ObjectstoreKey(response.key))
}

/// Returns `true` if the item should **not** be uploaded to the objectstore.
///
/// This is the case for:
/// - Zero-size attachments
/// - Attachment placeholders
fn should_skip_upload(item: &Item) -> bool {
item.is_empty() || item.is_attachment_ref()
}
}
6 changes: 6 additions & 0 deletions relay-server/src/services/outcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ pub enum DiscardReason {
/// (Relay) A trace attachment that has invalid item headers or attachment meta-data.
InvalidTraceAttachment,

/// (Relay) An attachment ref that has invalid item headers or payload.
#[cfg(feature = "processing")]
InvalidAttachmentRef,

/// (Relay) A required feature is not enabled.
FeatureDisabled(Feature),

Expand Down Expand Up @@ -555,6 +559,8 @@ impl DiscardReason {
DiscardReason::InvalidSpan => "invalid_span",
DiscardReason::InvalidSpanAttachment => "invalid_span_attachment",
DiscardReason::InvalidTraceAttachment => "invalid_trace_attachment",
#[cfg(feature = "processing")]
DiscardReason::InvalidAttachmentRef => "invalid_placeholder_attachment",
DiscardReason::FeatureDisabled(_) => "feature_disabled",
DiscardReason::TransactionAttachment => "transaction_attachment",
DiscardReason::InvalidCheckIn => "invalid_check_in",
Expand Down
8 changes: 8 additions & 0 deletions relay-server/src/services/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,10 @@ pub enum ProcessingError {
ProcessingGroupMismatch,
#[error("new processing pipeline failed")]
ProcessingFailure,

#[cfg(feature = "processing")]
#[error("invalid attachment reference")]
InvalidAttachmentRef,
}

impl ProcessingError {
Expand Down Expand Up @@ -662,6 +666,10 @@ impl ProcessingError {
Self::ProcessingGroupMismatch => Some(Outcome::Invalid(DiscardReason::Internal)),
// Outcomes are emitted in the new processing pipeline already.
Self::ProcessingFailure => None,
#[cfg(feature = "processing")]
Self::InvalidAttachmentRef => {
Some(Outcome::Invalid(DiscardReason::InvalidAttachmentRef))
}
}
}

Expand Down
Loading
Loading