Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
358 lines
13 KiB
Go
358 lines
13 KiB
Go
package server
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/textproto"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
type smtpTestDelivery struct {
|
|
message string
|
|
commands []string
|
|
tls bool
|
|
}
|
|
|
|
func fakeSMTP(t *testing.T, stall string, secure bool) (string, *tls.Config, <-chan smtpTestDelivery) {
|
|
t.Helper()
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
certificateServer := httptest.NewTLSServer(http.NotFoundHandler())
|
|
certificate := certificateServer.TLS.Certificates[0]
|
|
root, err := x509.ParseCertificate(certificate.Certificate[0])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
certificateServer.Close()
|
|
roots := x509.NewCertPool()
|
|
roots.AddCert(root)
|
|
done := make(chan smtpTestDelivery, 1)
|
|
go func() {
|
|
result := smtpTestDelivery{}
|
|
defer func() { done <- result; close(done) }()
|
|
connection, err := listener.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer connection.Close()
|
|
_ = connection.SetDeadline(time.Now().Add(2 * time.Second))
|
|
pause := func() { _, _ = io.Copy(io.Discard, connection) }
|
|
if stall == "greeting" {
|
|
pause()
|
|
return
|
|
}
|
|
reader := bufio.NewReader(connection)
|
|
_, _ = io.WriteString(connection, "220 localhost test relay\r\n")
|
|
for {
|
|
line, err := reader.ReadString('\n')
|
|
if err != nil {
|
|
return
|
|
}
|
|
line = strings.TrimSpace(line)
|
|
result.commands = append(result.commands, line)
|
|
switch {
|
|
case strings.HasPrefix(line, "EHLO"), strings.HasPrefix(line, "HELO"):
|
|
if stall == "ehlo" {
|
|
pause()
|
|
return
|
|
}
|
|
if secure && !result.tls {
|
|
_, _ = io.WriteString(connection, "250-localhost\r\n250-STARTTLS\r\n250 AUTH PLAIN\r\n")
|
|
} else {
|
|
_, _ = io.WriteString(connection, "250-localhost\r\n250 AUTH PLAIN\r\n")
|
|
}
|
|
case line == "STARTTLS":
|
|
_, _ = io.WriteString(connection, "220 start TLS\r\n")
|
|
upgraded := tls.Server(connection, &tls.Config{Certificates: []tls.Certificate{certificate}, MinVersion: tls.VersionTLS12})
|
|
if err := upgraded.Handshake(); err != nil {
|
|
return
|
|
}
|
|
connection = upgraded
|
|
reader = bufio.NewReader(connection)
|
|
result.tls = true
|
|
case strings.HasPrefix(line, "AUTH"):
|
|
_, _ = io.WriteString(connection, "235 authenticated\r\n")
|
|
case strings.HasPrefix(line, "MAIL FROM:"), strings.HasPrefix(line, "RCPT TO:"):
|
|
_, _ = io.WriteString(connection, "250 accepted\r\n")
|
|
case line == "DATA":
|
|
if stall == "data" {
|
|
pause()
|
|
return
|
|
}
|
|
_, _ = io.WriteString(connection, "354 send data\r\n")
|
|
body, err := textproto.NewReader(reader).ReadDotBytes()
|
|
if err != nil {
|
|
return
|
|
}
|
|
result.message = string(body)
|
|
_, _ = io.WriteString(connection, "250 queued\r\n")
|
|
case line == "QUIT":
|
|
if stall == "quit" {
|
|
pause()
|
|
return
|
|
}
|
|
_, _ = io.WriteString(connection, "221 bye\r\n")
|
|
return
|
|
default:
|
|
_, _ = io.WriteString(connection, "500 unsupported\r\n")
|
|
}
|
|
}
|
|
}()
|
|
t.Cleanup(func() { listener.Close() })
|
|
return listener.Addr().String(), &tls.Config{RootCAs: roots, MinVersion: tls.VersionTLS12}, done
|
|
}
|
|
|
|
func TestSMTPDeliveryTLSAndEnvelope(t *testing.T) {
|
|
address, trust, done := fakeSMTP(t, "", true)
|
|
config := store.AuthProviderConfig{SMTPAddress: address, SMTPFrom: "Wiki <wiki@example.test>", SMTPUsername: "user", SMTPPassword: "secret"}
|
|
if err := sendMagicLinkSMTPWithTLS(context.Background(), config, "Reader <reader@example.test>", "https://pages.example.test/_/auth/magic-link/verify?token=one-use", trust); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result := <-done
|
|
if !result.tls || !strings.Contains(strings.Join(result.commands, "\n"), "MAIL FROM:<wiki@example.test>") || !strings.Contains(result.message, `To: "Reader" <reader@example.test>`) || !strings.Contains(result.message, "token=one-use") {
|
|
t.Fatalf("unexpected SMTP delivery: %+v", result)
|
|
}
|
|
if strings.Contains(result.message, "secret") {
|
|
t.Fatal("SMTP password leaked into message")
|
|
}
|
|
}
|
|
|
|
func TestSMTPRejectsPlaintextCredentialsAndUntrustedTLS(t *testing.T) {
|
|
for _, secure := range []bool{false, true} {
|
|
t.Run(fmt.Sprint(secure), func(t *testing.T) {
|
|
address, _, done := fakeSMTP(t, "", secure)
|
|
config := store.AuthProviderConfig{SMTPAddress: address, SMTPFrom: "wiki@example.test", SMTPUsername: "user", SMTPPassword: "secret"}
|
|
if err := sendMagicLinkSMTP(context.Background(), config, "reader@example.test", "https://pages.example.test/link"); err == nil {
|
|
t.Fatal("unsafe SMTP authenticated")
|
|
}
|
|
result := <-done
|
|
if strings.Contains(strings.Join(result.commands, "\n"), "AUTH PLAIN") || result.message != "" {
|
|
t.Fatal("credentials or login link leaked over unsafe SMTP")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSMTPDeadlineCoversEntireProtocol(t *testing.T) {
|
|
for _, stage := range []string{"greeting", "ehlo", "data", "quit"} {
|
|
t.Run(stage, func(t *testing.T) {
|
|
address, _, done := fakeSMTP(t, stage, false)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Millisecond)
|
|
defer cancel()
|
|
started := time.Now()
|
|
if err := sendMagicLinkSMTP(ctx, store.AuthProviderConfig{SMTPAddress: address, SMTPFrom: "wiki@example.test"}, "reader@example.test", "https://pages.example.test/link"); err == nil {
|
|
t.Fatal("stalled SMTP succeeded")
|
|
}
|
|
if time.Since(started) > time.Second {
|
|
t.Fatal("SMTP ignored deadline")
|
|
}
|
|
<-done
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSMTPMessageRejectsInjection(t *testing.T) {
|
|
for _, address := range []string{"a@example.test\r\nBcc: evil@example.test", "a@example.test,b@example.test", "a@example.test\x00", "not-a-mailbox"} {
|
|
if _, err := smtpMailbox(address); err == nil {
|
|
t.Fatalf("invalid mailbox accepted: %q", address)
|
|
}
|
|
}
|
|
for _, config := range []store.AuthProviderConfig{
|
|
{SMTPAddress: "localhost:25", SMTPFrom: "wiki@example.test\r\nBcc: x@example.test"},
|
|
{SMTPAddress: "localhost:25\r\nDATA", SMTPFrom: "wiki@example.test"},
|
|
{SMTPAddress: "localhost:99999", SMTPFrom: "wiki@example.test"},
|
|
} {
|
|
if _, _, _, _, err := smtpMessage(config, "reader@example.test", "https://pages.example.test/link"); err == nil {
|
|
t.Fatal("unsafe SMTP message accepted")
|
|
}
|
|
}
|
|
if _, _, _, _, err := smtpMessage(store.AuthProviderConfig{SMTPAddress: "localhost:25", SMTPFrom: "wiki@example.test"}, "reader@example.test", "https://pages.example.test/\r\nBcc:x"); err == nil {
|
|
t.Fatal("link injection accepted")
|
|
}
|
|
}
|
|
|
|
func TestSMTPRejectsOversizedGreeting(t *testing.T) {
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer listener.Close()
|
|
done := make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
connection, err := listener.Accept()
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer connection.Close()
|
|
_, _ = io.WriteString(connection, "220 "+strings.Repeat("a", 300<<10)+"\r\n")
|
|
}()
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
if err := sendMagicLinkSMTP(ctx, store.AuthProviderConfig{SMTPAddress: listener.Addr().String(), SMTPFrom: "wiki@example.test"}, "reader@example.test", "https://pages.example.test/link"); err == nil {
|
|
t.Fatal("unbounded greeting accepted")
|
|
}
|
|
<-done
|
|
}
|
|
|
|
func TestMagicLinkLifecycleAndMethods(t *testing.T) {
|
|
for _, state := range []string{"active", "readonly", "suspended", "quarantined", "membership suspended", "email changed", "provider disabled", "legacy challenge"} {
|
|
t.Run(state, func(t *testing.T) {
|
|
s, token, domain := configFixture(t)
|
|
principal, err := s.store.PrincipalForSession(domain, token)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.store.DB.Exec(`UPDATE principals SET email='reader@example.test' WHERE uuid=?`, principal.UUID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
config := store.AuthProviderConfig{Kind: "smtp", Enabled: state != "provider disabled", SMTPAddress: "localhost:25", SMTPFrom: "wiki@example.test"}
|
|
if err := s.store.SetAuthProviderConfig(domain, config); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
payload := magicLinkChallenge{Return: "/destination", Email: "reader@example.test"}
|
|
if state == "legacy challenge" {
|
|
payload.Email = ""
|
|
}
|
|
challenge, err := s.store.CreateAuthChallenge(domain, principal.UUID, "magic-link", payload, time.Minute)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if state == "membership suspended" {
|
|
_, err = s.store.DB.Exec(`UPDATE domain_memberships SET state='suspended' WHERE principal_uuid=?`, principal.UUID)
|
|
} else if state == "email changed" {
|
|
_, err = s.store.DB.Exec(`UPDATE principals SET email='new@example.test' WHERE uuid=?`, principal.UUID)
|
|
} else if state == "suspended" || state == "quarantined" || state == "readonly" {
|
|
_, err = s.store.DB.Exec(`UPDATE principals SET state=? WHERE uuid=?`, state, principal.UUID)
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request := httptest.NewRequest("GET", "http://localhost/_/auth/magic-link/verify?token="+url.QueryEscape(challenge), nil)
|
|
response := httptest.NewRecorder()
|
|
s.verifyMagicLink(response, request)
|
|
allowed := state == "active" || state == "readonly"
|
|
if allowed && response.Code != http.StatusSeeOther || !allowed && response.Code != http.StatusBadRequest {
|
|
t.Fatalf("lifecycle %s: %d %s", state, response.Code, response.Body.String())
|
|
}
|
|
if !allowed {
|
|
for _, cookie := range response.Result().Cookies() {
|
|
if cookie.Name == authCookieName {
|
|
t.Fatal("revoked identity received session")
|
|
}
|
|
}
|
|
}
|
|
replay := httptest.NewRecorder()
|
|
s.verifyMagicLink(replay, request)
|
|
if replay.Code != http.StatusBadRequest {
|
|
t.Fatal("magic link replay accepted")
|
|
}
|
|
})
|
|
}
|
|
s := &Server{}
|
|
for _, test := range []struct {
|
|
handler http.HandlerFunc
|
|
method, allow string
|
|
}{{s.requestMagicLink, "GET", "POST"}, {s.verifyMagicLink, "POST", "GET"}} {
|
|
response := httptest.NewRecorder()
|
|
test.handler(response, httptest.NewRequest(test.method, "https://localhost/", nil))
|
|
if response.Code != 405 || response.Header().Get("Allow") != test.allow {
|
|
t.Fatal("wrong magic-link method accepted")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMagicLinkRequestBoundsAndGenericResponse(t *testing.T) {
|
|
s, _, domain := configFixture(t)
|
|
s.smtpSlots = make(chan struct{}, 2)
|
|
if _, err := s.store.DB.Exec(`UPDATE principals SET email='reader@example.test'`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.store.SetAuthProviderConfig(domain, store.AuthProviderConfig{Kind: "smtp", Enabled: true, SMTPAddress: "127.0.0.1:1", SMTPFrom: "wiki@example.test"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.smtpSlots <- struct{}{}
|
|
s.smtpSlots <- struct{}{} // full admission queue: no challenge should be created
|
|
var reference string
|
|
for _, email := range []string{"reader@example.test", "missing@example.test", "evil@example.test\r\nBcc: x"} {
|
|
request := httptest.NewRequest("POST", "http://localhost/_/auth/magic-link", strings.NewReader(url.Values{"email": {email}}.Encode()))
|
|
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
response := httptest.NewRecorder()
|
|
s.requestMagicLink(response, request)
|
|
if response.Code != 200 {
|
|
t.Fatalf("generic request: %d", response.Code)
|
|
}
|
|
if reference == "" {
|
|
reference = response.Body.String()
|
|
} else if reference != response.Body.String() {
|
|
t.Fatal("response reveals address validity")
|
|
}
|
|
}
|
|
var count int
|
|
if err := s.store.DB.QueryRow(`SELECT COUNT(*) FROM auth_challenges WHERE kind='magic-link'`).Scan(&count); err != nil || count != 0 {
|
|
t.Fatal("full delivery slots created unused challenges")
|
|
}
|
|
request := httptest.NewRequest("POST", "http://localhost/_/auth/magic-link", strings.NewReader("email="+strings.Repeat("x", 65<<10)))
|
|
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
response := httptest.NewRecorder()
|
|
s.requestMagicLink(response, request)
|
|
if response.Code != 400 {
|
|
t.Fatalf("oversized form accepted: %d", response.Code)
|
|
}
|
|
}
|
|
|
|
func TestMagicLinkBackgroundDeliveryReleasesSlot(t *testing.T) {
|
|
s, _, domain := configFixture(t)
|
|
s.smtpSlots = make(chan struct{}, 2)
|
|
address, _, done := fakeSMTP(t, "", false)
|
|
if _, err := s.store.DB.Exec(`UPDATE principals SET email='reader@example.test'`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.store.SetAuthProviderConfig(domain, store.AuthProviderConfig{Kind: "smtp", Enabled: true, SMTPAddress: address, SMTPFrom: "wiki@example.test"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request := httptest.NewRequest("POST", "http://localhost/_/auth/magic-link", strings.NewReader("email=reader%40example.test&return=%2Fdestination"))
|
|
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
response := httptest.NewRecorder()
|
|
s.requestMagicLink(response, request)
|
|
if response.Code != 200 {
|
|
t.Fatalf("request: %d", response.Code)
|
|
}
|
|
result := <-done
|
|
var link string
|
|
for _, word := range strings.Fields(result.message) {
|
|
if strings.HasPrefix(word, "http://localhost/_/auth/magic-link/verify?") {
|
|
link = word
|
|
}
|
|
}
|
|
if link == "" {
|
|
t.Fatal("delivery omitted the magic link")
|
|
}
|
|
deadline := time.Now().Add(time.Second)
|
|
for len(s.smtpSlots) != 0 && time.Now().Before(deadline) {
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
if len(s.smtpSlots) != 0 {
|
|
t.Fatal("delivery worker retained its slot")
|
|
}
|
|
verified := httptest.NewRecorder()
|
|
s.verifyMagicLink(verified, httptest.NewRequest("GET", link, nil))
|
|
if verified.Code != 303 || verified.Header().Get("Location") != "/destination" {
|
|
t.Fatalf("delivered link: %d %s", verified.Code, verified.Body.String())
|
|
}
|
|
}
|