Skip to content

Commit

Permalink
mime/quotedprintable: accept trailing soft line-break at the end of m…
Browse files Browse the repository at this point in the history
…essage

Fixes golang#15486.

Change-Id: Id879dc9acef9232003df9a0f6f54312191374a60
Reviewed-on: https://go-review.googlesource.com/27530
Run-TryBot: Minux Ma <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
minux committed Sep 11, 2016
1 parent fca3dd3 commit f7fce1e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/mime/quotedprintable/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ func (r *Reader) Read(p []byte) (n int, err error) {
// 1. in addition to "=\r\n", "=\n" is also treated as soft line break.
// 2. it will pass through a '\r' or '\n' not preceded by '=', consistent
// with other broken QP encoders & decoders.
// 3. it accepts soft line-break (=) at end of message (issue 15486); i.e.
// the final byte read from the underlying reader is allowed to be '=',
// and it will be silently ignored.
for len(p) > 0 {
if len(r.line) == 0 {
if r.rerr != nil {
Expand All @@ -89,7 +92,8 @@ func (r *Reader) Read(p []byte) (n int, err error) {
if bytes.HasSuffix(r.line, softSuffix) {
rightStripped := wholeLine[len(r.line):]
r.line = r.line[:len(r.line)-1]
if !bytes.HasPrefix(rightStripped, lf) && !bytes.HasPrefix(rightStripped, crlf) {
if !bytes.HasPrefix(rightStripped, lf) && !bytes.HasPrefix(rightStripped, crlf) &&
!(len(rightStripped) == 0 && len(r.line) > 0 && r.rerr == io.EOF) {
r.rerr = fmt.Errorf("quotedprintable: invalid bytes after =: %q", rightStripped)
}
} else if hasLF {
Expand Down
3 changes: 3 additions & 0 deletions src/mime/quotedprintable/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func TestReader(t *testing.T) {
{in: "foo=\nbar", want: "foobar"},
{in: "foo=\rbar", want: "foo", err: "quotedprintable: invalid hex byte 0x0d"},
{in: "foo=\r\r\r \nbar", want: "foo", err: `quotedprintable: invalid bytes after =: "\r\r\r \n"`},
// Issue 15486, accept trailing soft line-break at end of input.
{in: "foo=", want: "foo"},
{in: "=", want: "", err: `quotedprintable: invalid bytes after =: ""`},

// Example from RFC 2045:
{in: "Now's the time =\n" + "for all folk to come=\n" + " to the aid of their country.",
Expand Down

0 comments on commit f7fce1e

Please sign in to comment.