统一SMTP事务入队并复核投递授权与邮件日期
将SMTP信封收件人与授权快照持久保存,按收件人投递并在执行前复核凭据状态;保留原邮件日期,新增队列元数据迁移并补齐相关回归用例源码。
This commit is contained in:
1 parent
7b380277f6
commit
3d6a59092d
16 files changed
+633
-375
No files matched your search
@@ -1,6 +1,10 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
|
||||
@@ -8,6 +12,7 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/services/credentials"
|
||||
"git.pcux.cn/xuans/Smail/services/notification"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
// ProtocolSession 保存不可变身份与凭据快照,拒绝后的连接不能通过重新启用恢复权限。
|
||||
@@ -18,6 +23,85 @@ type ProtocolSession struct {
|
||||
invalid atomic.Bool
|
||||
}
|
||||
|
||||
type QueuedAuthorization struct {
|
||||
UserID int `json:"user_id"`
|
||||
Kind string `json:"kind"`
|
||||
ID int `json:"id"`
|
||||
Version int `json:"version"`
|
||||
Credential string `json:"credential"`
|
||||
Identity string `json:"identity"`
|
||||
}
|
||||
|
||||
func (s *ProtocolSession) QueueAuthorization() (QueuedAuthorization, error) {
|
||||
var result QueuedAuthorization
|
||||
if err := s.Check(); err != nil {
|
||||
return result, err
|
||||
}
|
||||
if s.IsNotification() {
|
||||
result.Kind, result.ID, result.Version, result.Credential = s.notification.QueueBinding()
|
||||
} else {
|
||||
result.Kind, result.ID, result.Version, result.Credential = s.grant.QueueBinding()
|
||||
}
|
||||
if result.Kind == "" || result.ID <= 0 || result.Version <= 0 {
|
||||
return QueuedAuthorization{}, notification.ErrDenied
|
||||
}
|
||||
result.Identity = queuedUserIdentity(s.user)
|
||||
result.UserID = s.user.ID
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func queuedDigest(value string) string {
|
||||
hash := sha256.Sum256([]byte(value))
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
||||
func queuedUserIdentity(user models.User) string {
|
||||
value, _ := json.Marshal([]any{
|
||||
user.ID, user.Account, user.Password, user.SessionVersion,
|
||||
user.SecurityVersion, user.EffectiveRole(), user.MustChangePassword,
|
||||
})
|
||||
return queuedDigest(string(value))
|
||||
}
|
||||
|
||||
func (a QueuedAuthorization) Check(engine *xorm.Engine, user models.User) (bool, error) {
|
||||
if engine == nil {
|
||||
return false, notification.ErrUnavailable
|
||||
}
|
||||
if user.ID <= 0 || a.UserID != user.ID || user.Disabled != 0 || user.MustChangePassword != 0 || a.ID <= 0 || a.Version <= 0 ||
|
||||
subtle.ConstantTimeCompare([]byte(a.Identity), []byte(queuedUserIdentity(user))) != 1 {
|
||||
return false, nil
|
||||
}
|
||||
var hash string
|
||||
switch a.Kind {
|
||||
case "smtp":
|
||||
var item models.ProtocolCredential
|
||||
found, err := engine.ID(a.ID).Where("user_id = ? AND protocol = ? AND enabled = ? AND code_version = ? AND revoked_at IS NULL", user.ID, "smtp", true, a.Version).Get(&item)
|
||||
if err != nil || !found {
|
||||
return false, err
|
||||
}
|
||||
hash = item.CodeHash
|
||||
case "client":
|
||||
var item models.ClientCredential
|
||||
found, err := engine.ID(a.ID).Where("user_id = ? AND code_version = ? AND revoked_at IS NULL", user.ID, a.Version).Get(&item)
|
||||
if err != nil || !found {
|
||||
return false, err
|
||||
}
|
||||
hash = item.CodeHash
|
||||
case "notification":
|
||||
if a.ID != user.ID {
|
||||
return false, nil
|
||||
}
|
||||
item, err := notification.Load(engine, user.ID)
|
||||
if err != nil || !notification.Configured(item, user) || item.CodeVersion != a.Version {
|
||||
return false, err
|
||||
}
|
||||
hash = item.CodeHash
|
||||
default:
|
||||
return false, nil
|
||||
}
|
||||
return hash != "" && subtle.ConstantTimeCompare([]byte(a.Credential), []byte(queuedDigest(hash))) == 1, nil
|
||||
}
|
||||
|
||||
func AuthenticateProtocolSession(protocol, remoteAddress, account, plain string) (*models.User, *ProtocolSession, error) {
|
||||
if notification.IsUsername(account) {
|
||||
return authenticateNotification(protocol, remoteAddress, account, plain)
|
||||
|
||||
@@ -2,6 +2,7 @@ package auth
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -11,9 +12,74 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/internal/passwordtest"
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/services/credentials"
|
||||
"git.pcux.cn/xuans/Smail/services/notification"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func TestQueuedNotificationAuthorizationTracksCredentialVersion(t *testing.T) {
|
||||
oldDB := db.Instance
|
||||
engine, err := xorm.NewEngine("sqlite", filepath.Join(t.TempDir(), "notification.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = engine.Close(); db.Instance = oldDB })
|
||||
db.Instance = engine
|
||||
if err := engine.Sync2(&models.User{}, &models.NotificationGrant{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
user := models.User{Account: "sender", Password: "test-hash", Role: "user", SessionVersion: 1, SecurityVersion: 1}
|
||||
if _, err := engine.Insert(&user); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
code := "sn_" + strings.Repeat("A", 43)
|
||||
item := models.NotificationGrant{
|
||||
UserID: user.ID, Enabled: 1, CodeVersion: 2, CodeHash: queuedDigest(code),
|
||||
SessionVersion: user.SessionVersion, SecurityVersion: user.SecurityVersion,
|
||||
}
|
||||
if _, err := engine.Insert(&item); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
grant, err := notification.Authenticate(engine, user, code)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
session := &ProtocolSession{user: user, notification: grant}
|
||||
snapshot, err := session.QueueAuthorization()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if snapshot.Kind != "notification" || snapshot.ID != user.ID || snapshot.Version != item.CodeVersion ||
|
||||
snapshot.Credential == code || snapshot.Credential == item.CodeHash {
|
||||
t.Fatal("notification snapshot is not bound to the original credential")
|
||||
}
|
||||
if valid, err := snapshot.Check(engine, user); err != nil || !valid {
|
||||
t.Fatalf("notification queue authorization = %v, %v", valid, err)
|
||||
}
|
||||
for _, change := range []string{"disable", "version", "hash", "session", "security"} {
|
||||
t.Run(change, func(t *testing.T) {
|
||||
updated := item
|
||||
switch change {
|
||||
case "disable":
|
||||
updated.Enabled = 0
|
||||
case "version":
|
||||
updated.CodeVersion++
|
||||
case "hash":
|
||||
updated.CodeHash = queuedDigest("replacement")
|
||||
case "session":
|
||||
updated.SessionVersion++
|
||||
case "security":
|
||||
updated.SecurityVersion++
|
||||
}
|
||||
if _, err := engine.ID(user.ID).Cols("enabled", "code_version", "code_hash", "session_version", "security_version").Update(&updated); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if valid, err := snapshot.Check(engine, user); err != nil || valid {
|
||||
t.Fatalf("stale notification queue authorization = %v, %v", valid, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestProtocolSessionRechecksUserAndCredential(t *testing.T) {
|
||||
for _, kind := range []string{"protocol", "client"} {
|
||||
for _, change := range []string{"revoke", "disabled", "password", "session_version", "security_version", "role", "deleted", "database_failure"} {
|
||||
@@ -62,6 +128,16 @@ func TestProtocolSessionRechecksUserAndCredential(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
queued, err := session.QueueAuthorization()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if valid, err := queued.Check(engine, user); err != nil || !valid {
|
||||
t.Fatalf("new queued authorization = %v, %v", valid, err)
|
||||
}
|
||||
if queued.Credential == code || queued.Identity == user.Password || queued.Kind == "" {
|
||||
t.Fatal("queued authorization contains reusable credentials or has no kind")
|
||||
}
|
||||
// 命令复核不占用密码哈希容量,也不累加登录失败计数。
|
||||
release := passwordtest.Hold(t)
|
||||
if err := session.Check(); err != nil {
|
||||
@@ -93,6 +169,17 @@ func TestProtocolSessionRechecksUserAndCredential(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
current := user
|
||||
if change != "database_failure" {
|
||||
current = models.User{}
|
||||
if _, err := engine.ID(user.ID).Get(¤t); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
valid, checkErr := queued.Check(engine, current)
|
||||
if valid || (change == "database_failure") != (checkErr != nil) {
|
||||
t.Fatalf("stale queued authorization = %v, %v", valid, checkErr)
|
||||
}
|
||||
var wait sync.WaitGroup
|
||||
for range 4 {
|
||||
wait.Add(1)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package credentials
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
@@ -17,6 +20,18 @@ type ProtocolGrant struct {
|
||||
hash string
|
||||
}
|
||||
|
||||
func (g *ProtocolGrant) QueueBinding() (string, int, int, string) {
|
||||
if g == nil || g.protocol != "smtp" {
|
||||
return "", 0, 0, ""
|
||||
}
|
||||
kind := "smtp"
|
||||
if g.client {
|
||||
kind = "client"
|
||||
}
|
||||
hash := sha256.Sum256([]byte(g.hash))
|
||||
return kind, g.id, g.version, hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
||||
func (g *ProtocolGrant) Valid(engine *xorm.Engine) bool {
|
||||
if g == nil || engine == nil || engine != g.engine || g.id <= 0 {
|
||||
return false
|
||||
|
||||
@@ -216,6 +216,13 @@ type Grant struct {
|
||||
securityVersion int
|
||||
}
|
||||
|
||||
func (g *Grant) QueueBinding() (string, int, int, string) {
|
||||
if g == nil {
|
||||
return "", 0, 0, ""
|
||||
}
|
||||
return "notification", g.userID, g.version, digest(g.hash)
|
||||
}
|
||||
|
||||
func Authenticate(engine *xorm.Engine, user models.User, plain string) (*Grant, error) {
|
||||
if len(plain) != 46 || !strings.HasPrefix(plain, "sn_") {
|
||||
return nil, ErrDenied
|
||||
|
||||
@@ -2,18 +2,24 @@ package outbox
|
||||
|
||||
import (
|
||||
stdcontext "context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/dto/parsemail"
|
||||
"git.pcux.cn/xuans/Smail/hooks"
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
"git.pcux.cn/xuans/Smail/services/dbmigration"
|
||||
"git.pcux.cn/xuans/Smail/services/notification"
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
"git.pcux.cn/xuans/Smail/utils/send"
|
||||
"git.pcux.cn/xuans/Smail/utils/smtp"
|
||||
"git.pcux.cn/xuans/Smail/utils/validation"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
@@ -121,6 +127,17 @@ func (r *Runtime) processWritable(job models.OutboundDelivery) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
var submission Submission
|
||||
authorized := true
|
||||
if job.Submission != "" {
|
||||
authorized = userFound && json.Unmarshal([]byte(job.Submission), &submission) == nil
|
||||
if authorized {
|
||||
authorized, err = submission.Authorization.Check(r.engine, user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
owned, err := claim(r.engine, &job)
|
||||
if err != nil || !owned {
|
||||
return err
|
||||
@@ -144,6 +161,17 @@ func (r *Runtime) processWritable(job models.OutboundDelivery) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if job.Submission != "" {
|
||||
if !authorized || !authorizedQueuedSender(submission, message, user) {
|
||||
return notification.ErrDenied
|
||||
}
|
||||
message.EnvelopeFrom = submission.EnvelopeFrom
|
||||
if submission.Date != "" {
|
||||
message.Date = submission.Date
|
||||
}
|
||||
message.EnvelopeTo = []*parsemail.User{{EmailAddress: job.Recipient}}
|
||||
message.DeliveryTrigger = submission.Trigger
|
||||
}
|
||||
return r.deliver(ctx, message, job.Recipient)
|
||||
}()
|
||||
// 固定首次结果及退避时间,后续只重试落库,绝不再次调用 SMTP。
|
||||
@@ -151,6 +179,22 @@ func (r *Runtime) processWritable(job models.OutboundDelivery) error {
|
||||
return r.flushResult()
|
||||
}
|
||||
|
||||
func authorizedQueuedSender(submission Submission, message *parsemail.Email, user models.User) bool {
|
||||
if message == nil || message.From == nil || message.Sender == nil || submission.Trigger == "" {
|
||||
return false
|
||||
}
|
||||
domains := config.CurrentMailDomains()
|
||||
if submission.Authorization.Kind == "notification" {
|
||||
sender := validation.NotificationAddress(domains.Primary)
|
||||
return sender != "" && strings.EqualFold(submission.EnvelopeFrom, sender) &&
|
||||
strings.EqualFold(message.From.EmailAddress, sender) && strings.EqualFold(message.Sender.EmailAddress, sender)
|
||||
}
|
||||
allowAlias := user.EffectiveRole() == access.RoleSuperAdmin
|
||||
return validation.IsAuthorizedSender(message.From.EmailAddress, user.Account, allowAlias, domains.Accepted) &&
|
||||
validation.IsAuthorizedSender(message.Sender.EmailAddress, user.Account, allowAlias, domains.Accepted) &&
|
||||
(submission.EnvelopeFrom == "" || validation.IsAuthorizedSender(submission.EnvelopeFrom, user.Account, allowAlias, domains.Accepted))
|
||||
}
|
||||
|
||||
func (r *Runtime) flushResult() error {
|
||||
result := r.pending
|
||||
if result == nil {
|
||||
|
||||
@@ -2,8 +2,10 @@ package outbox
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/mail"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -12,6 +14,7 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/db"
|
||||
"git.pcux.cn/xuans/Smail/dto/parsemail"
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/services/auth"
|
||||
"git.pcux.cn/xuans/Smail/services/logstore"
|
||||
"git.pcux.cn/xuans/Smail/utils/send"
|
||||
"git.pcux.cn/xuans/Smail/utils/smtp"
|
||||
@@ -34,6 +37,32 @@ func Enqueue(session *xorm.Session, email models.Email) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return enqueueRecipients(session, email, message, "")
|
||||
}
|
||||
|
||||
type Submission struct {
|
||||
EnvelopeFrom string `json:"envelope_from"`
|
||||
Date string `json:"date"`
|
||||
Trigger string `json:"trigger"`
|
||||
Authorization auth.QueuedAuthorization `json:"authorization"`
|
||||
}
|
||||
|
||||
func EnqueueSMTP(session *xorm.Session, email models.Email, recipients []string, submission Submission) error {
|
||||
if submission.Authorization.Kind == "" || submission.Authorization.ID <= 0 || submission.Authorization.UserID != email.SendUserID || submission.Trigger == "" {
|
||||
return errors.New("SMTP submission authorization is missing")
|
||||
}
|
||||
encoded, err := json.Marshal(submission)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
message := &parsemail.Email{To: make([]*parsemail.User, len(recipients))}
|
||||
for index, address := range recipients {
|
||||
message.To[index] = &parsemail.User{EmailAddress: address}
|
||||
}
|
||||
return enqueueRecipients(session, email, message, string(encoded))
|
||||
}
|
||||
|
||||
func enqueueRecipients(session *xorm.Session, email models.Email, message *parsemail.Email, submission string) error {
|
||||
seen := make(map[string]bool)
|
||||
for _, user := range append(append(message.To, message.Cc...), message.Bcc...) {
|
||||
if user == nil || user.EmailAddress == "" {
|
||||
@@ -41,6 +70,10 @@ func Enqueue(session *xorm.Session, email models.Email) error {
|
||||
}
|
||||
// 本地部分保留大小写,仅合并完全相同的 SMTP 信封地址。
|
||||
address := user.EmailAddress
|
||||
parsed, err := mail.ParseAddress(address)
|
||||
if err != nil || parsed.Address != address || len(address) > 320 {
|
||||
return errors.New("invalid queue recipient")
|
||||
}
|
||||
if at := strings.LastIndexByte(address, '@'); at >= 0 {
|
||||
address = address[:at+1] + strings.ToLower(address[at+1:])
|
||||
}
|
||||
@@ -48,7 +81,7 @@ func Enqueue(session *xorm.Session, email models.Email) error {
|
||||
continue
|
||||
}
|
||||
seen[address] = true
|
||||
job := models.OutboundDelivery{EmailID: email.Id, Recipient: address, State: queued}
|
||||
job := models.OutboundDelivery{EmailID: email.Id, Recipient: address, State: queued, Submission: submission}
|
||||
if _, err := session.Insert(&job); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package outbox
|
||||
|
||||
import (
|
||||
stdcontext "context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/textproto"
|
||||
@@ -10,10 +11,13 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/consts"
|
||||
"git.pcux.cn/xuans/Smail/db"
|
||||
"git.pcux.cn/xuans/Smail/dto/parsemail"
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/services/auth"
|
||||
"git.pcux.cn/xuans/Smail/services/credentials"
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
"git.pcux.cn/xuans/Smail/utils/smtp"
|
||||
"xorm.io/xorm"
|
||||
@@ -57,6 +61,131 @@ func queueFixture(t *testing.T) (*xorm.Engine, models.Email, []models.OutboundDe
|
||||
return engine, email, jobs
|
||||
}
|
||||
|
||||
func smtpQueueFixture(t *testing.T) (*xorm.Engine, []models.OutboundDelivery, *credentials.Service) {
|
||||
t.Helper()
|
||||
engine, email, jobs := queueFixture(t)
|
||||
oldDB, oldConfig := db.Instance, config.Instance
|
||||
db.Instance = engine
|
||||
config.Instance = &config.Config{Domain: "example.com", Domains: []string{"example.com"}}
|
||||
t.Cleanup(func() { db.Instance, config.Instance = oldDB, oldConfig })
|
||||
if err := engine.Sync2(&models.ProtocolCredential{}, &models.ClientCredential{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
service, err := credentials.NewService(engine)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := service.SetProtocolEnabled(email.SendUserID, "smtp", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
code, err := service.GenerateProtocolCode(email.SendUserID, "smtp")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, session, err := auth.AuthenticateProtocolSession("smtp", "192.0.2.95:1234", "sender@example.com", code)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
authorization, err := session.QueueAuthorization()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
submission, err := json.Marshal(Submission{
|
||||
EnvelopeFrom: "sender@example.com", Date: "2026-09-27T12:30:00+08:00",
|
||||
Trigger: "SMTP", Authorization: authorization,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
email.Sender = `{"EmailAddress":"sender@example.com"}`
|
||||
if _, err := engine.ID(email.Id).Cols("sender").Update(&email); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for index := range jobs {
|
||||
jobs[index].Submission = string(submission)
|
||||
if _, err := engine.ID(jobs[index].ID).Cols("submission").Update(&jobs[index]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return engine, jobs, service
|
||||
}
|
||||
|
||||
func TestSMTPQueueRestoresEnvelopeWithoutRewritingHeaders(t *testing.T) {
|
||||
engine, jobs, _ := smtpQueueFixture(t)
|
||||
calls := 0
|
||||
r := &Runtime{engine: engine, deliver: func(ctx *context.Context, message *parsemail.Email, recipient string) error {
|
||||
calls++
|
||||
if ctx.UserAccount != "sender" || recipient != jobs[0].Recipient || message.EnvelopeFrom != "sender@example.com" {
|
||||
t.Fatal("worker did not restore SMTP identity or envelope")
|
||||
}
|
||||
if len(message.EnvelopeTo) != 1 || message.EnvelopeTo[0].EmailAddress != recipient || len(message.To) != 2 {
|
||||
t.Fatal("envelope recipients leaked into message headers")
|
||||
}
|
||||
if message.MsgID != "stable@example.com" || message.Date != "2026-09-27T12:30:00+08:00" || message.DeliveryTrigger != "SMTP" {
|
||||
t.Fatal("worker lost immutable submission metadata")
|
||||
}
|
||||
return nil
|
||||
}}
|
||||
if err := r.process(jobs[0]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if calls != 1 {
|
||||
t.Fatalf("deliveries = %d, want 1", calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSMTPQueueRejectsStaleAuthorizationBeforeDelivery(t *testing.T) {
|
||||
for _, change := range []string{"revoke", "disable", "rotate", "malformed", "sender", "database_failure"} {
|
||||
t.Run(change, func(t *testing.T) {
|
||||
engine, jobs, service := smtpQueueFixture(t)
|
||||
var err error
|
||||
switch change {
|
||||
case "revoke":
|
||||
err = service.RevokeProtocolCode(1, "smtp")
|
||||
case "disable":
|
||||
err = service.SetProtocolEnabled(1, "smtp", false)
|
||||
case "rotate":
|
||||
_, err = service.GenerateProtocolCode(1, "smtp")
|
||||
case "malformed":
|
||||
jobs[0].Submission = "{"
|
||||
case "sender":
|
||||
var submission Submission
|
||||
if err := json.Unmarshal([]byte(jobs[0].Submission), &submission); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
submission.EnvelopeFrom = "other@example.com"
|
||||
var encoded []byte
|
||||
encoded, err = json.Marshal(submission)
|
||||
jobs[0].Submission = string(encoded)
|
||||
case "database_failure":
|
||||
_, err = engine.Exec("ALTER TABLE protocol_credential RENAME TO unavailable_credential")
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := &Runtime{engine: engine, deliver: func(*context.Context, *parsemail.Email, string) error {
|
||||
t.Fatal("worker delivered without current authorization")
|
||||
return nil
|
||||
}}
|
||||
err = r.process(jobs[0])
|
||||
if (change == "database_failure") != (err != nil) {
|
||||
t.Fatalf("process error = %v", err)
|
||||
}
|
||||
var stored models.OutboundDelivery
|
||||
if found, err := engine.ID(jobs[0].ID).Get(&stored); err != nil || !found {
|
||||
t.Fatalf("read queued job: found=%v err=%v", found, err)
|
||||
}
|
||||
if change == "database_failure" {
|
||||
if stored.State != queued || stored.Attempts != 0 {
|
||||
t.Fatal("database read failure consumed a delivery attempt")
|
||||
}
|
||||
} else if stored.State != failed {
|
||||
t.Fatalf("revoked submission state = %s", stored.State)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func assertStatus(t *testing.T, engine *xorm.Engine, emailID int, status int8) {
|
||||
t.Helper()
|
||||
var email models.Email
|
||||
|
||||
Reference in new issue
Block a user