整理前后端与测试构建目录并完善配置和安全引导
Security Checks / security (push) Waiting to run

统一 frontend、backend、test、test-log 和 out 目录,更新构建、部署及测试入口;纳入配置管理兼容与首次安全引导修复;排除本机配置、密钥和测试产物。
This commit is contained in:
xuans committed 2026-09-05 19:19:52 +08:00
1 parent 28cd9d3406
commit f60d3b0880
603 files changed
+2351 -660

No files matched your search

+87
View File
@@ -0,0 +1,87 @@
package match
import (
"git.pcux.cn/xuans/Smail/dto/parsemail"
"git.pcux.cn/xuans/Smail/utils/context"
)
const (
RuleTypeRegex = "regex"
RuleTypeContains = "contains"
RuleTypeEq = "equal"
maxRuleFieldBytes = 1 << 20
)
type Match interface {
Match(ctx *context.Context, email *parsemail.Email) bool
}
func buildUsers(users []*parsemail.User) string {
ret := ""
for i, u := range users {
if i != 0 {
ret += ","
}
ret += u.EmailAddress
}
return ret
}
func limitedFieldString(value []byte) string {
if len(value) > maxRuleFieldBytes {
value = value[:maxRuleFieldBytes]
}
return string(value)
}
func limitedFieldJoin(first, second []byte) string {
remaining := maxRuleFieldBytes
capacity := len(first) + len(second)
if capacity > remaining {
capacity = remaining
}
result := make([]byte, 0, capacity)
if len(first) > remaining {
first = first[:remaining]
}
result = append(result, first...)
remaining -= len(first)
if len(second) > remaining {
second = second[:remaining]
}
result = append(result, second...)
return string(result)
}
func getFieldContent(field string, email *parsemail.Email) string {
if email == nil {
return ""
}
switch field {
case "ReplyTo":
return buildUsers(email.ReplyTo)
case "From":
if email.From != nil {
return email.From.EmailAddress
}
case "Subject":
return email.Subject
case "To":
return buildUsers(email.To)
case "Bcc":
return buildUsers(email.Bcc)
case "Cc":
return buildUsers(email.Cc)
case "Text":
return limitedFieldString(email.Text)
case "Html":
return limitedFieldString(email.HTML)
case "Sender":
if email.Sender != nil {
return email.Sender.EmailAddress
}
case "Content":
return limitedFieldJoin(email.HTML, email.Text)
}
return ""
}
@@ -0,0 +1,24 @@
package match
import (
"git.pcux.cn/xuans/Smail/dto/parsemail"
"git.pcux.cn/xuans/Smail/utils/context"
"strings"
)
type ContainsMatch struct {
Rule string
Field string
}
func NewContainsMatch(field, rule string) *ContainsMatch {
return &ContainsMatch{
Rule: rule,
Field: field,
}
}
func (r *ContainsMatch) Match(ctx *context.Context, email *parsemail.Email) bool {
content := getFieldContent(r.Field, email)
return strings.Contains(content, r.Rule)
}
@@ -0,0 +1,23 @@
package match
import (
"git.pcux.cn/xuans/Smail/dto/parsemail"
"git.pcux.cn/xuans/Smail/utils/context"
)
type EqualMatch struct {
Rule string
Field string
}
func NewEqualMatch(field, rule string) *EqualMatch {
return &EqualMatch{
Rule: rule,
Field: field,
}
}
func (r *EqualMatch) Match(ctx *context.Context, email *parsemail.Email) bool {
content := getFieldContent(r.Field, email)
return content == r.Rule
}
@@ -0,0 +1,62 @@
package match
import (
"fmt"
"time"
"git.pcux.cn/xuans/Smail/dto/parsemail"
"git.pcux.cn/xuans/Smail/utils/context"
"github.com/dlclark/regexp2"
log "github.com/sirupsen/logrus"
)
const (
maxRegexPatternBytes = 2048
regexMatchTimeout = 100 * time.Millisecond
)
type RegexMatch struct {
Rule string
Field string
}
func NewRegexMatch(field, rule string) *RegexMatch {
return &RegexMatch{
Rule: rule,
Field: field,
}
}
func ValidateRegex(pattern string) error {
_, err := compileRegex(pattern)
return err
}
func compileRegex(pattern string) (*regexp2.Regexp, error) {
if pattern == "" || len(pattern) > maxRegexPatternBytes {
return nil, fmt.Errorf("regex pattern length is invalid")
}
re, err := regexp2.Compile(pattern, 0)
if err != nil {
return nil, fmt.Errorf("compile regex: %w", err)
}
re.MatchTimeout = regexMatchTimeout
return re, nil
}
func (r *RegexMatch) Match(ctx *context.Context, email *parsemail.Email) bool {
if r == nil || email == nil {
return false
}
re, err := compileRegex(r.Rule)
if err != nil {
log.WithContext(ctx).Warn("rule regex rejected")
return false
}
matched, err := re.MatchString(getFieldContent(r.Field, email))
if err != nil {
log.WithContext(ctx).Warn("rule regex match failed")
return false
}
return matched
}
@@ -0,0 +1,72 @@
package match
import (
"bytes"
"strings"
"testing"
"time"
"git.pcux.cn/xuans/Smail/dto/parsemail"
appcontext "git.pcux.cn/xuans/Smail/utils/context"
log "github.com/sirupsen/logrus"
)
func TestRegexMatch(t *testing.T) {
email := &parsemail.Email{Subject: "safe@example.com"}
if !NewRegexMatch("Subject", `^(?!.*blocked\.example$).*`).Match(&appcontext.Context{}, email) {
t.Fatal("valid regex did not match")
}
}
func TestRegexMatchRejectsInvalidPatternWithoutPanic(t *testing.T) {
email := &parsemail.Email{Subject: "subject"}
if NewRegexMatch("Subject", "(").Match(&appcontext.Context{}, email) {
t.Fatal("invalid regex matched")
}
}
func TestRegexMatchTimesOutCatastrophicBacktracking(t *testing.T) {
email := &parsemail.Email{Subject: strings.Repeat("a", 200000) + "!"}
var output bytes.Buffer
logger := log.StandardLogger()
oldOutput := logger.Out
logger.SetOutput(&output)
t.Cleanup(func() { logger.SetOutput(oldOutput) })
started := time.Now()
if NewRegexMatch("Subject", `^(a+)+$`).Match(&appcontext.Context{}, email) {
t.Fatal("catastrophic regex unexpectedly matched")
}
if elapsed := time.Since(started); elapsed > 2*time.Second {
t.Fatalf("regex timeout took too long: %s", elapsed)
}
if strings.Contains(output.String(), email.Subject) || output.Len() > 4096 {
t.Fatalf("regex failure leaked or amplified message content: log_bytes=%d", output.Len())
}
}
func TestValidateRegexRejectsOversizedPattern(t *testing.T) {
if err := ValidateRegex(strings.Repeat("a", maxRegexPatternBytes+1)); err == nil {
t.Fatal("oversized regex was accepted")
}
}
func TestRuleBodyMatchingIsSizeLimited(t *testing.T) {
email := &parsemail.Email{Text: []byte(strings.Repeat("a", maxRuleFieldBytes) + "needle")}
if NewContainsMatch("Text", "needle").Match(&appcontext.Context{}, email) {
t.Fatal("rule matched content beyond the field inspection limit")
}
email.Text = []byte("needle" + strings.Repeat("a", maxRuleFieldBytes))
if !NewContainsMatch("Text", "needle").Match(&appcontext.Context{}, email) {
t.Fatal("rule did not match content within the field inspection limit")
}
}
func TestGetFieldContentHandlesMissingAddresses(t *testing.T) {
email := &parsemail.Email{}
if got := getFieldContent("From", email); got != "" {
t.Fatalf("missing From returned %q", got)
}
if got := getFieldContent("Sender", email); got != "" {
t.Fatalf("missing Sender returned %q", got)
}
}
+212
View File
@@ -0,0 +1,212 @@
package rule
import (
"fmt"
"strings"
"git.pcux.cn/xuans/Smail/config"
"git.pcux.cn/xuans/Smail/db"
"git.pcux.cn/xuans/Smail/dto"
"git.pcux.cn/xuans/Smail/dto/parsemail"
"git.pcux.cn/xuans/Smail/models"
"git.pcux.cn/xuans/Smail/services/del_email"
"git.pcux.cn/xuans/Smail/services/group"
"git.pcux.cn/xuans/Smail/services/rule/match"
"git.pcux.cn/xuans/Smail/utils/context"
"git.pcux.cn/xuans/Smail/utils/send"
log "github.com/sirupsen/logrus"
"github.com/spf13/cast"
)
func GetAllRules(ctx *context.Context, userId int) ([]*dto.Rule, error) {
if userId <= 0 {
return nil, fmt.Errorf("invalid user id")
}
var storedRules []*models.Rule
if err := db.Instance.Where("user_id=?", userId).Decr("sort").Find(&storedRules); err != nil {
return nil, fmt.Errorf("load rules: %w", err)
}
ret := make([]*dto.Rule, 0, len(storedRules))
for _, storedRule := range storedRules {
decoded, err := (&dto.Rule{}).Decode(storedRule)
if err != nil {
return nil, fmt.Errorf("decode rule %d: %w", storedRule.Id, err)
}
ret = append(ret, decoded)
}
return ret, nil
}
func MatchRule(ctx *context.Context, rule *dto.Rule, email *parsemail.Email) bool {
if rule == nil || email == nil || len(rule.Rules) == 0 || ruleContextDone(ctx) {
return false
}
for _, r := range rule.Rules {
if ruleContextDone(ctx) || r == nil {
return false
}
var m match.Match
switch r.Type {
case match.RuleTypeRegex:
m = match.NewRegexMatch(r.Field, r.Rule)
case match.RuleTypeContains:
m = match.NewContainsMatch(r.Field, r.Rule)
case match.RuleTypeEq:
m = match.NewEqualMatch(r.Field, r.Rule)
}
if m == nil {
return false
}
if !m.Match(ctx, email) || ruleContextDone(ctx) {
return false
}
}
return true
}
func ruleContextDone(ctx *context.Context) bool {
return ctx != nil && ctx.Context != nil && ctx.Err() != nil
}
func DoRule(ctx *context.Context, rule *dto.Rule, email *parsemail.Email, user *models.User, rawEmailData []byte) error {
if ctx == nil || rule == nil || email == nil || user == nil || db.Instance == nil {
return fmt.Errorf("invalid rule execution context")
}
if rule.UserId <= 0 || user.ID <= 0 || rule.UserId != user.ID {
return fmt.Errorf("rule owner does not match execution user")
}
executionContext := *ctx
executionContext.UserID = user.ID
ctx = &executionContext
log.WithContext(ctx).Debugf("执行规则:%s", rule.Name)
switch rule.Action {
case dto.READ:
if email.MessageId <= 0 {
return fmt.Errorf("email has not been saved")
}
var relation models.UserEmail
found, err := db.Instance.Table(&models.UserEmail{}).Where("email_id=? and user_id=?", email.MessageId, user.ID).Get(&relation)
if err != nil {
return fmt.Errorf("load rule message relation: %w", err)
}
if !found {
return fmt.Errorf("rule message relation not found")
}
if relation.IsRead == 1 {
return nil
}
affected, err := db.Instance.Table(&models.UserEmail{}).Where("email_id=? and user_id=? and is_read=0", email.MessageId, user.ID).Cols("is_read").Update(map[string]interface{}{"is_read": 1})
if err != nil {
return fmt.Errorf("mark rule message as read: %w", err)
}
if affected != 1 {
return fmt.Errorf("mark rule message as read affected %d rows", affected)
}
case dto.DELETE:
if email.MessageId <= 0 {
return fmt.Errorf("email has not been saved")
}
if err := del_email.DelEmail(ctx, []int{cast.ToInt(email.MessageId)}, false); err != nil {
return fmt.Errorf("delete rule message: %w", err)
}
case dto.FORWARD:
if err := doForward(ctx, email, rule.Params, user, rawEmailData); err != nil {
return fmt.Errorf("forward rule message: %w", err)
}
case dto.MOVE:
if err := doMove(ctx, rule, email); err != nil {
return fmt.Errorf("move rule message: %w", err)
}
default:
return fmt.Errorf("unsupported rule action")
}
return nil
}
func doForward(ctx *context.Context, email *parsemail.Email, forwardAddress string, user *models.User, rawEmailData []byte) error {
forwardUser := parsemail.BuilderUser(forwardAddress)
if forwardUser == nil || forwardUser.EmailAddress == "" {
return fmt.Errorf("invalid forward address: %s", forwardAddress)
}
account, domain := forwardUser.GetDomainAccount()
if account == "" || domain == "" {
return fmt.Errorf("invalid forward address: %s", forwardAddress)
}
if isLocalDomain(domain) {
if strings.EqualFold(account, user.Account) {
return fmt.Errorf("loop forwarding to self: %s", forwardAddress)
}
return forwardToLocalUser(ctx, email, account, forwardAddress)
}
if len(rawEmailData) > 0 {
err := send.ForwardRaw(ctx, email, rawEmailData, forwardAddress, user)
if err == nil {
return nil
}
log.WithContext(ctx).Warnf("Raw forward failed, retry remail forward:%v", err)
}
return send.Forward(ctx, email, forwardAddress, user)
}
func forwardToLocalUser(ctx *context.Context, email *parsemail.Email, account, forwardAddress string) error {
if email.MessageId <= 0 {
return fmt.Errorf("email has not been saved before local forwarding")
}
var user models.User
has, err := db.Instance.Table(&models.User{}).
Where("LOWER(account)=LOWER(?) and disabled=0", account).
Get(&user)
if err != nil {
return err
}
if !has {
return fmt.Errorf("local forward user not found: %s", forwardAddress)
}
var ue models.UserEmail
has, err = db.Instance.Table(&models.UserEmail{}).
Where("email_id=? and user_id=?", email.MessageId, user.ID).
Get(&ue)
if err != nil {
return err
}
if has {
return nil
}
_, err = db.Instance.Insert(&models.UserEmail{
UserID: user.ID,
EmailID: cast.ToInt(email.MessageId),
Status: cast.ToInt8(email.Status),
})
return err
}
func isLocalDomain(domain string) bool {
domains := config.CurrentMailDomains()
for _, localDomain := range domains.Accepted {
if strings.EqualFold(domain, localDomain) {
return true
}
}
return strings.EqualFold(domain, domains.Primary)
}
func doMove(ctx *context.Context, rule *dto.Rule, email *parsemail.Email) error {
if rule == nil || email == nil || email.MessageId <= 0 {
return fmt.Errorf("invalid move rule context")
}
groupID := cast.ToInt(rule.Params)
if name, ok := models.GroupCodeToName[groupID]; ok {
return group.Move2DefaultBox(ctx, []int{cast.ToInt(email.MessageId)}, name)
}
return group.MoveMailToGroup(ctx, []int{cast.ToInt(email.MessageId)}, groupID)
}
@@ -0,0 +1,91 @@
package rule
import (
"strings"
"testing"
"git.pcux.cn/xuans/Smail/db"
"git.pcux.cn/xuans/Smail/dto"
"git.pcux.cn/xuans/Smail/dto/parsemail"
"git.pcux.cn/xuans/Smail/models"
"git.pcux.cn/xuans/Smail/utils/context"
_ "modernc.org/sqlite"
"xorm.io/xorm"
)
func newRuleTestEngine(t *testing.T) *xorm.Engine {
t.Helper()
oldDB := db.Instance
engine, err := xorm.NewEngine("sqlite", ":memory:")
if err != nil {
t.Fatal(err)
}
db.Instance = engine
t.Cleanup(func() {
_ = engine.Close()
db.Instance = oldDB
})
if err := engine.Sync2(&models.UserEmail{}, &models.Group{}); err != nil {
t.Fatal(err)
}
return engine
}
func TestDoRuleReadDistinguishesExistingAndMissingRelation(t *testing.T) {
engine := newRuleTestEngine(t)
if _, err := engine.Insert(&models.UserEmail{UserID: 1, EmailID: 10, IsRead: 0}); err != nil {
t.Fatal(err)
}
ctx := &context.Context{}
rule := &dto.Rule{UserId: 1, Action: dto.READ}
user := &models.User{ID: 1}
email := &parsemail.Email{MessageId: 10}
if err := DoRule(ctx, rule, email, user, nil); err != nil {
t.Fatal(err)
}
if err := DoRule(ctx, rule, email, user, nil); err != nil {
t.Fatalf("already-read relation should be idempotent: %v", err)
}
email.MessageId = 11
if err := DoRule(ctx, rule, email, user, nil); err == nil || !strings.Contains(err.Error(), "relation not found") {
t.Fatalf("missing relation was not rejected: %v", err)
}
}
func TestDoRuleRejectsOwnerMismatch(t *testing.T) {
newRuleTestEngine(t)
err := DoRule(&context.Context{}, &dto.Rule{UserId: 2, Action: dto.READ}, &parsemail.Email{MessageId: 10}, &models.User{ID: 1}, nil)
if err == nil || !strings.Contains(err.Error(), "owner") {
t.Fatalf("owner mismatch was not rejected: %v", err)
}
}
func TestDoRuleMoveRejectsAnotherUsersGroup(t *testing.T) {
engine := newRuleTestEngine(t)
if _, err := engine.Insert(&models.UserEmail{UserID: 1, EmailID: 10}, &models.Group{ID: 5, UserId: 2, Name: "private"}); err != nil {
t.Fatal(err)
}
err := DoRule(&context.Context{}, &dto.Rule{UserId: 1, Action: dto.MOVE, Params: "5"}, &parsemail.Email{MessageId: 10}, &models.User{ID: 1}, nil)
if err == nil {
t.Fatal("another user's group was accepted")
}
}
func TestDoRuleReturnsDatabaseError(t *testing.T) {
engine := newRuleTestEngine(t)
if err := engine.Close(); err != nil {
t.Fatal(err)
}
err := DoRule(&context.Context{}, &dto.Rule{UserId: 1, Action: dto.READ}, &parsemail.Email{MessageId: 10}, &models.User{ID: 1}, nil)
if err == nil {
t.Fatal("closed database error was ignored")
}
}
func TestDoRuleRejectsInvalidForwardAddress(t *testing.T) {
newRuleTestEngine(t)
err := DoRule(&context.Context{}, &dto.Rule{UserId: 1, Action: dto.FORWARD, Params: "invalid"}, &parsemail.Email{MessageId: 10}, &models.User{ID: 1}, nil)
if err == nil {
t.Fatal("invalid forward address was accepted")
}
}
@@ -0,0 +1,85 @@
package rule
import (
"strings"
"testing"
"git.pcux.cn/xuans/Smail/config"
"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"
_ "modernc.org/sqlite"
"xorm.io/xorm"
)
func TestForwardToLocalUser(t *testing.T) {
oldConfig := config.Instance
oldDB := db.Instance
defer func() {
config.Instance = oldConfig
db.Instance = oldDB
}()
config.Instance = &config.Config{
Domain: "example.com",
Domains: []string{"example.com"},
}
engine, err := xorm.NewEngine("sqlite", ":memory:")
if err != nil {
t.Fatal(err)
}
defer engine.Close()
db.Instance = engine
if err := engine.Sync2(&models.User{}, &models.UserEmail{}); err != nil {
t.Fatal(err)
}
sender := &models.User{ID: 1, Account: "sender", Name: "sender"}
recipient := &models.User{ID: 2, Account: "recipient", Name: "recipient", IsAdmin: 1}
if _, err := engine.Insert(sender, recipient); err != nil {
t.Fatal(err)
}
email := &parsemail.Email{MessageId: 123, Status: 0}
if err := doForward(&context.Context{}, email, "recipient@example.com", sender, nil); err != nil {
t.Fatal(err)
}
var links []models.UserEmail
if err := engine.Table(&models.UserEmail{}).Where("email_id=?", email.MessageId).Find(&links); err != nil {
t.Fatal(err)
}
if len(links) != 1 || links[0].UserID != recipient.ID {
t.Fatalf("expected one user_email link for recipient, got %+v", links)
}
if err := doForward(&context.Context{}, email, "recipient@example.com", sender, nil); err != nil {
t.Fatal(err)
}
var count int64
count, err = engine.Table(&models.UserEmail{}).Where("email_id=? and user_id=?", email.MessageId, recipient.ID).Count()
if err != nil {
t.Fatal(err)
}
if count != 1 {
t.Fatalf("expected idempotent local forward link, got %d links", count)
}
}
func TestForwardToLocalUserRejectsSelfLoop(t *testing.T) {
oldConfig := config.Instance
defer func() { config.Instance = oldConfig }()
config.Instance = &config.Config{
Domain: "example.com",
Domains: []string{"example.com"},
}
err := doForward(&context.Context{}, &parsemail.Email{MessageId: 1}, "sender@example.com", &models.User{Account: "sender"}, nil)
if err == nil || !strings.Contains(err.Error(), "loop forwarding to self") {
t.Fatalf("expected self-loop error, got %v", err)
}
}
@@ -0,0 +1,61 @@
package rule
import (
stdcontext "context"
"testing"
"git.pcux.cn/xuans/Smail/db"
"git.pcux.cn/xuans/Smail/dto"
"git.pcux.cn/xuans/Smail/dto/parsemail"
"git.pcux.cn/xuans/Smail/models"
"git.pcux.cn/xuans/Smail/utils/context"
_ "modernc.org/sqlite"
"xorm.io/xorm"
)
func TestMatchRuleFailsClosedForInvalidRules(t *testing.T) {
email := &parsemail.Email{Subject: "report"}
cases := []*dto.Rule{
nil,
{},
{Rules: []*dto.Value{nil}},
{Rules: []*dto.Value{{Field: "Subject", Type: "unknown", Rule: "report"}}},
}
for index, rule := range cases {
if MatchRule(&context.Context{}, rule, email) {
t.Fatalf("invalid rule %d matched", index)
}
}
}
func TestMatchRuleStopsWhenExecutionBudgetExpires(t *testing.T) {
base, cancel := stdcontext.WithCancel(stdcontext.Background())
cancel()
ctx := &context.Context{Context: base}
rule := &dto.Rule{Rules: []*dto.Value{{Field: "Subject", Type: "contains", Rule: "report"}}}
if MatchRule(ctx, rule, &parsemail.Email{Subject: "report"}) {
t.Fatal("expired rule execution context was ignored")
}
}
func TestGetAllRulesRejectsCorruptedStoredJSON(t *testing.T) {
oldDB := db.Instance
engine, err := xorm.NewEngine("sqlite", ":memory:")
if err != nil {
t.Fatal(err)
}
db.Instance = engine
t.Cleanup(func() {
_ = engine.Close()
db.Instance = oldDB
})
if err := engine.Sync2(&models.Rule{}); err != nil {
t.Fatal(err)
}
if _, err := engine.Insert(&models.Rule{UserId: 1, Name: "broken", Value: "{"}); err != nil {
t.Fatal(err)
}
if _, err := GetAllRules(&context.Context{}, 1); err == nil {
t.Fatal("corrupted stored rule was accepted")
}
}