Skip to content

A platform for community discussion. Free, open, simple.

FIX: Email::Cleaner must clear the encoding of replaced bodies

Michael Brown371eff1f667b
diff --git a/lib/email/cleaner.rb b/lib/email/cleaner.rb
index 961a67fc5a2..c30ae5a9311 100644
--- a/lib/email/cleaner.rb
+++ b/lib/email/cleaner.rb
@@ -32,7 +32,12 @@ module Email
private
def truncate!
- parts.each { |part| part.body = part.body.decoded.truncate(truncate_limit, omission: "") }
+ parts.each do |part|
+ part.body = part.body.decoded.truncate(truncate_limit, omission: "")
+ # let the serialiser re-encode it with an appropriate format - leaving
+ # the original one set tells Mail that the content is *already* encoded
+ part.content_transfer_encoding = nil
+ end
end
def parts
diff --git a/spec/fixtures/emails/base64_encoded_body.eml b/spec/fixtures/emails/base64_encoded_body.eml
new file mode 100644
index 00000000000..9f988ab7956
--- /dev/null
+++ b/spec/fixtures/emails/base64_encoded_body.eml
@@ -0,0 +1,36 @@
+Return-Path: <[email protected]>
+Received: from mail-24430.protonmail.ch (mail-24430.protonmail.ch. [109.224.244.30])
+ by mx.google.com with ESMTPS id 00721157ae682-7864ba682b8si14738347b3.39.2025.10.31.08.53.17
+ (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
+ Fri, 31 Oct 2025 08:53:17 -0700 (PDT)
+Received-SPF: pass (google.com: domain of [email protected] designates 109.224.244.30 as permitted sender) client-ip=109.224.244.30;
+Authentication-Results: mx.google.com;
+ dkim=pass [email protected] header.s=l3nkficqdbcgzpt3sm74fowyba.protonmail header.b=NCXT0FzU;
+ spf=pass (google.com: domain of [email protected] designates 109.224.244.30 as permitted sender) [email protected];
+ dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me
+Date: Fri, 31 Oct 2025 15:53:13 +0000
+From: bilbo <[email protected]>
+Subject: Ceci n'est pas un email
+Message-ID: <JGnmBVSOxt_Xn4dcgO9BCUcI5lVspt6aSL_FXd6lIYApVO7-IFUP2v5fRp9GN1-1cSWgrc37npStneeB6ZhA5W4wnfWrSiTfBRMKbTuPNkY=@proton.me>
+Feedback-ID: 125571894:user:proton
+X-Pm-Message-ID: 4f3ea7c8155ec5a74f83a5f65274dc3f5269b7a4
+MIME-Version: 1.0
+Content-Type: multipart/alternative; boundary="b1=_WSpo40vhsD8KyQGJNYap4ALwcgqbhrVWd51oUYgyI"
+
+--b1=_WSpo40vhsD8KyQGJNYap4ALwcgqbhrVWd51oUYgyI
+Content-Type: text/plain; charset=utf-8
+Content-Transfer-Encoding: base64
+
+U2VudCBmcm9tIFtQcm90b24gTWFpbF0oaHR0cHM6Ly9wcm90b24ubWUvbWFpbC9ob21lKSBmb3Ig
+QW5kcm9pZC4=
+--b1=_WSpo40vhsD8KyQGJNYap4ALwcgqbhrVWd51oUYgyI
+Content-Type: text/html; charset=utf-8
+Content-Transfer-Encoding: base64
+
+PGh0bWw+PGhlYWQ+PC9oZWFkPjxib2R5Pjxicj48YnI+PGRpdiBjbGFzcz0icHJvdG9ubWFpbF9z
+aWduYXR1cmVfYmxvY2stdXNlciI+PC9kaXY+PGJyPjxicj5TZW50IGZyb20gPGEgdGFyZ2V0PSJf
+YmxhbmsiIGhyZWY9Imh0dHBzOi8vcHJvdG9uLm1lL21haWwvaG9tZSIgcmVsPSJub3JlZmVycmVy
+Ij5Qcm90b24gTWFpbDwvYT4gZm9yIEFuZHJvaWQuPC9ib2R5PjwvaHRtbD4=
+--b1=_WSpo40vhsD8KyQGJNYap4ALwcgqbhrVWd51oUYgyI--
diff --git a/spec/lib/email/cleaner_spec.rb b/spec/lib/email/cleaner_spec.rb
index 802042224d2..66705bcdc29 100644
--- a/spec/lib/email/cleaner_spec.rb
+++ b/spec/lib/email/cleaner_spec.rb
@@ -29,6 +29,15 @@ RSpec.describe Email::Cleaner do
expect(described_class.new(email, rejected: true).execute).to eq(expected_message)
end
+ it "does not mangle encoded text bodies" do
+ raw = email(:base64_encoded_body)
+ email = Mail.new(raw)
+ cleaned_email = Mail.new(described_class.new(raw).execute)
+
+ expect(email.parts[0].body.decoded).to eq(cleaned_email.parts[0].body.decoded)
+ expect(email.parts[1].body.decoded).to eq(cleaned_email.parts[1].body.decoded)
+ end
+
it "does not sort message parts" do
raw = email(:unsorted_parts)
email = Mail.new(raw)