From 137b19781e720ced9d1f58e737fbafefc412e559 Mon Sep 17 00:00:00 2001 From: Benjamin Bock Date: Mon, 4 Sep 2023 21:41:54 +0200 Subject: [PATCH] Add support for null MX rfc7505 --- CHANGELOG.md | 3 +++ lib/valid_email2/address.rb | 6 ++++++ spec/valid_email2_spec.rb | 10 ++++++++++ 3 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 98761e5..57b7559 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## unreleased +* support null MX rfc7505 #206 + ## Version 4.0.6 * Remove false positives https://github.com/micke/valid_email2/pull/200 * Remove unused default option https://github.com/micke/valid_email2/pull/201 diff --git a/lib/valid_email2/address.rb b/lib/valid_email2/address.rb index b9396ae..ef692a1 100644 --- a/lib/valid_email2/address.rb +++ b/lib/valid_email2/address.rb @@ -91,12 +91,14 @@ def blacklisted? def valid_mx? return false unless valid? + return false if null_mx? mx_or_a_servers.any? end def valid_strict_mx? return false unless valid? + return false if null_mx? mx_servers.any? end @@ -138,6 +140,10 @@ def mx_servers end end + def null_mx? + mx_servers.length == 1 && mx_servers.first.preference == 0 && mx_servers.first.exchange.length == 0 + end + def mx_or_a_servers @mx_or_a_servers ||= Resolv::DNS.open do |dns| dns.timeouts = @dns_timeout diff --git a/spec/valid_email2_spec.rb b/spec/valid_email2_spec.rb index 2a5478f..550150a 100644 --- a/spec/valid_email2_spec.rb +++ b/spec/valid_email2_spec.rb @@ -255,6 +255,11 @@ def set_whitelist user = TestUserMX.new(email: "foo@subdomain.gmail.com") expect(user.valid?).to be_falsey end + + it "is invalid if a null mx is found" do + user = TestUserMX.new(email: "foo@gmail.de") + expect(user.valid?).to be_falsey + end end describe "with strict mx validation" do @@ -272,6 +277,11 @@ def set_whitelist user = TestUserStrictMX.new(email: "foo@subdomain.gmail.com") expect(user.valid?).to be_falsey end + + it "is invalid if a null mx is found" do + user = TestUserMX.new(email: "foo@gmail.de") + expect(user.valid?).to be_falsey + end end describe "with dotted validation" do