From mboxrd@z Thu Jan 1 00:00:00 1970 Received: by bl2.djc.id.au (Postfix, from userid 1000) id D85102D037B; Thu, 19 Nov 2026 21:13:04 +1100 (AEDT) Date: Thu, 19 Nov 2026 21:13:05 +1100 To: sup-commits@supmua.dev Subject: [sup.git] 01/02: don't mangle non-UTF-8 attachments with 8bit transfer encoding MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Reply-To: Dan Callaghan In-Reply-To: <179508318470.228777.12759209988489611778@bl2.djc.id.au> References: <179508318470.228777.12759209988489611778@bl2.djc.id.au> Thread-Index: AQAAEjRWUmvPWPj2T92INWdgOjRhpw== X-Git-Host: bl2.djc.id.au X-Git-Repo: sup.git X-Git-Refname: refs/heads/develop X-Git-Reftype: branch X-Git-Rev: eae4a257f16aea67d34957b9a0f64689683a5f90 X-Git-NotificationType: diff X-Git-Multimail-Version: 1.7.dev Auto-Submitted: auto-generated Message-Id: <20261119101304.D85102D037B@bl2.djc.id.au> From: Dan Callaghan List-Id: New commit on branch develop: https://supmua.dev/git/sup/commit/eae4a257f16aea67d34957b9a0f64689683a5f90.html commit eae4a257f16aea67d34957b9a0f64689683a5f90 Author: Dan Callaghan 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