1use relay_config::ConfigSnapshot;
2
3use crate::envelope::{Envelope, Item, ItemCount, ItemData};
4use crate::integrations::Integration;
5use crate::managed::Managed;
6use crate::services::outcome::DiscardItemType;
7use crate::statsd::RelayCounters;
8
9pub fn check_envelope_size_limits(
17 config: &ConfigSnapshot,
18 envelope: &Envelope,
19) -> Result<(), DiscardItemType> {
20 let mut empty = Limit {
24 item_size: 0,
25 total_count: 0,
26 total_size: 0,
27 };
28 let mut event = Limit {
29 item_size: config.max_event_size(),
30 total_count: 1,
31 total_size: config.max_event_size(),
32 };
33 let mut attachment = Limit {
34 item_size: config.max_attachment_size(),
35 total_count: config.max_attachment_count(),
36 total_size: config.max_attachments_size(),
37 };
38 let mut replay_recording = Limit {
39 item_size: config.max_replay_compressed_size(),
40 total_count: 1,
41 total_size: config.max_replay_compressed_size(),
42 };
43 let mut replay_video = Limit {
44 item_size: config.max_replay_compressed_size(),
45 total_count: 1,
46 total_size: config.max_replay_compressed_size(),
47 };
48 let mut session = Limit {
49 item_size: config.max_sessions_size(),
50 total_count: config.max_session_count(),
51 total_size: config.max_sessions_size(),
52 };
53 let mut client_report = Limit {
54 item_size: config.max_client_reports_size(),
55 total_count: config.max_client_reports_count(),
56 total_size: config.max_client_reports_size(),
57 };
58 let mut profile = Limit {
59 item_size: config.max_profile_size(),
60 total_count: 1,
61 total_size: config.max_profile_size(),
62 };
63 let mut check_in = Limit {
64 item_size: config.max_check_in_size(),
65 total_count: 1,
66 total_size: config.max_check_in_size(),
67 };
68 let mut metric_bucket = Limit {
69 item_size: config.max_metric_buckets_size(),
70 total_count: 1,
71 total_size: config.max_metric_buckets_size(),
72 };
73 let mut log = Limit {
74 item_size: config.max_log_size(),
75 total_count: 1,
76 total_size: config.max_container_size(),
77 };
78 let mut log_integration = Limit {
79 item_size: config.max_container_size(),
80 total_count: 1,
81 total_size: config.max_container_size(),
82 };
83 let mut trace_metric = Limit {
84 item_size: config.max_trace_metric_size(),
85 total_count: 1,
86 total_size: config.max_container_size(),
87 };
88 let mut span = Limit {
89 item_size: config.max_span_size(),
90 total_count: 1,
91 total_size: config.max_container_size(),
92 };
93 let mut span_integration = Limit {
94 item_size: config.max_container_size(),
95 total_count: 1,
96 total_size: config.max_container_size(),
97 };
98 let mut span_legacy = Limit {
99 item_size: config.max_span_size(),
100 total_count: config.max_standalone_span_count(),
101 total_size: config.max_container_size(),
102 };
103
104 for item in envelope.items() {
105 let result = match item {
106 Item::Event(item) => event.apply(item.data(), None),
107 Item::Transaction(item) => event.apply(item.data(), None),
108 Item::Security(item) => event.apply(item.data(), None),
109 Item::ReplayEvent(item) => event.apply(item.data(), None),
110 Item::RawSecurity(item) => event.apply(item.data(), None),
111 Item::UserReportV2(item) => event.apply(item.data(), None),
112 Item::FormData(item) => event.apply(item.data(), None),
113 Item::Attachment(item) => attachment.apply(item.data(), None),
114 Item::UnrealReport(item) => attachment.apply(item.data(), None),
115 Item::UserReport(item) => attachment.apply(item.data(), None),
116 Item::ReplayRecording(item) => replay_recording.apply(item.data(), None),
117 Item::ReplayVideo(item) => replay_video.apply(item.data(), None),
118 Item::Session(item) => session.apply(item.data(), item.item_count()),
119 Item::Sessions(item) => session.apply(item.data(), item.item_count()),
120 Item::ClientReport(item) => client_report.apply(item.data(), item.item_count()),
121 Item::Profile(item) => profile.apply(item.data(), item.item_count()),
122 Item::ProfileChunk(item) => profile.apply(item.data(), item.item_count()),
123 Item::CheckIn(item) => check_in.apply(item.data(), item.item_count()),
124 Item::MetricBuckets(item) => metric_bucket.apply(item.data(), item.item_count()),
125 Item::Log(item) => log.apply(item.data(), item.item_count()),
126 Item::TraceMetric(item) => trace_metric.apply(item.data(), item.item_count()),
127 Item::Span(item) => match item.data().is_container() {
128 true => span.apply(item.data(), item.item_count()),
129 false => span_legacy.apply(item.data(), item.item_count()),
130 },
131 Item::Integration(item) => match item.integration() {
132 Some(Integration::Errors(_)) => attachment.apply(item.data(), None),
133 Some(Integration::Logs(_)) => log_integration.apply(item.data(), None),
134 Some(Integration::Spans(_)) => span_integration.apply(item.data(), None),
135 None => {
136 debug_assert!(false);
138 empty.apply(item.data(), None)
139 }
140 },
141 Item::Unknown(_) => continue,
143 };
144
145 if let Err(err) = result {
146 relay_statsd::metric!(
147 counter(RelayCounters::EnvelopeSizeLimited) += 1,
148 item = item.ty().name(),
149 limit = match err {
150 Rejected::ItemSize => "item_size",
151 Rejected::TotalCount => "total_count",
152 Rejected::TotalSize => "total_size",
153 }
154 );
155
156 return Err(item.into());
157 }
158 }
159
160 Ok(())
161}
162
163enum Rejected {
164 ItemSize,
165 TotalCount,
166 TotalSize,
167}
168
169struct Limit {
170 item_size: usize,
172 total_count: usize,
174 total_size: usize,
176}
177
178impl Limit {
179 fn apply(&mut self, data: &ItemData, item_count: Option<u32>) -> Result<(), Rejected> {
180 let size = match data.is_container() {
181 true => data.len() / item_count.unwrap_or(1).max(1) as usize,
185 false => data.len(),
186 };
187 if size > self.item_size {
188 return Err(Rejected::ItemSize);
189 }
190
191 self.total_count = self
192 .total_count
193 .checked_sub(1)
194 .ok_or(Rejected::TotalCount)?;
195
196 self.total_size = self
197 .total_size
198 .checked_sub(data.len())
199 .ok_or(Rejected::TotalSize)?;
200
201 Ok(())
202 }
203}
204
205pub fn remove_unknown_items(config: &ConfigSnapshot, envelope: &mut Managed<Box<Envelope>>) {
210 if config.accept_unknown_items() {
211 return;
212 }
213
214 envelope.modify(|envelope, records| {
215 envelope.retain_items(|item| {
216 if item.is_unknown() {
217 relay_log::debug!("dropping unknown item");
218 records.reject_err(None, &*item);
220 false
221 } else {
222 true
223 }
224 });
225 });
226}
227
228#[cfg(test)]
229mod tests {
230 use relay_config::Config;
231 use serde_json::json;
232
233 use super::*;
234
235 #[test]
236 fn test_check_envelope_size_limits_container_counts() {
237 let config = Config::from_json_value(json!({
238 "limits": {
239 "max_event_size": 4,
240 "max_log_size": 4,
241 "max_span_size": 4,
242 "max_container_size": 20,
243 },
244 }))
245 .unwrap();
246
247 for (tag, content_type, count, accepted) in [
248 (
249 "log",
250 "application/vnd.sentry.items.log+json",
251 json!(3),
252 true,
253 ),
254 (
255 "span",
256 "application/vnd.sentry.items.span.v2+json",
257 json!(3),
258 true,
259 ),
260 ("log", "application/json", json!(3), false),
261 ("span", "application/json", json!(3), false),
262 (
263 "transaction",
264 "application/vnd.sentry.items.log+json",
265 json!(3),
266 false,
267 ),
268 (
269 "log",
270 "application/vnd.sentry.items.log+json",
271 json!(0),
272 false,
273 ),
274 (
275 "log",
276 "application/vnd.sentry.items.log+json",
277 json!(null),
278 false,
279 ),
280 (
281 "log",
282 "application/vnd.sentry.items.log+json",
283 json!("invalid"),
284 false,
285 ),
286 ] {
287 let header = json!({
288 "type": tag,
289 "content_type": content_type,
290 "item_count": count,
291 });
292 let envelope = Envelope::parse_bytes(
293 format!("{{\"dsn\":\"https://e12d836b15bb49d7bbf99e64295d995b@sentry.io/42\"}}\n{header}\n123456789\n").into(),
294 )
295 .unwrap();
296 assert_eq!(
297 check_envelope_size_limits(&config.current(), &envelope).is_ok(),
298 accepted,
299 "{header}",
300 );
301 }
302 }
303}