Skip to content

Commit

Permalink
fix(GODT-2851): Determine Body MIMEType for all Multipart when import…
Browse files Browse the repository at this point in the history
…ing to backend.
  • Loading branch information
rlejeune74 committed Nov 30, 2023
1 parent 0ee691e commit e8aa47d
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 26 deletions.
84 changes: 76 additions & 8 deletions message_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package proton_test

import (
"context"
"github.com/ProtonMail/gluon/rfc822"
"github.com/bradenaw/juniper/stream"
"reflect"
"testing"

"github.com/ProtonMail/gluon/rfc822"
"github.com/ProtonMail/go-proton-api"
"github.com/ProtonMail/go-proton-api/server"
"github.com/bradenaw/juniper/stream"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -89,14 +89,82 @@ func TestMessageImport_RelatedInlinePlaintext(t *testing.T) {
want rfc822.MIMEType
}{
{
name: "RelatedInlinePlaintext",
literal: "\"From: Nathaniel Borenstein <[email protected]>\\nTo: Ned Freed <[email protected]>\\nSubject: Sample message (import inline)\\nMIME-Version: 1.0\\nContent-type: multipart/related; boundary=\\\"BOUNDARY\\\"\\n\\n--BOUNDARY\\nContent-type: text/plain; charset=us-ascii\\n\\nHello world\\n--BOUNDARY\\nContent-Type: image/gif; name=\\\"email-action-left.gif\\\"\\nContent-Transfer-Encoding: base64\\nContent-ID: <[email protected]>\\nContent-Disposition: inline; filename=\\\"email-action-left.gif\\\"\\n\\nSGVsbG8gQXR0YWNobWVudA==\\n--BOUNDARY--\"",
want: rfc822.TextPlain,
name: "RelatedInlinePlaintext",
literal: "From: Nathaniel Borenstein <[email protected]>\n" +
"To: Ned Freed <[email protected]>\n" +
"Subject: Sample message (import inline)\n" +
"MIME-Version: 1.0\nContent-type: multipart/related; boundary=\"BOUNDARY\"\n\n" +
"--BOUNDARY\nContent-type: text/plain; charset=us-ascii\n\n" +
"Hello world\n" +
"--BOUNDARY\n" +
"Content-Type: image/gif; name=\"email-action-left.gif\"\n" +
"Content-Transfer-Encoding: base64\n" +
"Content-ID: <[email protected]>\n" +
"Content-Disposition: inline; filename=\"email-action-left.gif\"\n\n" +
"SGVsbG8gQXR0YWNobWVudA==\n" +
"--BOUNDARY--",
want: rfc822.TextPlain,
},
{
name: "RelatedInlineHTML",
literal: "From: Bridge Second Test <[email protected]>\n" +
"To: Bridge Test <[email protected]>\n" +
"Subject: Html Inline Importing\n" +
"Content-Type: multipart/related; boundary=\"61FA22A41A3F46E8E90EF528\"\n\n" +
"This is a multi-part message in MIME format.\n" +
"--61FA22A41A3F46E8E90EF528\n" +
"Content-Type: text/html; charset=utf-8\n" +
"Content-Transfer-Encoding: 7bit\n\n" +
"<html>\n<head>\n<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n</head>\n" +
"<body text=\"#000000\" bgcolor=\"#FFFFFF\">\n" +
"<p><br>\n</p>\n<p>Behold! An inline <img moz-do-not-send=\"false\"\nsrc=\"cid:[email protected]\" alt=\"\"\nwidth=\"24\" height=\"24\"><br>\n</p>\n" +
"</body>\n</html>\n\n" +
"--61FA22A41A3F46E8E90EF528\n" +
"Content-Type: image/gif; name=\"email-action-left.gif\"\n" +
"Content-Transfer-Encoding: base64\nContent-ID: <[email protected]>\n" +
"Content-Disposition: inline; filename=\"email-action-left.gif\"\n\n" +
"R0lGODlhGAAYANUAACcsKOHs4kppTH6tgYWxiIq0jTVENpG5lDI/M7bRuEaJSkqOTk2RUU+P\nU16lYl+lY2iva262cXS6d3rDfYLNhWeeamKTZGSVZkNbRGqhbOPt4////+7u7qioqFZWVlNT\nUyIiIgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAAGAAYAAAG\n/8CNcLjRJAqVRqNSSGiI0GFgoKhar4NAdHioMhyRCYUyiTgY1cOWUH1ILgIDAGAQXCSPKgHa\nXUAyGCCCg4IYGRALCmpCAVUQFgiEkiAIFhBVWhtUDxmRk5IIGXkDRQoMEoGfHpIYEmhGCg4X\nnyAdHB+SFw4KRwoRArQdG7eEAhEKSAoTBoIdzs/Cw7iCBhMKSQoUAIJbQ8QgABQKStnbIN1C\n3+HjFcrMtdDO6dMg1dcFvsCfwt+CxsgJYs3a10+QLl4aTKGitYpQq1eaFHDyREtQqFGMHEGq\nSMkSJi4K/ACiZQiRIihsJL6JM6fOnTwK9kTpYgqMGDJm0JzsNuWKTw0FWdANMYJECRMnW4IA\nADs=\n\n" +
"--61FA22A41A3F46E8E90EF528--",
want: rfc822.TextHTML,
},
{
name: "Multipart/mixed single part plain",
literal: "From: Nathaniel Borenstein <[email protected]>\n" +
"To: Ned Freed <[email protected]>\n" +
"Subject: Sample message (import inline)\n" +
"MIME-Version: 1.0\nContent-type: multipart/mixed; boundary=\"BOUNDARY\"\n\n" +
"--BOUNDARY\nContent-type: text/plain; charset=us-ascii\n\n" +
"Hello world\n" +
"--BOUNDARY\n" +
"Content-Type: image/gif; name=\"email-action-left.gif\"\n" +
"Content-Transfer-Encoding: base64\n" +
"Content-ID: <[email protected]>\n" +
"Content-Disposition: inline; filename=\"email-action-left.gif\"\n\n" +
"SGVsbG8gQXR0YWNobWVudA==\n" +
"--BOUNDARY--",
want: rfc822.TextPlain,
},
{
name: "RelatedInlineHTML",
literal: "Date: 01 Jan 1980 00:00:00 +0000\nFrom: Bridge Second Test <[email protected]>\nTo: Bridge Test <[email protected]>\nSubject: Html Inline Importing\nContent-Disposition: inline\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.0\nMIME-Version: 1.0\nContent-Language: en-US\nContent-Type: multipart/related; boundary=\"61FA22A41A3F46E8E90EF528\"\n\nThis is a multi-part message in MIME format.\n--61FA22A41A3F46E8E90EF528\nContent-Type: text/html; charset=utf-8\nContent-Transfer-Encoding: 7bit\n\n<html>\n<head>\n<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n</head>\n<body text=\"#000000\" bgcolor=\"#FFFFFF\">\n<p><br>\n</p>\n<p>Behold! An inline <img moz-do-not-send=\"false\"\nsrc=\"cid:[email protected]\" alt=\"\"\nwidth=\"24\" height=\"24\"><br>\n</p>\n</body>\n</html>\n\n--61FA22A41A3F46E8E90EF528\nContent-Type: image/gif; name=\"email-action-left.gif\"\nContent-Transfer-Encoding: base64\nContent-ID: <[email protected]>\nContent-Disposition: inline; filename=\"email-action-left.gif\"\n\nR0lGODlhGAAYANUAACcsKOHs4kppTH6tgYWxiIq0jTVENpG5lDI/M7bRuEaJSkqOTk2RUU+P\nU16lYl+lY2iva262cXS6d3rDfYLNhWeeamKTZGSVZkNbRGqhbOPt4////+7u7qioqFZWVlNT\nUyIiIgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAAGAAYAAAG\n/8CNcLjRJAqVRqNSSGiI0GFgoKhar4NAdHioMhyRCYUyiTgY1cOWUH1ILgIDAGAQXCSPKgHa\nXUAyGCCCg4IYGRALCmpCAVUQFgiEkiAIFhBVWhtUDxmRk5IIGXkDRQoMEoGfHpIYEmhGCg4X\nnyAdHB+SFw4KRwoRArQdG7eEAhEKSAoTBoIdzs/Cw7iCBhMKSQoUAIJbQ8QgABQKStnbIN1C\n3+HjFcrMtdDO6dMg1dcFvsCfwt+CxsgJYs3a10+QLl4aTKGitYpQq1eaFHDyREtQqFGMHEGq\nSMkSJi4K/ACiZQiRIihsJL6JM6fOnTwK9kTpYgqMGDJm0JzsNuWKTw0FWdANMYJECRMnW4IA\nADs=\n\n--61FA22A41A3F46E8E90EF528--",
want: rfc822.TextHTML,
name: "Multipart/mixed single part HTML",
literal: "From: Bridge Second Test <[email protected]>\n" +
"To: Bridge Test <[email protected]>\n" +
"Subject: Html Inline Importing\n" +
"Content-Type: multipart/mixed; boundary=\"61FA22A41A3F46E8E90EF528\"\n\n" +
"This is a multi-part message in MIME format.\n" +
"--61FA22A41A3F46E8E90EF528\n" +
"Content-Type: text/html; charset=utf-8\n" +
"Content-Transfer-Encoding: 7bit\n\n" +
"<html>\n<head>\n<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n</head>\n" +
"<body text=\"#000000\" bgcolor=\"#FFFFFF\">\n" +
"<p><br>\n</p>\n<p>Behold! An inline <img moz-do-not-send=\"false\"\nsrc=\"cid:[email protected]\" alt=\"\"\nwidth=\"24\" height=\"24\"><br>\n</p>\n" +
"</body>\n</html>\n\n" +
"--61FA22A41A3F46E8E90EF528\n" +
"Content-Type: image/gif; name=\"email-action-left.gif\"\n" +
"Content-Transfer-Encoding: base64\nContent-ID: <[email protected]>\n" +
"Content-Disposition: inline; filename=\"email-action-left.gif\"\n\n" +
"R0lGODlhGAAYANUAACcsKOHs4kppTH6tgYWxiIq0jTVENpG5lDI/M7bRuEaJSkqOTk2RUU+P\nU16lYl+lY2iva262cXS6d3rDfYLNhWeeamKTZGSVZkNbRGqhbOPt4////+7u7qioqFZWVlNT\nUyIiIgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAAGAAYAAAG\n/8CNcLjRJAqVRqNSSGiI0GFgoKhar4NAdHioMhyRCYUyiTgY1cOWUH1ILgIDAGAQXCSPKgHa\nXUAyGCCCg4IYGRALCmpCAVUQFgiEkiAIFhBVWhtUDxmRk5IIGXkDRQoMEoGfHpIYEmhGCg4X\nnyAdHB+SFw4KRwoRArQdG7eEAhEKSAoTBoIdzs/Cw7iCBhMKSQoUAIJbQ8QgABQKStnbIN1C\n3+HjFcrMtdDO6dMg1dcFvsCfwt+CxsgJYs3a10+QLl4aTKGitYpQq1eaFHDyREtQqFGMHEGq\nSMkSJi4K/ACiZQiRIihsJL6JM6fOnTwK9kTpYgqMGDJm0JzsNuWKTw0FWdANMYJECRMnW4IA\nADs=\n\n" +
"--61FA22A41A3F46E8E90EF528--",
want: rfc822.TextHTML,
},
}
for _, tt := range tests {
Expand Down
37 changes: 19 additions & 18 deletions server/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,26 +486,27 @@ func (s *Server) parseMessage(literal []byte) (*rfc822.Header, []string, []*rfc8

// Force all multipart types to be multipart/mixed.
if mimeType.Type() == "multipart" {
if mimeType == rfc822.MultipartRelated {
children, err := root.Children()
if err != nil {
mimeType = "multipart/mixed"
} else {
var isHtml = false
for _, child := range children {
contentType, _, err := child.ContentType()
if err == nil && contentType == rfc822.TextHTML {
isHtml = true
}
}
if isHtml {
mimeType = "text/html"
} else {
mimeType = "text/plain"
mimeType = "multipart/mixed"
children, err := root.Children()
// or determine it if there is only one (non-attachment) child
if err == nil && (len(children) - len(atts)) <= 1 {
var isHtml = false
var isTxt = false
for _, child := range children {
contentType, _, err := child.ContentType()
if err != nil {
continue
}else if contentType == rfc822.TextHTML {
isHtml = true
} else if contentType == rfc822.TextPlain {
isTxt = true
}
}
} else {
mimeType = "multipart/mixed"
if isHtml {
mimeType = "text/html"
} else if isTxt {
mimeType = "text/plain"
}
}
}

Expand Down

0 comments on commit e8aa47d

Please sign in to comment.