保存邮件回复链路接入界面前的开发检查点
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-27 20:48:33 +08:00
1 parent e1c44bbe33
commit d5073cccbe
14 files changed
+361 -111

No files matched your search

+8 -2
View File
@@ -29,6 +29,7 @@ var errDraftConflict = errors.New("draft missing, changed or no longer editable"
const ownedDraftRelation = "EXISTS (SELECT 1 FROM user_email WHERE user_id = ? AND email_id = ? AND status = ? AND is_deleted = 0)"
var draftColumns = []string{
"reply_email_id", "in_reply_to", "thread_references",
"subject", "reply_to", "from_name", "from_address", "to", "bcc", "cc",
"text", "html", "sender", "attachments", "spf_check", "dkim_check",
"status", "send_date", "cron_send_time", "msg_id", "size", "error",
@@ -113,6 +114,9 @@ func buildDraft(ctx *context.Context, input sendRequest) (*models.Email, error)
Text: []byte(parsemail.SanitizeText(input.Text)), HTML: []byte(parsemail.SanitizeHTML(input.HTML)),
}
total := 0
if err := applyReplyThread(ctx, input, message); err != nil {
return nil, err
}
for _, item := range input.Attachments {
contentType, content, err := decodeAttachment(item.Data)
if err != nil {
@@ -149,6 +153,7 @@ func buildDraft(ctx *context.Context, input sendRequest) (*models.Email, error)
now := time.Now()
return &models.Email{
Type: consts.EmailTypeSend, Status: consts.EmailStatusDrafts, SendUserID: ctx.UserID,
ReplyEmailID: input.ReplyEmailID, InReplyTo: message.InReplyTo, ThreadReferences: message.ThreadReferences,
Subject: message.Subject, FromName: message.From.Name, FromAddress: message.From.EmailAddress,
ReplyTo: fields.ReplyTo, To: fields.To, Cc: fields.Cc, Bcc: fields.Bcc, Sender: fields.Sender,
Text: sql.NullString{String: string(message.Text), Valid: true},
@@ -231,8 +236,9 @@ func persistDraft(engine *xorm.Engine, userID int, draft *models.Email, id int,
func draftMessage(draft models.Email) (sendRequest, error) {
message := sendRequest{
DraftID: draft.Id, DraftVersion: draft.MsgID,
From: user{Name: draft.FromName, Email: draft.FromAddress},
Subject: draft.Subject, Text: draft.Text.String, HTML: draft.Html.String,
ReplyEmailID: draft.ReplyEmailID,
From: user{Name: draft.FromName, Email: draft.FromAddress},
Subject: draft.Subject, Text: draft.Text.String, HTML: draft.Html.String,
Attachments: []attachment{},
}
var sender *parsemail.User
+50
View File
@@ -0,0 +1,50 @@
package email
import (
"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/utils/context"
"git.pcux.cn/xuans/Smail/utils/validation"
)
func applyReplyThread(ctx *context.Context, input sendRequest, message *parsemail.Email) error {
id := input.ReplyEmailID
if message == nil {
return validation.ErrInvalidEmail
}
message.InReplyTo, message.ThreadReferences = "", ""
if id == 0 {
return nil
}
if id < 0 || ctx == nil || ctx.UserID <= 0 || db.Instance == nil || message == nil {
return validation.ErrInvalidEmail
}
if input.DraftID > 0 {
session := db.Instance.NewSession()
draft, err := loadOwnedDraft(session, ctx.UserID, input.DraftID, input.DraftVersion)
session.Close()
if err != nil {
return err
}
if draft.ReplyEmailID == id {
message.InReplyTo = parsemail.NormalizeThreadHeader(draft.InReplyTo)
message.ThreadReferences = parsemail.NormalizeThreadHeader(draft.ThreadReferences)
return nil
}
}
var original models.Email
found, err := db.Instance.ID(id).
Where("status <> ?", consts.EmailStatusDrafts).
And("EXISTS (SELECT 1 FROM user_email WHERE email_id = ? AND user_id = ? AND is_deleted = 0)", id, ctx.UserID).
Get(&original)
if err != nil {
return err
}
if !found {
return validation.ErrInvalidEmail
}
message.InReplyTo, message.ThreadReferences = parsemail.ReplyThread(original.MsgID, original.ThreadReferences, original.InReplyTo)
return nil
}
+31 -21
View File
@@ -37,6 +37,7 @@ const (
)
type sendRequest struct {
ReplyEmailID int `json:"reply_email_id"`
DraftID int `json:"draft_id"`
DraftVersion string `json:"draft_version"`
ReplyTo []user `json:"reply_to"`
@@ -122,6 +123,11 @@ func Send(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
}
e := &parsemail.Email{}
if err := applyReplyThread(ctx, reqData, e); err != nil {
response.NewErrorResponse(response.ParamsError, i18n.GetText(ctx.Lang, "send_fail"), "").FPrint(w)
return
}
inReplyTo, threadReferences := e.InReplyTo, e.ThreadReferences
for _, to := range reqData.To {
e.To = append(e.To, &parsemail.User{
@@ -216,6 +222,7 @@ func Send(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
return
}
e.InReplyTo, e.ThreadReferences = inReplyTo, threadReferences
msgID, err := parsemail.GenerateMsgID(domains.Primary)
if err != nil {
log.WithContext(ctx).WithError(err).Error("generate outgoing message ID failed")
@@ -236,27 +243,30 @@ func Send(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
messageSize += len(item.Content)
}
modelEmail := models.Email{
Type: consts.EmailTypeSend,
Subject: e.Subject,
ReplyTo: storedFields.ReplyTo,
FromName: e.From.Name,
FromAddress: e.From.EmailAddress,
To: storedFields.To,
Bcc: storedFields.Bcc,
Cc: storedFields.Cc,
Text: sql.NullString{String: string(e.Text), Valid: true},
Html: sql.NullString{String: string(e.HTML), Valid: true},
Sender: storedFields.Sender,
Attachments: storedFields.Attachments,
SPFCheck: 1,
DKIMCheck: 1,
SendUserID: ctx.UserID,
SendDate: now,
CronSendTime: now,
Status: consts.EmailStatusDeliveryPending,
CreateTime: now,
MsgID: msgID,
Size: messageSize,
Type: consts.EmailTypeSend,
Subject: e.Subject,
ReplyTo: storedFields.ReplyTo,
ReplyEmailID: reqData.ReplyEmailID,
InReplyTo: e.InReplyTo,
ThreadReferences: e.ThreadReferences,
FromName: e.From.Name,
FromAddress: e.From.EmailAddress,
To: storedFields.To,
Bcc: storedFields.Bcc,
Cc: storedFields.Cc,
Text: sql.NullString{String: string(e.Text), Valid: true},
Html: sql.NullString{String: string(e.HTML), Valid: true},
Sender: storedFields.Sender,
Attachments: storedFields.Attachments,
SPFCheck: 1,
DKIMCheck: 1,
SendUserID: ctx.UserID,
SendDate: now,
CronSendTime: now,
Status: consts.EmailStatusDeliveryPending,
CreateTime: now,
MsgID: msgID,
Size: messageSize,
}
err = persistOutgoingEmailWithDraft(db.Instance, ctx.UserID, &modelEmail, reqData.DraftID, reqData.DraftVersion)