From mboxrd@z Thu Jan 1 00:00:00 1970 Received: by bl2.djc.id.au (Postfix, from userid 1000) id 9DD792D0032; Thu, 19 Nov 2026 11:26:55 +1100 (AEDT) Date: Thu, 19 Nov 2026 11:26:55 +1100 To: sup-commits@supmua.dev Subject: [sup.git] branch develop updated: handle invalid UTF-7 instead of crashing MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Message-ID: <179504801550.224268.17399803960079922870@bl2.djc.id.au> Reply-To: Dan Callaghan X-Git-Host: bl2.djc.id.au X-Git-Repo: sup.git X-Git-Refname: refs/heads/develop X-Git-Reftype: branch X-Git-Oldrev: 1cfa015ac59dda1303cd77382ca7548b397c6952 X-Git-Newrev: 0c550cb0f70bb45601e073d497613df6933ab42a X-Git-Rev: 0c550cb0f70bb45601e073d497613df6933ab42a X-Git-NotificationType: ref_changed_plus_diff X-Git-Multimail-Version: 1.7.dev Auto-Submitted: auto-generated From: Dan Callaghan List-Id: Pushed a commit to branch develop: https://supmua.dev/git/sup/commit/0c550cb0f70bb45601e073d497613df6933ab42a.html The following commit(s) were added to refs/heads/develop by this push: new 0c550cb0 handle invalid UTF-7 instead of crashing 0c550cb0 is described below commit 0c550cb0f70bb45601e073d497613df6933ab42a Author: Dan Callaghan AuthorDate: Sat Nov 7 20:11:11 2026 +1100 handle invalid UTF-7 instead of crashing --- lib/sup/message_chunks.rb | 2 +- lib/sup/util.rb | 4 ++-- test/fixtures/rfc2047-header-encoding.eml | 1 + test/fixtures/text-attachments-with-charset.eml | 7 +++++++ test/test_message.rb | 9 +++++++-- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/sup/message_chunks.rb b/lib/sup/message_chunks.rb index c7cf47c9..e9055d86 100644 --- 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 index 793cf731..e9944ad0 100644 --- 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 index 7c108ef4..6a05938a 100644 --- 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 index ee268d3e..448a0c5b 100644 --- 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 index 097ceb8a..95b7378a 100644 --- 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