From b7809d9c387c7338402d5e5a9ef2d3f74c7b80bc Mon Sep 17 00:00:00 2001 From: Sebastijan Zindl Date: Fri, 10 Jul 2026 12:42:38 +0200 Subject: [PATCH] fix(BRIDGE-582): empty address list return nil instead of empty list per RFC3501 --- imap/envelope.go | 2 +- imap/envelope_test.go | 29 +++++++++++++++++++++++++++++ imap/params.go | 4 ++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/imap/envelope.go b/imap/envelope.go index df5a5afc..4085d1fd 100644 --- a/imap/envelope.go +++ b/imap/envelope.go @@ -80,7 +80,7 @@ func tryParseAddressList(val string) []*mail.Address { addr, err := rfc5322.ParseAddressList(val) if err != nil { logrus.WithError(err).Error("Failed to parse address") - return []*mail.Address{{Name: val}} + return nil // RFC3501 empty address list should return NIL } return addr diff --git a/imap/envelope_test.go b/imap/envelope_test.go index d933be80..733990d4 100644 --- a/imap/envelope_test.go +++ b/imap/envelope_test.go @@ -24,3 +24,32 @@ func TestEnvelope(t *testing.T) { assert.Equal(t, "(\"Sat, 03 Apr 2021 15:13:53 +0000\" \"this is currently a draft\" ((NIL NIL \"somebody\" \"pm.me\")) ((NIL NIL \"somebody\" \"pm.me\")) ((NIL NIL \"somebody\" \"pm.me\")) ((\"Somebody\" NIL \"somebody\" \"pm.me\")) NIL NIL NIL \"\")", envelope) } + +func TestEnvelopeEmptyAddressList(t *testing.T) { + t.Parallel() + + for name, tc := range map[string]struct { + msg string + envelope string + }{ + "empty To": { + msg: "From: a@b.com\r\nTo:\r\nSubject: test\r\n\r\nbody", + envelope: `(NIL "test" ((NIL NIL "a" "b.com")) ((NIL NIL "a" "b.com")) ((NIL NIL "a" "b.com")) NIL NIL NIL NIL NIL)`, + }, + "empty Cc": { + msg: "From: a@b.com\r\nTo: x@y.com\r\nCc:\r\nSubject: test\r\n\r\nbody", + envelope: `(NIL "test" ((NIL NIL "a" "b.com")) ((NIL NIL "a" "b.com")) ((NIL NIL "a" "b.com")) ((NIL NIL "x" "y.com")) NIL NIL NIL NIL)`, + }, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + + header, err := rfc822.Parse([]byte(tc.msg)).ParseHeader() + require.NoError(t, err) + + envelope, err := imap.Envelope(header) + require.NoError(t, err) + assert.Equal(t, tc.envelope, envelope) + }) + } +} diff --git a/imap/params.go b/imap/params.go index 57959d93..4dd1516f 100644 --- a/imap/params.go +++ b/imap/params.go @@ -130,6 +130,10 @@ func (c *paramList) addMap(writer parListWriter, v map[string]string) *paramList } func (c *paramList) addAddresses(writer parListWriter, v []*mail.Address) *paramList { + if len(v) == 0 { + return c.addString(writer, "") + } + c.onWrite(writer) child := c.newChildList(writer)