fix(GODT-2212): Preserver Header order in message building

https://github.com/ProtonMail/go-proton-api/pull/100
This commit is contained in:
Leander Beernaert 2023-09-25 14:32:46 +02:00
parent a1a5ffba5d
commit e422b28bc3
7 changed files with 37 additions and 22 deletions

View File

@ -123,6 +123,7 @@ Proton Mail Bridge includes the following 3rd party software:
* [codec](https://github.com/ugorji/go/codec) available under [license](https://github.com/ugorji/go/codec/blob/master/LICENSE)
* [tagparser](https://github.com/vmihailenco/tagparser/v2) available under [license](https://github.com/vmihailenco/tagparser/v2/blob/master/LICENSE)
* [smetrics](https://github.com/xrash/smetrics) available under [license](https://github.com/xrash/smetrics/blob/master/LICENSE)
* [go-ordered-json](https://gitlab.com/c0b/go-ordered-json)
* [arch](https://golang.org/x/arch) available under [license](https://cs.opensource.google/go/x/arch/+/master:LICENSE)
* [crypto](https://golang.org/x/crypto) available under [license](https://cs.opensource.google/go/x/crypto/+/master:LICENSE)
* [mod](https://golang.org/x/mod) available under [license](https://cs.opensource.google/go/x/mod/+/master:LICENSE)

3
go.mod
View File

@ -7,7 +7,7 @@ require (
github.com/Masterminds/semver/v3 v3.2.0
github.com/ProtonMail/gluon v0.17.1-0.20230911134257-5eb2eeebbef5
github.com/ProtonMail/go-autostart v0.0.0-20210130080809-00ed301c8e9a
github.com/ProtonMail/go-proton-api v0.4.1-0.20230915070741-3de73982c764
github.com/ProtonMail/go-proton-api v0.4.1-0.20230925123025-331ad8e6d5ee
github.com/ProtonMail/gopenpgp/v2 v2.7.1-proton
github.com/PuerkitoBio/goquery v1.8.1
github.com/abiosoft/ishell v2.0.0+incompatible
@ -108,6 +108,7 @@ require (
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
gitlab.com/c0b/go-ordered-json v0.0.0-20201030195603-febf46534d5a // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/mod v0.8.0 // indirect

6
go.sum
View File

@ -35,8 +35,8 @@ github.com/ProtonMail/go-message v0.13.1-0.20230526094639-b62c999c85b7 h1:+j+Kd/
github.com/ProtonMail/go-message v0.13.1-0.20230526094639-b62c999c85b7/go.mod h1:NBAn21zgCJ/52WLDyed18YvYFm5tEoeDauubFqLokM4=
github.com/ProtonMail/go-mime v0.0.0-20230322103455-7d82a3887f2f h1:tCbYj7/299ekTTXpdwKYF8eBlsYsDVoggDAuAjoK66k=
github.com/ProtonMail/go-mime v0.0.0-20230322103455-7d82a3887f2f/go.mod h1:gcr0kNtGBqin9zDW9GOHcVntrwnjrK+qdJ06mWYBybw=
github.com/ProtonMail/go-proton-api v0.4.1-0.20230915070741-3de73982c764 h1:2rEmoo5BgEap+9Y484xAL8cod1bbjDaeWaGFLS/a1Ec=
github.com/ProtonMail/go-proton-api v0.4.1-0.20230915070741-3de73982c764/go.mod h1:nS8hMGjJLgC0Iej0JMYbsI388LesEkM1Hj/jCCxQeaQ=
github.com/ProtonMail/go-proton-api v0.4.1-0.20230925123025-331ad8e6d5ee h1:CzFXOiflEZZqT3HQqj2I5AkIprRbc/c6/lToPdEKzxM=
github.com/ProtonMail/go-proton-api v0.4.1-0.20230925123025-331ad8e6d5ee/go.mod h1:Y3ea3i1UbqHz5vq43odmAAd6lmR4nx0ZIQ32tqMfxTY=
github.com/ProtonMail/go-srp v0.0.7 h1:Sos3Qk+th4tQR64vsxGIxYpN3rdnG9Wf9K4ZloC1JrI=
github.com/ProtonMail/go-srp v0.0.7/go.mod h1:giCp+7qRnMIcCvI6V6U3S1lDDXDQYx2ewJ6F/9wdlJk=
github.com/ProtonMail/gopenpgp/v2 v2.7.1-proton h1:YS6M20yvjCJPR1r4ADW5TPn6rahs4iAyZaACei86bEc=
@ -399,6 +399,8 @@ github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRT
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
gitlab.com/c0b/go-ordered-json v0.0.0-20201030195603-febf46534d5a h1:DxppxFKRqJ8WD6oJ3+ZXKDY0iMONQDl5UTg2aTyHh8k=
gitlab.com/c0b/go-ordered-json v0.0.0-20201030195603-febf46534d5a/go.mod h1:NREvu3a57BaK0R1+ztrEzHWiZAihohNLQ6trPxlIqZI=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=

View File

@ -340,10 +340,9 @@ func TestDownloadStage_JobAbortsOnAttachmentDownloadError(t *testing.T) {
MessageMetadata: proton.MessageMetadata{
ID: "msg",
},
Header: "",
ParsedHeaders: nil,
Body: "",
MIMEType: "",
Header: "",
Body: "",
MIMEType: "",
Attachments: []proton.Attachment{{
ID: "attach",
}},
@ -387,11 +386,10 @@ func buildDownloadStageData(tj *tjob, numMessages int, with422 bool) ([]string,
ID: msgID,
Size: len([]byte(msgID)),
},
Header: "",
ParsedHeaders: nil,
Body: msgID,
MIMEType: "",
Attachments: nil,
Header: "",
Body: msgID,
MIMEType: "",
Attachments: nil,
},
AttData: nil,
}

View File

@ -503,8 +503,8 @@ func getAttachmentPartHeader(att proton.Attachment) message.Header {
func toMessageHeader(hdr proton.Headers) message.Header {
var res message.Header
for key, val := range hdr {
for _, val := range val {
for _, key := range hdr.Order {
for _, val := range hdr.Values[key] {
// Using AddRaw instead of Add to save key-value pair as byte buffer within Header.
// This buffer is used latter on in message writer to construct message and avoid crash
// when key length is more than 76 characters long.

View File

@ -64,12 +64,20 @@ func newTestMessageWithHeaders(
func newRawTestMessageWithHeaders(messageID, addressID, mimeType, body string, date time.Time, headers map[string][]string) proton.Message {
msgHeaders := proton.Headers{
"Content-Type": {mimeType},
"Date": {date.In(time.UTC).Format(time.RFC1123Z)},
Values: map[string][]string{
"Content-Type": {mimeType},
"Date": {date.In(time.UTC).Format(time.RFC1123Z)},
},
Order: []string{"Content-Type", "Date"},
}
for k, v := range headers {
msgHeaders[k] = v
_, ok := msgHeaders.Values[k]
if !ok {
msgHeaders.Order = append(msgHeaders.Order, k)
}
msgHeaders.Values[k] = v
}
return proton.Message{
@ -98,9 +106,12 @@ func addTestAttachment(
Name: name,
MIMEType: rfc822.MIMEType(mimeType),
Headers: proton.Headers{
"Content-Type": {mimeType},
"Content-Disposition": {disposition},
"Content-Transfer-Encoding": {"base64"},
Values: map[string][]string{
"Content-Type": {mimeType},
"Content-Disposition": {disposition},
"Content-Transfer-Encoding": {"base64"},
},
Order: []string{"Content-Type", "Content-Disposition", "Content-Transfer-Encoding"},
},
Disposition: proton.Disposition(disposition),
KeyPackets: base64.StdEncoding.EncodeToString(enc.GetBinaryKeyPacket()),

View File

@ -54,7 +54,8 @@ func TestBuildPlainMessageWithLongKey(t *testing.T) {
kr := utils.MakeKeyRing(t)
msg := newTestMessage(t, kr, "messageID", "addressID", "text/plain", "body", time.Now())
msg.ParsedHeaders["ReallyVeryVeryVeryVeryVeryLongLongLongLongLongLongLongKeyThatWillHaveNotSoLongValue"] = []string{"value"}
msg.ParsedHeaders.Values["ReallyVeryVeryVeryVeryVeryLongLongLongLongLongLongLongKeyThatWillHaveNotSoLongValue"] = []string{"value"}
msg.ParsedHeaders.Order = append(msg.ParsedHeaders.Order, "ReallyVeryVeryVeryVeryVeryLongLongLongLongLongLongLongKeyThatWillHaveNotSoLongValue")
res, err := DecryptAndBuildRFC822(kr, msg, nil, JobOptions{})
require.NoError(t, err)
@ -977,7 +978,8 @@ func TestBuildIncludeMessageIDReference(t *testing.T) {
msg := newTestMessage(t, kr, "messageID", "addressID", "text/plain", "body", time.Now())
// Add references.
msg.ParsedHeaders["References"] = []string{"<myreference@domain.com>"}
msg.ParsedHeaders.Values["References"] = []string{"<myreference@domain.com>"}
msg.ParsedHeaders.Order = append(msg.ParsedHeaders.Order, "References")
res, err := DecryptAndBuildRFC822(kr, msg, nil, JobOptions{})
require.NoError(t, err)