Skip to content

Commit

Permalink
Verify that user is properly authenticated before sending mail if AUT…
Browse files Browse the repository at this point in the history
…H is required (#6)

* Verify that user is properly authenticated before sending mail if AUTH is required

* Add testcase to verify that user is properly authenticated before sending mail if authenticator is setup

* Fix TestErrors() to not misuse auth bypass
  • Loading branch information
decke committed Jun 7, 2020
1 parent 7c73bd1 commit 32be721
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
5 changes: 5 additions & 0 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ func (session *session) handleMAIL(cmd command) {
return
}

if session.server.Authenticator != nil && session.peer.Username == "" {
session.reply(530, "Authentication Required.")
return
}

if !session.tls && session.server.ForceTLS {
session.reply(502, "Please turn on TLS by issuing a STARTTLS command.")
return
Expand Down
41 changes: 36 additions & 5 deletions smtpd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,33 @@ func TestAuthNotSupported(t *testing.T) {

}

func TestAuthBypass(t *testing.T) {

addr, closer := runsslserver(t, &smtpd.Server{
Authenticator: func(peer smtpd.Peer, username, password string) error {
return smtpd.Error{Code: 550, Message: "Denied"}
},
ForceTLS: true,
ProtocolLogger: log.New(os.Stdout, "log: ", log.Lshortfile),
})

defer closer()

c, err := smtp.Dial(addr)
if err != nil {
t.Fatalf("Dial failed: %v", err)
}

if err := c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
t.Fatalf("STARTTLS failed: %v", err)
}

if err := c.Mail("sender@example.org"); err == nil {
t.Fatal("Unexpected MAIL success")
}

}

func TestConnectionCheck(t *testing.T) {

addr, closer := runserver(t, &smtpd.Server{
Expand Down Expand Up @@ -1270,12 +1297,8 @@ func TestErrors(t *testing.T) {
t.Fatalf("AUTH didn't fail: %v", err)
}

if err := c.Mail("sender@example.org"); err != nil {
t.Fatalf("MAIL failed: %v", err)
}

if err := c.Mail("sender@example.org"); err == nil {
t.Fatal("Duplicate MAIL didn't fail")
t.Fatalf("MAIL didn't fail")
}

if err := cmd(c.Text, 502, "STARTTLS"); err != nil {
Expand Down Expand Up @@ -1310,6 +1333,14 @@ func TestErrors(t *testing.T) {
t.Fatalf("AUTH didn't work: %v", err)
}

if err := c.Mail("sender@example.org"); err != nil {
t.Fatalf("MAIL failed: %v", err)
}

if err := c.Mail("sender@example.org"); err == nil {
t.Fatalf("Duplicate MAIL didn't fail")
}

if err := c.Quit(); err != nil {
t.Fatalf("Quit failed: %v", err)
}
Expand Down

0 comments on commit 32be721

Please sign in to comment.