Commit notifications for all Sup repositories
 help / color / mirror / Atom feed
* [sup.git] branch develop updated: handle invalid UTF-7 instead of crashing
@ 2026-11-19  0:26 Dan Callaghan
  0 siblings, 0 replies; only message in thread
From: Dan Callaghan @ 2026-11-19  0:26 UTC (permalink / raw)
  To: sup-commits

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 <djc@djc.id.au>
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

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-19  0:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19  0:26 [sup.git] branch develop updated: handle invalid UTF-7 instead of crashing Dan Callaghan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox