mailrs-inbound 1.1.0

Composable SMTP receive pipeline framework — Stage trait + early-reject executor + pure decision logic + RFC 8601 Authentication-Results helpers. Framework-only; bring your own greylist / DKIM / virus-scan / scoring stages.
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
//! The [`Pipeline`] executor + its builder.
//!
//! A `Pipeline` is a sequence of [`Stage`]s plus a spam-score threshold.
//! [`Pipeline::run`] evaluates the stages in order on a [`ReceiveContext`],
//! short-circuiting on the first `Decide` outcome. If every stage returns
//! `Continue`, the final decision is computed by
//! [`make_delivery_decision`] over the accumulated signals.

use crate::context::ReceiveContext;
use crate::decision::{make_delivery_decision, DeliveryDecision};
use crate::stage::{Stage, StageOutcome};

/// Default spam-score threshold above which the message goes to Junk.
///
/// Picked to be sensitive enough to catch obvious bulk mail (content score
/// 3-4 + tracking pixel + non-FCrDNS sender) but not so aggressive that
/// legitimate mail with one minor signal gets junked.
pub const DEFAULT_SPAM_THRESHOLD: f64 = 5.0;

/// Composable receive pipeline.
///
/// Build via [`Pipeline::builder`], then call [`Pipeline::run`] for each
/// inbound message. A single `Pipeline` is shareable across requests
/// (typically `Arc<Pipeline>`).
pub struct Pipeline {
    stages: Vec<Box<dyn Stage>>,
    spam_threshold: f64,
}

impl Pipeline {
    /// Begin building a pipeline.
    pub fn builder() -> PipelineBuilder {
        PipelineBuilder {
            stages: Vec::new(),
            spam_threshold: DEFAULT_SPAM_THRESHOLD,
        }
    }

    /// Run the pipeline on `ctx`. Stages are evaluated in the order they
    /// were added; the first `Decide(_)` short-circuits.
    ///
    /// If every stage returns `Continue`, the final decision is computed
    /// from the accumulated signals via
    /// [`make_delivery_decision`](crate::make_delivery_decision).
    ///
    /// **Tracing.** Emits one `info_span!("inbound.pipeline", n_stages, …)`
    /// for the whole run + one nested `debug_span!("inbound.stage",
    /// name=…)` per evaluated stage. Each per-stage future is attached
    /// via `tracing::Instrument` so the span correctly survives `.await`
    /// suspension. If a `tracing-subscriber` is set up and the caller's
    /// connection handler is itself in a span (e.g. `smtp.conn`), the
    /// pipeline span nests under it automatically.
    #[tracing::instrument(
        name = "inbound.pipeline",
        skip(self, ctx),
        fields(n_stages = self.stages.len(), spam_threshold = self.spam_threshold),
    )]
    pub async fn run(&self, ctx: &mut ReceiveContext) -> DeliveryDecision {
        use tracing::Instrument;
        for stage in &self.stages {
            let stage_span = tracing::debug_span!("inbound.stage", name = stage.name());
            let outcome = stage.evaluate(ctx).instrument(stage_span).await;
            if let StageOutcome::Decide(d) = outcome {
                tracing::debug!(stage = stage.name(), "pipeline short-circuit");
                return d;
            }
        }
        let decision = make_delivery_decision(&ctx.to_pipeline_input(self.spam_threshold));
        tracing::debug!(?decision, "pipeline ran to completion");
        decision
    }

    /// Iterate over stage names, in order. Useful for introspection /
    /// debug logging.
    pub fn stage_names(&self) -> impl Iterator<Item = &str> {
        self.stages.iter().map(|s| s.name())
    }

    /// The configured spam-score threshold.
    pub fn spam_threshold(&self) -> f64 {
        self.spam_threshold
    }
}

/// Builder for [`Pipeline`]. Chain `.add(...)` calls then `.build()`.
pub struct PipelineBuilder {
    stages: Vec<Box<dyn Stage>>,
    spam_threshold: f64,
}

impl PipelineBuilder {
    /// Append a stage. Stages run in the order they were added.
    #[allow(clippy::should_implement_trait)]
    pub fn add<S: Stage + 'static>(mut self, stage: S) -> Self {
        self.stages.push(Box::new(stage));
        self
    }

    /// Override the spam-score threshold (default: [`DEFAULT_SPAM_THRESHOLD`]).
    pub fn spam_threshold(mut self, threshold: f64) -> Self {
        self.spam_threshold = threshold;
        self
    }

    /// Finish the builder and return the [`Pipeline`].
    pub fn build(self) -> Pipeline {
        Pipeline {
            stages: self.stages,
            spam_threshold: self.spam_threshold,
        }
    }
}

#[cfg(test)]
mod tests {
    use std::net::{IpAddr, Ipv4Addr};
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;

    use async_trait::async_trait;

    use super::*;
    use crate::context::DmarcPolicy;

    fn ctx() -> ReceiveContext {
        ReceiveContext::new(
            IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
            "ehlo",
            "a@x",
            "b@x",
            b"body".to_vec(),
            "mx",
        )
    }

    struct RecordingStage {
        name: &'static str,
        counter: Arc<AtomicUsize>,
        outcome: StageOutcome,
    }

    #[async_trait]
    impl Stage for RecordingStage {
        fn name(&self) -> &str {
            self.name
        }
        async fn evaluate(&self, _ctx: &mut ReceiveContext) -> StageOutcome {
            self.counter.fetch_add(1, Ordering::SeqCst);
            self.outcome.clone()
        }
    }

    struct ScoringStage(f64);

    #[async_trait]
    impl Stage for ScoringStage {
        fn name(&self) -> &str {
            "scoring"
        }
        async fn evaluate(&self, ctx: &mut ReceiveContext) -> StageOutcome {
            ctx.content_score += self.0;
            StageOutcome::Continue
        }
    }

    #[tokio::test]
    async fn empty_pipeline_falls_through_to_accept() {
        let p = Pipeline::builder().build();
        let mut c = ctx();
        c.auth_results.dmarc_policy = DmarcPolicy::Pass;
        match p.run(&mut c).await {
            DeliveryDecision::Accept { .. } => {}
            other => panic!("expected Accept, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn stages_run_in_order_when_all_continue() {
        let counter = Arc::new(AtomicUsize::new(0));
        let p = Pipeline::builder()
            .add(RecordingStage {
                name: "first",
                counter: counter.clone(),
                outcome: StageOutcome::Continue,
            })
            .add(RecordingStage {
                name: "second",
                counter: counter.clone(),
                outcome: StageOutcome::Continue,
            })
            .add(RecordingStage {
                name: "third",
                counter: counter.clone(),
                outcome: StageOutcome::Continue,
            })
            .build();
        let mut c = ctx();
        p.run(&mut c).await;
        assert_eq!(counter.load(Ordering::SeqCst), 3);
    }

    #[tokio::test]
    async fn early_decide_short_circuits_remaining_stages() {
        let counter = Arc::new(AtomicUsize::new(0));
        let p = Pipeline::builder()
            .add(RecordingStage {
                name: "first",
                counter: counter.clone(),
                outcome: StageOutcome::Continue,
            })
            .add(RecordingStage {
                name: "second",
                counter: counter.clone(),
                outcome: StageOutcome::Decide(DeliveryDecision::Greylist),
            })
            .add(RecordingStage {
                name: "third_should_not_run",
                counter: counter.clone(),
                outcome: StageOutcome::Continue,
            })
            .build();
        let mut c = ctx();
        assert_eq!(p.run(&mut c).await, DeliveryDecision::Greylist);
        assert_eq!(
            counter.load(Ordering::SeqCst),
            2,
            "stage 3 must not have run"
        );
    }

    #[tokio::test]
    async fn stages_can_mutate_context_signals() {
        let p = Pipeline::builder()
            .add(ScoringStage(3.0))
            .add(ScoringStage(2.5))
            .spam_threshold(5.0)
            .build();
        let mut c = ctx();
        c.auth_results.dmarc_policy = DmarcPolicy::Pass;
        match p.run(&mut c).await {
            DeliveryDecision::Junk { reason, .. } => {
                // 3.0 + 2.5 = 5.5 ≥ 5.0
                assert!(reason.contains("5.5"));
            }
            other => panic!("expected Junk, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn spam_threshold_default_is_five() {
        let p = Pipeline::builder().build();
        assert_eq!(p.spam_threshold(), DEFAULT_SPAM_THRESHOLD);
        assert_eq!(p.spam_threshold(), 5.0);
    }

    #[tokio::test]
    async fn custom_spam_threshold_is_threaded_through() {
        let p = Pipeline::builder()
            .add(ScoringStage(4.5))
            .spam_threshold(10.0) // raise the bar
            .build();
        let mut c = ctx();
        c.auth_results.dmarc_policy = DmarcPolicy::Pass;
        // 4.5 < 10.0, should accept
        assert!(matches!(
            p.run(&mut c).await,
            DeliveryDecision::Accept { .. }
        ));
    }

    #[tokio::test]
    async fn stage_names_introspection() {
        let counter = Arc::new(AtomicUsize::new(0));
        let p = Pipeline::builder()
            .add(RecordingStage {
                name: "alpha",
                counter: counter.clone(),
                outcome: StageOutcome::Continue,
            })
            .add(RecordingStage {
                name: "beta",
                counter,
                outcome: StageOutcome::Continue,
            })
            .build();
        let names: Vec<&str> = p.stage_names().collect();
        assert_eq!(names, vec!["alpha", "beta"]);
    }

    // ===== Additional corner-case tests =====

    #[tokio::test]
    async fn mixed_continue_and_decide_short_circuits_only_at_decide() {
        // Pattern: continue, continue, decide(reject), continue
        // Only first three stages should run.
        let counter = Arc::new(AtomicUsize::new(0));
        let p = Pipeline::builder()
            .add(RecordingStage {
                name: "a",
                counter: counter.clone(),
                outcome: StageOutcome::Continue,
            })
            .add(RecordingStage {
                name: "b",
                counter: counter.clone(),
                outcome: StageOutcome::Continue,
            })
            .add(RecordingStage {
                name: "c-decides",
                counter: counter.clone(),
                outcome: StageOutcome::Decide(DeliveryDecision::Reject {
                    code: 550,
                    message: "5.7.1 blocked".into(),
                }),
            })
            .add(RecordingStage {
                name: "d-never-runs",
                counter: counter.clone(),
                outcome: StageOutcome::Continue,
            })
            .build();
        let mut c = ctx();
        let decision = p.run(&mut c).await;
        match decision {
            DeliveryDecision::Reject { code, .. } => assert_eq!(code, 550),
            other => panic!("expected Reject, got {other:?}"),
        }
        assert_eq!(counter.load(Ordering::SeqCst), 3, "only 3 stages ran (a,b,c-decides)");
    }

    #[tokio::test]
    async fn stage_names_match_add_order_exactly() {
        // Verify stage_names preserves insertion order (Vec semantics).
        let counter = Arc::new(AtomicUsize::new(0));
        let p = Pipeline::builder()
            .add(RecordingStage { name: "rate-limit", counter: counter.clone(), outcome: StageOutcome::Continue })
            .add(RecordingStage { name: "ptr", counter: counter.clone(), outcome: StageOutcome::Continue })
            .add(RecordingStage { name: "spf", counter: counter.clone(), outcome: StageOutcome::Continue })
            .add(RecordingStage { name: "dkim", counter, outcome: StageOutcome::Continue })
            .build();
        let names: Vec<&str> = p.stage_names().collect();
        assert_eq!(names, vec!["rate-limit", "ptr", "spf", "dkim"]);
    }

    #[tokio::test]
    async fn spam_threshold_set_before_stages_still_applies() {
        // Builder allows interleaving spam_threshold() and add() calls.
        let p = Pipeline::builder()
            .spam_threshold(2.0) // set first
            .add(ScoringStage(2.5))
            .build();
        assert_eq!(p.spam_threshold(), 2.0);
        let mut c = ctx();
        c.auth_results.dmarc_policy = DmarcPolicy::Pass;
        // 2.5 >= 2.0 → Junk
        match p.run(&mut c).await {
            DeliveryDecision::Junk { .. } => {}
            other => panic!("expected Junk, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn spam_threshold_set_twice_takes_last_value() {
        let p = Pipeline::builder()
            .spam_threshold(2.0)
            .spam_threshold(10.0) // overrides 2.0
            .build();
        assert_eq!(p.spam_threshold(), 10.0);
    }

    #[tokio::test]
    async fn first_stage_decide_skips_all_other_stages() {
        // Decide on the first stage skips everything else.
        let counter = Arc::new(AtomicUsize::new(0));
        let p = Pipeline::builder()
            .add(RecordingStage {
                name: "first-decides",
                counter: counter.clone(),
                outcome: StageOutcome::Decide(DeliveryDecision::Greylist),
            })
            .add(RecordingStage {
                name: "never",
                counter: counter.clone(),
                outcome: StageOutcome::Continue,
            })
            .add(RecordingStage {
                name: "also-never",
                counter: counter.clone(),
                outcome: StageOutcome::Continue,
            })
            .build();
        let mut c = ctx();
        assert_eq!(p.run(&mut c).await, DeliveryDecision::Greylist);
        assert_eq!(counter.load(Ordering::SeqCst), 1);
    }

    #[tokio::test]
    async fn last_stage_decide_short_circuits_default_branch() {
        // Even on the last stage, Decide overrides make_delivery_decision.
        let counter = Arc::new(AtomicUsize::new(0));
        let p = Pipeline::builder()
            .add(RecordingStage {
                name: "first",
                counter: counter.clone(),
                outcome: StageOutcome::Continue,
            })
            .add(RecordingStage {
                name: "last-decides",
                counter: counter.clone(),
                outcome: StageOutcome::Decide(DeliveryDecision::Greylist),
            })
            .build();
        let mut c = ctx();
        // Default branch would Accept; the last-stage Decide must win.
        assert_eq!(p.run(&mut c).await, DeliveryDecision::Greylist);
    }
}