sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit eae4a257f16aea67d34957b9a0f64689683a5f90
parent 7a1133a166c8af5a871ebc0c1b7bf75e9556dd06
Author: Dan Callaghan <djc@djc.id.au>
Date:   Thu, 19 Nov 2026 19:27:48 +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.

Diffstat:
M lib/sup/modes/edit_message_mode.rb | 9 +++------
M lib/sup/util.rb | 2 +-
M 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
@@ -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
@@ -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
@@ -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