Skip to main content

AuthenticatedMessage

Struct AuthenticatedMessage 

Source
pub struct AuthenticatedMessage<'x> { /* private fields */ }
Expand description

An RFC 5322 message prepared for DKIM, DKIM2, ARC and DMARC verification.

The message borrows the raw bytes it was parsed from. Parsing records the position of every header, the From addresses (used for DMARC, RFC 9989), the number of Received headers and the presence of Date and Message-ID. Authentication headers that fail to parse do not abort parsing: they are recorded in errors and flagged by has_dkim_errors, has_dkim2_errors and has_arc_errors.

§Example

use mail_auth::AuthenticatedMessage;

let raw = b"From: jdoe@example.org\r\nSubject: Hi\r\n\r\nHello\r\n";
let message = AuthenticatedMessage::parse(raw).unwrap();
assert_eq!(message.first_from_address(), "jdoe@example.org");
assert_eq!(message.raw_body(), b"Hello\r\n");

Implementations§

Source§

impl<'x> AuthenticatedMessage<'x>

Source

pub fn canonicalized_dkim_headers(&self) -> Option<Vec<u8>>

Returns the canonicalized signed header fields of the first DKIM-Signature whose x= tag is absent or later than its t= tag.

The output is the header hash input of RFC 6376, Section 3.7: the headers listed in h=, followed by the DKIM-Signature header itself with an empty b= value, canonicalized with the signature’s header canonicalization. The current time is not checked. Returns None when the message has no such signature.

Source§

impl AuthenticatedMessage<'_>

Source

pub fn dkim2_return_path(&self) -> Option<&str>

Returns the address a DSN about this message must be sent to.

This is the mf= (MAIL FROM) of the signature with the highest i=, including angle brackets. Returns None if the message has no DKIM2 signature, if that signature uses nd=, or if its MAIL FROM is the null reverse-path <>. Signatures that failed to parse are not considered, so when has_dkim2_errors is true the address may come from an older hop.

Source

pub fn dkim2_dsn_domains(&self) -> impl Iterator<Item = &str>

Returns the domains that may sign a DSN about this message (§16.2).

Source

pub fn dkim2_dsn_aligned(&self, domain: &str) -> bool

Returns true if domain may sign a DSN about this message (§16.2).

Source

pub fn dkim2_arrival_headers(&self, hops: usize) -> Vec<u8> ⓘ

Returns the header block as it was before the last hops DKIM2 signers handled it, for the returned part of a DSN (§16.3).

Source§

impl<'x> AuthenticatedMessage<'x>

Source

pub fn parse(raw_message: &'x [u8]) -> Option<Self>

Parses a raw RFC 5322 message in strict mode.

Equivalent to parse_with_opts(raw_message, None, true). Returns None when no header could be parsed.

Source

pub fn from_parsed<'y>( parsed: &'y Message<'x>, raw_message: &'x [u8], strict: bool, ) -> Self

Builds an AuthenticatedMessage from a message already parsed by mail_parser, reusing its header offsets instead of parsing again.

raw_message must be the exact bytes parsed was parsed from. Only the headers of the root part are read. strict has the same meaning as in parse_with_opts.

Source

pub fn parse_with_opts( raw_message: &'x [u8], prepend_headers: Option<&'x [u8]>, strict: bool, ) -> Option<Self>

Parses a raw RFC 5322 message.

prepend_headers, when set, holds header lines that are parsed as if they appeared before the headers of raw_message (for example, headers added by the receiving MTA but not yet written to the message).

When strict is true, DKIM-Signature and ARC-Message-Signature headers that carry a body length tag (l=) are rejected and recorded as errors (DkimError::BodyLengthTag for DKIM, ArcError::BodyLengthTag for ARC), because a length limit lets an attacker append content to a signed body (RFC 6376 Section 8.2). When false, such signatures are kept.

Returns None when no header could be parsed.

Source

pub fn received_headers_count(&self) -> usize

Returns the number of Received headers in the message.

Source

pub fn has_message_id_header(&self) -> bool

Returns true if the message has a Message-ID header.

Source

pub fn has_date_header(&self) -> bool

Returns true if the message has a Date header.

Source

pub fn raw_message(&self) -> &[u8] ⓘ

Returns the raw message bytes, headers and body.

Source

pub fn raw_headers(&self) -> &[u8] ⓘ

Returns the raw header section of the message, including the blank line that separates it from the body when one is present. Prepended headers are not included.

Source

pub fn headers(&self) -> &[(&'x [u8], &'x [u8])]

Returns every header as a (name, value) pair of raw bytes, in message order. Prepended headers come first. The name excludes the colon; the value keeps its folding and the trailing line ending.

Source

pub fn raw_body(&self) -> &[u8] ⓘ

Returns the raw message body. Empty when the message has no body.

Source

pub fn body_offset(&self) -> usize

Returns the byte offset of the body within raw_message.

Source

pub fn from_addresses(&self) -> &[String]

Returns the lowercased addresses found in the From header, including the members of address groups.

Source

pub fn first_from_address(&self) -> &str

Returns the first From address, or an empty string if there is none. This is the author address used for DMARC (RFC 9989).

Source

pub fn dkim_signatures(&self) -> &[Header<'x, Signature>]

Returns the DKIM-Signature headers (RFC 6376) that parsed successfully, in message order.

Source

pub fn dkim2_signatures(&self) -> &[Header<'x, Signature>]

Returns the DKIM2-Signature headers that parsed successfully, sorted by signature sequence number (i= tag).

Source

pub fn dkim2_instances(&self) -> &[Header<'x, MessageInstance>]

Returns the Message-Instance headers that parsed successfully, sorted by revision number (m= tag).

Source

pub fn errors(&self) -> &[Header<'x, Error>]

Returns the authentication headers that could not be parsed or were rejected, each paired with the reason.

Source

pub fn has_dkim_errors(&self) -> bool

Returns true if a DKIM-Signature header failed to parse or was rejected (including by retain_dkim_signatures).

Source

pub fn has_dkim2_errors(&self) -> bool

Returns true if a DKIM2-Signature or Message-Instance header failed to parse.

Source

pub fn retain_dkim_signatures( &mut self, accept: impl FnMut(&Signature) -> Result<(), DkimError>, )

Keeps only the DKIM signatures accepted by accept.

accept is called once for every parsed DKIM-Signature. Signatures for which it returns Err are removed from dkim_signatures; the error is added to errors as Error::Dkim and has_dkim_errors becomes true, so verify_dkim reports each rejected signature as neutral with that error. Use it to apply a local policy, such as rejecting rsa-sha1 signatures, before verification.

Trait Implementations§

Source§

impl<'x> AsRef<AuthenticatedMessage<'x>> for AuthenticatedMessage<'x>

Source§

fn as_ref(&self) -> &AuthenticatedMessage<'x>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'x> Clone for AuthenticatedMessage<'x>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'x> Debug for AuthenticatedMessage<'x>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'x> Default for AuthenticatedMessage<'x>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'x> Eq for AuthenticatedMessage<'x>

Source§

impl<'x> From<&'x AuthenticatedMessage<'x>> for Parameters<'x, &'x AuthenticatedMessage<'x>, NoCache>

Source§

fn from(params: &'x AuthenticatedMessage<'x>) -> Self

Converts to this type from the input type.
Source§

impl<'x> PartialEq for AuthenticatedMessage<'x>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<'x> StructuralPartialEq for AuthenticatedMessage<'x>

Source§

impl<'x> TryFrom<&'x Vec<u8>> for AuthenticatedMessage<'x>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: &'x Vec<u8>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'x> TryFrom<&'x [u8]> for AuthenticatedMessage<'x>

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: &'x [u8]) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more