Skip to content

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

FIX: Email::Cleaner must tell Mail to not sort body parts on serialisation

Michael Brown4b4836f8f1b6
diff --git a/lib/email/cleaner.rb b/lib/email/cleaner.rb
index 16668dd3fb7..961a67fc5a2 100644
--- a/lib/email/cleaner.rb
+++ b/lib/email/cleaner.rb
@@ -5,6 +5,12 @@ module Email
def initialize(mail, remove_attachments: true, truncate: true, rejected: false)
@mail = Mail.new(mail)
@mail.charset = "UTF-8"
+ # The default sort order may incorrectly put postscripts to the message
+ # (e.g. such as those that might be appended by mailing lists as in
+ # https://meta.discourse.org/t/377793/21) ahead of "real content"
+ #
+ # default is: "text/plain", "text/enriched", "text/html", "multipart/alternative"
+ @mail.body.set_sort_order([])
@remove_attachments = remove_attachments
@truncate = truncate
@rejected = rejected
diff --git a/spec/fixtures/emails/unsorted_parts.eml b/spec/fixtures/emails/unsorted_parts.eml
new file mode 100644
index 00000000000..be3b1e6b0c4
--- /dev/null
+++ b/spec/fixtures/emails/unsorted_parts.eml
@@ -0,0 +1,56 @@
+Subject: [tor-relays] Subject
+Date: Wed 14 Jan 2026 06:21:10 PM +0000
+Message-ID: <[email protected]>
+From: Bilbo Baggins via tor-relays <[email protected]>
+Reply-To: Bilbo Baggins <[email protected]>
+Content-Type: multipart/mixed; boundary="===============8958541500975114832=="
+
+--===============8958541500975114832==
+Content-Type: multipart/signed; boundary="Sig_/gizYC_1dGsAzUHvksdaMIe2";
+ protocol="application/pgp-signature"; micalg=pgp-sha256
+
+--Sig_/gizYC_1dGsAzUHvksdaMIe2
+Content-Type: text/plain; charset=US-ASCII
+Content-Transfer-Encoding: quoted-printable
+
+Here is the actual message content.
+
+--=20
+kind regards
+Bilbo
+
+--Sig_/gizYC_1dGsAzUHvksdaMIe2
+Content-Type: application/pgp-signature
+Content-Description: Digitale Signatur von OpenPGP
+
+-----BEGIN PGP SIGNATURE-----
+
+iQJPBAEBCAA5FiEEpXefSZn9R6zNZtTQE76RLz2tRfAFAmlYvpEbFIAAAAAABAAO
+bWFudTIsMi41KzEuMTEsMiwyAAoJEBO+kS89rUXw8kgP/2jkrwfSWHY6EY4WJjn6
+EDEqT00pgpwEn9ZpUqLTreS3/ocfHC4g29HIsxpJcj/bH+hNAx96HEz9YmC4JfEt
+LDjYc6D+5NBBFQGy0vaJ/LXLQc63CRE/yySSOYxFBZK+uMytNHoZDTjhfRroICbQ
+guoO7A4/VuYrGAzCWQkBUmnBjj2LJhuLDW84ObMXhA/EuNy5FIAqyLZxoGmFEfvu
+We5d0Hr3+wihzyrgGiG4u8UGFOyL+/PC11CFQyQ0j03cBzhZ5PVdtkqPNHauAcjQ
+Gt/HQmaOSGKq0VODRjiHAe5TuRtV6jOfUNgS1Q2vB4FKYmeDQb82ooNfOiJWy3ey
+Jpwgg700ppqgZUclpMPlzxKwi2dT/PSO6yYuy+G5sfa0Hxmn5DsQaiSPMTiEP2WC
+NwAENYIuHeQOHWiS8B3oVSRW/naLzkmpfChFnTKGsrhLqKQc/iuvv639aHwg9BP7
+YEbWbdpFpIU36czfxoTcDYDR1e4JLWryEFKIgo4TIaz4t17NmkxjXB6dHZKLLAdU
+AT6LmL6mOTaXe9ewD9pf9Vf2nG0RGVJyZRUDmFzfU0Rx2qi7KdcmmRpZg/2QtJeA
+Pmrv8NFuFEL0BrhTvo7C60m+gjLaXPNClgKEN0vkEzjLp/ZKjI9FslP61xUMg8lQ
+3xT/HTkNt9uNH2ziBMXLK+5c
+=0Euf
+-----END PGP SIGNATURE-----
+
+--Sig_/gizYC_1dGsAzUHvksdaMIe2--
+
+--===============8958541500975114832==
+Content-Type: text/plain; charset="us-ascii"
+MIME-Version: 1.0
+Content-Transfer-Encoding: 7bit
+Content-Disposition: inline
+
+_______________________________________________
+tor-relays mailing list -- [email protected]
+To unsubscribe send an email to [email protected]
+
+--===============8958541500975114832==--
diff --git a/spec/lib/email/cleaner_spec.rb b/spec/lib/email/cleaner_spec.rb
index 82ed9ad37d8..802042224d2 100644
--- a/spec/lib/email/cleaner_spec.rb
+++ b/spec/lib/email/cleaner_spec.rb
@@ -28,4 +28,16 @@ RSpec.describe Email::Cleaner do
"Return-Path: <[email protected]>\r\nDate: Sat, 30 Jan 2016 01:10:11 +0100\r\nFrom: Foo Bar <[email protected]>\r\nTo: [email protected]\r\nMessage-ID: <[email protected]>\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed;\r\n boundary=\"--==_mimepart_56abff5d49749_ddf83fca6d033a28548ad\";\r\n charset=UTF-8\r\nContent-Transfer-Encoding: 7bit\r\n\r\n\r\n----==_mimepart_56abff5d49749_ddf83fca6d033a28548ad\r\nContent-Type: text/plain;\r\n charset=UTF-8\r\nContent-Transfer-Encoding: 7bit\r\n\r\nPlease fin\r\n----==_mimepart_56abff5d49749_ddf83fca6d033a28548ad--\r\n"
expect(described_class.new(email, rejected: true).execute).to eq(expected_message)
end
+
+ it "does not sort message parts" do
+ raw = email(:unsorted_parts)
+ email = Mail.new(raw)
+ cleaned_email = Mail.new(described_class.new(raw).execute)
+
+ expect(email.parts.length).to eq(cleaned_email.parts.length)
+ email.parts.length.times do |i|
+ next if email.parts[i].multipart?
+ expect(email.parts[i].body.decoded.to_s).to eq(cleaned_email.parts[i].body.decoded.to_s)
+ end
+ end
end