parsimony/internal/outbound/tls_test.go
tgiachi cc9fbc6b24 test: pin the relay settings that reach the SMTP client
- Check the EHLO name, its host-name default, server_name and the password end to end
- Pin the wording and codes of the authentication error
2026-11-10 11:11:50 +01:00

411 lines
13 KiB
Go

package outbound
import (
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"errors"
"io"
"math/big"
"net"
"strings"
"sync"
"testing"
"time"
"github.com/emersion/go-sasl"
"github.com/emersion/go-smtp"
)
// --- a throwaway CA and certificates -------------------------------------------------------
type pki struct {
pool *x509.CertPool
good tls.Certificate // valid for 127.0.0.1 and localhost
wrong tls.Certificate // signed by the CA but for another name
rogue tls.Certificate // valid for 127.0.0.1 but signed by an unknown CA
}
func newPKI(t *testing.T) *pki {
t.Helper()
mk := func(parent *x509.Certificate, parentKey *ecdsa.PrivateKey, tmpl *x509.Certificate) (*x509.Certificate, *ecdsa.PrivateKey, []byte) {
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatal(err)
}
if parent == nil {
parent, parentKey = tmpl, key
}
der, err := x509.CreateCertificate(rand.Reader, tmpl, parent, &key.PublicKey, parentKey)
if err != nil {
t.Fatal(err)
}
c, _ := x509.ParseCertificate(der)
return c, key, der
}
serial := int64(1)
tmpl := func(cn string, ca bool, dns []string, ips []net.IP) *x509.Certificate {
serial++
return &x509.Certificate{
SerialNumber: big.NewInt(serial), Subject: pkix.Name{CommonName: cn},
NotBefore: time.Now().Add(-time.Hour), NotAfter: time.Now().Add(24 * time.Hour),
IsCA: ca, BasicConstraintsValid: true,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
DNSNames: dns, IPAddresses: ips,
}
}
ca, caKey, _ := mk(nil, nil, tmpl("test CA", true, nil, nil))
rogueCA, rogueKey, _ := mk(nil, nil, tmpl("rogue CA", true, nil, nil))
leaf := func(parent *x509.Certificate, pk *ecdsa.PrivateKey, dns []string, ips []net.IP) tls.Certificate {
_, key, der := mk(parent, pk, tmpl("relay", false, dns, ips))
return tls.Certificate{Certificate: [][]byte{der}, PrivateKey: key}
}
pool := x509.NewCertPool()
pool.AddCert(ca)
loop := []net.IP{net.ParseIP("127.0.0.1")}
return &pki{
pool: pool,
good: leaf(ca, caKey, []string{"localhost"}, loop),
wrong: leaf(ca, caKey, []string{"other.example"}, nil),
rogue: leaf(rogueCA, rogueKey, []string{"localhost"}, loop),
}
}
// --- an SMTP server with TLS and AUTH --------------------------------------------------------
type relayOpts struct {
cert *tls.Certificate // nil: no TLS at all
implicit bool // TLS from the first byte (port 465 style)
user, pass string // when set, AUTH PLAIN must match
requireAuth bool // answer 530 to MAIL until authenticated
}
type testRelay struct {
mu sync.Mutex
addr string
messages []captured
auths []string // "user:pass" seen
hellos []string // EHLO names seen at MAIL time
tlsSeen []bool // whether each message arrived over TLS
}
type relayBackend struct {
r *testRelay
o relayOpts
}
func (b *relayBackend) NewSession(c *smtp.Conn) (smtp.Session, error) {
return &relaySession{b: b, conn: c}, nil
}
type relaySession struct {
b *relayBackend
conn *smtp.Conn
authed bool
from string
to []string
}
func (s *relaySession) Reset() { s.from, s.to = "", nil }
func (s *relaySession) Logout() error { return nil }
func (s *relaySession) AuthMechanisms() []string {
if s.b.o.user == "" {
return nil
}
return []string{sasl.Plain}
}
func (s *relaySession) Auth(mech string) (sasl.Server, error) {
return sasl.NewPlainServer(func(identity, username, password string) error {
s.b.r.mu.Lock()
s.b.r.auths = append(s.b.r.auths, username+":"+password)
s.b.r.mu.Unlock()
if username != s.b.o.user || password != s.b.o.pass {
return &smtp.SMTPError{Code: 535, EnhancedCode: smtp.EnhancedCode{5, 7, 8}, Message: "Authentication credentials invalid"}
}
s.authed = true
return nil
}), nil
}
func (s *relaySession) Mail(from string, _ *smtp.MailOptions) error {
if s.b.o.requireAuth && !s.authed {
return &smtp.SMTPError{Code: 530, EnhancedCode: smtp.EnhancedCode{5, 7, 0}, Message: "Authentication required"}
}
s.from = from
s.b.r.mu.Lock()
s.b.r.hellos = append(s.b.r.hellos, s.conn.Hostname())
s.b.r.mu.Unlock()
return nil
}
func (s *relaySession) Rcpt(to string, _ *smtp.RcptOptions) error {
s.to = append(s.to, to)
return nil
}
func (s *relaySession) Data(r io.Reader) error {
b, _ := io.ReadAll(r)
_, isTLS := s.conn.TLSConnectionState()
s.b.r.mu.Lock()
s.b.r.messages = append(s.b.r.messages, captured{s.from, s.to, string(b)})
s.b.r.tlsSeen = append(s.b.r.tlsSeen, isTLS)
s.b.r.mu.Unlock()
return nil
}
func startRelay(t *testing.T, o relayOpts) *testRelay {
t.Helper()
r := &testRelay{}
srv := smtp.NewServer(&relayBackend{r: r, o: o})
srv.AllowInsecureAuth = false // credentials are only accepted over TLS
if o.cert != nil {
srv.TLSConfig = &tls.Config{Certificates: []tls.Certificate{*o.cert}}
}
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
r.addr = ln.Addr().String()
serveLn := ln
if o.implicit {
serveLn = nil
go srv.Serve(tls.NewListener(ln, srv.TLSConfig))
} else {
go srv.Serve(ln)
}
_ = serveLn
t.Cleanup(func() { srv.Close() })
return r
}
func (r *testRelay) count() int {
r.mu.Lock()
defer r.mu.Unlock()
return len(r.messages)
}
func send(r *SMTPRelay) error {
return r.Send(context.Background(), "bounce@example.org", []string{"a@x.com"}, []byte("Subject: hi\r\n\r\nbody\r\n"))
}
// --- STARTTLS ---------------------------------------------------------------------------------
func TestStartTLSDeliversOverAnEncryptedConnection(t *testing.T) {
p := newPKI(t)
srv := startRelay(t, relayOpts{cert: &p.good})
r := &SMTPRelay{Addr: srv.addr, TLS: "starttls", RootCAs: p.pool}
if err := send(r); err != nil {
t.Fatal(err)
}
if srv.count() != 1 || !srv.tlsSeen[0] {
t.Fatalf("the message must arrive over TLS: %d %v", srv.count(), srv.tlsSeen)
}
}
func TestStartTLSIsRequiredNotOptional(t *testing.T) {
srv := startRelay(t, relayOpts{}) // a server that does not offer STARTTLS
err := send(&SMTPRelay{Addr: srv.addr, TLS: "starttls"})
if err == nil || !strings.Contains(err.Error(), "STARTTLS") {
t.Fatalf("want a STARTTLS error, got %v", err)
}
if srv.count() != 0 {
t.Fatal("a downgrade attack must not make us send the message in clear")
}
if IsPermanent(err) {
t.Fatalf("a relay that cannot do TLS is a configuration problem to fix, not a reason to drop mail: %v", err)
}
}
func TestImplicitTLS(t *testing.T) {
p := newPKI(t)
srv := startRelay(t, relayOpts{cert: &p.good, implicit: true})
if err := send(&SMTPRelay{Addr: srv.addr, TLS: "tls", RootCAs: p.pool}); err != nil {
t.Fatal(err)
}
if srv.count() != 1 || !srv.tlsSeen[0] {
t.Fatalf("%d %v", srv.count(), srv.tlsSeen)
}
}
func TestCertificateProblemsAreRefusedAndAreNotPermanent(t *testing.T) {
p := newPKI(t)
for name, tc := range map[string]struct {
cert *tls.Certificate
mode string
impl bool
}{
"wrong name, starttls": {&p.wrong, "starttls", false},
"wrong name, implicit": {&p.wrong, "tls", true},
"unknown CA, starttls": {&p.rogue, "starttls", false},
"unknown CA, implicit": {&p.rogue, "tls", true},
} {
srv := startRelay(t, relayOpts{cert: tc.cert, implicit: tc.impl})
err := send(&SMTPRelay{Addr: srv.addr, TLS: tc.mode, RootCAs: p.pool})
if err == nil {
t.Errorf("%s: an untrusted relay must be refused", name)
continue
}
if !strings.Contains(strings.ToLower(err.Error()), "certificate") {
t.Errorf("%s: say it is a certificate problem: %v", name, err)
}
if srv.count() != 0 {
t.Errorf("%s: nothing may be sent to an untrusted relay", name)
}
if IsPermanent(err) {
t.Errorf("%s: a certificate problem must not drop the mail: %v", name, err)
}
}
}
func TestServerNameOverridesTheNameToVerify(t *testing.T) {
p := newPKI(t)
srv := startRelay(t, relayOpts{cert: &p.wrong}) // the certificate is for other.example
if err := send(&SMTPRelay{Addr: srv.addr, TLS: "starttls", RootCAs: p.pool, ServerName: "other.example"}); err != nil {
t.Fatalf("server_name is the name the certificate must match: %v", err)
}
}
// --- AUTH ---------------------------------------------------------------------------------------
func TestAuthPlainOverStartTLS(t *testing.T) {
p := newPKI(t)
srv := startRelay(t, relayOpts{cert: &p.good, user: "ses-user", pass: "s3cret pass", requireAuth: true})
r := &SMTPRelay{Addr: srv.addr, TLS: "starttls", RootCAs: p.pool, Username: "ses-user", Password: "s3cret pass"}
if err := send(r); err != nil {
t.Fatal(err)
}
if srv.count() != 1 || len(srv.auths) != 1 || srv.auths[0] != "ses-user:s3cret pass" {
t.Fatalf("messages=%d auths=%v", srv.count(), srv.auths)
}
}
func TestAuthPlainOverImplicitTLS(t *testing.T) {
p := newPKI(t)
srv := startRelay(t, relayOpts{cert: &p.good, implicit: true, user: "u", pass: "p", requireAuth: true})
if err := send(&SMTPRelay{Addr: srv.addr, TLS: "tls", RootCAs: p.pool, Username: "u", Password: "p"}); err != nil {
t.Fatal(err)
}
if srv.count() != 1 {
t.Fatal("not delivered")
}
}
func TestCredentialsAreNeverSentInClearToARemoteRelay(t *testing.T) {
// 192.0.2.1 is reserved for documentation: the client must refuse before it even connects.
for _, mode := range []string{"", "none"} {
start := time.Now()
err := send(&SMTPRelay{Addr: "192.0.2.1:25", TLS: mode, Username: "u", Password: "p"})
if err == nil || !strings.Contains(err.Error(), "clear") {
t.Fatalf("mode %q: want a refusal to send credentials in clear, got %v", mode, err)
}
if time.Since(start) > time.Second {
t.Fatalf("mode %q: the refusal must not wait for a connection", mode)
}
}
// A clear connection to a relay that does not offer AUTH without TLS: nothing reaches the server.
srv := startRelay(t, relayOpts{user: "u", pass: "p", requireAuth: true})
if err := send(&SMTPRelay{Addr: srv.addr, TLS: "none", Username: "u", Password: "p"}); err == nil {
t.Fatal("the relay cannot authenticate us in clear")
}
if len(srv.auths) != 0 {
t.Fatalf("the password reached the server: %v", srv.auths)
}
}
func TestAWrongPasswordKeepsTheMailQueuedAndLeaksNothing(t *testing.T) {
p := newPKI(t)
srv := startRelay(t, relayOpts{cert: &p.good, user: "u", pass: "right", requireAuth: true})
err := send(&SMTPRelay{Addr: srv.addr, TLS: "starttls", RootCAs: p.pool, Username: "u", Password: "Wr0ngPassw0rd"})
if err == nil {
t.Fatal("want an authentication error")
}
if IsPermanent(err) {
t.Fatalf("a wrong password must not fail every message for good: the admin fixes it and the mail goes out: %v", err)
}
if !strings.Contains(err.Error(), "relay authentication failed (535") {
t.Fatalf("say what went wrong, with the reply code: %v", err)
}
if strings.Contains(err.Error(), "Wr0ngPassw0rd") {
t.Fatal("never put the password in an error")
}
if srv.count() != 0 {
t.Fatal("nothing delivered")
}
}
func TestARelayThatWantsAuthenticationIsATemporaryProblem(t *testing.T) {
p := newPKI(t)
srv := startRelay(t, relayOpts{cert: &p.good, user: "u", pass: "p", requireAuth: true})
// We forgot to configure credentials: the relay answers 530 at MAIL FROM.
err := send(&SMTPRelay{Addr: srv.addr, TLS: "starttls", RootCAs: p.pool})
if err == nil || IsPermanent(err) {
t.Fatalf("530 means credentials are missing: retry after the admin fixes it, got %v", err)
}
if !strings.Contains(err.Error(), "relay authentication failed (530") {
t.Fatalf("%v", err)
}
}
// --- EHLO name ----------------------------------------------------------------------------------
func TestHeloNameIsUsed(t *testing.T) {
p := newPKI(t)
srv := startRelay(t, relayOpts{cert: &p.good})
if err := send(&SMTPRelay{Addr: srv.addr, TLS: "starttls", RootCAs: p.pool, Helo: "lists.example.org"}); err != nil {
t.Fatal(err)
}
if len(srv.hellos) != 1 || srv.hellos[0] != "lists.example.org" {
t.Fatalf("EHLO names: %v", srv.hellos)
}
plain := startRelay(t, relayOpts{})
if err := send(&SMTPRelay{Addr: plain.addr, Helo: "lists.example.org"}); err != nil {
t.Fatal(err)
}
if plain.hellos[0] != "lists.example.org" {
t.Fatalf("EHLO over a plain connection: %v", plain.hellos)
}
}
func TestInvalidHeloNamesAreRejectedBeforeConnecting(t *testing.T) {
srv := startRelay(t, relayOpts{})
if err := send(&SMTPRelay{Addr: srv.addr, Helo: "bad name\r\nMAIL FROM:<x@y>"}); err == nil {
t.Fatal("a name with a line break must never reach the wire")
}
}
// --- unchanged behaviour -------------------------------------------------------------------------
func TestPlainRelayStillWorksWithoutAnyTLSSetting(t *testing.T) {
srv := startRelay(t, relayOpts{})
if err := send(&SMTPRelay{Addr: srv.addr}); err != nil {
t.Fatal(err)
}
if srv.count() != 1 || srv.tlsSeen[0] {
t.Fatalf("the default is the plain connection used so far: %v", srv.tlsSeen)
}
}
func TestAuthOverPlainConnectionToLoopbackIsAllowedWhenTheServerOffersIt(t *testing.T) {
// A local Postfix with SASL on 127.0.0.1: no TLS needed to reach it.
srv := startPlainAuthRelay(t)
err := send(&SMTPRelay{Addr: srv, TLS: "none", Username: "u", Password: "p"})
if err != nil {
t.Fatalf("%v", err)
}
}
// startPlainAuthRelay is a loopback relay that accepts AUTH without TLS.
func startPlainAuthRelay(t *testing.T) string {
t.Helper()
r := &testRelay{}
srv := smtp.NewServer(&relayBackend{r: r, o: relayOpts{user: "u", pass: "p", requireAuth: true}})
srv.AllowInsecureAuth = true
ln, _ := net.Listen("tcp", "127.0.0.1:0")
go srv.Serve(ln)
t.Cleanup(func() { srv.Close() })
return ln.Addr().String()
}
var _ = errors.New