mox/smtpserver/fuzz_test.go
Mechiel Lukkien 2f5d6069bf
implement "requiretls", rfc 8689
with requiretls, the tls verification mode/rules for email deliveries can be
changed by the sender/submitter. in two ways:

1. "requiretls" smtp extension to always enforce verified tls (with mta-sts or
dnssec+dane), along the entire delivery path until delivery into the final
destination mailbox (so entire transport is verified-tls-protected).

2. "tls-required: no" message header, to ignore any tls and tls verification
errors even if the recipient domain has a policy that requires tls verification
(mta-sts and/or dnssec+dane), allowing delivery of non-sensitive messages in
case of misconfiguration/interoperability issues (at least useful for sending
tls reports).

we enable requiretls by default (only when tls is active), for smtp and
submission. it can be disabled through the config.

for each delivery attempt, we now store (per recipient domain, in the account
of the sender) whether the smtp server supports starttls and requiretls. this
support is shown (after having sent a first message) in the webmail when
sending a message (the previous 3 bars under the address input field are now 5
bars, the first for starttls support, the last for requiretls support). when
all recipient domains for a message are known to implement requiretls,
requiretls is automatically selected for sending (instead of "default" tls
behaviour). users can also select the "fallback to insecure" to add the
"tls-required: no" header.

new metrics are added for insight into requiretls errors and (some, not yet
all) cases where tls-required-no ignored a tls/verification error.

the admin can change the requiretls status for messages in the queue. so with
default delivery attempts, when verified tls is required by failing, an admin
could potentially change the field to "tls-required: no"-behaviour.

messages received (over smtp) with the requiretls option, get a comment added
to their Received header line, just before "id", after "with".
2023-10-24 10:10:46 +02:00

113 lines
2.8 KiB
Go

package smtpserver
import (
"fmt"
"net"
"os"
"path/filepath"
"testing"
"time"
"github.com/mjl-/mox/dns"
"github.com/mjl-/mox/mox-"
"github.com/mjl-/mox/queue"
"github.com/mjl-/mox/store"
)
// Fuzz the server. For each fuzz string, we set up servers in various connection states, and write the string as command.
func FuzzServer(f *testing.F) {
f.Add("HELO remote")
f.Add("EHLO remote")
f.Add("AUTH PLAIN")
f.Add("MAIL FROM:<remote@remote>")
f.Add("RCPT TO:<local@mox.example>")
f.Add("DATA")
f.Add(".")
f.Add("RSET")
f.Add("VRFY x")
f.Add("EXPN x")
f.Add("HELP")
f.Add("NOOP")
f.Add("QUIT")
mox.Context = ctxbg
mox.ConfigStaticPath = filepath.FromSlash("../testdata/smtpserverfuzz/mox.conf")
mox.MustLoadConfig(true, false)
dataDir := mox.ConfigDirPath(mox.Conf.Static.DataDir)
os.RemoveAll(dataDir)
acc, err := store.OpenAccount("mjl")
if err != nil {
f.Fatalf("open account: %v", err)
}
defer acc.Close()
err = acc.SetPassword("testtest")
if err != nil {
f.Fatalf("set password: %v", err)
}
defer store.Switchboard()()
err = queue.Init()
if err != nil {
f.Fatalf("queue init: %v", err)
}
defer queue.Shutdown()
comm := store.RegisterComm(acc)
defer comm.Unregister()
var cid int64 = 1
var fl *os.File
if false {
fl, err = os.Create("fuzz.log")
if err != nil {
f.Fatalf("fuzz log")
}
defer fl.Close()
}
flog := func(err error, msg string) {
if fl != nil && err != nil {
fmt.Fprintf(fl, "%s: %v\n", msg, err)
}
}
f.Fuzz(func(t *testing.T, s string) {
run := func(cmds []string) {
limitersInit() // Reset rate limiters.
serverConn, clientConn := net.Pipe()
defer serverConn.Close()
defer clientConn.Close()
go func() {
err := clientConn.SetDeadline(time.Now().Add(time.Second))
flog(err, "set client deadline")
_, err = clientConn.Read(make([]byte, 1024))
flog(err, "read ehlo")
for _, cmd := range cmds {
_, err = clientConn.Write([]byte(cmd + "\r\n"))
flog(err, "write command")
_, err = clientConn.Read(make([]byte, 1024))
flog(err, "read response")
}
_, err = clientConn.Write([]byte(s + "\r\n"))
flog(err, "write test command")
_, err = clientConn.Read(make([]byte, 1024))
flog(err, "read test response")
clientConn.Close()
serverConn.Close()
}()
resolver := dns.MockResolver{}
const submission = false
err := serverConn.SetDeadline(time.Now().Add(time.Second))
flog(err, "set server deadline")
serve("test", cid, dns.Domain{ASCII: "mox.example"}, nil, serverConn, resolver, submission, false, 100<<10, false, false, false, nil, 0)
cid++
}
run([]string{})
run([]string{"EHLO remote"})
run([]string{"EHLO remote", "MAIL FROM:<remote@example.org>"})
run([]string{"EHLO remote", "MAIL FROM:<remote@example.org>", "RCPT TO:<mjl@mox.example>"})
// todo: submission with login
})
}