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

set a DNS timeout #181

Merged
merged 1 commit into from
Jun 29, 2021
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,15 @@ To validate that the domain has an MX record or A record:
```ruby
validates :email, 'valid_email_2/email': { mx: true }
```

To validate strictly that the domain has an MX record:
```ruby
validates :email, 'valid_email_2/email': { strict_mx: true }
```
`strict_mx` and `mx` both default to a 5 second timeout for DNS lookups. To
override this timeout, specify a `dns_timeout` option:
```ruby
validates :email, 'valid_email_2/email': { strict_mx: true, dns_timeout: 10 }

To validate that the domain is not a disposable email (checks domain and MX server):
```ruby
Expand Down
5 changes: 4 additions & 1 deletion lib/valid_email2/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ def self.prohibited_domain_characters_regex=(val)
@prohibited_domain_characters_regex = val
end

def initialize(address)
def initialize(address, dns_timeout = 5)
@parse_error = false
@raw_address = address
@dns_timeout = dns_timeout

begin
@address = Mail::Address.new(address)
Expand Down Expand Up @@ -132,12 +133,14 @@ def address_contain_emoticons?(email)

def mx_servers
@mx_servers ||= Resolv::DNS.open do |dns|
dns.timeouts = @dns_timeout
dns.getresources(address.domain, Resolv::DNS::Resource::IN::MX)
end
end

def mx_or_a_servers
@mx_or_a_servers ||= Resolv::DNS.open do |dns|
dns.timeouts = @dns_timeout
(mx_servers.any? && mx_servers) ||
dns.getresources(address.domain, Resolv::DNS::Resource::IN::A)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/valid_email2/email_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
module ValidEmail2
class EmailValidator < ActiveModel::EachValidator
def default_options
{ regex: true, disposable: false, mx: false, strict_mx: false, disallow_subaddressing: false, multiple: false }
{ regex: true, disposable: false, mx: false, strict_mx: false, disallow_subaddressing: false, multiple: false, dns_timeout: 5 }
end

def validate_each(record, attribute, value)
return unless value.present?
options = default_options.merge(self.options)

addresses = sanitized_values(value).map { |v| ValidEmail2::Address.new(v) }
addresses = sanitized_values(value).map { |v| ValidEmail2::Address.new(v, options[:dns_timeout]) }

error(record, attribute) && return unless addresses.all?(&:valid?)

Expand Down