Crowdin / Upload translations / upload-translations (push) Skipped
Historical data migration test / test (14-alpine) (push) Canceled after 0s
Historical data migration test / test (15-alpine) (push) Canceled after 0s
Historical data migration test / test (16-alpine) (push) Canceled after 0s
Historical data migration test / test (17-alpine) (push) Canceled after 0s
Bundler Audit / security (push) Canceled after 0s
Haml Linting / lint (push) Canceled after 0s
JavaScript Linting / lint (push) Canceled after 0s
Ruby Linting / lint (push) Canceled after 0s
JavaScript Testing / test (push) Canceled after 0s
Ruby Testing / build (production) (push) Canceled after 0s
Ruby Testing / test (.ruby-version) (push) Canceled after 0s
Ruby Testing / test (3.3) (push) Canceled after 0s
Ruby Testing / test (3.4) (push) Canceled after 0s
Ruby Testing / End to End testing (.ruby-version) (push) Canceled after 0s
Ruby Testing / End to End testing (3.3) (push) Canceled after 0s
Ruby Testing / End to End testing (3.4) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Check i18n / check-i18n (push) Canceled after 0s
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (javascript) (push) Canceled after 0s
CodeQL / Analyze (ruby) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, opensearchproject/opensearch:2) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (3.3, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (3.4, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Check formatting / lint (push) Canceled after 0s
CSS Linting / lint (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:8.19.2) (push) Canceled after 0s
Chromatic / Check for relevant changes (push) Canceled after 0s
Chromatic / Run Chromatic (push) Canceled after 0s
Snapshot of upstream tag v4.7.3 (commit 8ff11c2bb7b9f0714e947b10fa3629c6542c7121) from https://github.com/mastodon/mastodon, unmodified.
65 lines
1.8 KiB
Ruby
65 lines
1.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'resolv'
|
|
|
|
class EmailMxValidator < ActiveModel::EachValidator
|
|
def validate_each(record, attribute, value)
|
|
return if value.blank?
|
|
|
|
domain = get_domain(value)
|
|
|
|
if domain.blank? || domain.include?('..')
|
|
record.errors.add(attribute, :invalid)
|
|
elsif !on_allowlist?(domain)
|
|
resolved_ips, resolved_domains = resolve_mx(domain)
|
|
|
|
if resolved_ips.empty?
|
|
record.errors.add(attribute, :unreachable)
|
|
elsif email_domain_blocked?([domain, *resolved_domains], options[:attempt_ip].is_a?(Symbol) ? record.public_send(options[:attempt_ip]) : nil)
|
|
record.errors.add(attribute, :blocked)
|
|
end
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def get_domain(value)
|
|
_, domain = value.split('@', 2)
|
|
|
|
return nil if domain.nil?
|
|
|
|
TagManager.instance.normalize_domain(domain)
|
|
rescue Addressable::URI::InvalidURIError
|
|
nil
|
|
end
|
|
|
|
def on_allowlist?(domain)
|
|
return false if Rails.configuration.x.email_domains_allowlist.blank?
|
|
|
|
Rails.configuration.x.email_domains_allowlist.include?(domain)
|
|
end
|
|
|
|
def resolve_mx(domain)
|
|
records = []
|
|
ips = []
|
|
|
|
Resolv::DNS.open do |dns|
|
|
dns.timeouts = 5
|
|
|
|
records = dns.getresources(domain, Resolv::DNS::Resource::IN::MX).to_a.map { |e| e.exchange.to_s }
|
|
next if records == [''] # This domain explicitly rejects emails
|
|
|
|
([domain] + records).uniq.each do |hostname|
|
|
ips.concat(dns.getresources(hostname, Resolv::DNS::Resource::IN::A).to_a.map { |e| e.address.to_s })
|
|
ips.concat(dns.getresources(hostname, Resolv::DNS::Resource::IN::AAAA).to_a.map { |e| e.address.to_s })
|
|
end
|
|
end
|
|
|
|
[ips, records]
|
|
end
|
|
|
|
def email_domain_blocked?(domains, attempt_ip)
|
|
EmailDomainBlock.block?(domains, attempt_ip: attempt_ip)
|
|
end
|
|
end
|