1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:55:01 -07:00

mime: fix panic parsing 'encoded-word', be stricter

Fixes #19416

Change-Id: I23c69ff637abaa202909f1cba6ed41b3cfe3d117
Reviewed-on: https://go-review.googlesource.com/37812
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Hiroshi Ioka 2017-03-06 09:59:32 +09:00 committed by Brad Fitzpatrick
parent 68177d9ec0
commit f639353330
2 changed files with 14 additions and 5 deletions

View File

@ -194,22 +194,29 @@ type WordDecoder struct {
// Decode decodes an RFC 2047 encoded-word. // Decode decodes an RFC 2047 encoded-word.
func (d *WordDecoder) Decode(word string) (string, error) { func (d *WordDecoder) Decode(word string) (string, error) {
if !strings.HasPrefix(word, "=?") || !strings.HasSuffix(word, "?=") || strings.Count(word, "?") != 4 { // See https://tools.ietf.org/html/rfc2047#section-2
if len(word) < 9 || !strings.HasPrefix(word, "=?") || !strings.HasSuffix(word, "?=") || strings.Count(word, "?") != 4 {
return "", errInvalidWord return "", errInvalidWord
} }
word = word[2 : len(word)-2] word = word[2 : len(word)-2]
// split delimits the first 2 fields // split delimits the first 2 fields
split := strings.IndexByte(word, '?') split := strings.IndexByte(word, '?')
// split word "UTF-8?q?ascii" into "UTF-8", 'q', and "ascii"
charset := word[:split]
if len(charset) == 0 {
return "", errInvalidWord
}
encoding := word[split+1]
// the field after split must only be one byte // the field after split must only be one byte
if word[split+2] != '?' { if word[split+2] != '?' {
return "", errInvalidWord return "", errInvalidWord
} }
// split word "UTF-8?q?ascii" into "UTF-8", 'q', and "ascii"
charset := word[:split]
encoding := word[split+1]
text := word[split+3:] text := word[split+3:]
if len(text) == 0 {
return "", errInvalidWord
}
content, err := decode(encoding, text) content, err := decode(encoding, text)
if err != nil { if err != nil {

View File

@ -88,6 +88,8 @@ func TestDecodeWord(t *testing.T) {
{"=?UTF-8?Q?A=B?=", "", true}, {"=?UTF-8?Q?A=B?=", "", true},
{"=?UTF-8?Q?=A?=", "", true}, {"=?UTF-8?Q?=A?=", "", true},
{"=?UTF-8?A?A?=", "", true}, {"=?UTF-8?A?A?=", "", true},
{"=????=", "", true},
{"=?UTF-8?Q??=", "", true},
} }
for _, test := range tests { for _, test := range tests {