commit 0c550cb0f70bb45601e073d497613df6933ab42a
parent 1cfa015ac59dda1303cd77382ca7548b397c6952
Author: Dan Callaghan <djc@djc.id.au>
Date: Sat, 7 Nov 2026 20:11:11 +1100
handle invalid UTF-7 instead of crashing
Diffstat:
5 files changed, 18 insertions(+), 5 deletions(-)
diff --git a/lib/sup/message_chunks.rb b/lib/sup/message_chunks.rb
@@ -105,7 +105,7 @@ EOS
text = case @content_type
when /^text\/plain\b/
if /^UTF-7$/i =~ encoded_content.charset
- @raw_content.decode_utf7
+ @raw_content.decode_utf7 :invalid => :replace, :undef => :replace
else
begin
charset = Encoding.find(encoded_content.charset || 'US-ASCII')
diff --git a/lib/sup/util.rb b/lib/sup/util.rb
@@ -364,10 +364,10 @@ class String
## Net::IMAP::decode_utf7 which only handles the IMAP "modified UTF-7"
## encoding. This implementation is inspired by that one but handles
## standard UTF-7 shift characters and not the IMAP-specific variation.
- def decode_utf7
+ def decode_utf7 **opts
gsub(/\+([^-]+)?-/) {
if $1
- ($1 + "===").unpack("m")[0].encode(Encoding::UTF_8, Encoding::UTF_16BE)
+ ($1 + "===").unpack("m")[0].encode(Encoding::UTF_8, Encoding::UTF_16BE, **opts)
else
"+"
end
diff --git a/test/fixtures/rfc2047-header-encoding.eml b/test/fixtures/rfc2047-header-encoding.eml
@@ -8,6 +8,7 @@ Subject:
=?UTF-7?b?LCBGcmlkYSBFbmcrQVBnLQ?=
bad: =?UTF16?q?badcharsetname?= =?US-ASCII?b?/w?=
=?UTF-7?Q?=41=6D=65=72=69=63=61=E2=80=99=73?=
+ =?UTF-7?b?K0FHRUEt?=
The subject header contains various RFC2047 encoded words.
For completeness we test both base64 and quoted-printable, and some
diff --git a/test/fixtures/text-attachments-with-charset.eml b/test/fixtures/text-attachments-with-charset.eml
@@ -57,4 +57,11 @@ Content-Disposition: attachment; filename="ascii.txt"
This is +Jyg-UTF-7+Jyg-
--===============2385509127900810307==
+Content-Type: text/plain; charset="utf-7"
+Content-Transfer-Encoding: 7bit
+MIME-Version: 1.0
+Content-Disposition: attachment; filename="bad-utf7.txt"
+
+Bad +AGEA- UTF-7
+--===============2385509127900810307==
diff --git a/test/test_message.rb b/test/test_message.rb
@@ -181,7 +181,7 @@ class TestMessage < Minitest::Test
sup_message.load_from_source!
chunks = sup_message.load_from_source!
- assert_equal(7, chunks.length)
+ assert_equal(8, chunks.length)
assert(chunks[0].is_a? Redwood::Chunk::Text)
## The first attachment declares charset=us-ascii
assert(chunks[1].is_a? Redwood::Chunk::Attachment)
@@ -203,6 +203,10 @@ class TestMessage < Minitest::Test
## The sixth attachment is UTF-7 encoded
assert(chunks[6].is_a? Redwood::Chunk::Attachment)
assert_equal(["This is ✨UTF-7✨"], chunks[6].lines)
+ ## The seventh attachment is UTF-7 with an incomplete sequence,
+ ## which will be replaced with U+FFFD REPLACEMENT CHARACTER
+ assert(chunks[7].is_a? Redwood::Chunk::Attachment)
+ assert_equal(["Bad a� UTF-7"], chunks[7].lines)
end
def test_mailing_list_header
@@ -264,7 +268,8 @@ class TestMessage < Minitest::Test
assert_equal("Hans Martin Djupvik, Ingrid Bø, Ирина Сидорова, " +
"Jesper Berg, Frida Engø " +
"bad: =?UTF16?q?badcharsetname?==?US-ASCII?b?/w?=" +
- "=?UTF-7?Q?=41=6D=65=72=69=63=61=E2=80=99=73?=",
+ "=?UTF-7?Q?=41=6D=65=72=69=63=61=E2=80=99=73?=" +
+ "=?UTF-7?b?K0FHRUEt?=",
sup_message.subj)
assert_equal "=?utf-8?q?YouTube-tj=E4nst?=", sup_message.from.name
end