Skip to content

NoMethodError in allow_listed? and disposable_domain? when address is unparseable #316

Description

@kwent

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions