Skip to content

Fix exception when multibyte UTF-8 chars are in multipart boundary. #1601

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Bug Fixes:

* Regression: Preserve message-level charset when adding parts (related to Rails ActionMailer) @shields
* Regression: Adding a part should not reset the mail's charset to nil @railsbob
* Fix exception when multibyte UTF-8 chars are in multipart boundary @pdg137

Performance:

Expand Down
9 changes: 8 additions & 1 deletion lib/mail/body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,20 @@ def default_encoding

# split parts by boundary, ignore first part if empty, append final part when closing boundary was missing
def extract_parts
clean_boundary = boundary || ""
if raw_source.encoding == Encoding::ASCII_8BIT &&
clean_boundary.encoding == Encoding::UTF_8
clean_boundary.force_encoding(Encoding::ASCII_8BIT)
end
clean_boundary = Regexp.escape(boundary || "")

parts_regex = /
(?: # non-capturing group
\A | # start of string OR
\r?\n # line break with optional CR
)
(
--#{Regexp.escape(boundary || "")} # boundary delimiter
--#{clean_boundary} # boundary delimiter
(?:--)? # with non-capturing optional closing
)
(?=\s*$) # lookahead matching zero or more spaces followed by line-ending
Expand Down
7 changes: 7 additions & 0 deletions spec/fixtures/emails/error_emails/utf8_multipart_boundary.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Content-Type: multipart/alternative; boundary="bad-char-µ"

--bad-char-µ
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

Hello, world!
5 changes: 5 additions & 0 deletions spec/mail/example_emails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,5 +392,10 @@
expect(mail.encoded).to include("Subject: =?UTF-8?Q?Forma=E7=E3o_FrenetikPolis:_Mega_Campanha_Final?=\r\n =?UTF-8?Q?_Ver=E3o_|_Cursos_de_Setembro?=")
end
end

it 'does not fail on UTF-8 in multipart boundary' do
mail = read_fixture('emails', 'error_emails', 'utf8_multipart_boundary.eml')
expect(mail.parts.length).to eq(1)
end
end
end