Description
allow_listed? and disposable_domain? raise NoMethodError: undefined method 'domain' for nil when called on an unparseable email address (e.g. "><test>").
Root cause
When Mail::Address.new raises a ParseError, the @address instance variable remains nil and @parse_error is set to true. However, allow_listed? and disposable_domain? access address.domain without first checking valid?:
# These methods don't guard — they crash on nil @address:
def allow_listed?
domain_is_in?(address.domain, ValidEmail2.allow_list) # address is nil → NoMethodError
end
def disposable_domain?
domain_is_in?(address.domain, ValidEmail2.disposable_emails) # same issue
end
# These methods DO guard correctly:
def deny_listed?
valid? && domain_is_in?(address.domain, ValidEmail2.deny_list) # ✅
end
def valid_mx?
return false unless valid? # ✅
# ...
end
Reproduction
require 'valid_email2'
require 'valid_email2/address'
a = ValidEmail2::Address.new('"><test>')
a.valid? # => false (correct)
a.allow_listed? # => NoMethodError: undefined method 'domain' for nil
Expected behavior
allow_listed? and disposable_domain? should return false for unparseable addresses, consistent with deny_listed? and valid_mx?.
Suggested fix
Add a valid? guard to both methods:
def allow_listed?
valid? && domain_is_in?(address.domain, ValidEmail2.allow_list)
end
def disposable_domain?
valid? && domain_is_in?(address.domain, ValidEmail2.disposable_emails)
end
Environment
- valid_email2 7.0.15
- Ruby 4.0.5
- mail gem 2.9.0
Description
allow_listed?anddisposable_domain?raiseNoMethodError: undefined method 'domain' for nilwhen called on an unparseable email address (e.g."><test>").Root cause
When
Mail::Address.newraises aParseError, the@addressinstance variable remainsniland@parse_erroris set totrue. However,allow_listed?anddisposable_domain?accessaddress.domainwithout first checkingvalid?:Reproduction
Expected behavior
allow_listed?anddisposable_domain?should returnfalsefor unparseable addresses, consistent withdeny_listed?andvalid_mx?.Suggested fix
Add a
valid?guard to both methods:Environment