Skip to content

Commit

Permalink
Use relaxed parsing for DNS names (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdecimus committed Mar 11, 2024
1 parent a992707 commit 8be9cb4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
mail-auth 0.3.9
================================
- Use relaxed parsing for DNS names (#25)

mail-auth 0.3.8
================================
- Made `pct` field accessible.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "mail-auth"
description = "DKIM, ARC, SPF and DMARC library for Rust"
version = "0.3.8"
version = "0.3.9"
edition = "2021"
authors = [ "Stalwart Labs <hello@stalw.art>"]
license = "Apache-2.0 OR MIT"
Expand Down
30 changes: 23 additions & 7 deletions src/common/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use hickory_resolver::{
error::{ResolveError, ResolveErrorKind},
proto::rr::RecordType,
system_conf::read_system_conf,
AsyncResolver,
AsyncResolver, Name,
};

use crate::{
Expand Down Expand Up @@ -104,7 +104,7 @@ impl Resolver {
let mut result = vec![];
for record in self
.resolver
.txt_lookup(key.into_fqdn().as_ref())
.txt_lookup(Name::from_str_relaxed(key.into_fqdn().as_ref())?)
.await?
.as_lookup()
.record_iter()
Expand Down Expand Up @@ -133,7 +133,10 @@ impl Resolver {
return mock_resolve(key.as_ref());
}

let txt_lookup = self.resolver.txt_lookup(key.as_ref()).await?;
let txt_lookup = self
.resolver
.txt_lookup(Name::from_str_relaxed(key.as_ref())?)
.await?;
let mut result = Err(Error::InvalidRecordType);
let records = txt_lookup.as_lookup().record_iter().filter_map(|r| {
let txt_data = r.data()?.as_txt()?.txt_data();
Expand Down Expand Up @@ -174,7 +177,10 @@ impl Resolver {
return mock_resolve(key.as_ref());
}

let mx_lookup = self.resolver.mx_lookup(key.as_ref()).await?;
let mx_lookup = self
.resolver
.mx_lookup(Name::from_str_relaxed(key.as_ref())?)
.await?;
let mx_records = mx_lookup.as_lookup().records();
let mut records: Vec<MX> = Vec::with_capacity(mx_records.len());
for mx_record in mx_records {
Expand Down Expand Up @@ -214,7 +220,10 @@ impl Resolver {
return mock_resolve(key.as_ref());
}

let ipv4_lookup = self.resolver.ipv4_lookup(key.as_ref()).await?;
let ipv4_lookup = self
.resolver
.ipv4_lookup(Name::from_str_relaxed(key.as_ref())?)
.await?;
let ips: Vec<Ipv4Addr> = ipv4_lookup
.as_lookup()
.record_iter()
Expand All @@ -240,7 +249,10 @@ impl Resolver {
return mock_resolve(key.as_ref());
}

let ipv6_lookup = self.resolver.ipv6_lookup(key.as_ref()).await?;
let ipv6_lookup = self
.resolver
.ipv6_lookup(Name::from_str_relaxed(key.as_ref())?)
.await?;
let ips = ipv6_lookup
.as_lookup()
.record_iter()
Expand Down Expand Up @@ -341,7 +353,11 @@ impl Resolver {
}

let key = key.into_fqdn();
match self.resolver.lookup_ip(key.as_ref()).await {
match self
.resolver
.lookup_ip(Name::from_str_relaxed(key.as_ref())?)
.await
{
Ok(result) => Ok(result.as_lookup().record_iter().any(|r| {
r.data().map_or(false, |d| {
matches!(d.record_type(), RecordType::A | RecordType::AAAA)
Expand Down
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ use arc::Set;
use common::{crypto::HashAlgorithm, headers::Header, lru::LruCache, verify::DomainKey};
use dkim::{Atps, Canonicalization, DomainKeyReport};
use dmarc::Dmarc;
use hickory_resolver::{proto::op::ResponseCode, TokioAsyncResolver};
use hickory_resolver::{
proto::{error::ProtoError, op::ResponseCode},
TokioAsyncResolver,
};
use mta_sts::{MtaSts, TlsRpt};
use spf::{Macro, Spf};

Expand Down Expand Up @@ -570,6 +573,12 @@ impl From<io::Error> for Error {
}
}

impl From<ProtoError> for Error {
fn from(err: ProtoError) -> Self {
Error::DnsError(err.to_string())
}
}

#[cfg(feature = "rsa")]
impl From<rsa::errors::Error> for Error {
fn from(err: rsa::errors::Error) -> Self {
Expand Down

0 comments on commit 8be9cb4

Please sign in to comment.