Preserve whole diagnostic attachments and report incomplete uploads (#44606)

## Why

Truncating diagnostic attachments can corrupt compressed archives, while skipping unreadable files can hide incomplete diagnostic delivery.

## What changed

- Remove attachment truncation and the 40,000,000-byte wire cap. Enforce the 200 MiB decoded envelope limit, including framing, and report oversized or unreadable attachments as upload failures.
- Use gzip only when it reduces envelope size, and set `Content-Encoding` according to the request bytes for both interactive and report uploads.
- Document that successful uploads mean HTTP acceptance, not durable attachment storage, and that callers must retain source files.

## Testing

Add coverage for incomplete uploads caused by missing or oversized files. Update large-file coverage to verify byte-for-byte delivery of JSONL files and a gzip archive exceeding the former wire cap, including successful archive decompression.

GitOrigin-RevId: a5316e9f407f16ee5a3dfb3f15122b1db51d6a94
This commit is contained in:
chess authored and copyberry committed 2026-09-10 18:10:26 +00:00
1 parent 818f1cca8c
commit 242c5ce01c
5 files changed
+160 -215

No files matched your search

+39 -52
View File
@@ -17,11 +17,10 @@ use sentry::ClientOptions;
use sentry::protocol::Attachment;
use sentry::protocol::Envelope;
use sentry::protocol::EnvelopeHeaders;
use sentry::protocol::EnvelopeItem;
use sentry::types::Dsn;
use crate::MAX_DECODED_UPLOAD_BYTES;
use crate::MAX_UPLOAD_BYTES;
use crate::attachment_truncation::truncate_attachment;
pub(super) const DEFAULT_RATE_LIMIT: Duration = Duration::from_secs(/*secs*/ 60);
@@ -30,7 +29,7 @@ pub(super) enum EnvelopeKind {
Attachment,
}
pub(super) fn gzip_envelope_request(
pub(super) fn envelope_request(
client_pool: &RouteAwareClientPool,
dsn: &Dsn,
body: Bytes,
@@ -38,16 +37,21 @@ pub(super) fn gzip_envelope_request(
) -> RouteAwareRequestBuilder {
let sentry_options = ClientOptions::default();
let sentry_auth = dsn.to_auth(Some(sentry_options.user_agent.as_ref()));
client_pool
let request = client_pool
.post(dsn.envelope_api_url().as_str())
.header("X-Sentry-Auth", sentry_auth.to_string())
.header("Content-Encoding", "gzip")
.body(body)
.timeout(timeout)
.header("X-Sentry-Auth", sentry_auth.to_string());
// Persisted parts retain their encoding. Gzip's magic bytes cannot begin
// an uncompressed envelope, which starts with a JSON header.
let request = if body.starts_with(&[0x1f, 0x8b]) {
request.header("Content-Encoding", "gzip")
} else {
request
};
request.body(body).timeout(timeout)
}
/// Send an already-serialized envelope within the report's shared deadline.
pub(super) async fn send_gzip_envelope(
pub(super) async fn send_envelope(
client_pool: &RouteAwareClientPool,
dsn: &Dsn,
body: Vec<u8>,
@@ -56,12 +60,12 @@ pub(super) async fn send_gzip_envelope(
rate_limited: &mut bool,
) -> Result<StatusCode> {
let body = Bytes::from(body);
// Retain the exact gzip bytes for at most two diagnostic retries. Never replay the core.
// Retain the exact request bytes for at most two diagnostic retries. Never replay the core.
let mut retry_delays = [250, 500].map(Duration::from_millis).into_iter();
let response = loop {
let timeout = deadline.saturating_duration_since(Instant::now());
anyhow::ensure!(!timeout.is_zero(), "feedback upload deadline exceeded");
let response = gzip_envelope_request(client_pool, dsn, body.clone(), timeout)
let response = envelope_request(client_pool, dsn, body.clone(), timeout)
.send()
.await;
if response.as_ref().is_ok_and(|response| {
@@ -158,56 +162,39 @@ pub(super) fn parse_retry_after(value: &str) -> Option<Duration> {
.ok()
}
pub(super) fn gzip_envelope(envelope: &Envelope) -> io::Result<(Vec<u8>, usize)> {
pub(super) fn encode_envelope(envelope: &Envelope) -> io::Result<(Vec<u8>, usize)> {
let mut writer = CountingWriter {
inner: GzEncoder::new(Vec::new(), Compression::fast()),
bytes: 0,
};
envelope.to_writer(&mut writer)?;
Ok((writer.inner.finish()?, writer.bytes))
let decoded_bytes = writer.bytes;
let mut body = writer.inner.finish()?;
// Already-compressed attachments can grow beyond Sentry's 200 MiB wire limit.
// Reuse the buffer for the raw envelope when gzip does not make it smaller.
if body.len() >= decoded_bytes {
body.clear();
envelope.to_writer(&mut body)?;
}
Ok((body, decoded_bytes))
}
pub(super) fn gzip_attachment_envelope(
pub(super) fn encode_attachment_envelope(
headers: &EnvelopeHeaders,
mut attachment: Attachment,
attachment: Attachment,
) -> Result<Vec<u8>> {
let envelope = Envelope::new().with_headers(headers.clone());
// Apply the file reader's bound to in-memory attachments too. The extra byte
// lets the format-aware truncation below detect and label a shortened copy.
attachment.buffer.truncate(MAX_DECODED_UPLOAD_BYTES + 1);
let mut target_bytes = MAX_DECODED_UPLOAD_BYTES;
loop {
truncate_attachment(
&mut attachment.filename,
&mut attachment.buffer,
target_bytes,
)?;
let mut writer = CountingWriter {
inner: GzEncoder::new(Vec::new(), Compression::fast()),
bytes: 0,
};
// Use the SDK's framing while retaining the buffer for in-place truncation.
// https://github.com/getsentry/sentry-rust/blob/0.46.1/sentry-types/src/protocol/envelope.rs#L453-L487
envelope.to_writer(&mut writer)?;
attachment.to_writer(&mut writer)?;
writeln!(writer)?;
let decoded_bytes = writer.bytes;
let body = writer.inner.finish()?;
if body.len() <= MAX_UPLOAD_BYTES && decoded_bytes <= MAX_DECODED_UPLOAD_BYTES {
return Ok(body);
}
anyhow::ensure!(
!attachment.buffer.is_empty(),
"feedback attachment headers exceed the size limit"
);
target_bytes = attachment
.buffer
.len()
.saturating_sub(decoded_bytes.saturating_sub(MAX_DECODED_UPLOAD_BYTES));
if body.len() > MAX_UPLOAD_BYTES {
target_bytes /= 2;
}
}
anyhow::ensure!(
attachment.buffer.len() <= MAX_DECODED_UPLOAD_BYTES,
"feedback attachment exceeds the size limit"
);
let mut envelope = Envelope::new().with_headers(headers.clone());
envelope.add_item(EnvelopeItem::Attachment(attachment));
let (body, decoded_bytes) = encode_envelope(&envelope)?;
anyhow::ensure!(
decoded_bytes <= MAX_DECODED_UPLOAD_BYTES,
"feedback attachment envelope exceeds the size limit"
);
Ok(body)
}
struct CountingWriter<W> {