debugbundle 1.0.0-alpha.1

Safe, bounded runtime error reporting for Rust, Axum and Actix Web
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! Browser relay ingress. Connected durable delivery is withheld until persisted.
#[cfg(any(feature = "axum", feature = "actix-web"))]
pub(crate) mod native;
mod security;
pub(crate) mod spool;
pub use spool::SpoolStatus;
pub(crate) mod validation;
use crate::{privacy::Privacy, Client};
use serde_json::{json, Value};
use std::{
    collections::BTreeMap,
    net::IpAddr,
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc, Mutex,
    },
    time::Instant,
};

/// A store owns its minute-window clock and must update each key atomically.
/// The SDK calls it on the existing bounded callback worker, with a finite wait.
pub trait RateLimitStore: Send + Sync + 'static {
    fn allow(&self, key: RateLimitKey) -> Result<bool, RateLimitError>;
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RateLimitKey {
    /// Digest bound to endpoint and project credentials; never the token itself.
    pub namespace: String,
    pub peer: Option<IpAddr>,
    pub limit_per_minute: u32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RateLimitError {
    Unavailable,
}

#[derive(Clone, Debug)]
pub struct RelayOptions {
    pub allowed_origins: Vec<String>,
    pub trusted_proxies: Vec<IpAddr>,
    pub rate_limit_per_minute: u32,
    pub durable_write: bool,
    pub service: Option<String>,
    pub environment: Option<String>,
}
impl Default for RelayOptions {
    fn default() -> Self {
        Self {
            allowed_origins: Vec::new(),
            trusted_proxies: Vec::new(),
            rate_limit_per_minute: 60,
            durable_write: true,
            service: None,
            environment: None,
        }
    }
}
#[derive(Default)]
pub struct RelayRequest {
    pub method: String,
    pub scheme: String,
    pub host: String,
    pub origin: Option<String>,
    pub referer: Option<String>,
    pub content_type: Option<String>,
    pub peer: Option<IpAddr>,
    pub forwarded_host: Option<String>,
    pub forwarded_proto: Option<String>,
    pub forwarded_for: Option<String>,
    pub evidence: bool,
}
pub struct RelayResponse {
    pub status: u16,
    pub headers: BTreeMap<String, String>,
    pub body: Option<Value>,
}
impl RelayResponse {
    fn status(status: u16) -> Self {
        Self {
            status,
            headers: BTreeMap::new(),
            body: None,
        }
    }
    fn malformed() -> Self {
        let mut response = Self::status(400);
        response.body = Some(
            json!({"accepted":0,"rejected":0,"errors":[{"index":-1,"reason":"malformed_payload"}]}),
        );
        response
    }
    fn cors(mut self, origin: &str) -> Self {
        self.headers
            .insert("access-control-allow-origin".into(), origin.into());
        self.headers.insert("vary".into(), "Origin".into());
        self
    }
}
struct Inner {
    client: Client,
    privacy: Privacy,
    options: RelayOptions,
    budget: Arc<IngressBudget>,
}
/// All native handlers for one client share ingress and IP accounting. Framework
/// worker factories must not multiply retained uploads or reset rate windows.
#[derive(Default)]
pub(crate) struct IngressBudget {
    limiter: Mutex<security::Limiter>,
    active: AtomicUsize,
    idle: tokio::sync::Notify,
}
struct Admission(Arc<Inner>);
impl Drop for Admission {
    fn drop(&mut self) {
        self.0.budget.active.fetch_sub(1, Ordering::AcqRel);
        self.0.budget.idle.notify_waiters();
    }
}
impl Inner {
    fn admit(self: &Arc<Self>) -> Option<Admission> {
        for _ in 0..32 {
            let active = self.budget.active.load(Ordering::Acquire);
            if active >= 8 {
                return None;
            }
            if self
                .budget
                .active
                .compare_exchange_weak(active, active + 1, Ordering::AcqRel, Ordering::Acquire)
                .is_ok()
            {
                return Some(Admission(self.clone()));
            }
        }
        None
    }
}
#[derive(Clone)]
pub struct Relay {
    inner: Option<Arc<Inner>>,
}
impl Relay {
    pub fn new(client: Client, mut options: RelayOptions) -> Self {
        let Some(privacy) = client.relay_privacy() else {
            return Self { inner: None };
        };
        let Some(budget) = client.relay_budget() else {
            return Self { inner: None };
        };
        if options.allowed_origins.len() > 64
            || options.trusted_proxies.len() > 16
            || options.rate_limit_per_minute == 0
            || options.rate_limit_per_minute > 10000
            || [&options.service, &options.environment]
                .iter()
                .any(|value| {
                    value
                        .as_ref()
                        .is_some_and(|value| !crate::config::technical_identity(value, &privacy))
                })
        {
            return Self { inner: None };
        }
        let Some(origins) = options
            .allowed_origins
            .iter()
            .map(|origin| crate::config::trace_origin(origin))
            .collect::<Option<Vec<_>>>()
        else {
            return Self { inner: None };
        };
        options.allowed_origins = origins;
        if options.durable_write {
            client.start_relay_spool();
        }
        Self {
            inner: Some(Arc::new(Inner {
                client,
                privacy,
                options,
                budget,
            })),
        }
    }
    pub fn status(&self) -> SpoolStatus {
        self.inner
            .as_ref()
            .map(|inner| inner.client.relay_spool_status())
            .unwrap_or_default()
    }
    pub async fn flush_async(&self, timeout: std::time::Duration) -> bool {
        if crate::runtime::internal() {
            return false;
        }
        let deadline = std::time::Instant::now() + timeout.min(std::time::Duration::from_secs(30));
        match &self.inner {
            Some(inner) => {
                loop {
                    let idle = inner.budget.idle.notified();
                    tokio::pin!(idle);
                    idle.as_mut().enable();
                    if inner.budget.active.load(Ordering::Acquire) == 0 {
                        break;
                    }
                    if tokio::time::timeout(
                        deadline.saturating_duration_since(std::time::Instant::now()),
                        idle,
                    )
                    .await
                    .is_err()
                    {
                        return false;
                    }
                }
                inner
                    .client
                    .flush_relay_spool(
                        deadline.saturating_duration_since(std::time::Instant::now()),
                    )
                    .await
            }
            None => false,
        }
    }
    /// Bounded request-side persistence is an explicit relay operation, not capture.
    pub async fn handle(&self, request: &RelayRequest, body: &[u8]) -> RelayResponse {
        match self.begin(request, Some(body.len() as u64)).await {
            Ok(admission) => admission.finish(body).await,
            Err(response) => response,
        }
    }
    pub(crate) async fn begin<'a>(
        &self,
        request: &'a RelayRequest,
        declared: Option<u64>,
    ) -> Result<RequestAdmission<'a>, RelayResponse> {
        if crate::runtime::internal() {
            return Err(RelayResponse::status(503));
        }
        let Some(inner) = &self.inner else {
            return Err(RelayResponse::status(503));
        };
        if !inner.client.relay_live() {
            return Err(RelayResponse::status(503));
        };
        let Some(origin) = security::origin(request, &inner.options) else {
            return Err(RelayResponse::status(403));
        };
        if request.method == "OPTIONS" {
            let mut response = RelayResponse::status(204);
            response.headers.insert(
                "access-control-allow-methods".into(),
                "POST, OPTIONS".into(),
            );
            response.headers.insert(
                "access-control-allow-headers".into(),
                "content-type,x-debugbundle-evidence".into(),
            );
            return Err(response.cors(&origin));
        }
        if request.method != "POST" {
            return Err(RelayResponse::status(405).cors(&origin));
        }
        if declared.is_some_and(|length| length > crate::wire::MAX_BODY_BYTES as u64) {
            return Err(RelayResponse::status(413).cors(&origin));
        }
        if !request.content_type.as_deref().is_some_and(|value| {
            value.len() <= 128
                && value
                    .split(';')
                    .next()
                    .is_some_and(|value| value.trim().eq_ignore_ascii_case("application/json"))
        }) {
            return Err(RelayResponse::malformed().cors(&origin));
        }
        let peer = security::peer(request, &inner.options);
        if !inner
            .budget
            .limiter
            .try_lock()
            .ok()
            .is_some_and(|mut limiter| {
                limiter.allow(peer, inner.options.rate_limit_per_minute, Instant::now())
            })
        {
            let mut response = RelayResponse::status(429);
            response.headers.insert("retry-after".into(), "60".into());
            return Err(response.cors(&origin));
        }
        let Some(permit) = inner.admit() else {
            return Err(RelayResponse::status(503).cors(&origin));
        };
        let admission = RequestAdmission {
            inner: inner.clone(),
            request,
            origin,
            _permit: permit,
        };
        match inner
            .client
            .relay_rate_limit(peer, inner.options.rate_limit_per_minute)
            .await
        {
            Ok(true) => {}
            Ok(false) => {
                let mut response = admission.failure(429);
                response.headers.insert("retry-after".into(), "60".into());
                return Err(response);
            }
            Err(()) => return Err(admission.failure(503)),
        }
        Ok(admission)
    }
    async fn process(inner: &Inner, request: &RelayRequest, body: &[u8]) -> RelayResponse {
        let deadline = Instant::now() + inner.client.relay_timeout();
        if body.len() > crate::wire::MAX_BODY_BYTES {
            return RelayResponse::status(413);
        }
        let Ok(input) = serde_json::from_slice::<Value>(body) else {
            return RelayResponse::malformed();
        };
        let Some(root) = input.as_object().filter(|root| root.len() == 1) else {
            return RelayResponse::malformed();
        };
        let Some(batch) = root
            .get("batch")
            .and_then(Value::as_array)
            .filter(|batch| batch.len() <= 100)
        else {
            return RelayResponse::malformed();
        };
        let features = inner.client.relay_features();
        let mut events = Vec::new();
        let mut errors = Vec::new();
        let mut legacy = Vec::new();
        for (index, candidate) in batch.iter().enumerate() {
            if let Some(event) =
                validation::event(candidate, &inner.privacy, &inner.options, &features)
            {
                match inner.client.relay_finalize(event, deadline).await {
                    Ok(Some(event)) => match validation::for_delivery(
                        &event,
                        &inner.privacy,
                        &inner.client.relay_features(),
                    ) {
                        Some(event) => events.push(event),
                        None => {
                            errors.push(json!({"index":index,"reason":"local_policy"}));
                            legacy.push(format!("batch[{index}]: Event withheld by local policy."));
                        }
                    },
                    Ok(None) => {
                        errors.push(json!({"index":index,"reason":"local_policy"}));
                        legacy.push(format!("batch[{index}]: Event withheld by local policy."));
                    }
                    Err(()) => return RelayResponse::status(503),
                }
            } else {
                errors.push(json!({"index":index,"reason":"invalid_event"}));
                legacy.push(if candidate.get("event_type").and_then(Value::as_str)==Some("backend_exception") {
                    format!("batch[{index}]: Unsupported browser relay event type backend_exception.")
                } else {
                    format!("batch[{index}]: invalid browser relay event payload.")
                });
            }
        }
        let accepted = events.len();
        if !events.is_empty()
            && !inner
                .client
                .relay_deliver(events, inner.options.durable_write)
                .await
        {
            return RelayResponse::status(503);
        }
        // Existing browsers opt into indexed outcomes through evidence_errors;
        // older relay clients retain the established mixed-batch 400 behavior.
        let status = if errors.is_empty() || request.evidence {
            202
        } else {
            400
        };
        let mut response = RelayResponse::status(status);
        let mut body = json!({"accepted":accepted,"rejected":errors.len(),"errors":legacy});
        if request.evidence {
            body["evidence_errors"] = json!(errors);
            body["evidence_capabilities"] = crate::wire::descriptor(&inner.client.relay_features());
        }
        response.body = Some(body);
        response
    }
}

pub(crate) struct RequestAdmission<'a> {
    inner: Arc<Inner>,
    request: &'a RelayRequest,
    origin: String,
    _permit: Admission,
}
impl RequestAdmission<'_> {
    fn failure(self, status: u16) -> RelayResponse {
        RelayResponse::status(status).cors(&self.origin)
    }
    pub(crate) async fn finish(self, body: &[u8]) -> RelayResponse {
        Relay::process(&self.inner, self.request, body)
            .await
            .cors(&self.origin)
    }
    #[cfg(any(feature = "axum", feature = "actix-web"))]
    pub(crate) fn reject(self, status: u16) -> RelayResponse {
        self.failure(status)
    }
}

/// Verified transport facts for an Axum TLS listener or custom connection adapter.
/// Populate this from the accepted socket, never from request headers.
#[derive(Clone, Copy, Debug, Default)]
pub struct RelayConnection {
    pub secure: bool,
    pub peer: Option<IpAddr>,
}