Skip to content

Commit

Permalink
fix(GODT-2582): Fix hash for messages
Browse files Browse the repository at this point in the history
When hashing child parts, do not hash the boundary parameter of the
`Content-Type` header field as this is unique to each message.
  • Loading branch information
LBeernaertProton committed Apr 26, 2023
1 parent 8ec759b commit aed4b8a
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 61 deletions.
26 changes: 24 additions & 2 deletions rfc822/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"crypto/sha256"
"encoding/base64"
"github.com/sirupsen/logrus"
"strings"
)

// GetMessageHash returns the hash of the given message.
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions tests/recovery_mailbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
46 changes: 46 additions & 0 deletions tests/testdata/duplicate.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
From: "Testing" <foobar@protonmail.com>
To: "Test 123" <barfoo@gmail.com>
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

<div style=3D"font-family: Arial, sans-serif; font-size: 14px; color: =
rgb(0, 0, 0);">Simple Body<br></div>
------=_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--
57 changes: 0 additions & 57 deletions tests/testdata/multipart-mixed2.eml

This file was deleted.

46 changes: 46 additions & 0 deletions tests/testdata/original.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
From: "Testing" <foobar@protonmail.com>
To: "Test 123" <barfoo@gmail.com>
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

<div style=3D"font-family: Arial, sans-serif; font-size: 14px; color: =
rgb(0, 0, 0);">Simple Body<br></div>
------=_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--

0 comments on commit aed4b8a

Please sign in to comment.