]> git.openstreetmap.org Git - rails.git/blob - app/validators/deliverable_email_validator.rb
Merge remote-tracking branch 'upstream/pull/7611'
[rails.git] / app / validators / deliverable_email_validator.rb
1 # frozen_string_literal: true
2
3 # Validates that an email address is both RFC-compliant, using the
4 # validates_email_format_of gem, and will also be accepted by exim relays.
5 #
6 # Exim's default configuration rejects recipients on remote domains whose
7 # local part matches:
8 #
9 #   local_parts = ^[./|] : ^.*[@%!] : ^.*/\\.\\./
10 #
11 # Many such addresses are valid using RFC 5322, so otherwise the gem would accept them.
12
13 class DeliverableEmailValidator < ActiveModel::EachValidator
14   INVALID_LOCAL_PART_CHARS = "@%!"
15   INVALID_LOCAL_PART = %r{\A[./|]|/\.\./}
16
17   def validate_each(record, attribute, value)
18     errors = ValidatesEmailFormatOf.validate_email_format(value, options) || exim_errors(value)
19
20     errors.each { |error| record.errors.add(attribute, error) }
21   end
22
23   private
24
25   def exim_errors(value)
26     local_part = value.rpartition("@").first
27     errors = []
28
29     errors << I18n.t("validations.email_characters", :characters => INVALID_LOCAL_PART_CHARS) if /[#{INVALID_LOCAL_PART_CHARS}]/o.match?(local_part)
30     errors << I18n.t("validations.invalid_characters") if INVALID_LOCAL_PART.match?(local_part)
31
32     errors
33   end
34 end