Skip to content

Commit

Permalink
net/textproto: properly trim continued lines in MIME headers
Browse files Browse the repository at this point in the history
A MIME header can include values defined on several lines.
Only the first line of each value was trimmed.

Make sure all the lines are trimmed before being aggregated.

Fixes #11204

Change-Id: Id92f384044bc6c4ca836e5dba2081fe82c82dc85
Reviewed-on: https://go-review.googlesource.com/15683
Reviewed-by: Brad Fitzpatrick <[email protected]>
Run-TryBot: Brad Fitzpatrick <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
  • Loading branch information
dspezia authored and bradfitz committed Oct 11, 2015
1 parent c478c48 commit 6f77278
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/net/textproto/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func (r *Reader) readContinuedLineSlice() ([]byte, error) {
break
}
r.buf = append(r.buf, ' ')
r.buf = append(r.buf, line...)
r.buf = append(r.buf, trim(line)...)
}
return r.buf, nil
}
Expand Down
26 changes: 26 additions & 0 deletions src/net/textproto/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,32 @@ func TestReadMIMEHeaderNonCompliant(t *testing.T) {
}
}

// Test that continued lines are properly trimmed. Issue 11204.
func TestReadMIMEHeaderTrimContinued(t *testing.T) {
// In this header, \n and \r\n terminated lines are mixed on purpose.
// We expect each line to be trimmed (prefix and suffix) before being concatenated.
// Keep the spaces as they are.
r := reader("" + // for code formatting purpose.
"a:\n" +
" 0 \r\n" +
"b:1 \t\r\n" +
"c: 2\r\n" +
" 3\t\n" +
" \t 4 \r\n\n")
m, err := r.ReadMIMEHeader()
if err != nil {
t.Fatal(err)
}
want := MIMEHeader{
"A": {"0"},
"B": {"1"},
"C": {"2 3 4"},
}
if !reflect.DeepEqual(m, want) {
t.Fatalf("ReadMIMEHeader mismatch.\n got: %q\nwant: %q", m, want)
}
}

type readResponseTest struct {
in string
inCode int
Expand Down

0 comments on commit 6f77278

Please sign in to comment.