eml-codec 0.4.0

Email enCOder DECoder in Rust. Support Internet Message Format and MIME (RFC 822, 5322, 2045, 2046, 2047, 2048, 2049, 6532).
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#[cfg(feature = "arbitrary")]
use arbitrary::Arbitrary;
use bounded_static::ToStatic;
use std::borrow::Cow;
use std::fmt;
#[cfg(feature = "tracing-recover")]
use tracing::warn;
#[cfg(feature = "tracing")]
use tracing::{span, Level};

use crate::header;
use crate::message;
use crate::mime;
use crate::part::{self, field::NaiveEntityFields, AnyPart};
use crate::raw_input::RawInput;
use crate::text::boundary::{boundary, Delimiter};
#[cfg(feature = "arbitrary")]
use crate::{arbitrary_utils::arbitrary_vec_nonempty, fuzz_eq::FuzzEq};

//--- Multipart
#[derive(Clone, PartialEq, ToStatic)]
#[cfg_attr(feature = "arbitrary", derive(FuzzEq))]
pub struct Multipart<'a> {
    pub mime: mime::MIME<'a, mime::r#type::Multipart<'a>>,
    // Invariant: `children` is non-empty
    pub children: Vec<AnyPart<'a>>,
    #[cfg_attr(feature = "arbitrary", fuzz_eq(ignore))]
    pub preamble: Cow<'a, [u8]>,
    #[cfg_attr(feature = "arbitrary", fuzz_eq(ignore))]
    pub epilogue: Cow<'a, [u8]>,
    pub raw_body: RawInput<'a>,
}
impl<'a> fmt::Debug for Multipart<'a> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct("part::Multipart")
            .field("mime", &self.mime)
            .field("children", &self.children)
            .field("preamble", &String::from_utf8_lossy(&self.preamble))
            .field("epilogue", &String::from_utf8_lossy(&self.epilogue))
            .field("raw_body", &self.raw_body)
            .finish()
    }
}
#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for Multipart<'a> {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        Ok(Multipart {
            mime: u.arbitrary()?,
            children: arbitrary_vec_nonempty(u)?,
            preamble: b"".into(),
            epilogue: b"".into(),
            raw_body: RawInput::none(),
        })
    }
}

// REQUIRES: `m.ctype.boundary` is `Some(_)`. This is guaranteed by
// the parser for `mime::MIME<_, Multipart>`.
pub fn multipart<'a>(
    m: mime::MIME<'a, mime::r#type::Multipart<'a>>,
) -> impl Fn(&'a [u8]) -> (&'a [u8], Multipart<'a>) {
    let m = m.clone();

    move |input| {
        #[cfg(feature = "tracing")]
        let _span = span!(Level::DEBUG, "part::composite::multipart", ?m).entered();

        let full_input = input;

        // init
        // NOTE: the `.unwrap()` cannot fail as long as `m` is produced by
        // the parser, which always specifies a `boundary` (the boundary
        // used by the input).
        let bound = m.ctype.boundary.as_ref().unwrap().as_bytes();
        let part_raw = part_raw(bound);
        let mut mparts: Vec<AnyPart> = vec![];

        // preamble
        let (mut input_loop, preamble) = part_raw(input);

        let (rest, mut multipart) = loop {
            let input = match boundary(bound)(input_loop) {
                Err(_) => {
                    // We encountered a malformed boundary, stop parsing.
                    let raw_body = &full_input[0..full_input.len() - input_loop.len()];
                    break (
                        input_loop,
                        Multipart {
                            mime: m.clone(),
                            children: mparts,
                            preamble: preamble.into(),
                            epilogue: [][..].into(),
                            raw_body: raw_body.into(),
                        },
                    );
                }
                Ok((inp, Delimiter::Last)) => {
                    break (
                        &[],
                        Multipart {
                            mime: m.clone(),
                            children: mparts,
                            preamble: preamble.into(),
                            epilogue: inp.into(),
                            raw_body: full_input.into(),
                        },
                    )
                }
                Ok((inp, Delimiter::Next)) => inp,
            };

            // parse mime headers, otherwise pick default mime
            let (input_body, fields_raw) = header::header_kv(input);
            let NaiveEntityFields { entries, mime } =
                fields_raw.into_iter().collect::<NaiveEntityFields>();

            // interpret mime according to context
            let mime = match m.ctype.subtype {
                mime::r#type::MultipartSubtype::Digest => {
                    mime.to_interpreted(mime::DefaultType::Digest)
                }
                _ => mime.to_interpreted(mime::DefaultType::Generic),
            };

            // parse raw part for the body
            let (input_next, rpart) = part_raw(input_body);

            // parse mime body
            // XXX this can be an (indirect) recursive call;
            // -> risk of stack overflow
            let mime_body = part::part_body(mime)(rpart);
            mparts.push(AnyPart {
                entries,
                mime_body,
                raw: input[0..input.len() - input_next.len()].into(),
                raw_headers: input[0..input.len() - input_body.len()].into(),
            });

            input_loop = input_next;
        };

        // RFC2064 specifies that a multipart must have at least one child part.
        // If there is no child part, insert an empty part as recovery strategy.
        if multipart.children.is_empty() {
            #[cfg(feature = "tracing-recover")]
            warn!("multipart containing zero parts");
            multipart.children.push(AnyPart::default());
        }

        (rest, multipart)
    }
}

// Recognizes bytes for the next part, until the next boundary or the end of the input.
fn part_raw<'a, 'b>(bound: &[u8]) -> impl Fn(&'a [u8]) -> (&'a [u8], &'a [u8]) + 'b {
    use memchr::memmem::Finder;
    // This low-level implementation (which basically just calls `memmem`) is faster
    // than trying to express this using parser combinators.

    // search for "--{bound}"
    let mut needle = b"--".to_vec();
    needle.extend(bound.iter());
    let finder = Finder::new(&needle).into_owned();

    move |input| {
        for i in finder.find_iter(input) {
            // a boundary can be at the beginning of the input
            if i == 0 {
                return (input, &[]);
            }

            // or it can be after a newline
            if i.checked_sub(1).is_some_and(|j| input[j] == b'\n') {
                // best-effort: recognize both \n and \r\n before the boundary
                let i = i
                    .checked_sub(2)
                    .filter(|j| input[*j] == b'\r')
                    .unwrap_or(i - 1);
                return (&input[i..], &input[0..i]);
            }
        }
        // no matching boundary found; return the entire input
        (&[], input)
    }
}

//--- Message

// Invariant: if message headers use non-ascii UTF-8, message subtype RFC822
// must not be used and subtype Global must be used instead.
#[derive(Clone, Debug, PartialEq, ToStatic)]
#[cfg_attr(feature = "arbitrary", derive(FuzzEq))]
pub struct Message<'a> {
    pub mime: mime::MIME<'a, mime::r#type::Message<'a>>,

    // NOTE: RFC2046 specifies that an encapsulated message "isn't restricted
    // to material in strict conformance to RFC822" and that it "could well be a
    // News article or a MIME message".
    //
    // This could be interpreted as saying that we shouldn't parse the embedded
    // message as a toplevel message. However, it is not clear whether there
    // actually exist embedded messages that are not actually toplevel messages.
    //
    // Additionally, IMAP requires that we are able to construct an IMF envelope
    // for an embedded message, and our AST for messages is able to handle missing
    // fields that wouldn't be strictly compliant.
    //
    // We thus decide to parse the contents as a toplevel message, i.e. an
    // `message::Message`, which involves interpreting IMF headers.
    pub child: Box<message::Message<'a>>,
    pub raw_body: RawInput<'a>,
}

#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for Message<'a> {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        let mut mime: mime::MIME<'a, mime::r#type::Message<'a>> = u.arbitrary()?;
        let child: Box<message::Message<'a>> = u.arbitrary()?;
        // TODO: clarify whether we should take the body into account as well, and
        // not just the headers (for later when we start interpreting bodies?)
        if matches!(mime.ctype.subtype, mime::r#type::MessageSubtype::RFC822)
            && child.contains_utf8_headers()
        {
            mime.ctype.subtype = mime::r#type::MessageSubtype::Global
        }
        Ok(Message {
            mime,
            child,
            raw_body: RawInput::none(),
        })
    }
}

/// Parse an embedded message.
///
/// This function always consumes its entire input.
pub fn message<'a>(
    m: mime::MIME<'a, mime::r#type::Message<'a>>,
) -> impl Fn(&'a [u8]) -> Message<'a> {
    move |input: &[u8]| {
        #[cfg(feature = "tracing")]
        let _span = span!(Level::DEBUG, "part::composite::message", ?m).entered();

        // parse the input as a toplevel message
        let msg = message::message(input);
        let mut msg_mime = m.clone();
        // If the headers contain non-ascii UTF8 and if this is a
        // message/RFC822, promote the message outer MIME to message/global
        if msg.contains_utf8_headers()
            && matches!(msg_mime.ctype.subtype, mime::r#type::MessageSubtype::RFC822)
        {
            msg_mime.ctype.subtype = mime::r#type::MessageSubtype::Global;
        }

        Message {
            mime: msg_mime,
            child: Box::new(msg),
            raw_body: input.into(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mime::field::Entry;
    use crate::part::discrete::Text;
    use crate::part::field::EntityEntry;
    use crate::part::{AnyPart, MimeBody};
    use crate::text::charset::EmailCharset;
    use pretty_assertions::assert_eq;

    #[test]
    fn test_preamble() {
        assert_eq!(
            part_raw(b"hello")(
                b"blip
bloup

blip
bloup--
--bim
--bim--

--hello
Field: Body
"
            ),
            (
                &b"\n--hello\nField: Body\n"[..],
                &b"blip\nbloup\n\nblip\nbloup--\n--bim\n--bim--\n"[..],
            )
        );
    }

    #[test]
    fn test_part_raw() {
        assert_eq!(
            part_raw(b"simple boundary")(b"Content-type: text/plain; charset=us-ascii

This is explicitly typed plain US-ASCII text.
It DOES end with a linebreak.

--simple boundary--
"),
            (
                &b"\n--simple boundary--\n"[..],
                &b"Content-type: text/plain; charset=us-ascii\n\nThis is explicitly typed plain US-ASCII text.\nIt DOES end with a linebreak.\n"[..],
            )
        );
    }

    #[test]
    fn test_multipart() {
        let base_mime = mime::MIME {
            ctype: mime::r#type::Multipart {
                subtype: mime::r#type::MultipartSubtype::Alternative,
                boundary: Some("simple boundary".to_string()),
                other_params: vec![],
            },
            fields: mime::CommonMIME::default(),
        };

        let input = b"This is the preamble.  It is to be ignored, though it
is a handy place for composition agents to include an
explanatory note to non-MIME conformant readers.

--simple boundary

This is implicitly typed plain US-ASCII text.
It does NOT end with a linebreak.
--simple boundary
Content-type: text/plain; charset=us-ascii

This is explicitly typed plain US-ASCII text.
It DOES end with a linebreak.

--simple boundary--

This is the epilogue. It is also to be ignored.
";

        let preamble = b"This is the preamble.  It is to be ignored, though it
is a handy place for composition agents to include an
explanatory note to non-MIME conformant readers.
";

        let epilogue = b"
This is the epilogue. It is also to be ignored.
";

        assert_eq!(
            multipart(base_mime.clone())(input),
            (&b""[..],
             Multipart {
                 mime: base_mime,
                 preamble: preamble.into(),
                 epilogue: epilogue.into(),
                 children: vec![
                     AnyPart {
                         entries: vec![],
                         mime_body: MimeBody::Txt(Text {
                             mime: mime::MIME {
                                 ctype: mime::r#type::Text::default(),
                                 fields: mime::CommonMIME::default(),
                             },
                             body: b"This is implicitly typed plain US-ASCII text.\nIt does NOT end with a linebreak.".into(),
                             raw_body: RawInput::between(input, b"This is implicitly", b"NOT end with a linebreak."),
                         }),
                         raw: RawInput::between(input, b"\nThis is implicitly", b"NOT end with a linebreak."),
                         raw_headers: b"\n".into(),
                     },
                     AnyPart {
                         entries: vec![EntityEntry::MIME { e: Entry::Type, raw_body: b" text/plain; charset=us-ascii".into() }],
                         mime_body: MimeBody::Txt(Text {
                             mime: mime::MIME {
                                 ctype: mime::r#type::Text {
                                     subtype: mime::r#type::TextSubtype::Plain,
                                     charset: EmailCharset::US_ASCII,
                                     other_params: vec![],
                                 },
                                 fields: mime::CommonMIME::default(),
                             },
                             body: b"This is explicitly typed plain US-ASCII text.\nIt DOES end with a linebreak.\n".into(),
                             raw_body: RawInput::between(input, b"This is explicitly", b"DOES end with a linebreak.\n"),
                         }),
                         raw: RawInput::between(input, b"Content-type", b"DOES end with a linebreak.\n"),
                         raw_headers: b"Content-type: text/plain; charset=us-ascii\n\n".into(),
                     },
                 ],
                 raw_body: input.into(),
             },
            )
        );
    }

    // The terminator of a multipart entity can be missing.
    // This should be properly handled even for nested multiparts
    // (RFC2046 specifies this in sec 5.1.2).
    #[test]
    fn test_nested_multipart_inner_broken() {
        let base_mime = mime::MIME {
            ctype: mime::r#type::Multipart {
                subtype: mime::r#type::MultipartSubtype::Mixed,
                boundary: Some("outer boundary".to_string()),
                other_params: vec![],
            },
            fields: mime::CommonMIME::default(),
        };

        let input = b"
--outer boundary
Content-Type: multipart/mixed; boundary=\"inner boundary\"

--inner boundary

This is the inner part; it misses its terminator
--outer boundary

This is implicitly typed plain US-ASCII text.
--outer boundary--";

        assert_eq!(
            multipart(base_mime.clone())(input),
            (
                &b""[..],
                Multipart {
                    mime: base_mime,
                    preamble: b"".into(),
                    epilogue: b"".into(),
                    children: vec![
                        AnyPart {
                            entries: vec![EntityEntry::MIME {
                                e: Entry::Type,
                                raw_body: b" multipart/mixed; boundary=\"inner boundary\"".into(),
                            },],
                            mime_body: MimeBody::Mult(Multipart {
                                mime: mime::MIME {
                                    ctype: mime::r#type::Multipart {
                                        subtype: mime::r#type::MultipartSubtype::Mixed,
                                        boundary: Some("inner boundary".to_string()),
                                        other_params: vec![],
                                    },
                                    fields: mime::CommonMIME::default(),
                                },
                                preamble: b"".into(),
                                epilogue: b"".into(),
                                children: vec![AnyPart {
                                    entries: vec![],
                                    mime_body: MimeBody::Txt(Text {
                                        mime: mime::MIME {
                                            ctype: mime::r#type::Text::default(),
                                            fields: mime::CommonMIME::default(),
                                        },
                                        body: b"This is the inner part; it misses its terminator"
                                            .into(),
                                        raw_body: RawInput::between(
                                            input,
                                            b"This is the inner",
                                            b"terminator"
                                        ),
                                    }),
                                    raw: RawInput::between(
                                        input,
                                        b"\nThis is the inner",
                                        b"terminator"
                                    ),
                                    raw_headers: b"\n".into(),
                                },],
                                raw_body: RawInput::between(
                                    input,
                                    b"--inner boundary\n\nThis is the inner",
                                    b"terminator"
                                ),
                            }),
                            raw: RawInput::between(input, b"Content-Type", b"terminator"),
                            raw_headers:
                                b"Content-Type: multipart/mixed; boundary=\"inner boundary\"\n\n"
                                    .into(),
                        },
                        AnyPart {
                            entries: vec![],
                            mime_body: MimeBody::Txt(Text {
                                mime: mime::MIME {
                                    ctype: mime::r#type::Text::default(),
                                    fields: mime::CommonMIME::default(),
                                },
                                body: b"This is implicitly typed plain US-ASCII text.".into(),
                                raw_body: b"This is implicitly typed plain US-ASCII text.".into(),
                            }),
                            raw: b"\nThis is implicitly typed plain US-ASCII text.".into(),
                            raw_headers: b"\n".into(),
                        },
                    ],
                    raw_body: input.into(),
                },
            )
        );
    }

    // Parsing stops on a broken boundary that starts with the correct boundary
    // but is followed by a suffix containing junk
    // FIXME: the RFC requires that we handle whitespace characters as a suffix,
    // but this is not done currently.
    #[test]
    fn test_broken_boundary() {
        let base_mime = mime::MIME {
            ctype: mime::r#type::Multipart {
                subtype: mime::r#type::MultipartSubtype::Mixed,
                boundary: Some("boundary".to_string()),
                other_params: vec![],
            },
            fields: mime::CommonMIME::default(),
        };

        let input = b"
--boundary

Part text
--boundary+++out of cheese

leftovers";

        assert_eq!(
            multipart(base_mime.clone())(input),
            (
                &b"\n--boundary+++out of cheese\n\nleftovers"[..],
                Multipart {
                    mime: base_mime,
                    preamble: b"".into(),
                    epilogue: b"".into(),
                    children: vec![AnyPart {
                        entries: vec![],
                        mime_body: MimeBody::Txt(Text {
                            mime: mime::MIME {
                                ctype: mime::r#type::Text::default(),
                                fields: mime::CommonMIME::default(),
                            },
                            body: b"Part text".into(),
                            raw_body: b"Part text".into(),
                        }),
                        raw: b"\nPart text".into(),
                        raw_headers: b"\n".into(),
                    },],
                    raw_body: b"\n--boundary\n\nPart text".into(),
                },
            )
        );
    }

    #[test]
    fn test_multipart_cr() {
        let base_mime = mime::MIME {
            ctype: mime::r#type::Multipart {
                subtype: mime::r#type::MultipartSubtype::Alternative,
                boundary: Some("boundary".to_string()),
                other_params: vec![],
            },
            fields: mime::CommonMIME::default(),
        };

        let input = b"--boundary

\r\r
--boundary--
";

        assert_eq!(
            multipart(base_mime.clone())(input),
            (
                &b""[..],
                Multipart {
                    mime: base_mime,
                    preamble: b"".into(),
                    epilogue: b"".into(),
                    children: vec![AnyPart {
                        entries: vec![],
                        mime_body: MimeBody::Txt(Text {
                            mime: mime::MIME {
                                ctype: mime::r#type::Text::default(),
                                fields: mime::CommonMIME::default(),
                            },
                            body: b"\r".into(),
                            raw_body: b"\r".into(),
                        }),
                        raw: b"\n\r".into(),
                        raw_headers: b"\n".into(),
                    },],
                    raw_body: input.into(),
                },
            )
        );
    }

    #[test]
    fn test_multipart_no_parts() {
        let base_mime = mime::MIME {
            ctype: mime::r#type::Multipart {
                subtype: mime::r#type::MultipartSubtype::Alternative,
                boundary: Some("boundary".to_string()),
                other_params: vec![],
            },
            fields: mime::CommonMIME::default(),
        };

        let input = b"--boundary--";

        assert_eq!(
            multipart(base_mime.clone())(input),
            (
                &b""[..],
                Multipart {
                    mime: base_mime,
                    preamble: b"".into(),
                    epilogue: b"".into(),
                    children: vec![AnyPart::default()],
                    raw_body: input.into(),
                },
            )
        );
    }
}