FIX: Strip null bytes in mail subjects.

This commit is contained in:
Guo Xiang Tan 2018-10-11 09:46:32 +08:00
parent ffc241eb25
commit 5039a6c3f1
3 changed files with 23 additions and 1 deletions

View File

@ -52,10 +52,12 @@ module Email
raise EmptyEmailError if mail_string.blank?
@staged_users = []
@raw_email = mail_string
COMMON_ENCODINGS.each do |encoding|
fixed = try_to_encode(mail_string, encoding)
break @raw_email = fixed if fixed.present?
end
@mail = Mail.new(@raw_email)
@message_id = @mail.message_id.presence || Digest::MD5.hexdigest(mail_string)
@opts = opts
@ -482,7 +484,10 @@ module Email
end
def subject
@suject ||= @mail.subject.presence || I18n.t("emails.incoming.default_subject", email: @from_email)
@subject ||= begin
mail_subject = @mail.subject.delete("\u0000")
mail_subject.presence || I18n.t("emails.incoming.default_subject", email: @from_email)
end
end
def find_user(email)

View File

@ -117,6 +117,12 @@ describe Email::Receiver do
expect(IncomingEmail.last.error).to eq("RuntimeError")
end
it "strips null bytes from the subject" do
expect do
process(:null_byte_in_subject)
end.to raise_error(Email::Receiver::BadDestinationAddress)
end
context "bounces to VERP" do
let(:bounce_key) { "14b08c855160d67f2e0c2f8ef36e251e" }

View File

@ -0,0 +1,11 @@
Return-Path: <discourse@bar.com>
From: Foo Bar <discourse@bar.com>
To: category@foo.com
Subject: =?ISO_8859-1?Q?testing=00?=
Date: Fri, 15 Jan 2016 00:12:43 +0100
Message-ID: <31@foo.bar.mail>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Hey, this is a topic from a complete stranger ;)