修复邮件协议整数边界并补充事务回滚回归

This commit is contained in:
xuans committed 2026-09-21 00:39:26 +08:00
1 parent 97b02b8776
commit 3e135e2b7b
10 files changed
+299 -45

No files matched your search

+21 -1
View File
@@ -64,6 +64,14 @@ type IMAPSearchResult struct {
// SearchEmails performs IMAP search based on criteria
func SearchEmails(ctx *context.Context, groupName string, criteria *imap.SearchCriteria, snapshot ...[]int) ([]*response.UserEmailUIDData, error) {
var snapshotCount uint32
if len(snapshot) > 0 {
var err error
snapshotCount, err = imapSnapshotCount(snapshot[0])
if err != nil {
return nil, err
}
}
// First get the base list for the mailbox
baseList, err := GetUEListByUID(ctx, groupName, 0, 0, nil)
if err != nil {
@@ -104,7 +112,7 @@ func SearchEmails(ctx *context.Context, groupName string, criteria *imap.SearchC
}
if len(snapshot) > 0 {
maxUID = 0
maxSequence = uint32(len(snapshot[0]))
maxSequence = snapshotCount
for _, uid := range snapshot[0] {
if number, ok := validIMAPNumber(uid); ok {
maxUID = max(maxUID, number)
@@ -195,6 +203,18 @@ func validIMAPNumber(value int) (uint32, bool) {
return cast.ToUint32(value), true
}
func imapSnapshotCount(snapshot []int) (uint32, error) {
if uint64(len(snapshot)) > math.MaxUint32 {
return 0, fmt.Errorf("邮箱邮件数量超出协议允许范围")
}
for _, value := range snapshot {
if _, ok := validIMAPNumber(value); !ok {
return 0, fmt.Errorf("邮箱邮件标识超出协议允许范围")
}
}
return cast.ToUint32(len(snapshot)), nil
}
// filterByUIDSets filters the list by UID sets
func filterByUIDSets(list []*response.UserEmailUIDData, uidSets []imap.UIDSet, upper ...uint32) []*response.UserEmailUIDData {
var maximum uint32
@@ -0,0 +1,27 @@
package list
import (
"math"
"strconv"
"testing"
)
func TestIMAPSnapshotCountRejectsInvalidUIDBeforeDatabaseRead(t *testing.T) {
for _, snapshot := range [][]int{{0}, {-1}, {1, 0}} {
if _, err := SearchEmails(nil, "INBOX", nil, snapshot); err == nil {
t.Fatal("非法快照必须在查询数据库之前拒绝")
}
}
if count, err := imapSnapshotCount(nil); err != nil || count != 0 {
t.Fatalf("空快照验证失败:%d %v", count, err)
}
if strconv.IntSize >= 64 {
limit := uint64(math.MaxUint32)
if count, err := imapSnapshotCount([]int{int(limit)}); err != nil || count != 1 {
t.Fatalf("最大合法 UID 被拒绝:%d %v", count, err)
}
if _, err := SearchEmails(nil, "INBOX", nil, []int{int(limit + 1)}); err == nil {
t.Fatal("越界 UID 不应被截断为搜索结果")
}
}
}