From 713eb62bc1e3b4b0855a7bc47b1b4c3a6fa0c8a8 Mon Sep 17 00:00:00 2001 From: Beowulf Date: Mon, 13 May 2024 22:25:06 +0200 Subject: [PATCH] do not add text/plain inline attachments as attachments to the comment text/plain inline attachments are parsed into the body of the comment, so we do not add them as an attachment to avoid duplicates The test for the signature has been changed. Messages are now no longer cut off after "-", "--" or "---", which may cause some signatures to be added to the comment, but it's better to add a little more rather than cut off important content. Everything after more than 4 "-" are cut off. "--" is used as a separator between inline attachments "---" is also often used by users to separate paragraphs --- services/mailer/incoming/incoming.go | 2 +- services/mailer/incoming/incoming_test.go | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/services/mailer/incoming/incoming.go b/services/mailer/incoming/incoming.go index 5baff6ec17..ac6f32c540 100644 --- a/services/mailer/incoming/incoming.go +++ b/services/mailer/incoming/incoming.go @@ -379,7 +379,7 @@ func getContentFromMailReader(env *enmime.Envelope) *MailContent { } inlineAttachments := make([]*Attachment, 0, len(env.Inlines)) for _, inline := range env.Inlines { - if inline.FileName != "" { + if inline.FileName != "" && inline.ContentType != "text/plain" { inlineAttachments = append(inlineAttachments, &Attachment{ Name: inline.FileName, Content: inline.Content, diff --git a/services/mailer/incoming/incoming_test.go b/services/mailer/incoming/incoming_test.go index f2bb7fc498..001374d371 100644 --- a/services/mailer/incoming/incoming_test.go +++ b/services/mailer/incoming/incoming_test.go @@ -125,15 +125,27 @@ func TestGetContentFromMailReader(t *testing.T) { "Content-Disposition: inline; filename=attachment.txt\r\n" + "\r\n" + "attachment content\r\n" + + "--message-boundary\r\n" + + "Content-Type: text/html\r\n" + + "Content-Disposition: inline; filename=attachment.html\r\n" + + "\r\n" + + "

html attachment content

\r\n" + + "--message-boundary\r\n" + + "Content-Type: image/png\r\n" + + "Content-Disposition: inline; filename=attachment.png\r\n" + + "Content-Transfer-Encoding: base64\r\n" + + "\r\n" + + "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAABlBMVEX///+/v7+jQ3Y5AAAADklEQVQI12P4AIX8EAgALgAD/aNpbtEAAAAASUVORK5CYII\r\n" + "--message-boundary--\r\n" env, err = enmime.ReadEnvelope(strings.NewReader(mailString)) assert.NoError(t, err) content = getContentFromMailReader(env) - assert.Equal(t, "mail content", content.Content) - assert.Len(t, content.Attachments, 1) - assert.Equal(t, "attachment.txt", content.Attachments[0].Name) - assert.Equal(t, []byte("attachment content"), content.Attachments[0].Content) + assert.Equal(t, "mail content\n--\nattachment content", content.Content) + assert.Len(t, content.Attachments, 2) + assert.Equal(t, "attachment.html", content.Attachments[0].Name) + assert.Equal(t, []byte("

html attachment content

"), content.Attachments[0].Content) + assert.Equal(t, "attachment.png", content.Attachments[1].Name) mailString = "Content-Type: multipart/mixed; boundary=message-boundary\r\n" + "\r\n" + @@ -164,7 +176,7 @@ func TestGetContentFromMailReader(t *testing.T) { "Content-Disposition: inline\r\n" + "\r\n" + "mail content without signature\r\n" + - "--\r\n" + + "----\r\n" + "signature\r\n" + "--text-boundary--\r\n" + "--message-boundary--\r\n"