Skip to content

Commit

Permalink
Improve email address validation (mastodon#14565)
Browse files Browse the repository at this point in the history
* Increase DNS timeout from 1 second to 5 seconds for MX check

1 seconds is rather short when using a recursive DNS resolver which
hasn't got a cached result already available. Use 5 seconds instead,
which is the timeout value we use for outgoing HTTP queries.

* Add more precise error messages for invalid e-mail addresses
  • Loading branch information
ClearlyClaire authored and thenameisnigel-old committed Sep 7, 2020
1 parent fd924eb commit 5bacb28
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/email_domain_blocks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def create
ips = []

Resolv::DNS.open do |dns|
dns.timeouts = 1
dns.timeouts = 5

hostnames = dns.getresources(@email_domain_block.domain, Resolv::DNS::Resource::IN::MX).to_a.map { |e| e.exchange.to_s }

Expand Down
2 changes: 1 addition & 1 deletion app/validators/blacklisted_email_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def validate(user)

@email = user.email

user.errors.add(:email, I18n.t('users.invalid_email')) if blocked_email?
user.errors.add(:email, I18n.t('users.blocked_email_provider')) if blocked_email?
end

private
Expand Down
30 changes: 22 additions & 8 deletions app/validators/email_mx_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,38 @@

class EmailMxValidator < ActiveModel::Validator
def validate(user)
user.errors.add(:email, I18n.t('users.invalid_email')) if invalid_mx?(user.email)
domain = get_domain(user.email)

if domain.nil?
user.errors.add(:email, I18n.t('users.invalid_email'))
else
ips, hostnames = resolve_mx(domain)
if ips.empty?
user.errors.add(:email, I18n.t('users.invalid_email_mx'))
elsif on_blacklist?(hostnames + ips)
user.errors.add(:email, I18n.t('users.blocked_email_provider'))
end
end
end

private

def invalid_mx?(value)
def get_domain(value)
_, domain = value.split('@', 2)

return true if domain.nil?
return nil if domain.nil?

TagManager.instance.normalize_domain(domain)
rescue Addressable::URI::InvalidURIError
nil
end

domain = TagManager.instance.normalize_domain(domain)
def resolve_mx(domain)
hostnames = []
ips = []

Resolv::DNS.open do |dns|
dns.timeouts = 1
dns.timeouts = 5

hostnames = dns.getresources(domain, Resolv::DNS::Resource::IN::MX).to_a.map { |e| e.exchange.to_s }

Expand All @@ -29,9 +45,7 @@ def invalid_mx?(value)
end
end

ips.empty? || on_blacklist?(hostnames + ips)
rescue Addressable::URI::InvalidURIError
true
[ips, hostnames]
end

def on_blacklist?(values)
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1325,9 +1325,11 @@ en:
tips: Tips
title: Welcome aboard, %{name}!
users:
blocked_email_provider: This e-mail provider isn't allowed
follow_limit_reached: You cannot follow more than %{limit} people
generic_access_help_html: Trouble accessing your account? You may get in touch with %{email} for assistance
invalid_email: The e-mail address is invalid
invalid_email_mx: The e-mail address does not seem to exist
invalid_otp_token: Invalid two-factor code
invalid_sign_in_token: Invalid security code
otp_lost_help_html: If you lost access to both, you may get in touch with %{email}
Expand Down
2 changes: 1 addition & 1 deletion lib/mastodon/email_domain_blocks_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def add(*domains)
ips = []

Resolv::DNS.open do |dns|
dns.timeouts = 1
dns.timeouts = 5
hostnames = dns.getresources(email_domain_block.domain, Resolv::DNS::Resource::IN::MX).to_a.map { |e| e.exchange.to_s }

([email_domain_block.domain] + hostnames).uniq.each do |hostname|
Expand Down
4 changes: 2 additions & 2 deletions spec/validators/blacklisted_email_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
let(:blocked_email) { true }

it 'calls errors.add' do
expect(errors).to have_received(:add).with(:email, I18n.t('users.invalid_email'))
expect(errors).to have_received(:add).with(:email, I18n.t('users.blocked_email_provider'))
end
end

context '!blocked_email?' do
let(:blocked_email) { false }

it 'not calls errors.add' do
expect(errors).not_to have_received(:add).with(:email, I18n.t('users.invalid_email'))
expect(errors).not_to have_received(:add).with(:email, I18n.t('users.blocked_email_provider'))
end
end
end
Expand Down

0 comments on commit 5bacb28

Please sign in to comment.