From d14ec675c61d67a0f41571247576aefa7da0adda Mon Sep 17 00:00:00 2001
From: Robert Sesek <rsesek@bluestatic.org>
Date: Sun, 18 Dec 2016 02:45:12 -0500
Subject: [PATCH] Fix not handling invalid RCPT TO addresses.

---
 pop3/conn_test.go |  4 +++-
 smtp/conn.go      |  3 ++-
 smtp/conn_test.go | 22 ++++++++++++++++++++--
 3 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/pop3/conn_test.go b/pop3/conn_test.go
index 061deff..4671bca 100644
--- a/pop3/conn_test.go
+++ b/pop3/conn_test.go
@@ -11,6 +11,8 @@ import (
 	"runtime"
 	"strings"
 	"testing"
+
+	"github.com/uber-go/zap"
 )
 
 func _fl(depth int) string {
@@ -59,7 +61,7 @@ func runServer(t *testing.T, po PostOffice) net.Listener {
 			if err != nil {
 				return
 			}
-			go AcceptConnection(conn, po)
+			go AcceptConnection(conn, po, zap.New(zap.NullEncoder()))
 		}
 	}()
 	return l
diff --git a/smtp/conn.go b/smtp/conn.go
index 33bca29..8b4f124 100644
--- a/smtp/conn.go
+++ b/smtp/conn.go
@@ -210,7 +210,7 @@ func (conn *connection) doMAIL() {
 
 	var err error
 	conn.mailFrom, err = mail.ParseAddress(mailFrom)
-	if err != nil {
+	if err != nil || conn.mailFrom == nil {
 		conn.reply(ReplyBadSyntax)
 		return
 	}
@@ -236,6 +236,7 @@ func (conn *connection) doRCPT() {
 	address, err := mail.ParseAddress(rcptTo)
 	if err != nil {
 		conn.reply(ReplyBadSyntax)
+		return
 	}
 
 	if reply := conn.server.VerifyAddress(*address); reply != ReplyOK {
diff --git a/smtp/conn_test.go b/smtp/conn_test.go
index cd75299..0fa5bb6 100644
--- a/smtp/conn_test.go
+++ b/smtp/conn_test.go
@@ -10,6 +10,8 @@ import (
 	"strings"
 	"testing"
 	"time"
+
+	"github.com/uber-go/zap"
 )
 
 func _fl(depth int) string {
@@ -46,7 +48,7 @@ func runServer(t *testing.T, server Server) net.Listener {
 			if err != nil {
 				return
 			}
-			go AcceptConnection(conn, server)
+			go AcceptConnection(conn, server, zap.New(zap.NullEncoder()))
 		}
 	}()
 
@@ -166,6 +168,22 @@ func TestVerifyAddress(t *testing.T) {
 	})
 }
 
+func TestBadAddress(t *testing.T) {
+	l := runServer(t, &testServer{})
+	defer l.Close()
+
+	conn := createClient(t, l.Addr())
+	readCodeLine(t, conn, 220)
+
+	runTableTest(t, conn, []requestResponse{
+		{"EHLO test", 0, func(t testing.TB, conn *textproto.Conn) { conn.ReadResponse(250) }},
+		{"MAIL FROM:<sender>", 501, nil},
+		{"MAIL FROM:<sender@foo.com>", 250, nil},
+		{"RCPT TO:<banned.net>", 501, nil},
+		{"QUIT", 221, nil},
+	})
+}
+
 func TestCaseSensitivty(t *testing.T) {
 	s := &testServer{}
 	l := runServer(t, s)
@@ -229,7 +247,7 @@ func TestGetReceivedInfo(t *testing.T) {
 
 		conn.ehlo = test.params.ehlo
 		conn.esmtp = test.params.esmtp
-		conn.tls = test.params.tls
+		//conn.tls = test.params.tls
 
 		envelope := Envelope{
 			RcptTo:   []mail.Address{{"", test.params.address}},
-- 
2.43.5