]> Cypherpunks repositories - gostls13.git/commitdiff
mime: fix maximum length of encoded-words
authorAlexandre Cesaro <alexandre.cesaro@gmail.com>
Sun, 20 Mar 2016 16:29:56 +0000 (17:29 +0100)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 29 Mar 2016 11:19:31 +0000 (11:19 +0000)
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 <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/mime/encodedword.go
src/mime/encodedword_test.go

index d219bbd393bbaf4db4c0b2e22e64b32b434dfa5c..e6cbebe946d2b3e7083398762982192984700fb2 100644 (file)
@@ -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)
index f7fb2203b3f3aeec08cc484a948bcaa4b1224cdd..b7ca4d05e3a8cc206393c487283ad0300e7a6de3 100644 (file)
@@ -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