Files
discourse/lib/validators/email_address_validator.rb
Ted Johansson 60a3fe41d2 FIX: Disallow encoded words in e-mail addresses (#33083)
RFC 5322 allows special characters, including ? and =, to be used in e-mail addresses.

RFC 2047 is an extension that adds a feature called "encoded words" which let you embed different encodings in the same header. However, it explicitly says that these aren't allowed in e-mail address headers.

Encoded words have the format:

encoded-word = "=?" charset "?" encoding "?" encoded-text "?="
Where encoding is either Q or B, but could take on other values in the future.

After this change we consider e-mail addresses with an encoded word inside invalid.
2025-06-05 12:58:01 +08:00

31 lines
758 B
Ruby

# frozen_string_literal: true
class EmailAddressValidator
EMAIL_REGEX =
/\A[a-zA-Z0-9!#\$%&'*+\/=?\^_`{|}~\-]+(?:\.[a-zA-Z0-9!#\$%&'\*+\/=?\^_`{|}~\-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?$\z/
ENCODED_WORD_REGEX = /\=\?[^?]+\?[BbQq]\?[^?]+\?\=/
class << self
def valid_value?(email)
email.match?(email_regex) && !email.match?(encoded_word_regex) &&
decode(email)&.match?(email_regex)
end
def email_regex
EMAIL_REGEX
end
def encoded_word_regex
ENCODED_WORD_REGEX
end
private
def decode(email)
Mail::Address.new(email).decoded
rescue Mail::Field::ParseError, Mail::Field::IncompleteParseError
nil
end
end
end