mirror of
https://github.com/discourse/discourse.git
synced 2024-12-12 23:33:48 +08:00
FIX: Replace use of regular expression (#12838)
It used a regular expression to check if message IDs were in RFC format.
This commit is contained in:
parent
f8562d8d2f
commit
1525653dff
15
lib/email.rb
15
lib/email.rb
|
@ -3,9 +3,6 @@
|
||||||
require 'mail'
|
require 'mail'
|
||||||
|
|
||||||
module Email
|
module Email
|
||||||
# cute little guy ain't he?
|
|
||||||
MESSAGE_ID_REGEX = /<(.*@.*)+>/
|
|
||||||
|
|
||||||
def self.is_valid?(email)
|
def self.is_valid?(email)
|
||||||
return false unless String === email
|
return false unless String === email
|
||||||
!!(EmailValidator.email_regex =~ email)
|
!!(EmailValidator.email_regex =~ email)
|
||||||
|
@ -43,12 +40,16 @@ module Email
|
||||||
|
|
||||||
# https://tools.ietf.org/html/rfc850#section-2.1.7
|
# https://tools.ietf.org/html/rfc850#section-2.1.7
|
||||||
def self.message_id_rfc_format(message_id)
|
def self.message_id_rfc_format(message_id)
|
||||||
return message_id if message_id =~ MESSAGE_ID_REGEX
|
message_id.present? && !is_message_id_rfc?(message_id) ? "<#{message_id}>" : message_id
|
||||||
"<#{message_id}>"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.message_id_clean(message_id)
|
def self.message_id_clean(message_id)
|
||||||
return message_id if !(message_id =~ MESSAGE_ID_REGEX)
|
message_id.present? && is_message_id_rfc?(message_id) ? message_id.gsub(/^<|>$/, "") : message_id
|
||||||
message_id.tr("<>", "")
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def self.is_message_id_rfc?(message_id)
|
||||||
|
message_id.start_with?('<') && message_id.include?('@') && message_id.end_with?('>')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -44,4 +44,29 @@ describe Email do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "message_id_rfc_format" do
|
||||||
|
|
||||||
|
it "returns message ID in RFC format" do
|
||||||
|
expect(Email.message_id_rfc_format("test@test")).to eq("<test@test>")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns input if already in RFC format" do
|
||||||
|
expect(Email.message_id_rfc_format("<test@test>")).to eq("<test@test>")
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "message_id_clean" do
|
||||||
|
|
||||||
|
it "returns message ID if in RFC format" do
|
||||||
|
expect(Email.message_id_clean("<test@test>")).to eq("test@test")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns input if a clean message ID is not in RFC format" do
|
||||||
|
message_id = "<" + "@" * 50
|
||||||
|
expect(Email.message_id_clean(message_id)).to eq(message_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user