From: Dan Callaghan <dan@bl2.djc.id.au>
To: sup-commits@supmua.dev
Subject: [sup.git] branch develop updated: handle invalid UTF-7 instead of crashing
Date: Thu, 19 Nov 2026 11:26:55 +1100 [thread overview]
Message-ID: <179504801550.224268.17399803960079922870@bl2.djc.id.au> (raw)
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 ++--
| 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
--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
reply other threads:[~2026-11-19 0:26 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=179504801550.224268.17399803960079922870@bl2.djc.id.au \
--to=dan@bl2.djc.id.au \
--cc=djc@djc.id.au \
--cc=sup-commits@supmua.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox