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>
impl<'x> AuthenticatedMessage<'x>
Sourcepub fn canonicalized_dkim_headers(&self) -> Option<Vec<u8>>
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<'_>
impl AuthenticatedMessage<'_>
Sourcepub fn dkim2_return_path(&self) -> Option<&str>
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.
Sourcepub fn dkim2_dsn_domains(&self) -> impl Iterator<Item = &str>
pub fn dkim2_dsn_domains(&self) -> impl Iterator<Item = &str>
Returns the domains that may sign a DSN about this message (§16.2).
Sourcepub fn dkim2_dsn_aligned(&self, domain: &str) -> bool
pub fn dkim2_dsn_aligned(&self, domain: &str) -> bool
Returns true if domain may sign a DSN about this message (§16.2).
Source§impl<'x> AuthenticatedMessage<'x>
impl<'x> AuthenticatedMessage<'x>
Sourcepub fn parse(raw_message: &'x [u8]) -> Option<Self>
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.
Sourcepub fn from_parsed<'y>(
parsed: &'y Message<'x>,
raw_message: &'x [u8],
strict: bool,
) -> Self
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.
Sourcepub fn parse_with_opts(
raw_message: &'x [u8],
prepend_headers: Option<&'x [u8]>,
strict: bool,
) -> Option<Self>
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.
Sourcepub fn received_headers_count(&self) -> usize
pub fn received_headers_count(&self) -> usize
Returns the number of Received headers in the message.
Sourcepub fn has_message_id_header(&self) -> bool
pub fn has_message_id_header(&self) -> bool
Returns true if the message has a Message-ID header.
Sourcepub fn has_date_header(&self) -> bool
pub fn has_date_header(&self) -> bool
Returns true if the message has a Date header.
Sourcepub fn raw_message(&self) -> &[u8] ⓘ
pub fn raw_message(&self) -> &[u8] ⓘ
Returns the raw message bytes, headers and body.
Sourcepub fn raw_headers(&self) -> &[u8] ⓘ
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.
Sourcepub fn headers(&self) -> &[(&'x [u8], &'x [u8])]
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.
Sourcepub fn raw_body(&self) -> &[u8] ⓘ
pub fn raw_body(&self) -> &[u8] ⓘ
Returns the raw message body. Empty when the message has no body.
Sourcepub fn body_offset(&self) -> usize
pub fn body_offset(&self) -> usize
Returns the byte offset of the body within
raw_message.
Sourcepub fn from_addresses(&self) -> &[String]
pub fn from_addresses(&self) -> &[String]
Returns the lowercased addresses found in the From header, including
the members of address groups.
Sourcepub fn first_from_address(&self) -> &str
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).
Sourcepub fn dkim_signatures(&self) -> &[Header<'x, Signature>]
pub fn dkim_signatures(&self) -> &[Header<'x, Signature>]
Returns the DKIM-Signature headers (RFC 6376) that parsed
successfully, in message order.
Sourcepub fn dkim2_signatures(&self) -> &[Header<'x, Signature>]
pub fn dkim2_signatures(&self) -> &[Header<'x, Signature>]
Returns the DKIM2-Signature headers that parsed successfully, sorted
by signature sequence number (i= tag).
Sourcepub fn dkim2_instances(&self) -> &[Header<'x, MessageInstance>]
pub fn dkim2_instances(&self) -> &[Header<'x, MessageInstance>]
Returns the Message-Instance headers that parsed successfully,
sorted by revision number (m= tag).
Sourcepub fn errors(&self) -> &[Header<'x, Error>]
pub fn errors(&self) -> &[Header<'x, Error>]
Returns the authentication headers that could not be parsed or were rejected, each paired with the reason.
Sourcepub fn has_dkim_errors(&self) -> bool
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).
Sourcepub fn has_dkim2_errors(&self) -> bool
pub fn has_dkim2_errors(&self) -> bool
Returns true if a DKIM2-Signature or Message-Instance header
failed to parse.
Sourcepub fn retain_dkim_signatures(
&mut self,
accept: impl FnMut(&Signature) -> Result<(), DkimError>,
)
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>
impl<'x> AsRef<AuthenticatedMessage<'x>> for AuthenticatedMessage<'x>
Source§fn as_ref(&self) -> &AuthenticatedMessage<'x>
fn as_ref(&self) -> &AuthenticatedMessage<'x>
Source§impl<'x> Clone for AuthenticatedMessage<'x>
impl<'x> Clone for AuthenticatedMessage<'x>
Source§impl<'x> Debug for AuthenticatedMessage<'x>
impl<'x> Debug for AuthenticatedMessage<'x>
Source§impl<'x> Default for AuthenticatedMessage<'x>
impl<'x> Default for AuthenticatedMessage<'x>
impl<'x> Eq for AuthenticatedMessage<'x>
Source§impl<'x> From<&'x AuthenticatedMessage<'x>> for Parameters<'x, &'x AuthenticatedMessage<'x>, NoCache>
impl<'x> From<&'x AuthenticatedMessage<'x>> for Parameters<'x, &'x AuthenticatedMessage<'x>, NoCache>
Source§fn from(params: &'x AuthenticatedMessage<'x>) -> Self
fn from(params: &'x AuthenticatedMessage<'x>) -> Self
Source§impl<'x> PartialEq for AuthenticatedMessage<'x>
impl<'x> PartialEq for AuthenticatedMessage<'x>
impl<'x> StructuralPartialEq for AuthenticatedMessage<'x>
Auto Trait Implementations§
impl<'x> Freeze for AuthenticatedMessage<'x>
impl<'x> RefUnwindSafe for AuthenticatedMessage<'x>
impl<'x> Send for AuthenticatedMessage<'x>
impl<'x> Sync for AuthenticatedMessage<'x>
impl<'x> Unpin for AuthenticatedMessage<'x>
impl<'x> UnsafeUnpin for AuthenticatedMessage<'x>
impl<'x> UnwindSafe for AuthenticatedMessage<'x>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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