go-pop3 icon indicating copy to clipboard operation
go-pop3 copied to clipboard

ReadLines() does not handle dot stuffing correctly

Open stapelberg opened this issue 2 years ago • 0 comments

I discovered that reading a mail message using c.ReadLines() is incorrect: while the function interprets a single . as the end of message, it does not un-escape the dot when a line begins with a dot.

Dropping in the code from net/textproto helps:

diff --git i/pop3.go w/pop3.go
index de0727a..cd44007 100644
--- i/pop3.go
+++ w/pop3.go
@@ -72,10 +72,13 @@ func (c *Client) ReadLines() (lines []string, err error) {
 			return nil, err
 		}
 
-		// Look for a dot to indicate the end of a message
-		// from the server.
-		if line == "." {
-			break
+		// from go1.20.5/src/net/textproto/reader.go#L298:
+		// Dot by itself marks end; otherwise cut one dot.
+		if len(line) > 0 && line[0] == '.' {
+			if len(line) == 1 {
+				break
+			}
+			line = line[1:]
 		}
 		lines = append(lines, line)
 	}

I suspect that c.Retr() is similarly buggy because it uses mail.ReadMessage, which does use net/textproto, but does not call its DotReader() function, but I haven’t verified.

Filing this issue mostly so that other users are aware of the issue.

stapelberg avatar Jul 04 '23 20:07 stapelberg