From d733cef728e452eb50cc6bcb343cf0f753df57bb Mon Sep 17 00:00:00 2001 From: Alexandre Cesaro Date: Sun, 20 Mar 2016 17:29:56 +0100 Subject: [PATCH] mime: fix maximum length of encoded-words RFC 2047 recommends a maximum length of 75 characters for encoded-words. Due to a bug, encoded-words were limited to 77 characters instead of 75. Change-Id: I2ff9d013ab922df6fd542464ace70b1c46dc7ae7 Reviewed-on: https://go-review.googlesource.com/20918 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/mime/encodedword.go | 2 +- src/mime/encodedword_test.go | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/mime/encodedword.go b/src/mime/encodedword.go index d219bbd393..e6cbebe946 100644 --- a/src/mime/encodedword.go +++ b/src/mime/encodedword.go @@ -71,7 +71,7 @@ const ( maxEncodedWordLen = 75 // maxContentLen is how much content can be encoded, ignoring the header and // 2-byte footer. - maxContentLen = maxEncodedWordLen - len("=?UTF-8?") - len("?=") + maxContentLen = maxEncodedWordLen - len("=?UTF-8?q?") - len("?=") ) var maxBase64Len = base64.StdEncoding.DecodedLen(maxContentLen) diff --git a/src/mime/encodedword_test.go b/src/mime/encodedword_test.go index f7fb2203b3..b7ca4d05e3 100644 --- a/src/mime/encodedword_test.go +++ b/src/mime/encodedword_test.go @@ -31,10 +31,8 @@ func TestEncodeWord(t *testing.T) { {QEncoding, utf8, strings.Repeat("é", 11), "=?utf-8?q?" + strings.Repeat("=C3=A9", 10) + "?= =?utf-8?q?=C3=A9?="}, {QEncoding, iso88591, strings.Repeat("\xe9", 22), "=?iso-8859-1?q?" + strings.Repeat("=E9", 22) + "?="}, {QEncoding, utf8, strings.Repeat("\x80", 22), "=?utf-8?q?" + strings.Repeat("=80", 21) + "?= =?utf-8?q?=80?="}, - {BEncoding, utf8, strings.Repeat("é", 24), "=?utf-8?b?" + strings.Repeat("w6nDqcOp", 8) + "?="}, - {BEncoding, utf8, strings.Repeat("é", 27), "=?utf-8?b?" + strings.Repeat("w6nDqcOp", 8) + "?= =?utf-8?b?w6nDqcOp?="}, {BEncoding, iso88591, strings.Repeat("\xe9", 45), "=?iso-8859-1?b?" + strings.Repeat("6enp", 15) + "?="}, - {BEncoding, utf8, strings.Repeat("\x80", 51), "=?utf-8?b?" + strings.Repeat("gICA", 16) + "?= =?utf-8?b?gICA?="}, + {BEncoding, utf8, strings.Repeat("\x80", 48), "=?utf-8?b?" + strings.Repeat("gICA", 15) + "?= =?utf-8?b?gICA?="}, } for _, test := range tests { @@ -44,6 +42,37 @@ func TestEncodeWord(t *testing.T) { } } +func TestEncodedWordLength(t *testing.T) { + tests := []struct { + enc WordEncoder + src string + }{ + {QEncoding, strings.Repeat("à", 30)}, + {QEncoding, strings.Repeat("é", 60)}, + {BEncoding, strings.Repeat("ï", 25)}, + {BEncoding, strings.Repeat("ô", 37)}, + {BEncoding, strings.Repeat("\x80", 50)}, + {QEncoding, "{$firstname} Bienvendio a Apostolica, aquà inicia el camino de tu"}, + } + + for _, test := range tests { + s := test.enc.Encode("utf-8", test.src) + wordLen := 0 + for i := 0; i < len(s); i++ { + if s[i] == ' ' { + wordLen = 0 + continue + } + + wordLen++ + if wordLen > maxEncodedWordLen { + t.Errorf("Encode(%q) has more than %d characters: %q", + test.src, maxEncodedWordLen, s) + } + } + } +} + func TestDecodeWord(t *testing.T) { tests := []struct { src, exp string -- 2.48.1