138 lines
3.8 KiB
Go
138 lines
3.8 KiB
Go
package notify
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"mime"
|
|
"net"
|
|
"net/mail"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"sea-vhost/internal/store"
|
|
)
|
|
|
|
func TestSMTPDeliveryAndHeaderSafety(t *testing.T) {
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer listener.Close()
|
|
message := make(chan string, 1)
|
|
go func() {
|
|
connection, e := listener.Accept()
|
|
if e != nil {
|
|
return
|
|
}
|
|
defer connection.Close()
|
|
_ = connection.SetDeadline(time.Now().Add(10 * time.Second))
|
|
reader := bufio.NewReader(connection)
|
|
fmt.Fprint(connection, "220 localhost test SMTP\r\n")
|
|
for {
|
|
line, e := reader.ReadString('\n')
|
|
if e != nil {
|
|
return
|
|
}
|
|
switch {
|
|
case strings.HasPrefix(line, "EHLO"):
|
|
fmt.Fprint(connection, "250 localhost\r\n")
|
|
case strings.HasPrefix(line, "MAIL FROM:"), strings.HasPrefix(line, "RCPT TO:"):
|
|
fmt.Fprint(connection, "250 accepted\r\n")
|
|
case strings.HasPrefix(line, "DATA"):
|
|
fmt.Fprint(connection, "354 continue\r\n")
|
|
var data strings.Builder
|
|
for {
|
|
line, e = reader.ReadString('\n')
|
|
if e != nil {
|
|
return
|
|
}
|
|
if line == ".\r\n" {
|
|
break
|
|
}
|
|
data.WriteString(line)
|
|
}
|
|
message <- data.String()
|
|
fmt.Fprint(connection, "250 queued\r\n")
|
|
case strings.HasPrefix(line, "QUIT"):
|
|
fmt.Fprint(connection, "221 bye\r\n")
|
|
return
|
|
default:
|
|
fmt.Fprint(connection, "500 unexpected\r\n")
|
|
}
|
|
}
|
|
}()
|
|
host, port, _ := net.SplitHostPort(listener.Addr().String())
|
|
number, _ := strconv.Atoi(port)
|
|
err = send(context.Background(), smtpConfig{Host: host, Port: number, From: "sender@seavhost.test", AdminRecipients: "admin@seavhost.test"}, store.AdminNotification{Title: "主机超额\r\nBcc: attacker@example.test", Category: "quota", Severity: "critical", Detail: "仅本机测试字符串 SEA-SMTP-CANARY"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
select {
|
|
case raw := <-message:
|
|
parsed, e := mail.ReadMessage(strings.NewReader(raw))
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if parsed.Header.Get("Bcc") != "" {
|
|
t.Fatal("header injection")
|
|
}
|
|
subject, e := new(mime.WordDecoder).DecodeHeader(parsed.Header.Get("Subject"))
|
|
if e != nil || !strings.Contains(subject, "主机超额") {
|
|
t.Fatalf("invalid Unicode subject: %v", e)
|
|
}
|
|
body := base64.NewDecoder(base64.StdEncoding, parsed.Body)
|
|
scanner := bufio.NewScanner(body)
|
|
var text strings.Builder
|
|
for scanner.Scan() {
|
|
text.WriteString(scanner.Text())
|
|
}
|
|
if !strings.Contains(text.String(), "SEA-SMTP-CANARY") {
|
|
t.Fatal("notification body lost")
|
|
}
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("message not delivered")
|
|
}
|
|
}
|
|
|
|
func TestSMTPDoesNotSendCredentialsWithoutTLS(t *testing.T) {
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer listener.Close()
|
|
commands := make(chan string, 1)
|
|
go func() {
|
|
connection, e := listener.Accept()
|
|
if e != nil {
|
|
return
|
|
}
|
|
defer connection.Close()
|
|
_ = connection.SetDeadline(time.Now().Add(5 * time.Second))
|
|
fmt.Fprint(connection, "220 local\r\n")
|
|
reader := bufio.NewReader(connection)
|
|
var all strings.Builder
|
|
for {
|
|
line, e := reader.ReadString('\n')
|
|
if e != nil {
|
|
commands <- all.String()
|
|
return
|
|
}
|
|
all.WriteString(line)
|
|
fmt.Fprint(connection, "250 localhost\r\n")
|
|
}
|
|
}()
|
|
host, port, _ := net.SplitHostPort(listener.Addr().String())
|
|
number, _ := strconv.Atoi(port)
|
|
err = send(context.Background(), smtpConfig{Host: host, Port: number, Username: "test", Secret: "must-not-transmit"}, store.AdminNotification{})
|
|
if err == nil || !strings.Contains(err.Error(), "requires TLS") {
|
|
t.Fatalf("insecure authentication allowed: %v", err)
|
|
}
|
|
if command := <-commands; strings.Contains(command, "AUTH") || strings.Contains(command, "must-not-transmit") {
|
|
t.Fatal("plaintext credential sent")
|
|
}
|
|
}
|