mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 20:41:24 +08:00
SECURITY: Block registrations for encoded emails that are invalid
This commit is contained in:

committed by
Alan Guo Xiang Tan

parent
34d04e7507
commit
d7164d57ec
@ -1,11 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class EmailAddressValidator
|
||||
def self.valid_value?(email)
|
||||
email.match? email_regex
|
||||
end
|
||||
class << self
|
||||
def valid_value?(email)
|
||||
email.match?(email_regex) && decode(email)&.match?(email_regex)
|
||||
end
|
||||
|
||||
def self.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/
|
||||
def 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/
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def decode(email)
|
||||
Mail::Address.new(email).decoded
|
||||
rescue Mail::Field::ParseError, Mail::Field::IncompleteParseError
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user