diff --git a/rfc822/hash.go b/rfc822/hash.go index 8900f235..201b675a 100644 --- a/rfc822/hash.go +++ b/rfc822/hash.go @@ -4,6 +4,8 @@ import ( "bytes" "crypto/sha256" "encoding/base64" + "github.com/sirupsen/logrus" + "strings" ) // GetMessageHash returns the hash of the given message. @@ -60,8 +62,28 @@ func GetMessageHash(b []byte) (string, error) { return err } - if _, err := h.Write([]byte(header.Get("Content-Type"))); err != nil { - return err + contentType := header.Get("Content-Type") + mimeType, values, err := ParseMIMEType(contentType) + if err != nil { + logrus.Warnf("Message contains invalid mime type: %v", contentType) + } else { + if _, err := h.Write([]byte(mimeType)); err != nil { + return err + } + + for k, v := range values { + if strings.EqualFold(k, "boundary") { + continue + } + + if _, err := h.Write([]byte(k)); err != nil { + return err + } + + if _, err := h.Write([]byte(v)); err != nil { + return err + } + } } if _, err := h.Write([]byte(header.Get("Content-Disposition"))); err != nil { diff --git a/tests/recovery_mailbox_test.go b/tests/recovery_mailbox_test.go index 219a0945..aee4ac8a 100644 --- a/tests/recovery_mailbox_test.go +++ b/tests/recovery_mailbox_test.go @@ -249,8 +249,8 @@ func TestRecoveryMailboxOnlyReportsOnFirstDedupedMessage(t *testing.T) { status, err := client.Select("INBOX", false) require.NoError(t, err) require.Equal(t, uint32(0), status.Messages) - require.Error(t, doAppendWithClientFromFile(t, client, "INBOX", "testdata/multipart-mixed.eml", time.Now())) - require.Error(t, doAppendWithClientFromFile(t, client, "INBOX", "testdata/multipart-mixed2.eml", time.Now())) + require.Error(t, doAppendWithClientFromFile(t, client, "INBOX", "testdata/original.eml", time.Now())) + require.Error(t, doAppendWithClientFromFile(t, client, "INBOX", "testdata/duplicate.eml", time.Now())) { status, err := client.Status(ids.GluonRecoveryMailboxName, []goimap.StatusItem{goimap.StatusMessages}) diff --git a/tests/testdata/duplicate.eml b/tests/testdata/duplicate.eml new file mode 100644 index 00000000..e0bd03e7 --- /dev/null +++ b/tests/testdata/duplicate.eml @@ -0,0 +1,46 @@ +From: "Testing" +To: "Test 123" +Subject: Small doc attachment VALID v5 +Date: Wed, 26 Apr 2023 08:25:16 +0200 +MIME-Version: 1.0 +Content-Type: multipart/mixed; + boundary="----=_NextPart_000_0022_01D97836.53531060" +X-Mailer: Microsoft Outlook 16.0 +Content-Language: en-us +X-OlkEid: 00000000225333D1866CB24A8049706F3452C40E0700C3B68E10F77511CEB4CD00AA00BBB6E600000000000C00003BB97E4DCE360843BCF25AA31956C0E2000000000408000048AD3E35A4DC1A47ABA5F573BB76D09A + +This is a multipart message in MIME format. + +------=_NextPart_000_0022_01D97836.53531060 +Content-Type: multipart/alternative; + boundary="----=_NextPart_001_0023_01D97836.53531060" + + +------=_NextPart_001_0023_01D97836.53531060 +Content-Type: text/plain; + charset="utf-8" +Content-Transfer-Encoding: 7bit + +Simple Body + + +------=_NextPart_001_0023_01D97836.53531060 +Content-Type: text/html; + boundary="----=_NextPart_000_003E_01D97831.767F38C0"; + charset="utf-8" +Content-Transfer-Encoding: quoted-printable + +
Simple Body
+------=_NextPart_001_0023_01D97836.53531060-- + +------=_NextPart_000_0022_01D97836.53531060 +Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document; + name="test.docx" +Content-Transfer-Encoding: base64 +Content-Disposition: attachment; + filename="test.docx" + +UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC + +------=_NextPart_000_0022_01D97836.53531060-- diff --git a/tests/testdata/multipart-mixed2.eml b/tests/testdata/multipart-mixed2.eml deleted file mode 100644 index 0f558e0f..00000000 --- a/tests/testdata/multipart-mixed2.eml +++ /dev/null @@ -1,57 +0,0 @@ -Return-Path: -Received: from [10.1.1.121] ([185.159.157.131]) - by smtp.gmail.com with ESMTPSA id t8sm14889112wrr.10.2021.03.26.12.01.23 - for - (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); - Fri, 26 Mar 2021 12:01:24 -0700 (PDT) -To: somebody@gmail.com -From: BQA -Subject: Simple test mail -Message-ID: -Date: Fri, 26 Mar 2021 20:01:23 +0100 -User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) - Gecko/20100101 Thunderbird/78.8.1 -MIME-Version: 1.0 -Content-Type: multipart/mixed; - boundary="------------9DC5F36D876D5EED478B5FF9" -Content-Language: en-US - -This is a multi-part message in MIME format. ---------------9DC5F36D876D5EED478B5FF9 -Content-Type: multipart/alternative; - boundary="------------00FYF50B21CF279F489F0184" - - ---------------00FYF50B21CF279F489F0184 -Content-Type: text/plain; charset=utf-8; format=flowed -Content-Transfer-Encoding: 7bit - -*this */is**/_html_ -** - ---------------00FYF50B21CF279F489F0184 -Content-Type: text/html; charset=utf-8 -Content-Transfer-Encoding: 7bit - - - - - - - - this is html
- - - - ---------------00FYF50B21CF279F489F0184-- - ---------------9DC5F36D876D5EED478B5FF9 -Content-Type: text/plain; charset=UTF-8; x-mac-type="0"; x-mac-creator="0"; - name="thing.txt" -Content-Transfer-Encoding: base64 -Content-Disposition: attachment; - filename="thing.txt" - -dGhpcyBpcyBteSBhdHRhY2htZW50Cg== ---------------9DC5F36D876D5EED478B5FF9-- diff --git a/tests/testdata/original.eml b/tests/testdata/original.eml new file mode 100644 index 00000000..858995c8 --- /dev/null +++ b/tests/testdata/original.eml @@ -0,0 +1,46 @@ +From: "Testing" +To: "Test 123" +Subject: Small doc attachment VALID v5 +Date: Wed, 26 Apr 2023 08:25:16 +0200 +MIME-Version: 1.0 +Content-Type: multipart/mixed; + boundary="----=_NextPart_000_003E_01D97831.767F38C0" +X-Mailer: Microsoft Outlook 16.0 +Content-Language: en-us +X-OlkEid: 00000000225333D1866CB24A8049706F3452C40E0700C3B68E10F77511CEB4CD00AA00BBB6E600000000000C00003BB97E4DCE360843BCF25AA31956C0E20000000004030000DD8E24308ACA7243B56646E28B20359C + +This is a multipart message in MIME format. + +------=_NextPart_000_003E_01D97831.767F38C0 +Content-Type: multipart/alternative; + boundary="----=_NextPart_001_003F_01D97831.767F38C0" + + +------=_NextPart_001_003F_01D97831.767F38C0 +Content-Type: text/plain; + charset="utf-8" +Content-Transfer-Encoding: 7bit + +Simple Body + + +------=_NextPart_001_003F_01D97831.767F38C0 +Content-Type: text/html; + boundary="b1_CZjzIg4XCgPBIDFY9EOw4CTGxwvt0c6cIuThfhnqmVc"; + charset="utf-8" +Content-Transfer-Encoding: quoted-printable + +
Simple Body
+------=_NextPart_001_003F_01D97831.767F38C0-- + +------=_NextPart_000_003E_01D97831.767F38C0 +Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document; + name="test.docx" +Content-Transfer-Encoding: base64 +Content-Disposition: attachment; + filename="test.docx" + +UEsDBBQABgAIAAAAIQDfpNJsWgEAACAFAAATAAgCW0NvbnRlbnRfVHlwZXNdLnhtbCCiBAIooAAC + +------=_NextPart_000_003E_01D97831.767F38C0--