From 46703a0c6d11d17c75f6f77b2c7e8941b8cd505f Mon Sep 17 00:00:00 2001 From: Shulhan Date: Sat, 8 Jul 2023 14:54:24 +0700 Subject: mime/quotedprintable: fix encoding where a period alone on a line Given the following example of message body, A line that precisely have length 75 with . + LF will cause DATA truncation.\n \n Footer.\n The quotedprintable Writer will encode the message into, A line ... truncation=\r\n .\r\n \r\n Footer.\r\n If we pass the Writer output into SMTP DATA command, the server read the "\r\n.\r\n" as the end of DATA which cause the message truncated on the receiver. This changes fix this issue by encode the period at the end with "=2E" based on recommendation in RFC 2049 Section 3, point (8) [1]. [1] https://www.rfc-editor.org/rfc/rfc2049.html#section-3 Fixes #61235 Change-Id: I350387b183ac6b25886f4084a060dcfcb48232a9 --- src/mime/quotedprintable/reader_test.go | 5 +++++ src/mime/quotedprintable/writer.go | 7 +++++++ src/mime/quotedprintable/writer_test.go | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/src/mime/quotedprintable/reader_test.go b/src/mime/quotedprintable/reader_test.go index c12d2ca3db..885d1a650d 100644 --- a/src/mime/quotedprintable/reader_test.go +++ b/src/mime/quotedprintable/reader_test.go @@ -70,6 +70,11 @@ func TestReader(t *testing.T) { // Transport padding {in: "foo= \r\nbar", want: "foobar"}, {in: "foo=\t \r\nbar", want: "foobar"}, + { + // Line break with period at the end. + in: strings.Repeat(".", 75) + "=\r\n=2E\r\n=2E\r\nfooter.", + want: strings.Repeat(".", 76) + "\r\n.\r\nfooter.", + }, } for _, tt := range tests { var buf strings.Builder diff --git a/src/mime/quotedprintable/writer.go b/src/mime/quotedprintable/writer.go index 69b5a11232..7fae1f08a9 100644 --- a/src/mime/quotedprintable/writer.go +++ b/src/mime/quotedprintable/writer.go @@ -133,6 +133,13 @@ func (w *Writer) checkLastByte() error { } b := w.line[w.i-1] + if w.i == 1 && b == '.' { + w.i-- + if err := w.encode(b); err != nil { + return err + } + return nil + } if isWhitespace(b) { w.i-- if err := w.encode(b); err != nil { diff --git a/src/mime/quotedprintable/writer_test.go b/src/mime/quotedprintable/writer_test.go index 07411fe269..b53bb76d5e 100644 --- a/src/mime/quotedprintable/writer_test.go +++ b/src/mime/quotedprintable/writer_test.go @@ -88,6 +88,12 @@ func testWriter(t *testing.T, binary bool) { in: strings.Repeat(" ", 77), want: strings.Repeat(" ", 75) + "=\r\n =20", }, + { + // Line break with period at the end. + in: strings.Repeat(".", 76) + "\n.\nfooter.", + want: strings.Repeat(".", 75) + "=\r\n=2E\r\n=2E\r\nfooter.", + wantB: strings.Repeat(".", 75) + "=\r\n.=0A.=0Afooter.", + }, } for _, tt := range tests { -- cgit v1.3