Skip to content

Commit

Permalink
Fix SMTP AUTH LOGIN method for servers with uncommon success messages
Browse files Browse the repository at this point in the history
This fixes #94 and basically reverts d0f0435. As James points out correctly in #94, we should not assume specific responses from the server. As long as the spec is followed and the server returns the correct SMTP code, we should not do our own magic.

I've also extended the `getTestConnection` method in client_test.go, so that we can specify more test environment options like `TEST_PORT` and `TEST_TLS_SKIP_VERIFY`. This was needed for testing with the ProtonMail Bridge which listens on a different port and has non-trusted certificates.
  • Loading branch information
wneessen committed Jan 7, 2023
1 parent 3b3c1e6 commit 862749f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
12 changes: 3 additions & 9 deletions auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"errors"
"fmt"
"net/smtp"
"strings"
)

type loginAuth struct {
Expand All @@ -23,10 +22,6 @@ const (

// ServerRespPassword represents the "Password:" response by the SMTP server
ServerRespPassword = "Password:"

// ServerRespAuthSuccess represents the "Authentication successful:" response that is
// by sent by some SMTP servers
ServerRespAuthSuccess = "Authentication successful"
)

// LoginAuth returns an Auth that implements the LOGIN authentication
Expand Down Expand Up @@ -70,10 +65,9 @@ func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
return []byte(a.username), nil
case ServerRespPassword:
return []byte(a.password), nil
default:
return nil, fmt.Errorf("unexpected server response: %s", string(fromServer))
}
}
if strings.HasSuffix(string(fromServer), ServerRespAuthSuccess) {
return nil, nil
}
return nil, fmt.Errorf("unexpected server response: %s", string(fromServer))
return nil, nil
}
15 changes: 14 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"net/smtp"
"os"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -1087,10 +1088,22 @@ func getTestConnection(auth bool) (*Client, error) {
if th == "" {
return nil, fmt.Errorf("no TEST_HOST set")
}
c, err := NewClient(th)
tp := 25
if tps := os.Getenv("TEST_PORT"); tps != "" {
tpi, err := strconv.Atoi(tps)
if err == nil {
tp = tpi
}
}
sv := false
if sve := os.Getenv("TEST_TLS_SKIP_VERIFY"); sve != "" {
sv = true
}
c, err := NewClient(th, WithPort(tp))
if err != nil {
return c, err
}
c.tlsconfig.InsecureSkipVerify = sv
if auth {
st := os.Getenv("TEST_SMTPAUTH_TYPE")
if st != "" {
Expand Down

0 comments on commit 862749f

Please sign in to comment.