From: Dan Callaghan <dan@bl2.djc.id.au>
To: sup-commits@supmua.dev
Subject: [sup.git] 01/02: don't mangle non-UTF-8 attachments with 8bit transfer encoding
Date: Thu, 19 Nov 2026 21:13:05 +1100 [thread overview]
Message-ID: <20261119101304.D85102D037B@bl2.djc.id.au> (raw)
In-Reply-To: <179508318470.228777.12759209988489611778@bl2.djc.id.au>
New commit on branch develop:
https://supmua.dev/git/sup/commit/eae4a257f16aea67d34957b9a0f64689683a5f90.html
commit eae4a257f16aea67d34957b9a0f64689683a5f90
Author: Dan Callaghan <djc@djc.id.au>
AuthorDate: Thu Nov 19 19:27:48 2026 +1100
don't mangle non-UTF-8 attachments with 8bit transfer encoding
Commit 208a6ac6d2 called the fix_encoding! method on attachments, but if
the attachment is being sent with 8bit transfer encoding and it contains
invalid UTF-8 bytes (for example because it is encoded in some other
encoding) the attachment contents would be mangled: invalid sequences
would be wrongly converted to U+FFFD.
A better solution is to deal only with ASCII-8BIT (a.k.a. binary)
at the RMail layer. The body coming from Sup is already aggressively
fix_encoding!'ed so it is in $encoding, whereas the attachments can be
read as binary so as not to assume any particular encoding. We can force
the body to ASCII-8BIT at the last moment before passing to RMail so
that RMail is dealing solely with ASCII-8BIT strings. The resulting
binary data can then be piped directly to the sendmail command.
Relates to #135 and #136.
---
lib/sup/modes/edit_message_mode.rb | 9 +++------
lib/sup/util.rb | 2 +-
test/unit/test_edit_message_mode.rb | 35 ++++++++++++++++++++++++++++++++++-
3 files changed, 38 insertions(+), 8 deletions(-)
diff --git a/lib/sup/modes/edit_message_mode.rb b/lib/sup/modes/edit_message_mode.rb
index 01661731..eafb931b 100644
--- a/lib/sup/modes/edit_message_mode.rb
+++ b/lib/sup/modes/edit_message_mode.rb
@@ -577,7 +577,7 @@ protected
return false
end
else
- IO.popen(acct.sendmail, "w:UTF-8") { |p| p.puts m }
+ IO.popen(acct.sendmail, "wb") { |p| p.puts m }
raise SendmailCommandFailed, "Couldn't execute #{acct.sendmail}" unless $? == 0
end
@@ -605,7 +605,7 @@ protected
m.body += "\n" + sig_lines.join("\n") unless @sig_edited
## body must end in a newline or GPG signatures will be WRONG!
m.body += "\n" unless m.body =~ /\n\Z/
- m.body = m.body.fix_encoding!
+ m.body.force_encoding("ASCII-8BIT")
## there are attachments, so wrap body in an attachment of its own
unless @attachments.empty?
@@ -614,10 +614,7 @@ protected
m = RMail::Message.new
m.add_part body_m
- @attachments.each do |a|
- a.body = a.body.fix_encoding! if a.body.kind_of? String
- m.add_part a
- end
+ @attachments.each { |a| m.add_part a }
end
## do whatever crypto transformation is necessary
diff --git a/lib/sup/util.rb b/lib/sup/util.rb
index e9944ad0..b4d58f9a 100644
--- a/lib/sup/util.rb
+++ b/lib/sup/util.rb
@@ -80,7 +80,7 @@ module RMail
def self.make_file_attachment fn
bfn = File.basename fn
t = MIME::Types.type_for(bfn).first || MIME::Types.type_for("exe").first
- payload = IO.read fn
+ payload = IO.binread fn
## Need to encode as base64 or quoted-printable if any lines are longer than 998 chars.
encoding = if t.encoding != t.default_encoding and payload.each_line.any? { |l| l.length > 998 }
t.default_encoding
diff --git a/test/unit/test_edit_message_mode.rb b/test/unit/test_edit_message_mode.rb
index 838da113..8d1b664c 100644
--- a/test/unit/test_edit_message_mode.rb
+++ b/test/unit/test_edit_message_mode.rb
@@ -64,7 +64,7 @@ class TestEditMessageMode < Minitest::Test
msg = mode.send :build_message, Time.now
attachment = msg.part(1)
- assert_equal attachment_content, attachment.body
+ assert_equal attachment_content.b, attachment.body
assert_equal "8bit", attachment.header["Content-Transfer-Encoding"]
end
@@ -96,4 +96,37 @@ class TestEditMessageMode < Minitest::Test
## This was: https://github.com/sup-heliotrope/sup/issues/502
assert_equal ["quoted-printable"], attachment.header.fetch_all("Content-Transfer-Encoding")
end
+
+ ## https://github.com/sup-heliotrope/sup/issues/135
+ def test_attachment_8bit_non_utf8
+ body_content = "löl noñ-ÀSCII bodý"
+ ## CP1251 is not UTF-8 compatible.
+ ## This is a CSV only due to a quirk of mime-types, which wants to use 8bit for text/csv
+ ## (whereas text/plain is quoted-printable).
+ attachment_content = "С днем рождения!\n".encode("cp1251")
+ attachment_filename = File.join @path, "cp1251.csv"
+ File.write attachment_filename, attachment_content
+
+ opts = {
+ :header => {
+ "From" => +"sender@example.invalid",
+ "To" => +"recip@example.invalid",
+ },
+ :attachments => {
+ "cp1251.csv" => RMail::Message.make_file_attachment(attachment_filename),
+ },
+ :body => [body_content],
+ }
+ mode = Redwood::EditMessageMode.new opts
+
+ msg = mode.send :build_message, Time.now
+ attachment = msg.part(1)
+ assert_equal attachment_content.b, attachment.body
+ assert_equal "8bit", attachment.header["Content-Transfer-Encoding"]
+
+ ## Also check the serialized message, which is what the send_message method uses.
+ assert msg.to_s.lines.include? body_content.b + "\n"
+ assert msg.to_s.lines.include? "Content-Type: text/csv; name=\"cp1251.csv\"\n"
+ assert msg.to_s.lines.include? attachment_content.b
+ end
end
next prev parent reply other threads:[~2026-11-19 10:13 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 10:13 [sup.git] branch develop updated (7a1133a1 -> 154ff507) Dan Callaghan
2026-11-19 10:13 ` Dan Callaghan [this message]
2026-11-19 10:13 ` [sup.git] 02/02: fix wrong top-level Content-Type when sending messages with attachments Dan Callaghan
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=20261119101304.D85102D037B@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