Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix domain name length check in SPF verification #34

Merged
merged 2 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions resources/spf/basic.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
# If the <domain> is malformed (e.g., label longer than 63 characters,
# zero-length label not at the end, etc.) or is not a multi-label
# domain name, or if the DNS lookup returns "Name Error" (RCODE 3, also
# known as "NXDOMAIN" [RFC2308]), check_host() immediately returns the
# result "none".
# If the <domain> is malformed (e.g., label longer than 63 characters, total
# length longer than 255 characters, zero-length label not at the end, etc.) or
# is not a multi-label domain name, or if the DNS lookup returns "Name Error"
# (RCODE 3, also known as "NXDOMAIN" [RFC2308]), check_host() immediately
# returns the result "none".

name: Malformed Domains
records:
spf: extremely.ridiculously.long.domain.name.that.should.fail.immediately.com v=spf1 +all
spf: this.domain.name.is.extremely.long.because.we.want.to.explicitly.show.that.the.maximum.length.of.a.domain.name.is.255.characters.so.this.one.will.definitely.fail.immediately.due.to.its.excessive.length.and.ridiculously.large.number.of.characters.which.makes.it.invalid.com v=spf1 +all
spf: thislabelisjustoverthesixtythreecharacterlimitandshouldbeanerror.com v=spf1 +all
spf: nolabels v=spf1 +all
spf: none.test.org v=something-else not=spf for=sure
tests:
- domain: extremely.ridiculously.long.domain.name.that.should.fail.immediately.com
sender: sender@extremely.ridiculously.long.domain.name.that.should.fail.immediately.com
- domain: this.domain.name.is.extremely.long.because.we.want.to.explicitly.show.that.the.maximum.length.of.a.domain.name.is.255.characters.so.this.one.will.definitely.fail.immediately.due.to.its.excessive.length.and.ridiculously.large.number.of.characters.which.makes.it.invalid.com
sender: sender@this.domain.name.is.extremely.long.because.we.want.to.explicitly.show.that.the.maximum.length.of.a.domain.name.is.255.characters.so.this.one.will.definitely.fail.immediately.due.to.its.excessive.length.and.ridiculously.large.number.of.characters.which.makes.it.invalid.com
ip: 172.168.0.1
expect: none
- domain: thislabelisjustoverthesixtythreecharacterlimitandshouldbeanerror.com
sender: sender@thislabelisjustoverthesixtythreecharacterlimitandshouldbeanerror.com
ip: 172.168.0.1
expect: none
- domain: nolabels
Expand Down
24 changes: 16 additions & 8 deletions src/spf/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Resolver {
helo_domain: &str,
host_domain: &str,
) -> SpfOutput {
if helo_domain.has_labels() {
if helo_domain.has_valid_labels() {
self.check_host(
ip,
helo_domain,
Expand Down Expand Up @@ -88,7 +88,7 @@ impl Resolver {
sender: &str,
) -> SpfOutput {
let output = SpfOutput::new(domain.to_string());
if domain.is_empty() || domain.len() > 63 || !domain.has_labels() {
if domain.is_empty() || domain.len() > 255 || !domain.has_valid_labels() {
return output.with_result(SpfResult::None);
}
let mut vars = Variables::new();
Expand Down Expand Up @@ -495,24 +495,32 @@ impl LookupLimit {
}
}

pub trait HasLabels {
fn has_labels(&self) -> bool;
pub trait HasValidLabels {
fn has_valid_labels(&self) -> bool;
}

impl HasLabels for &str {
fn has_labels(&self) -> bool {
impl HasValidLabels for &str {
fn has_valid_labels(&self) -> bool {
let mut has_dots = false;
let mut has_chars = false;
let mut label_len = 0;
for ch in self.chars() {
label_len += 1;

if ch.is_alphanumeric() {
has_chars = true;
} else if ch == '.' {
has_dots = true;
label_len = 0;
}
if has_chars && has_dots {
return true;

if label_len > 63 {
return false;
}
}
if has_chars && has_dots {
return true;
}
false
}
}
Expand Down
Loading