精简初始化通知设置并默认使用Smail系统通知身份发信
This commit is contained in:
1 parent
229d150a08
commit
a9937c3e6a
16 files changed
+186
-48
No files matched your search
@@ -5,7 +5,6 @@ import (
|
||||
"errors"
|
||||
"html"
|
||||
"net/mail"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SendSecurityCode 仅向绑定邮箱发送验证码,不使用证书通知的收件人列表。
|
||||
@@ -24,7 +23,10 @@ func (s *Service) SendSecurityCode(ctx context.Context, recipient, code string)
|
||||
return errors.New("验证码格式无效")
|
||||
}
|
||||
}
|
||||
from := strings.TrimSpace(s.config.SenderEmail)
|
||||
fromName, from, err := s.config.SenderIdentity()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, value := range []string{from, recipient} {
|
||||
parsed, err := mail.ParseAddress(value)
|
||||
if err != nil || parsed.Address != value {
|
||||
@@ -34,7 +36,7 @@ func (s *Service) SendSecurityCode(ctx context.Context, recipient, code string)
|
||||
text := "有人正在部署服务器上重置 Scert 超级管理员密码。\n验证码:" + code +
|
||||
"\n验证码仅在当前本机重置会话中有效,有效期不超过两分钟。\n如果不是您本人操作,请勿提供此验证码,并检查服务器访问权限。"
|
||||
payload := mailPayload{
|
||||
From: address{Name: s.config.SenderName, Email: from},
|
||||
From: address{Name: fromName, Email: from},
|
||||
To: []address{{Email: recipient}},
|
||||
Subject: "[Scert] 超级管理员密码重置验证",
|
||||
Text: text, HTML: "<pre>" + html.EscapeString(text) + "</pre>",
|
||||
|
||||
@@ -371,7 +371,10 @@ func (s *Service) loadCredentials(ctx context.Context) (map[string]string, error
|
||||
}
|
||||
|
||||
func (s *Service) buildPayload(event Event) (mailPayload, error) {
|
||||
fromEmail := strings.TrimSpace(s.config.SenderEmail)
|
||||
fromName, fromEmail, err := s.config.SenderIdentity()
|
||||
if err != nil {
|
||||
return mailPayload{}, err
|
||||
}
|
||||
if fromEmail == "" {
|
||||
return mailPayload{}, errors.New("通知发件人地址不能为空")
|
||||
}
|
||||
@@ -410,7 +413,7 @@ func (s *Service) buildPayload(event Event) (mailPayload, error) {
|
||||
}
|
||||
text := buildText(event, title, status, domainName)
|
||||
return mailPayload{
|
||||
From: address{Name: s.config.SenderName, Email: fromEmail},
|
||||
From: address{Name: fromName, Email: fromEmail},
|
||||
To: recipients,
|
||||
Subject: fmt.Sprintf("[Scert] %s:%s(%s)", title, domainName, status),
|
||||
Text: text,
|
||||
|
||||
@@ -28,6 +28,7 @@ type smtpTestServer struct {
|
||||
rejectAuth bool
|
||||
mu sync.Mutex
|
||||
messages [][]byte
|
||||
mailFrom string
|
||||
connections int
|
||||
}
|
||||
|
||||
@@ -121,6 +122,7 @@ func (s *smtpTestServer) handle(connection net.Conn, startTLS bool) {
|
||||
}
|
||||
case strings.HasPrefix(upper, "MAIL FROM:"):
|
||||
s.mu.Lock()
|
||||
s.mailFrom = command
|
||||
fail := s.failMailOnce
|
||||
s.failMailOnce = false
|
||||
s.mu.Unlock()
|
||||
@@ -275,6 +277,42 @@ func TestSMTPSendsUTF8WithAllowedPlainAuthentication(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSmailSystemSenderUsesSameEnvelopeAndHeader(t *testing.T) {
|
||||
for _, security := range []bool{false, true} {
|
||||
t.Run(map[bool]string{false: "notification", true: "security_code"}[security], func(t *testing.T) {
|
||||
server := newSMTPTestServer(t, false, true)
|
||||
host, port := server.address()
|
||||
cfg := smtpTestConfig(host, port, "starttls")
|
||||
cfg.SMTPUsername = "notify:scert@example.test"
|
||||
cfg.SenderEmail = "admin@other.test"
|
||||
service := New(cfg, credentialReaderStub{values: map[string]string{"password": "test-only-notification-credential"}})
|
||||
service.smtpTLSConfig = server.clientTLSConfig
|
||||
var err error
|
||||
if security {
|
||||
err = service.SendSecurityCode(t.Context(), "bound@example.test", "01234567")
|
||||
} else {
|
||||
err = service.Send(t.Context(), Event{Type: "certificate_issue", Status: "success", Domain: "example.test"})
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
server.mu.Lock()
|
||||
envelope := server.mailFrom
|
||||
server.mu.Unlock()
|
||||
if !strings.HasPrefix(envelope, "MAIL FROM:<notification+system@example.test>") {
|
||||
t.Fatal("SMTP 信封没有使用系统通知地址")
|
||||
}
|
||||
message := string(server.message())
|
||||
if !strings.Contains(message, formatSMTPAddress(address{Name: "系统通知", Email: "notification+system@example.test"})) || strings.Contains(message, "admin@other.test") {
|
||||
t.Fatal("邮件头没有使用固定系统通知身份,或泄漏旧管理员发件地址")
|
||||
}
|
||||
if security && strings.Contains(message, "receiver@example.com") {
|
||||
t.Fatal("验证码不得发送给普通通知收件人")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSMTPStartTLS(t *testing.T) {
|
||||
server := newSMTPTestServer(t, false, true)
|
||||
host, port := server.address()
|
||||
|
||||
Reference in new issue
Block a user