From 6f7944158540dc02875023b77a8c122576dfc441 Mon Sep 17 00:00:00 2001 From: smail Date: Sun, 6 Sep 2026 13:02:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AE=A1=E6=9F=A5=E5=8F=91?= =?UTF-8?q?=E7=8E=B0=E7=9A=84=E8=AE=A4=E8=AF=81=E9=85=8D=E7=BD=AE=E9=82=AE?= =?UTF-8?q?=E4=BB=B6=E5=8D=8F=E8=AE=AE=E5=8F=8A=E5=89=8D=E7=AB=AF=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 补齐安全运行时清理、配置候选恢复、泛域名证书事务提交、邮件副本与协议边界、插件隐私和安装回滚回归,并更新修复状态及待验收清单。 --- backend/config/config.go | 18 ++ backend/config/manager_v2.go | 56 ++-- backend/config/partitioned_compatibility.go | 5 + backend/config/pending.go | 90 +++++++ backend/config/security.go | 11 +- backend/config/ssl_commit.go | 163 ++++++++++++ backend/config/ssl_storage.go | 73 ++++- backend/controllers/config.go | 9 +- backend/controllers/config_pending.go | 60 +++++ backend/controllers/login_verification.go | 23 +- backend/controllers/security.go | 119 ++++++--- backend/controllers/user.go | 15 +- backend/controllers/webauthn.go | 13 +- backend/dto/parsemail/email.go | 10 +- backend/dto/parsemail/imap.go | 131 +++++++++ backend/hooks/framework/hook_wire.go | 50 ++++ backend/hooks/wechat_push/wechat_push.go | 21 +- backend/internal/gopop/session.go | 19 +- backend/listen/http_server/http_server.go | 9 + backend/listen/imap_server/server.go | 5 + backend/listen/imap_server/session_copy.go | 34 +-- backend/listen/imap_server/session_expunge.go | 63 ++++- backend/listen/imap_server/session_fetch.go | 249 +++--------------- backend/listen/imap_server/session_idle.go | 40 ++- .../listen/imap_server/session_messages.go | 86 ++++++ backend/listen/imap_server/session_move.go | 38 +-- backend/listen/imap_server/session_poll.go | 49 +++- backend/listen/imap_server/session_search.go | 6 +- backend/listen/imap_server/session_select.go | 21 +- backend/listen/imap_server/session_store.go | 36 +-- backend/listen/pop3_server/action.go | 45 ++-- backend/res_init/init.go | 9 +- backend/services/list/imap_search.go | 111 ++++++-- backend/services/list/list.go | 3 + backend/services/recovery/service.go | 49 +++- backend/services/setup/ssl/ssl.go | 63 ++++- deploy/install-windows.ps1 | 31 ++- docs/代码逐行审查报告-20260906.md | 130 ++++++++- frontend/src/views/Editer/Editer.vue | 1 + frontend/src/views/List/List.vue | 1 + .../ConfigEditor/ConfigEditor.vue | 25 +- .../ConfigSettings/ConfigSettings.js | 73 ++++- .../PluginSettings/PluginSettings.vue | 2 + .../SpamBlockSettings/SpamBlockSettings.js | 57 +++- .../SpamBlockSettings/SpamBlockSettings.vue | 21 +- .../TelegramSettings/TelegramSettings.css | 43 +++ .../TelegramSettings/TelegramSettings.js | 90 +++++++ .../TelegramSettings/TelegramSettings.vue | 43 +++ .../components/RuleSettings/RuleSettings.js | 23 +- .../components/RuleSettings/RuleSettings.vue | 16 +- .../SecuritySettings/SecuritySettings.js | 26 +- .../ThirdPartyAuthorization.js | 1 + .../ThirdPartyAuthorization.vue | 2 +- plugins/pmail_telegram_push/hook/send.go | 81 +++--- test/frontend/config-settings.test.mjs | 65 ++++- test/frontend/telegram-settings.test.mjs | 77 ++++++ test/review/config_findings_test.go | 127 +++++++++ test/review/controller_findings_test.go | 9 + test/review/frontend-findings.test.mjs | 74 ++++-- test/review/imap_findings_test.go | 142 +++++++++- test/review/imap_wire_findings_test.go | 153 +++++++++++ test/review/lifecycle_findings_test.go | 56 ++++ test/review/message_findings_test.go | 28 +- test/review/pop3_action_findings_test.go | 65 ++++- test/review/recovery_findings_test.go | 66 +++++ test/review/run.mjs | 5 + test/review/runtime_findings_test.go | 77 ++++++ test/review/ssl_findings_test.go | 5 +- test/review/telegram_findings_test.go | 40 +++ test/review/wechat_findings_test.go | 55 ++++ test/review/windows-install-findings.ps1 | 5 +- 71 files changed, 2853 insertions(+), 664 deletions(-) create mode 100644 backend/config/pending.go create mode 100644 backend/config/ssl_commit.go create mode 100644 backend/controllers/config_pending.go create mode 100644 backend/dto/parsemail/imap.go create mode 100644 backend/hooks/framework/hook_wire.go create mode 100644 backend/listen/imap_server/session_messages.go create mode 100644 frontend/src/views/components/PluginSettings/TelegramSettings/TelegramSettings.css create mode 100644 frontend/src/views/components/PluginSettings/TelegramSettings/TelegramSettings.js create mode 100644 frontend/src/views/components/PluginSettings/TelegramSettings/TelegramSettings.vue create mode 100644 test/frontend/telegram-settings.test.mjs create mode 100644 test/review/imap_wire_findings_test.go create mode 100644 test/review/lifecycle_findings_test.go create mode 100644 test/review/wechat_findings_test.go diff --git a/backend/config/config.go b/backend/config/config.go index 968a34f1..7afb1c69 100644 --- a/backend/config/config.go +++ b/backend/config/config.go @@ -12,6 +12,7 @@ import ( "os" "path/filepath" "strings" + "sync" "sync/atomic" "time" ) @@ -20,6 +21,9 @@ var IsInit bool var legacyInitState atomic.Bool var DevelopmentMode bool +// 旧配置读取可能迁移凭据,因此读取、整份保存及 SSL 合并必须共用同一把锁。 +var legacyConfigMu sync.Mutex + type Config struct { LogLevel string `json:"logLevel"` // 日志级别 Domain string `json:"domain"` @@ -156,6 +160,8 @@ func (l *logFormatter) Format(entry *log.Entry) ([]byte, error) { return b.Bytes(), nil } func Init() error { + legacyConfigMu.Lock() + defer legacyConfigMu.Unlock() var cfgData []byte var err error args := os.Args @@ -276,6 +282,12 @@ func ReadPrivateKey() (*ecdsa.PrivateKey, bool, error) { return privateKey, created, nil } func WriteConfig(cfg *Config) error { + legacyConfigMu.Lock() + defer legacyConfigMu.Unlock() + return writeConfig(cfg) +} + +func writeConfig(cfg *Config) error { if err := writeProtectedConfigFile("config.json", cfg); err != nil { return err } @@ -312,6 +324,12 @@ func writeProtectedConfigFile(name string, cfg *Config) error { } func ReadConfig() (*Config, error) { + legacyConfigMu.Lock() + defer legacyConfigMu.Unlock() + return readConfig() +} + +func readConfig() (*Config, error) { configData := Config{ DkimPrivateKeyPath: ROOT_PATH + "config/dkim/dkim.priv", SSLPrivateKeyPath: ROOT_PATH + "config/ssl/private.key", diff --git a/backend/config/manager_v2.go b/backend/config/manager_v2.go index bfebf9c4..f0a9b554 100644 --- a/backend/config/manager_v2.go +++ b/backend/config/manager_v2.go @@ -73,9 +73,10 @@ type ValidationResult struct { } type UpdateResult struct { - Generation uint64 - Candidate RuntimeConfig - Pending bool + CandidateID string + Generation uint64 + Candidate RuntimeConfig + Pending bool } type ApplyResult struct { @@ -325,11 +326,15 @@ func (m *ConfigManager) Update(ctx context.Context, req UpdateRequest) (*UpdateR if err := contextError(ctx); err != nil { return nil, err } + id, err := newRollbackTicketID() + if err != nil { + return nil, err + } m.mu.Lock() - m.pending = &pendingConfig{Candidate: candidate, Section: req.Section, Actor: req.Actor, Confirmation: req.Confirmation} - m.status.PendingRestart = servicesForSection(req.Section) + m.pending = &pendingConfig{ID: id, Candidate: candidate, Section: req.Section, Actor: req.Actor, Confirmation: req.Confirmation, PreviousRestart: append([]ServiceName(nil), m.status.PendingRestart...)} + m.status.PendingRestart = mergeServices(m.status.PendingRestart, servicesForSection(req.Section)) m.mu.Unlock() - return &UpdateResult{Generation: candidate.Generation, Candidate: cloneRuntimeConfig(candidate), Pending: true}, nil + return &UpdateResult{CandidateID: id, Generation: candidate.Generation, Candidate: cloneRuntimeConfig(candidate), Pending: true}, nil } func (m *ConfigManager) Apply(ctx context.Context, generation uint64) (*ApplyResult, error) { @@ -344,7 +349,15 @@ func (m *ConfigManager) ApplyAuthorized(ctx context.Context, generation uint64, return m.apply(ctx, generation, actor) } -func (m *ConfigManager) apply(ctx context.Context, generation uint64, actor AuditActor) (*ApplyResult, error) { +// ApplyPendingAuthorized 同时校验候选标识和分区,避免取消后代次复用导致旧请求误应用。 +func (m *ConfigManager) ApplyPendingAuthorized(ctx context.Context, generation uint64, id string, section Section, actor AuditActor) (*ApplyResult, error) { + if id == "" || actor.UserID == "" || actor.SessionID == "" { + return nil, ErrAuthorizationRequired + } + return m.apply(ctx, generation, actor, id, string(section)) +} + +func (m *ConfigManager) apply(ctx context.Context, generation uint64, actor AuditActor, identity ...string) (*ApplyResult, error) { if err := contextError(ctx); err != nil { return nil, err } @@ -362,12 +375,20 @@ func (m *ConfigManager) apply(ctx context.Context, generation uint64, actor Audi } m.mu.RUnlock() candidate := pending.Candidate + if len(identity) > 0 && (len(identity) != 2 || identity[0] != pending.ID || identity[1] != string(pending.Section)) { + return nil, ErrGenerationConflict + } if err := contextError(ctx); err != nil { return nil, err } if actor.UserID != "" && (pending.Actor.UserID != actor.UserID || pending.Actor.SessionID != actor.SessionID) { return nil, ErrAuthorizationRequired } + if actor.UserID != "" { + if err := m.authorize(ctx, actor, ConfigOperationUpdate, pending.Section); err != nil { + return nil, err + } + } if generation != candidate.Generation || candidate.Generation != current.Generation+1 { return nil, ErrGenerationConflict } @@ -386,14 +407,19 @@ func (m *ConfigManager) apply(ctx context.Context, generation uint64, actor Audi if err := contextError(ctx); err != nil { return nil, err } - if _, _, err := m.transactions.CreateBackupIfDue(current, time.Now().UTC()); err != nil { - return nil, err - } - if err := contextError(ctx); err != nil { - return nil, err - } - if err := m.transactions.Commit(current, candidate); err != nil { - return nil, err + if !pending.Committed { + if _, _, err := m.transactions.CreateBackupIfDue(current, time.Now().UTC()); err != nil { + return nil, err + } + if err := contextError(ctx); err != nil { + return nil, err + } + if err := m.transactions.Commit(current, candidate); err != nil { + return nil, err + } + m.mu.Lock() + m.pending.Committed = true + m.mu.Unlock() } if err := m.publishSnapshot(candidate); err != nil { return nil, err diff --git a/backend/config/partitioned_compatibility.go b/backend/config/partitioned_compatibility.go index e4683d0a..edfc0305 100644 --- a/backend/config/partitioned_compatibility.go +++ b/backend/config/partitioned_compatibility.go @@ -32,6 +32,11 @@ func loadPartitionedCompatibility() (*Config, error) { if err != nil { return nil, err } + return runtimeCompatibility(runtime) +} + +// runtimeCompatibility 从同一已验证候选构造兼容快照,避免提交成功后再次读盘产生新失败窗口。 +func runtimeCompatibility(runtime RuntimeConfig) (*Config, error) { vault, err := credential.NewVault(ROOT_PATH) if err != nil { return nil, err diff --git a/backend/config/pending.go b/backend/config/pending.go new file mode 100644 index 00000000..4e7c1513 --- /dev/null +++ b/backend/config/pending.go @@ -0,0 +1,90 @@ +package config + +import ( + "context" + "time" +) + +type PendingView struct { + ID string `json:"candidateId"` + Generation uint64 `json:"generation"` + Section Section `json:"section"` + SameSession bool `json:"sameSession"` + Committed bool `json:"committed"` + Candidate RuntimeConfig `json:"-"` +} + +// Pending 返回独立快照,不向调用者泄露候选操作者的会话标识。 +func (m *ConfigManager) Pending(actor AuditActor) *PendingView { + if m == nil { + return nil + } + m.mu.RLock() + defer m.mu.RUnlock() + if m.pending == nil { + return nil + } + return &PendingView{ + ID: m.pending.ID, + Generation: m.pending.Candidate.Generation, Section: m.pending.Section, + SameSession: actor.UserID != "" && actor.UserID == m.pending.Actor.UserID && actor.SessionID != "" && actor.SessionID == m.pending.Actor.SessionID, + Committed: m.pending.Committed, Candidate: cloneRuntimeConfig(m.pending.Candidate), + } +} + +// ManagePending 显式恢复或取消指定代次;跨会话必须重新验证近期身份并确认操作。 +func (m *ConfigManager) ManagePending(ctx context.Context, generation uint64, id string, actor AuditActor, confirmation StepUpConfirmation, cancel bool) error { + if err := contextError(ctx); err != nil { + return err + } + if m == nil || actor.UserID == "" || actor.SessionID == "" { + return ErrAuthorizationRequired + } + m.writeMu.Lock() + defer m.writeMu.Unlock() + m.mu.RLock() + pending := m.pending + current := m.current.Generation + m.mu.RUnlock() + if pending == nil || id == "" || pending.ID != id || pending.Candidate.Generation != generation { + return ErrGenerationConflict + } + if err := m.authorize(ctx, actor, ConfigOperationUpdate, pending.Section); err != nil { + return err + } + sameSession := actor.UserID == pending.Actor.UserID && actor.SessionID == pending.Actor.SessionID + systemRecovery := pending.Committed && pending.Actor.UserID == "system" && actor.Role == "super_admin" + if actor.UserID != pending.Actor.UserID && !systemRecovery && (!cancel || actor.Role != "super_admin") { + return ErrAuthorizationRequired + } + if !sameSession && (!actor.RecentAuth || !validStepUpConfirmation(confirmation)) { + return ErrStepUpRequired + } + if err := m.confirm(ctx, actor, confirmation, ConfigOperationUpdate, pending.Section); err != nil { + return err + } + if cancel && pending.Committed { + return ErrRuntimeSnapshotUnavailable + } + field := "pending.resume" + if cancel { + field = "pending.cancel" + } + if err := m.writeAudit(ctx, ConfigAuditEvent{ + Operation: ConfigOperationUpdate, Section: pending.Section, ActorID: actor.UserID, ActorRole: actor.Role, + FromGeneration: current, ToGeneration: current, ChangedFields: []string{field}, + ConfirmationMethod: confirmation.Method, Result: "success", CreatedAt: time.Now().UTC(), + }); err != nil { + return err + } + m.mu.Lock() + defer m.mu.Unlock() + if cancel { + m.pending = nil + m.status.PendingRestart = append([]ServiceName(nil), pending.PreviousRestart...) + } else { + m.pending.Actor = actor + m.pending.Confirmation = confirmation + } + return nil +} diff --git a/backend/config/security.go b/backend/config/security.go index 74794705..81648464 100644 --- a/backend/config/security.go +++ b/backend/config/security.go @@ -50,10 +50,13 @@ type ConfigSecurity struct { } type pendingConfig struct { - Candidate RuntimeConfig - Section Section - Actor AuditActor - Confirmation StepUpConfirmation + ID string + Candidate RuntimeConfig + Section Section + Actor AuditActor + Confirmation StepUpConfirmation + Committed bool + PreviousRestart []ServiceName } type MemoryAuditWriter struct { diff --git a/backend/config/ssl_commit.go b/backend/config/ssl_commit.go new file mode 100644 index 00000000..5bac11f2 --- /dev/null +++ b/backend/config/ssl_commit.go @@ -0,0 +1,163 @@ +package config + +import ( + "context" + "os" + "path/filepath" + "reflect" + "sync" + "time" +) + +var sslCommitMu sync.Mutex + +// WriteSSLConfig 是初始化及自动续期的内部入口,只允许更新 SSL 引用和 HTTPS 开关。 +// 旧证书保持不动;已有人工候选或签发期间域名、证书策略变化时拒绝提交。 +func WriteSSLConfig(expected, next *Config) error { + if expected == nil || next == nil { + return ErrInvalidSSLReference + } + sslCommitMu.Lock() + defer sslCommitMu.Unlock() + legacyConfigMu.Lock() + defer legacyConfigMu.Unlock() + data, err := readConfigFile("config.json") + if err != nil && !os.IsNotExist(err) { + return err + } + partitioned := false + if len(data) > 0 { + partitioned, err = isPartitionedConfig(data) + if err != nil { + return err + } + } + if !partitioned { + merged := *next + if len(data) > 0 { + live, err := readConfig() + if err != nil { + return err + } + if !sameSSLPolicy(live, expected) { + return ErrGenerationConflict + } + // 签发期间其他设置可能已经保存,只合并本入口负责的 SSL 字段。 + merged = *live + merged.SSL = next.SSL + merged.SSLType = next.SSLType + merged.SSLPrivateKeyPath = next.SSLPrivateKeyPath + merged.SSLPublicKeyPath = next.SSLPublicKeyPath + merged.HttpsEnabled = next.HttpsEnabled + } + if err := writeConfig(&merged); err != nil { + return err + } + *next = merged + return nil + } + manager := CurrentManager() + if manager == nil { + manager, err = NewConfigManager(ROOT_PATH, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: !DevelopmentMode}}) + if err != nil { + return err + } + SetRuntimeManager(manager) + } + if filepath.Clean(manager.loader.root) != filepath.Clean(ROOT_PATH) { + return ErrGenerationConflict + } + manager.writeMu.Lock() + defer manager.writeMu.Unlock() + manager.mu.RLock() + current := cloneRuntimeConfig(manager.current) + pending := manager.pending != nil + manager.mu.RUnlock() + if pending { + return ErrGenerationConflict + } + live, err := readConfig() + if err != nil { + return err + } + if !sameSSLPolicy(live, expected) { + return ErrGenerationConflict + } + candidate := cloneRuntimeConfig(current) + candidate.SSL.TLS = next.SSL + candidate.Root.HTTP.HTTPSEnabled = next.HttpsEnabled == 1 + setCandidateGeneration(&candidate, current.Generation+1) + if err := ValidateRuntimeConfig(candidate, manager.options); err != nil { + return err + } + if err := ValidateRuntimeSecrets(manager.vault, candidate); err != nil { + return err + } + if err := manager.snapshots.Validate(candidate); err != nil { + return err + } + committed, err := runtimeCompatibility(candidate) + if err != nil { + return err + } + id, err := newRollbackTicketID() + if err != nil { + return err + } + if _, _, err := manager.transactions.CreateBackupIfDue(current, time.Now().UTC()); err != nil { + return err + } + if err := manager.transactions.Commit(current, candidate); err != nil { + return err + } + // 已落盘候选必须留在管理器中;运行态发布失败后管理员可显式恢复,而不能取消。 + manager.mu.Lock() + manager.pending = &pendingConfig{ + ID: id, Candidate: candidate, Section: SectionSSL, Committed: true, + Actor: AuditActor{UserID: "system", Role: "system"}, + PreviousRestart: append([]ServiceName(nil), manager.status.PendingRestart...), + } + manager.status.PendingRestart = mergeServices(manager.status.PendingRestart, servicesForSection(SectionSSL)) + manager.mu.Unlock() + if err := manager.publishSnapshot(candidate); err != nil { + return err + } + now := time.Now().UTC() + manager.mu.Lock() + manager.current = cloneRuntimeConfig(candidate) + manager.pending = nil + manager.status.SetupState = candidate.SetupState + manager.status.DegradedReasons = nil + manager.status.ActiveGeneration = candidate.Generation + manager.status.LastValidAt = &now + manager.mu.Unlock() + // 兼容快照与本次已提交候选一致,不再静默忽略提交后的磁盘读取失败。 + *next = *committed + PublishConfigSnapshot(committed) + return manager.writeAudit(context.Background(), ConfigAuditEvent{ + Operation: ConfigOperationUpdate, Section: SectionSSL, ActorID: "system", ActorRole: "system", + FromGeneration: current.Generation, ToGeneration: candidate.Generation, + ChangedFields: []string{"tls"}, Result: "success", CreatedAt: now, + }) +} + +func sameSSLPolicy(left, right *Config) bool { + return reflect.DeepEqual(left.SSL, right.SSL) && left.SSLType == right.SSLType && + left.SSLPrivateKeyPath == right.SSLPrivateKeyPath && left.SSLPublicKeyPath == right.SSLPublicKeyPath && + left.Domain == right.Domain && left.WebDomain == right.WebDomain && + reflect.DeepEqual(left.Domains, right.Domains) && left.HttpsEnabled == right.HttpsEnabled +} + +func mergeServices(previous, next []ServiceName) []ServiceName { + result := append([]ServiceName(nil), previous...) + for _, service := range next { + found := false + for _, existing := range result { + found = found || service == existing + } + if !found { + result = append(result, service) + } + } + return result +} diff --git a/backend/config/ssl_storage.go b/backend/config/ssl_storage.go index aac97beb..eedb5de0 100644 --- a/backend/config/ssl_storage.go +++ b/backend/config/ssl_storage.go @@ -197,6 +197,9 @@ func ResolveManagedSSLReferences(ssl SSLConfig) (ManagedSSLReference, error) { if err != nil { return ManagedSSLReference{}, err } + if filepath.Dir(certificatePath) != filepath.Dir(privateKeyPath) { + return ManagedSSLReference{}, ErrInvalidSSLReference + } if err := validateManagedSSLFiles(certificatePath, privateKeyPath); err != nil { return ManagedSSLReference{}, err } @@ -379,12 +382,80 @@ func resolveManagedReference(rootPath, reference, expectedName string) (string, if err != nil || filepath.Base(target) != expectedName || !pathWithin(rootPath, target) { return "", ErrInvalidSSLReference } - if filepath.Dir(target) != filepath.Join(rootPath, "current") { + directory := filepath.Dir(target) + version := filepath.Base(directory) + if filepath.Dir(directory) != rootPath || (version != "current" && !validSSLVersion(version)) { return "", ErrInvalidSSLReference } + resolved, err := filepath.EvalSymlinks(directory) + if err != nil || filepath.Clean(resolved) != filepath.Clean(directory) { + return "", ErrUnsafeSSLFile + } return target, nil } +func validSSLVersion(version string) bool { + if !strings.HasPrefix(version, "version-") || len(version) != len("version-")+32 { + return false + } + _, err := hex.DecodeString(strings.TrimPrefix(version, "version-")) + return err == nil +} + +// StageManagedSSL 只写入独立版本,不替换现用文件;配置事务成功后引用才生效。 +func StageManagedSSL(mode string, privateKeyPEM, certificatePEM, issuerPEM []byte, domains []string) (ManagedSSLReference, error) { + if err := validateSSLMaterial(privateKeyPEM, certificatePEM, issuerPEM, domains); err != nil { + return ManagedSSLReference{}, err + } + root, err := managedSSLRootPath(mode) + if err != nil { + return ManagedSSLReference{}, err + } + if err := ensureManagedDirectory(root); err != nil { + return ManagedSSLReference{}, err + } + token, err := randomStorageToken() + if err != nil { + return ManagedSSLReference{}, err + } + directory := filepath.Join(root, "version-"+token) + if err := os.Mkdir(directory, 0700); err != nil { + return ManagedSSLReference{}, err + } + storage, err := os.OpenRoot(directory) + if err != nil { + return ManagedSSLReference{}, err + } + defer storage.Close() + for _, file := range []struct { + name string + data []byte + }{{"private.key", privateKeyPEM}, {"public.crt", certificatePEM}, {"issuer.crt", issuerPEM}} { + if len(file.data) > 0 { + if err := atomicfile.WriteRoot(storage, file.name, file.data, 0600); err != nil { + return ManagedSSLReference{}, err + } + } + } + publicPath, privatePath := filepath.Join(directory, "public.crt"), filepath.Join(directory, "private.key") + if err := validateManagedSSLFiles(publicPath, privatePath); err != nil { + return ManagedSSLReference{}, err + } + rootPath, err := filepath.Abs(ROOT_PATH) + if err != nil { + return ManagedSSLReference{}, err + } + publicRef, err := filepath.Rel(rootPath, publicPath) + if err != nil { + return ManagedSSLReference{}, err + } + privateRef, err := filepath.Rel(rootPath, privatePath) + if err != nil { + return ManagedSSLReference{}, err + } + return ManagedSSLReference{Mode: mode, CertificateRef: filepath.ToSlash(publicRef), PrivateKeyRef: filepath.ToSlash(privateRef), CertificatePath: publicPath, PrivateKeyPath: privatePath}, nil +} + func validateManagedSSLFiles(certificatePath, privateKeyPath string) error { for _, item := range []struct { path string diff --git a/backend/controllers/config.go b/backend/controllers/config.go index cec33325..100500d9 100644 --- a/backend/controllers/config.go +++ b/backend/controllers/config.go @@ -120,16 +120,17 @@ func UpdateConfig(ctx *appcontext.Context, w http.ResponseWriter, req *http.Requ writeConfigError(w, err) return } - response.NewSuccessResponse(map[string]any{"generation": result.Generation, "pending": result.Pending}).FPrint(w) + response.NewSuccessResponse(map[string]any{"generation": result.Generation, "pending": result.Pending, "candidateId": result.CandidateID}).FPrint(w) } type configApplyRequest struct { - Generation uint64 `json:"generation"` + Generation uint64 `json:"generation"` + CandidateID string `json:"candidateId"` } // ApplyConfig 应用已保存的候选配置代次。 func ApplyConfig(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) { - manager, _, ok := configRequestManager(w, req) + manager, section, ok := configRequestManager(w, req) if !ok { return } @@ -142,7 +143,7 @@ func ApplyConfig(ctx *appcontext.Context, w http.ResponseWriter, req *http.Reque writeConfigError(w, errors.New("invalid configuration generation")) return } - result, err := manager.ApplyAuthorized(req.Context(), input.Generation, configActor(ctx, req)) + result, err := manager.ApplyPendingAuthorized(req.Context(), input.Generation, input.CandidateID, section, configActor(ctx, req)) if err != nil { writeConfigError(w, err) return diff --git a/backend/controllers/config_pending.go b/backend/controllers/config_pending.go new file mode 100644 index 00000000..324b496e --- /dev/null +++ b/backend/controllers/config_pending.go @@ -0,0 +1,60 @@ +package controllers + +import ( + "errors" + "net/http" + + "git.pcux.cn/xuans/Smail/config" + "git.pcux.cn/xuans/Smail/dto/response" + "git.pcux.cn/xuans/Smail/services/access" + appcontext "git.pcux.cn/xuans/Smail/utils/context" +) + +// GetPendingConfig 返回候选的脱敏视图,用于刷新或重新登录后的恢复。 +func GetPendingConfig(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) { + manager, ok := configManagerOrError(w) + if !ok { + return + } + if err := authorizeCapability(ctx, access.CapabilityConfigRead, "", false); err != nil { + writeConfigAccessError(w, err) + return + } + pending := manager.Pending(configActor(ctx, req)) + if pending == nil { + response.NewSuccessResponse(nil).FPrint(w) + return + } + public, err := publicConfigSection(pending.Candidate, pending.Section) + if err != nil { + writeConfigError(w, err) + return + } + response.NewSuccessResponse(map[string]any{"generation": pending.Generation, "candidateId": pending.ID, "section": pending.Section, "sameSession": pending.SameSession, "committed": pending.Committed, "payload": public}).FPrint(w) +} + +func ManagePendingConfig(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) { + manager, ok := configManagerOrError(w) + if !ok { + return + } + if err := authorizeCapability(ctx, access.CapabilityConfigWriteGlobal, "", false); err != nil { + writeConfigAccessError(w, err) + return + } + var input struct { + Generation uint64 `json:"generation"` + CandidateID string `json:"candidateId"` + Action string `json:"action"` + Confirmation config.StepUpConfirmation `json:"confirmation"` + } + if err := decodeJSONRequest(w, req, &input); err != nil || input.Generation == 0 || (input.Action != "cancel" && input.Action != "resume") { + writeConfigError(w, errors.New("invalid configuration request")) + return + } + if err := manager.ManagePending(req.Context(), input.Generation, input.CandidateID, configActor(ctx, req), input.Confirmation, input.Action == "cancel"); err != nil { + writeConfigError(w, err) + return + } + response.NewSuccessResponse(nil).FPrint(w) +} diff --git a/backend/controllers/login_verification.go b/backend/controllers/login_verification.go index 63604745..73e24d95 100644 --- a/backend/controllers/login_verification.go +++ b/backend/controllers/login_verification.go @@ -82,21 +82,22 @@ func loginVerificationPolicyEnabled(enabled bool, policy string) bool { } func beginLoginVerification(user *models.User, remoteIP string) (map[string]any, error) { + runtime := currentSecurityRuntime() if !loginVerificationEnabled() { return nil, nil } - if user == nil || user.ID <= 0 || securityRuntime.verification == nil || securityRuntime.external == nil { + if user == nil || user.ID <= 0 || runtime.verification == nil || runtime.external == nil { return nil, errLoginVerificationUnavailable } - binding, err := securityRuntime.external.FindVerifiedPrimaryByUser(user.ID) + binding, err := runtime.external.FindVerifiedPrimaryByUser(user.ID) if err != nil { return nil, errLoginVerificationUnavailable } - email, err := securityRuntime.external.Decrypt(*binding) + email, err := runtime.external.Decrypt(*binding) if err != nil { return nil, errLoginVerificationUnavailable } - challenge, err := securityRuntime.verification.Issue(verification.IssueRequest{ + challenge, err := runtime.verification.Issue(verification.IssueRequest{ Purpose: verification.PurposeLogin, Account: user.Account, Email: email, @@ -109,7 +110,7 @@ func beginLoginVerification(user *models.User, remoteIP string) (map[string]any, cleanupLoginVerificationLocked(time.Now()) if len(loginVerificationState.items) >= loginVerificationMaxEntries { loginVerificationState.Unlock() - securityRuntime.verification.Store.InvalidateChallenge(challenge.ID) + runtime.verification.Store.InvalidateChallenge(challenge.ID) return nil, errLoginVerificationUnavailable } loginVerificationState.items[challenge.ID] = loginVerificationItem{ @@ -128,6 +129,7 @@ func beginLoginVerification(user *models.User, remoteIP string) (map[string]any, // ResendLoginVerification 使旧登录验证码失效后重新发送新验证码。 func ResendLoginVerification(ctx *context.Context, w http.ResponseWriter, req *http.Request) { + runtime := currentSecurityRuntime() var input resendLoginVerificationRequest if err := decodeJSONRequest(w, req, &input); err != nil || strings.TrimSpace(input.ID) == "" { writeSecurityError(w, response.ParamsError, "验证码请求无效") @@ -145,12 +147,12 @@ func ResendLoginVerification(ctx *context.Context, w http.ResponseWriter, req *h writeSecurityError(w, response.ParamsError, "登录验证已失效,请重新登录") return } - if securityRuntime.verification == nil || securityRuntime.verification.Store == nil { + if runtime.verification == nil || runtime.verification.Store == nil { writeSecurityError(w, response.ParamsError, "验证码服务暂不可用") return } - securityRuntime.verification.Store.InvalidateChallenge(input.ID) - challenge, err := securityRuntime.verification.Issue(verification.IssueRequest{ + runtime.verification.Store.InvalidateChallenge(input.ID) + challenge, err := runtime.verification.Issue(verification.IssueRequest{ Purpose: verification.PurposeLogin, UserID: item.UserID, Account: item.Account, @@ -180,8 +182,9 @@ func ResendLoginVerification(ctx *context.Context, w http.ResponseWriter, req *h // CompleteLoginVerification 只有验证码成功后才创建完整 Session。 func CompleteLoginVerification(ctx *context.Context, w http.ResponseWriter, req *http.Request) { + runtime := currentSecurityRuntime() var input loginVerificationRequest - if err := decodeJSONRequest(w, req, &input); err != nil || securityRuntime.verification == nil { + if err := decodeJSONRequest(w, req, &input); err != nil || runtime.verification == nil { writeSecurityError(w, response.ParamsError, "验证失败") return } @@ -196,7 +199,7 @@ func CompleteLoginVerification(ctx *context.Context, w http.ResponseWriter, req writeSecurityError(w, response.ParamsError, "验证失败") return } - if err := securityRuntime.verification.Verify(input.ID, item.Account, item.Email, input.Code, verification.PurposeLogin, remoteIP(req)); err != nil { + if err := runtime.verification.Verify(input.ID, item.Account, item.Email, input.Code, verification.PurposeLogin, remoteIP(req)); err != nil { writeSecurityError(w, response.ParamsError, "验证失败") return } diff --git a/backend/controllers/security.go b/backend/controllers/security.go index 0b48f38b..2dea5608 100644 --- a/backend/controllers/security.go +++ b/backend/controllers/security.go @@ -9,6 +9,7 @@ import ( "net/url" "strconv" "strings" + "sync" "git.pcux.cn/xuans/Smail/config" "git.pcux.cn/xuans/Smail/db" @@ -68,42 +69,67 @@ func rejectUnavailableRecovery(w http.ResponseWriter) bool { return true } -var securityRuntime = struct { +type securityRuntimeState struct { verification *verification.Service external *externalemail.Repository recovery *recovery.Service credentials *credentials.Service webauthn *webauthnservice.Service -}{} +} + +var securityRuntime securityRuntimeState +var securityRuntimeMu sync.RWMutex + +// 每个请求持有同一份服务快照,发布锁不跨越数据库、投递和失效回调。 +func currentSecurityRuntime() securityRuntimeState { + securityRuntimeMu.RLock() + defer securityRuntimeMu.RUnlock() + return securityRuntime +} // ConfigureSecurityRuntime 由主程序在数据库和内存安全服务就绪后调用。 func ConfigureSecurityRuntime(verify *verification.Service, external *externalemail.Repository, recover *recovery.Service, credential *credentials.Service) { + if recover != nil { + recover.SetSecurityStateInvalidator(InvalidateSecurityRuntimeUser) + } + securityRuntimeMu.Lock() + defer securityRuntimeMu.Unlock() securityRuntime.verification = verify securityRuntime.external = external securityRuntime.recovery = recover securityRuntime.credentials = credential - if recover != nil { - recover.SetSecurityStateInvalidator(InvalidateSecurityRuntimeUser) - } } func ConfigureWebAuthnRuntime(service *webauthnservice.Service) { + securityRuntimeMu.Lock() + defer securityRuntimeMu.Unlock() securityRuntime.webauthn = service } +// ClearSecurityRuntime 在停止入口后一次撤下全部安全服务,避免保留已停止的旧库依赖。 +func ClearSecurityRuntime() { + securityRuntimeMu.Lock() + securityRuntime = securityRuntimeState{} + securityRuntimeMu.Unlock() + loginVerificationState.Lock() + clear(loginVerificationState.items) + loginVerificationState.Unlock() +} + // InvalidateSecurityRuntimeUser 清除密码重置后仍驻留在内存中的安全流程状态。 func InvalidateSecurityRuntimeUser(userID int, account string) { if userID <= 0 { return } - if securityRuntime.recovery != nil { - securityRuntime.recovery.InvalidateUser(userID) + runtime := currentSecurityRuntime() + if runtime.recovery != nil { + runtime.recovery.InvalidateUser(userID) } - if securityRuntime.verification != nil && securityRuntime.verification.Store != nil { - securityRuntime.verification.Store.InvalidateAccount(account) + if runtime.verification != nil && runtime.verification.Store != nil { + runtime.verification.Store.InvalidateAccount(account) } - if securityRuntime.webauthn != nil { - securityRuntime.webauthn.InvalidateUser(userID) + if runtime.webauthn != nil { + runtime.webauthn.InvalidateUser(userID) } loginVerificationState.Lock() for id, item := range loginVerificationState.items { @@ -122,7 +148,7 @@ func LoadSecurityContext(ctx *context.Context) { ctx.RecoveryCodeConfigured = false return } - service := securityRuntime.credentials + service := currentSecurityRuntime().credentials if service == nil && db.Instance != nil { service, _ = credentials.NewService(db.Instance) } @@ -136,8 +162,8 @@ func LoadSecurityContext(ctx *context.Context) { } func credentialRuntime() *credentials.Service { - if securityRuntime.credentials != nil { - return securityRuntime.credentials + if service := currentSecurityRuntime().credentials; service != nil { + return service } if db.Instance == nil { return nil @@ -406,6 +432,7 @@ func RevokeProtocolCredential(ctx *context.Context, w http.ResponseWriter, req * response.NewSuccessResponse(map[string]any{"protocol": protocol, "revoked": true}).FPrint(w) } func RequestVerification(ctx *context.Context, w http.ResponseWriter, req *http.Request) { + runtime := currentSecurityRuntime() if ctx == nil || ctx.UserID <= 0 { writeSecurityError(w, response.NeedLogin, "请先登录") return @@ -422,11 +449,11 @@ func RequestVerification(ctx *context.Context, w http.ResponseWriter, req *http. writeSecurityError(w, response.ParamsError, "请求无效") return } - if input.Email == "" || strings.TrimSpace(input.Account) == "" || !verification.IsSupportedPurpose(input.Purpose) || securityRuntime.verification == nil { + if input.Email == "" || strings.TrimSpace(input.Account) == "" || !verification.IsSupportedPurpose(input.Purpose) || runtime.verification == nil { writeSecurityError(w, response.ParamsError, "请求已受理") return } - challenge, err := securityRuntime.verification.Issue(verification.IssueRequest{Purpose: input.Purpose, UserID: ctx.UserID, Account: input.Account, Email: input.Email}, remoteIP(req)) + challenge, err := runtime.verification.Issue(verification.IssueRequest{Purpose: input.Purpose, UserID: ctx.UserID, Account: input.Account, Email: input.Email}, remoteIP(req)) if err != nil { writeSecurityError(w, response.ParamsError, "请求已受理") return @@ -436,12 +463,13 @@ func RequestVerification(ctx *context.Context, w http.ResponseWriter, req *http. // VerifyVerification 消费一次性验证码,只返回后续流程所需的短时票据。 func VerifyVerification(ctx *context.Context, w http.ResponseWriter, req *http.Request) { + runtime := currentSecurityRuntime() if ctx == nil || ctx.UserID <= 0 { writeSecurityError(w, response.NeedLogin, "请先登录") return } var input verificationCheckRequest - if err := decodeJSONRequest(w, req, &input); err != nil || securityRuntime.verification == nil { + if err := decodeJSONRequest(w, req, &input); err != nil || runtime.verification == nil { writeSecurityError(w, response.ParamsError, "验证失败") return } @@ -452,7 +480,7 @@ func VerifyVerification(ctx *context.Context, w http.ResponseWriter, req *http.R writeSecurityError(w, response.ParamsError, "验证失败") return } - if err := securityRuntime.verification.Verify(input.ID, input.Account, input.Email, input.Code, input.Purpose, remoteIP(req)); err != nil { + if err := runtime.verification.Verify(input.ID, input.Account, input.Email, input.Code, input.Purpose, remoteIP(req)); err != nil { writeSecurityError(w, response.ParamsError, "验证失败") return } @@ -460,11 +488,12 @@ func VerifyVerification(ctx *context.Context, w http.ResponseWriter, req *http.R } func BeginPasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *http.Request) { + runtime := currentSecurityRuntime() if rejectUnavailableRecovery(w) { return } var input verificationRequest - if err := decodeJSONRequest(w, req, &input); err != nil || securityRuntime.recovery == nil { + if err := decodeJSONRequest(w, req, &input); err != nil || runtime.recovery == nil { writeSecurityError(w, response.ParamsError, "请求已受理") return } @@ -486,7 +515,7 @@ func BeginPasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *htt writeSecurityError(w, response.ParamsError, "\u8bf7\u6c42\u5df2\u53d7\u7406") return } - id, err := securityRuntime.recovery.Begin(recovery.BeginRequest{Identifier: input.Account, Email: input.Email, BrowserContext: browserContext}, remoteIP(req)) + id, err := runtime.recovery.Begin(recovery.BeginRequest{Identifier: input.Account, Email: input.Email, BrowserContext: browserContext}, remoteIP(req)) if err != nil && id == "" { emailRecoveryFailureIPAttempts.recordFailure(emailRecoveryFailureIPAttempts.ipKey(req)) writeSecurityError(w, response.ParamsError, "请求已受理") @@ -497,6 +526,7 @@ func BeginPasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *htt } func VerifyPasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *http.Request) { + runtime := currentSecurityRuntime() if rejectUnavailableRecovery(w) { return } @@ -506,7 +536,7 @@ func VerifyPasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *ht return } var input verificationCheckRequest - if err := decodeJSONRequest(w, req, &input); err != nil || securityRuntime.recovery == nil { + if err := decodeJSONRequest(w, req, &input); err != nil || runtime.recovery == nil { emailRecoveryFailureIPAttempts.recordFailure(failureKey) writeSecurityError(w, response.ParamsError, "验证失败") return @@ -524,9 +554,9 @@ func VerifyPasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *ht return } if methodOffline { - ticket, err = securityRuntime.recovery.VerifyOfflineWithContext(input.Account, input.Question, input.Answer, input.RecoveryCode, remoteIP(req), browserContext) + ticket, err = runtime.recovery.VerifyOfflineWithContext(input.Account, input.Question, input.Answer, input.RecoveryCode, remoteIP(req), browserContext) } else { - ticket, err = securityRuntime.recovery.VerifyWithContext(input.ID, input.Account, input.Email, input.Code, input.RecoveryCode, remoteIP(req), browserContext) + ticket, err = runtime.recovery.VerifyWithContext(input.ID, input.Account, input.Email, input.Code, input.RecoveryCode, remoteIP(req), browserContext) } if err != nil { emailRecoveryFailureIPAttempts.recordFailure(failureKey) @@ -537,6 +567,7 @@ func VerifyPasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *ht } func CompletePasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *http.Request) { + runtime := currentSecurityRuntime() if rejectUnavailableRecovery(w) { return } @@ -546,7 +577,7 @@ func CompletePasswordRecovery(ctx *context.Context, w http.ResponseWriter, req * return } var input recoveryCompleteRequest - if err := decodeJSONRequest(w, req, &input); err != nil || securityRuntime.recovery == nil || strings.TrimSpace(input.Ticket) == "" { + if err := decodeJSONRequest(w, req, &input); err != nil || runtime.recovery == nil || strings.TrimSpace(input.Ticket) == "" { emailRecoveryFailureIPAttempts.recordFailure(failureKey) writeSecurityError(w, response.ParamsError, "重置失败") return @@ -557,7 +588,7 @@ func CompletePasswordRecovery(ctx *context.Context, w http.ResponseWriter, req * writeSecurityError(w, response.ParamsError, "\u9a8c\u8bc1\u5931\u8d25") return } - if err := securityRuntime.recovery.CompleteWithContext(input.Ticket, input.NewPassword, browserContext); err != nil { + if err := runtime.recovery.CompleteWithContext(input.Ticket, input.NewPassword, browserContext); err != nil { emailRecoveryFailureIPAttempts.recordFailure(failureKey) writeSecurityError(w, response.ParamsError, "重置失败") return @@ -609,11 +640,12 @@ func newOpaqueRecoveryChallengeID() (string, error) { } func ListExternalEmails(ctx *context.Context, w http.ResponseWriter, req *http.Request) { - if ctx == nil || ctx.UserID <= 0 || securityRuntime.external == nil { + runtime := currentSecurityRuntime() + if ctx == nil || ctx.UserID <= 0 || runtime.external == nil { writeSecurityError(w, response.NeedLogin, "请先登录") return } - items, err := securityRuntime.external.List(ctx.UserID) + items, err := runtime.external.List(ctx.UserID) if err != nil { log.WithError(err).Error("list external email failed") writeSecurityError(w, response.ServerError, "读取外部邮箱失败") @@ -621,7 +653,7 @@ func ListExternalEmails(ctx *context.Context, w http.ResponseWriter, req *http.R } result := make([]map[string]any, 0, len(items)) for _, item := range items { - email, err := securityRuntime.external.Decrypt(item) + email, err := runtime.external.Decrypt(item) if err != nil { continue } @@ -631,7 +663,8 @@ func ListExternalEmails(ctx *context.Context, w http.ResponseWriter, req *http.R } func BindExternalEmail(ctx *context.Context, w http.ResponseWriter, req *http.Request) { - if ctx == nil || ctx.UserID <= 0 || securityRuntime.external == nil || securityRuntime.verification == nil { + runtime := currentSecurityRuntime() + if ctx == nil || ctx.UserID <= 0 || runtime.external == nil || runtime.verification == nil { writeSecurityError(w, response.NeedLogin, "请先登录") return } @@ -660,14 +693,14 @@ func BindExternalEmail(ctx *context.Context, w http.ResponseWriter, req *http.Re return } input.Email = normalizedEmail - item, err := securityRuntime.external.Bind(externalemail.BindRequest{UserID: ctx.UserID, Email: input.Email, Type: input.Type}) + item, err := runtime.external.Bind(externalemail.BindRequest{UserID: ctx.UserID, Email: input.Email, Type: input.Type}) if err != nil { writeSecurityError(w, response.ParamsError, "外部邮箱绑定失败") return } - challenge, err := securityRuntime.verification.Issue(verification.IssueRequest{Purpose: verification.PurposeExternalEmailBind, UserID: ctx.UserID, Account: ctx.UserAccount, Email: input.Email, ResourceID: item.ID}, remoteIP(req)) + challenge, err := runtime.verification.Issue(verification.IssueRequest{Purpose: verification.PurposeExternalEmailBind, UserID: ctx.UserID, Account: ctx.UserAccount, Email: input.Email, ResourceID: item.ID}, remoteIP(req)) if err != nil { - _ = securityRuntime.external.AbandonPending(ctx.UserID, item.ID) + _ = runtime.external.AbandonPending(ctx.UserID, item.ID) writeSecurityError(w, response.ParamsError, "外部邮箱绑定失败") return } @@ -683,7 +716,8 @@ func currentUserPrimaryEmail(userID int, email string) (bool, error) { } func VerifyExternalEmail(ctx *context.Context, w http.ResponseWriter, req *http.Request) { - if ctx == nil || ctx.UserID <= 0 || securityRuntime.external == nil || securityRuntime.verification == nil { + runtime := currentSecurityRuntime() + if ctx == nil || ctx.UserID <= 0 || runtime.external == nil || runtime.verification == nil { writeSecurityError(w, response.NeedLogin, "请先登录") return } @@ -692,11 +726,11 @@ func VerifyExternalEmail(ctx *context.Context, w http.ResponseWriter, req *http. writeSecurityError(w, response.ParamsError, "验证失败") return } - if err := securityRuntime.verification.VerifyBound(input.ID, ctx.UserAccount, input.Email, input.Code, verification.PurposeExternalEmailBind, input.BindingID, remoteIP(req)); err != nil { + if err := runtime.verification.VerifyBound(input.ID, ctx.UserAccount, input.Email, input.Code, verification.PurposeExternalEmailBind, input.BindingID, remoteIP(req)); err != nil { writeSecurityError(w, response.ParamsError, "验证失败") return } - if err := securityRuntime.external.Verify(ctx.UserID, input.BindingID); err != nil { + if err := runtime.external.Verify(ctx.UserID, input.BindingID); err != nil { writeSecurityError(w, response.ParamsError, "验证失败") return } @@ -704,7 +738,8 @@ func VerifyExternalEmail(ctx *context.Context, w http.ResponseWriter, req *http. } func DisableExternalEmail(ctx *context.Context, w http.ResponseWriter, req *http.Request) { - if ctx == nil || ctx.UserID <= 0 || securityRuntime.external == nil { + runtime := currentSecurityRuntime() + if ctx == nil || ctx.UserID <= 0 || runtime.external == nil { writeSecurityError(w, response.NeedLogin, "请先登录") return } @@ -713,16 +748,16 @@ func DisableExternalEmail(ctx *context.Context, w http.ResponseWriter, req *http writeSecurityError(w, response.ParamsError, "请求无效") return } - if err := securityRuntime.external.Disable(ctx.UserID, input.ID); err != nil { + if err := runtime.external.Disable(ctx.UserID, input.ID); err != nil { writeSecurityError(w, response.ParamsError, "外部邮箱停用失败") return } - if securityRuntime.verification != nil && securityRuntime.verification.Store != nil { - securityRuntime.verification.Store.InvalidateResource(input.ID, verification.PurposeExternalEmailBind) - securityRuntime.verification.Store.InvalidateAccount(ctx.UserAccount) + if runtime.verification != nil && runtime.verification.Store != nil { + runtime.verification.Store.InvalidateResource(input.ID, verification.PurposeExternalEmailBind) + runtime.verification.Store.InvalidateAccount(ctx.UserAccount) } - if securityRuntime.recovery != nil { - securityRuntime.recovery.InvalidateUser(ctx.UserID) + if runtime.recovery != nil { + runtime.recovery.InvalidateUser(ctx.UserID) } loginVerificationState.Lock() for id, item := range loginVerificationState.items { diff --git a/backend/controllers/user.go b/backend/controllers/user.go index 854da96f..9dbb25f7 100644 --- a/backend/controllers/user.go +++ b/backend/controllers/user.go @@ -215,7 +215,10 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) { return } } - roleChanged := false + originalDisabled := user.Disabled + originalRole := user.EffectiveRole() + originalSessionVersion := user.SessionVersion + originalSecurityVersion := user.SecurityVersion if reqData.Role != "" && access.Role(reqData.Role) != targetRole { newRole := access.Role(reqData.Role) if !newRole.Valid() { @@ -237,7 +240,6 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) { return } user.ApplyRole(newRole) - roleChanged = true } if user.IsAdmin == 1 && reqData.Disabled != 0 { response.NewErrorResponse(response.ParamsError, "\u4e0d\u5141\u8bb8\u7981\u7528\u7ba1\u7406\u5458\u8d26\u6237", "").FPrint(w) @@ -247,10 +249,6 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) { response.NewErrorResponse(response.ParamsError, "\u8bf7\u901a\u8fc7\u5b89\u5168\u8bbe\u7f6e\u4fee\u6539\u5f53\u524d\u8d26\u6237\u5bc6\u7801", "").FPrint(w) return } - originalDisabled := user.Disabled - originalRole := user.EffectiveRole() - originalSessionVersion := user.SessionVersion - originalSecurityVersion := user.SecurityVersion if reqData.Username != "" { username, validationErr := validation.NormalizeDisplayName(reqData.Username) if validationErr != nil { @@ -308,7 +306,7 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) { if reqData.Password != "" || user.Disabled != originalDisabled || user.EffectiveRole() != originalRole { updateColumns = append(updateColumns, "session_version", "security_version") } - if num, err := tx.ID(user.ID).Where("session_version = ?", originalSessionVersion).Cols(updateColumns...).Update(&user); err != nil { + if num, err := tx.ID(user.ID).Where("session_version = ? AND security_version = ?", originalSessionVersion, originalSecurityVersion).Cols(updateColumns...).Update(&user); err != nil { log.WithContext(ctx).WithError(err).Error("update user failed") response.NewErrorResponse(response.ServerError, "更新用户失败", "").FPrint(w) return @@ -329,9 +327,8 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) { return } committed = true - if reqData.Password != "" { + if user.SessionVersion != originalSessionVersion { InvalidateSecurityRuntimeUser(user.ID, user.Account) } - _ = roleChanged response.NewSuccessResponse(user).FPrint(w) } diff --git a/backend/controllers/webauthn.go b/backend/controllers/webauthn.go index 8ebe5fa2..b484eb54 100644 --- a/backend/controllers/webauthn.go +++ b/backend/controllers/webauthn.go @@ -60,7 +60,7 @@ var ( ) func webAuthnRuntime() *webauthnservice.Service { - return securityRuntime.webauthn + return currentSecurityRuntime().webauthn } // BeginWebAuthnRegistration 开始当前用户的安全密钥注册流程。 @@ -218,7 +218,8 @@ func BeginWebAuthnLogin(ctx *context.Context, w http.ResponseWriter, req *http.R // FinishWebAuthnLogin 完成安全密钥登录并创建普通业务会话。 func FinishWebAuthnLogin(ctx *context.Context, w http.ResponseWriter, req *http.Request) { - service := webAuthnRuntime() + runtime := currentSecurityRuntime() + service := runtime.webauthn if service == nil { writeSecurityError(w, response.ParamsError, "\u5b89\u5168\u5bc6\u94a5\u767b\u5f55\u4e0d\u53ef\u7528") return @@ -259,8 +260,8 @@ func FinishWebAuthnLogin(ctx *context.Context, w http.ResponseWriter, req *http. writeSecurityError(w, response.ParamsError, "登录验证已失效,请重新登录") return } - if securityRuntime.verification != nil && securityRuntime.verification.Store != nil { - securityRuntime.verification.Store.InvalidateChallenge(input.ChallengeID) + if runtime.verification != nil && runtime.verification.Store != nil { + runtime.verification.Store.InvalidateChallenge(input.ChallengeID) } consumePendingLoginVerification(input.ChallengeID, item) completeLoginSession(ctx, w, req, &user) @@ -270,7 +271,7 @@ func BeginWebAuthnRecovery(ctx *context.Context, w http.ResponseWriter, req *htt if rejectUnavailableRecovery(w) { return } - service := securityRuntime.recovery + service := currentSecurityRuntime().recovery if service == nil { writeSecurityError(w, response.ParamsError, "\u5b89\u5168\u5bc6\u94a5\u6062\u590d\u4e0d\u53ef\u7528") return @@ -304,7 +305,7 @@ func VerifyWebAuthnRecovery(ctx *context.Context, w http.ResponseWriter, req *ht if rejectUnavailableRecovery(w) { return } - service := securityRuntime.recovery + service := currentSecurityRuntime().recovery if service == nil { writeSecurityError(w, response.ParamsError, "\u5b89\u5168\u5bc6\u94a5\u6062\u590d\u4e0d\u53ef\u7528") return diff --git a/backend/dto/parsemail/email.go b/backend/dto/parsemail/email.go index 22733e57..c31ca5b0 100644 --- a/backend/dto/parsemail/email.go +++ b/backend/dto/parsemail/email.go @@ -381,13 +381,20 @@ func NewEmailFromReader(to []string, r io.Reader, size int) (*Email, error) { smtpTo := buildUsers(to) ret.To = buildUsers(m.Header.Values("To")) + ret.Cc = buildUsers(m.Header.Values("Cc")) ret.Bcc = []*User{} for _, user := range smtpTo { in := false for _, u := range ret.To { - if u.EmailAddress == user.EmailAddress { + if strings.EqualFold(u.EmailAddress, user.EmailAddress) { + in = true + break + } + } + for _, u := range ret.Cc { + if strings.EqualFold(u.EmailAddress, user.EmailAddress) { in = true break } @@ -398,7 +405,6 @@ func NewEmailFromReader(to []string, r io.Reader, size int) (*Email, error) { } - ret.Cc = buildUsers(m.Header.Values("Cc")) ret.ReplyTo = buildUsers(m.Header.Values("Reply-To")) ret.Sender = buildUser(m.Header.Get("Sender")) if ret.Sender == nil || strings.TrimSpace(ret.Sender.EmailAddress) == "" { diff --git a/backend/dto/parsemail/imap.go b/backend/dto/parsemail/imap.go new file mode 100644 index 00000000..d39868aa --- /dev/null +++ b/backend/dto/parsemail/imap.go @@ -0,0 +1,131 @@ +package parsemail + +import ( + "bytes" + "crypto/sha256" + "encoding/json" + "errors" + "fmt" + "time" + + "git.pcux.cn/xuans/Smail/config" + "git.pcux.cn/xuans/Smail/utils/context" + "github.com/emersion/go-message" + "github.com/emersion/go-message/mail" +) + +// BuildIMAPBytes 为同一封存储邮件生成稳定的 MIME,确保分段读取与大小、结构一致。 +// 收件视图的密送信息由调用方过滤;此方法只用于 IMAP,不能用于 SMTP 投递。 +func (e *Email) BuildIMAPBytes(_ *context.Context) ([]byte, error) { + if e == nil || e.From == nil || e.From.EmailAddress == "" { + return nil, errors.New("invalid sender") + } + encoded, err := json.Marshal(e) + if err != nil { + return nil, err + } + digest := sha256.Sum256(encoded) + boundary := fmt.Sprintf("smail_%x", digest[:16]) + var h mail.Header + date, err := time.Parse(time.RFC3339, e.Date) + if err != nil { + date, err = time.ParseInLocation(time.DateTime, e.Date, time.Local) + } + if err != nil { + // 历史异常日期不能使用当前时间,否则重复 FETCH 的字节内容会变化。 + date = time.Unix(0, 0).UTC() + } + h.SetDate(date) + messageID := e.MsgID + if messageID == "" { + cfg := config.CurrentConfig() + if cfg == nil || cfg.Domain == "" { + return nil, errors.New("message id domain is not configured") + } + messageID = fmt.Sprintf("%d@%s", e.MessageId, cfg.Domain) + } + h.SetMessageID(messageID) + h.SetSubject(e.Subject) + sender := e.Sender + if sender == nil { + sender = e.From + } + for _, field := range []struct { + name string + users []*User + }{ + {"From", []*User{e.From}}, {"Sender", []*User{sender}}, + {"To", e.To}, {"Cc", e.Cc}, {"Bcc", e.Bcc}, {"Reply-To", e.ReplyTo}, + } { + if len(field.users) == 0 { + continue + } + addresses := make([]*mail.Address, 0, len(field.users)) + for _, user := range field.users { + if user == nil || user.EmailAddress == "" { + return nil, errors.New("invalid recipient") + } + addresses = append(addresses, &mail.Address{Name: user.Name, Address: user.EmailAddress}) + } + h.SetAddressList(field.name, addresses) + } + h.SetContentType("multipart/mixed", map[string]string{"boundary": boundary + "_mixed"}) + var output bytes.Buffer + writer, err := message.CreateWriter(&output, h.Header) + if err != nil { + return nil, err + } + var alternative message.Header + alternative.SetContentType("multipart/alternative", map[string]string{"boundary": boundary + "_alternative"}) + inline, err := writer.CreatePart(alternative) + if err != nil { + return nil, err + } + writePart := func(parent *message.Writer, header message.Header, content []byte) error { + header.Set("Content-Transfer-Encoding", "base64") + part, err := parent.CreatePart(header) + if err != nil { + return err + } + return writeMessagePart(part, content) + } + if len(e.Text) > 0 || len(e.HTML) == 0 { + var header message.Header + header.SetContentType("text/plain", map[string]string{"charset": "utf-8"}) + if err := writePart(inline, header, e.Text); err != nil { + return nil, err + } + } + if len(e.HTML) > 0 { + var header message.Header + header.SetContentType("text/html", map[string]string{"charset": "utf-8"}) + if err := writePart(inline, header, e.HTML); err != nil { + return nil, err + } + } + if err := inline.Close(); err != nil { + return nil, err + } + for _, attachment := range e.Attachments { + if attachment == nil { + return nil, errors.New("nil attachment") + } + var header mail.AttachmentHeader + contentType := attachment.ContentType + if contentType == "" { + contentType = "application/octet-stream" + } + header.Set("Content-Type", contentType) + header.SetFilename(attachment.Filename) + if attachment.ContentID != "" { + header.Set("Content-ID", fmt.Sprintf("<%s>", attachment.ContentID)) + } + if err := writePart(writer, header.Header, attachment.Content); err != nil { + return nil, err + } + } + if err := writer.Close(); err != nil { + return nil, err + } + return output.Bytes(), nil +} diff --git a/backend/hooks/framework/hook_wire.go b/backend/hooks/framework/hook_wire.go new file mode 100644 index 00000000..71a7d251 --- /dev/null +++ b/backend/hooks/framework/hook_wire.go @@ -0,0 +1,50 @@ +package framework + +import ( + "encoding/json" + "errors" +) + +// hookWire 保留插件字段名称,但以字符串或 null 传输错误,避免 error 接口丢失内容。 +type hookAlias HookDTO + +type hookWire struct { + *hookAlias + ErrMap map[string]*string +} + +func (dto HookDTO) MarshalJSON() ([]byte, error) { + wire := hookWire{hookAlias: (*hookAlias)(&dto)} + if dto.ErrMap != nil { + wire.ErrMap = make(map[string]*string, len(dto.ErrMap)) + for domain, err := range dto.ErrMap { + if err == nil { + wire.ErrMap[domain] = nil + } else { + text := err.Error() + wire.ErrMap[domain] = &text + } + } + } + return json.Marshal(wire) +} + +func (dto *HookDTO) UnmarshalJSON(data []byte) error { + var decoded HookDTO + wire := hookWire{hookAlias: (*hookAlias)(&decoded)} + if err := json.Unmarshal(data, &wire); err != nil { + return err + } + if wire.ErrMap != nil { + decoded.ErrMap = make(map[string]error, len(wire.ErrMap)) + for domain, text := range wire.ErrMap { + if text == nil { + decoded.ErrMap[domain] = nil + } else { + decoded.ErrMap[domain] = errors.New(*text) + } + } + } + *dto = decoded + return nil +} diff --git a/backend/hooks/wechat_push/wechat_push.go b/backend/hooks/wechat_push/wechat_push.go index f8b02ae9..53108a42 100644 --- a/backend/hooks/wechat_push/wechat_push.go +++ b/backend/hooks/wechat_push/wechat_push.go @@ -273,7 +273,26 @@ func (w *WeChatPushHook) sendUserMsg(ctx *context.Context, userId string, conten log.WithContext(ctx).Error("wechat push request was rejected") return } - _, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, maxWeChatResponseBytes)) + body, err := io.ReadAll(io.LimitReader(resp.Body, maxWeChatResponseBytes+1)) + var result struct { + ErrorCode *int `json:"errcode"` + } + if err != nil || len(body) > maxWeChatResponseBytes || json.Unmarshal(body, &result) != nil || result.ErrorCode == nil { + log.WithContext(ctx).Error("微信推送响应无效或超过大小限制") + return + } + if *result.ErrorCode != 0 { + // 只记录业务码,不能输出可能含凭据或收件人信息的响应正文。 + log.WithContext(ctx).WithField("errcode", *result.ErrorCode).Error("微信推送业务请求失败") + switch *result.ErrorCode { + case 40001, 40014, 42001: + w.tokenMu.Lock() + if w.token == token { + w.token, w.tokenExpires = "", 0 + } + w.tokenMu.Unlock() + } + } } func buildWeChatMessage(email *parsemail.Email) string { diff --git a/backend/internal/gopop/session.go b/backend/internal/gopop/session.go index b88ccce1..139f13be 100644 --- a/backend/internal/gopop/session.go +++ b/backend/internal/gopop/session.go @@ -15,13 +15,14 @@ const ( ) type Session struct { - Status Status - User string - DeleteIds []int64 - MessageIDs []int64 - MessageMap map[int64]int64 - Ctx context.Context - Conn net.Conn - InTls bool - AliveTime time.Time + Status Status + User string + DeleteIds []int64 + MessageIDs []int64 + MessageMap map[int64]int64 + MessageSizes map[int64]int + Ctx context.Context + Conn net.Conn + InTls bool + AliveTime time.Time } diff --git a/backend/listen/http_server/http_server.go b/backend/listen/http_server/http_server.go index 1fb27455..adf925bf 100644 --- a/backend/listen/http_server/http_server.go +++ b/backend/listen/http_server/http_server.go @@ -146,6 +146,15 @@ func configRoute(ctx *context.Context, w http.ResponseWriter, r *http.Request) { controllers.EnsureConfigRequestID(w, r) path := strings.TrimSuffix(r.URL.Path, "/") switch { + case path == "/api/v1/config/pending": + switch r.Method { + case http.MethodGet: + controllers.GetPendingConfig(ctx, w, r) + case http.MethodPost: + controllers.ManagePendingConfig(ctx, w, r) + default: + allowRouteMethod(w, r, http.MethodGet, http.MethodPost) + } case path == "/api/v1/config/status": if !allowRouteMethod(w, r, http.MethodGet) { return diff --git a/backend/listen/imap_server/server.go b/backend/listen/imap_server/server.go index 6c112775..07795472 100644 --- a/backend/listen/imap_server/server.go +++ b/backend/listen/imap_server/server.go @@ -56,6 +56,8 @@ type serverSession struct { connectTime time.Time deleteUidList []int remoteAddress string + mailboxMu sync.Mutex + mailboxUIDs []int } // NewSession creates a new IMAP session. @@ -277,7 +279,10 @@ func appendTooBigError() error { } func (s *serverSession) Unselect() error { + s.mailboxMu.Lock() + defer s.mailboxMu.Unlock() s.currentMailbox = "" s.readOnly = false + s.mailboxUIDs = nil return nil } diff --git a/backend/listen/imap_server/session_copy.go b/backend/listen/imap_server/session_copy.go index b3946fcb..d6b9b20d 100644 --- a/backend/listen/imap_server/session_copy.go +++ b/backend/listen/imap_server/session_copy.go @@ -6,7 +6,6 @@ import ( "git.pcux.cn/xuans/Smail/dto/response" "git.pcux.cn/xuans/Smail/models" "git.pcux.cn/xuans/Smail/services/group" - "git.pcux.cn/xuans/Smail/services/list" "git.pcux.cn/xuans/Smail/utils/context" "github.com/emersion/go-imap/v2" log "github.com/sirupsen/logrus" @@ -14,35 +13,9 @@ import ( ) func (s *serverSession) Copy(numSet imap.NumSet, dest string) (*imap.CopyData, error) { - var emailList []*response.EmailResponseData - - switch numSet.(type) { - case imap.SeqSet: - seqSet := numSet.(imap.SeqSet) - for _, seq := range seqSet { - res, err := list.GetEmailListByGroup(s.ctx, s.currentMailbox, list.ImapListReq{ - Star: cast.ToInt(seq.Start), - End: cast.ToInt(seq.Stop), - }, false) - if err != nil { - log.WithContext(s.ctx).WithError(err).Error("query IMAP messages for copy failed") - return nil, copyFailedError() - } - emailList = append(emailList, res...) - } - case imap.UIDSet: - uidSet := numSet.(imap.UIDSet) - for _, uid := range uidSet { - res, err := list.GetEmailListByGroup(s.ctx, s.currentMailbox, list.ImapListReq{ - Star: cast.ToInt(uint32(uid.Start)), - End: cast.ToInt(uint32(uid.Stop)), - }, true) - if err != nil { - log.WithContext(s.ctx).WithError(err).Error("query IMAP messages for UID copy failed") - return nil, copyFailedError() - } - emailList = append(emailList, res...) - } + emailList, err := s.selectedMessages(numSet) + if err != nil { + return nil, copyFailedError() } if len(emailList) == 0 { @@ -52,7 +25,6 @@ func (s *serverSession) Copy(numSet imap.NumSet, dest string) (*imap.CopyData, e var ( uidValidity int destUIDs []int - err error ) if group.IsDefaultBox(dest) { uidValidity, destUIDs, err = copy2defaultbox(s.ctx, emailList, dest) diff --git a/backend/listen/imap_server/session_expunge.go b/backend/listen/imap_server/session_expunge.go index f2b5688c..69eec2d3 100644 --- a/backend/listen/imap_server/session_expunge.go +++ b/backend/listen/imap_server/session_expunge.go @@ -5,6 +5,7 @@ import ( "log/slog" "sort" + "git.pcux.cn/xuans/Smail/dto/response" "git.pcux.cn/xuans/Smail/services/del_email" "git.pcux.cn/xuans/Smail/services/list" "github.com/emersion/go-imap/v2" @@ -23,19 +24,34 @@ func (s *serverSession) Expunge(w *imapserver.ExpungeWriter, uids *imap.UIDSet) if s == nil || s.ctx == nil || s.ctx.UserID <= 0 { return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Expunge failed"} } + // CLOSE 也会调用这里;只读会话正常关闭,但不能真正删除邮件。 + if s.readOnly { + return nil + } - maxUID := 0 - var err error - if uids != nil && uids.Dynamic() { - maxUID, err = list.GetMaxUIDByMailbox(s.ctx, s.currentMailbox) - if err != nil { - slog.Error("query IMAP maximum UID failed", "error", err) - return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Expunge failed"} + snapshot, err := s.selectedUIDs() + if err != nil { + return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Expunge failed"} + } + // 只匹配会话内真实存在的 UID,不展开巨大或稀疏区间。 + var maximum uint32 + for _, uid := range snapshot { + maximum = max(maximum, uint32(uid)) + } + requestedUIDs := make([]int, 0, len(snapshot)) + for _, uid := range snapshot { + matched := uids == nil + if uids != nil { + for _, span := range *uids { + matched = matched || numberInRange(uint32(uid), uint32(span.Start), uint32(span.Stop), maximum) + } + } + if matched { + requestedUIDs = append(requestedUIDs, uid) } } - requestedUIDs, err := expandIMAPUIDSet(uids, maxUID) - if err != nil { - return &imap.Error{Type: imap.StatusResponseTypeBad, Text: err.Error()} + if len(requestedUIDs) == 0 { + return nil } targets, err := markedDeletedMessages(s, requestedUIDs) if err != nil { @@ -64,6 +80,14 @@ func (s *serverSession) Expunge(w *imapserver.ExpungeWriter, uids *imap.UIDSet) return err } } + s.mailboxMu.Lock() + for index, uid := range s.mailboxUIDs { + if uid == target.uid { + s.mailboxUIDs = append(s.mailboxUIDs[:index], s.mailboxUIDs[index+1:]...) + break + } + } + s.mailboxMu.Unlock() } return nil } @@ -132,16 +156,29 @@ func expandIMAPUIDSet(uids *imap.UIDSet, maxUID int) ([]int, error) { } func markedDeletedMessages(s *serverSession, requested []int) ([]imapExpungeTarget, error) { - relations, err := list.GetUEListByUID(s.ctx, s.currentMailbox, 0, 0, requested) + uids, err := s.selectedUIDs() if err != nil { return nil, err } + sequences := make(map[int]int, len(uids)) + for index, uid := range uids { + sequences[uid] = index + 1 + } + var relations []*response.UserEmailUIDData + for start := 0; start < len(requested); start += imapUIDQueryBatchSize { + end := min(start+imapUIDQueryBatchSize, len(requested)) + batch, err := list.GetUEListByUID(s.ctx, s.currentMailbox, 0, 0, requested[start:end]) + if err != nil { + return nil, err + } + relations = append(relations, batch...) + } targets := make([]imapExpungeTarget, 0, len(relations)) for _, relation := range relations { - if relation == nil || relation.ID <= 0 || relation.IsDeleted != 1 { + if relation == nil || relation.ID <= 0 || relation.IsDeleted != 1 || sequences[relation.ID] == 0 { continue } - sequence, err := imapUint32(relation.SerialNumber) + sequence, err := imapUint32(sequences[relation.ID]) if err != nil || sequence == 0 { return nil, fmt.Errorf("invalid message sequence number") } diff --git a/backend/listen/imap_server/session_fetch.go b/backend/listen/imap_server/session_fetch.go index 6b6331d0..f00e705c 100644 --- a/backend/listen/imap_server/session_fetch.go +++ b/backend/listen/imap_server/session_fetch.go @@ -5,14 +5,11 @@ import ( "errors" "fmt" "io" - "mime" "strings" - "time" "git.pcux.cn/xuans/Smail/config" "git.pcux.cn/xuans/Smail/dto/parsemail" "git.pcux.cn/xuans/Smail/dto/response" - "git.pcux.cn/xuans/Smail/services/list" "git.pcux.cn/xuans/Smail/utils/context" "github.com/emersion/go-imap/v2" "github.com/emersion/go-imap/v2/imapserver" @@ -124,61 +121,11 @@ func messageIDForEmail(email *response.EmailResponseData) string { } func (s *serverSession) Fetch(w *imapserver.FetchWriter, numSet imap.NumSet, options *imap.FetchOptions) error { - switch numSet.(type) { - case imap.SeqSet: - seqSet := numSet.(imap.SeqSet) - for _, seq := range seqSet { - emailList, err := list.GetEmailListByGroup(s.ctx, s.currentMailbox, list.ImapListReq{ - Star: cast.ToInt(seq.Start), - End: cast.ToInt(seq.Stop), - }, false) - if err != nil { - log.WithContext(s.ctx).WithError(err).Error("query IMAP messages for fetch failed") - return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Fetch failed"} - } - if err := write(s.ctx, w, emailList, options); err != nil { - return err - } - } - - case imap.UIDSet: - uidSet := numSet.(imap.UIDSet) - maxUID := 0 - if uidSet.Dynamic() { - var err error - maxUID, err = list.GetMaxUIDByMailbox(s.ctx, s.currentMailbox) - if err != nil { - log.WithContext(s.ctx).WithError(err).Error("query IMAP maximum UID for fetch failed") - return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Fetch failed"} - } - } - for _, uid := range uidSet { - start := cast.ToInt(uint32(uid.Start)) - end := cast.ToInt(uint32(uid.Stop)) - if uid.Stop == 0 { - if uid.Start == 0 { - start = maxUID - end = maxUID - } else if maxUID == 0 || uid.Start > imap.UID(maxUID) { - continue - } else { - end = maxUID - } - } - emailList, err := list.GetEmailListByGroup(s.ctx, s.currentMailbox, list.ImapListReq{ - Star: start, - End: end, - }, true) - if err != nil { - log.WithContext(s.ctx).WithError(err).Error("query IMAP messages for UID fetch failed") - return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Fetch failed"} - } - if err := write(s.ctx, w, emailList, options); err != nil { - return err - } - } + rows, err := s.selectedMessages(numSet) + if err != nil { + return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Fetch failed"} } - return nil + return write(s.ctx, w, rows, options, s.readOnly) } // 纯文本 text/plain @@ -270,7 +217,7 @@ func bsMixedWithAttachments(alt *imap.BodyStructureMultiPart, extend bool, atts } } -func write(ctx *context.Context, w *imapserver.FetchWriter, emailList []*response.EmailResponseData, options *imap.FetchOptions) error { +func write(ctx *context.Context, w *imapserver.FetchWriter, emailList []*response.EmailResponseData, options *imap.FetchOptions, readOnly ...bool) error { for _, email := range emailList { sequence, err := imapUint32(email.SerialNumber) if err != nil || sequence == 0 { @@ -289,27 +236,19 @@ func write(ctx *context.Context, w *imapserver.FetchWriter, emailList []*respons return err } } - var htmlSize uint32 - var textSize uint32 - attachmentSizes := make([]uint32, len(traEmail.Attachments)) - if options.BodyStructure != nil { - if len(traEmail.HTML) > 0 { - htmlSize, err = imapUint32(len(traEmail.HTML)) - if err != nil { - return err - } + var emailContent []byte + if options.BodyStructure != nil || options.RFC822Size || len(options.BodySection) > 0 { + emailContent, err = traEmail.BuildIMAPBytes(ctx) + if err != nil { + return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Fetch failed"} } - if len(traEmail.Text) > 0 { - textSize, err = imapUint32(len(traEmail.Text)) - if err != nil { - return err - } - } - for index, attachment := range traEmail.Attachments { - attachmentSizes[index], err = imapUint32(len(attachment.Content)) - if err != nil { - return err + } + for _, section := range options.BodySection { + if !section.Peek && !(len(readOnly) > 0 && readOnly[0]) && email.IsRead == 0 { + if err := updateReadByUID(ctx.UserID, email.UeId, true); err != nil { + return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Fetch failed"} } + email.IsRead = 1 } } @@ -323,31 +262,9 @@ func write(ctx *context.Context, w *imapserver.FetchWriter, emailList []*respons writer.WriteEnvelope(env) } if options.BodyStructure != nil { - var html, text *imap.BodyStructureSinglePart - if len(traEmail.HTML) > 0 { - html = bsTextHTML(htmlSize, int64(bytes.Count(traEmail.HTML, []byte("\n"))+1)) - } - - if len(traEmail.Text) > 0 { - text = bsTextPlain(textSize, int64(bytes.Count(traEmail.Text, []byte("\n"))+1)) - } - - alt := bsAlternative(text, html) - - var attrs []imap.BodyStructure - for index, attachment := range traEmail.Attachments { - attrs = append(attrs, bsAttachment(attachment.Filename, attachment.ContentType, attachmentSizes[index], "base64")) - } - bs := bsMixedWithAttachments(alt, options.BodyStructure.Extended, attrs...) // 最终的 BodyStructure(接口值) - - writer.WriteBodyStructure(bs) + writer.WriteBodyStructure(imapserver.ExtractBodyStructure(bytes.NewReader(emailContent))) } if options.RFC822Size { - emailContent, err := traEmail.BuildBytes(ctx, false) - if err != nil { - log.WithContext(ctx).WithError(err).Error("build IMAP message size failed") - return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Fetch failed"} - } writer.WriteRFC822Size(cast.ToInt64(len(emailContent))) } if options.Flags { @@ -364,122 +281,11 @@ func write(ctx *context.Context, w *imapserver.FetchWriter, emailList []*respons writer.WriteInternalDate(email.CreateTime) } for _, section := range options.BodySection { - if !section.Peek { - if err := updateReadByUID(ctx.UserID, email.UeId, true); err != nil { - log.WithContext(ctx).WithError(err).Error("mark IMAP message as read failed") - return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Fetch failed"} - } - } - emailContent, err := traEmail.BuildBytes(ctx, false) - if err != nil { - log.WithContext(ctx).WithError(err).Error("build IMAP message body failed") + content := extractBodySection(emailContent, section) + bodyWriter := writer.WriteBodySection(section, int64(len(content))) + if err := writeBodySectionContent(bodyWriter, content); err != nil { return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Fetch failed"} } - - // 优先检查是否请求 HEADER.FIELDS - if len(section.HeaderFields) > 0 || section.Specifier == imap.PartSpecifierHeader { - var b bytes.Buffer - fields := section.HeaderFields - - if fields == nil || len(fields) == 0 { - // 没有指定字段,返回所有常见头部 - fields = []string{ - "date", "subject", "from", "to", "cc", "message-id", "content-type", - } - } - - for _, field := range fields { - fieldLower := strings.ToLower(field) - switch fieldLower { - case "date": - fmt.Fprintf(&b, "Date: %s\r\n", email.CreateTime.Format(time.RFC1123Z)) - case "subject": - fmt.Fprintf(&b, "Subject: %s\r\n", mime.QEncoding.Encode("utf-8", email.Subject)) - case "from": - if email.FromName != "" { - fmt.Fprintf(&b, "From: %s <%s>\r\n", mime.QEncoding.Encode("utf-8", email.FromName), email.FromAddress) - } else { - fmt.Fprintf(&b, "From: %s\r\n", email.FromAddress) - } - case "sender": - if email.FromName != "" { - fmt.Fprintf(&b, "Sender: %s <%s>\r\n", mime.QEncoding.Encode("utf-8", email.FromName), email.FromAddress) - } else { - fmt.Fprintf(&b, "Sender: %s\r\n", email.FromAddress) - } - case "reply-to": - if len(traEmail.ReplyTo) > 0 && traEmail.ReplyTo[0].EmailAddress != "" { - if traEmail.ReplyTo[0].Name != "" { - fmt.Fprintf(&b, "Reply-To: %s <%s>\r\n", mime.QEncoding.Encode("utf-8", traEmail.ReplyTo[0].Name), traEmail.ReplyTo[0].EmailAddress) - } else { - fmt.Fprintf(&b, "Reply-To: %s\r\n", traEmail.ReplyTo[0].EmailAddress) - } - } - case "to": - toStr := traEmail.BuildTo2String() - if toStr != "" { - fmt.Fprintf(&b, "To: %s\r\n", toStr) - } - case "cc": - if len(traEmail.Cc) > 0 { - fmt.Fprintf(&b, "Cc: %s\r\n", traEmail.BuildCc2String()) - } - case "bcc": - if len(traEmail.Bcc) > 0 { - fmt.Fprintf(&b, "Bcc: %s\r\n", traEmail.BuildBcc2String()) - } - case "message-id": - messageID := messageIDForEmail(email) - if messageID == "" { - return nil - } - fmt.Fprintf(&b, "Message-ID: %s\r\n", messageID) - case "content-type": - args := strings.SplitN(string(emailContent), "\r\n", 3) - if len(args) >= 2 { - fmt.Fprintf(&b, "%s%s\r\n", args[0], args[1]) - } - case "references", "in-reply-to", "thread-topic", "thread-index", "x-priority", "x-mailer", "x-android-message-id": - // 这些头部我们目前不存储,跳过 - default: - // 其他未知头部,忽略 - } - } - - // 添加结束空行 - b.WriteString("\r\n") - - bodyWriter := writer.WriteBodySection(section, cast.ToInt64(b.Len())) - if err := writeBodySectionContent(bodyWriter, b.Bytes()); err != nil { - log.WithContext(ctx).WithError(err).Error("write IMAP header section failed") - return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Fetch failed"} - } - } else if section.Specifier == imap.PartSpecifierNone || section.Specifier == imap.PartSpecifierText { - if len(section.Part) >= 1 { - // 获取指定 part 的内容 - partContent := traEmail.BuildPart(ctx, section.Part) - if partContent != nil { - bodyWriter := writer.WriteBodySection(section, cast.ToInt64(len(partContent))) - if err := writeBodySectionContent(bodyWriter, partContent); err != nil { - log.WithContext(ctx).WithError(err).Error("write IMAP message part failed") - return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Fetch failed"} - } - } else { - // Part 不存在,返回空 - bodyWriter := writer.WriteBodySection(section, 0) - if err := writeBodySectionContent(bodyWriter, nil); err != nil { - log.WithContext(ctx).WithError(err).Error("close empty IMAP message part failed") - return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Fetch failed"} - } - } - } else { - bodyWriter := writer.WriteBodySection(section, cast.ToInt64(len(emailContent))) - if err := writeBodySectionContent(bodyWriter, emailContent); err != nil { - log.WithContext(ctx).WithError(err).Error("write IMAP message body failed") - return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Fetch failed"} - } - } - } } if err := writer.Close(); err != nil { return err @@ -488,6 +294,21 @@ func write(ctx *context.Context, w *imapserver.FetchWriter, emailList []*respons return nil } +func extractBodySection(content []byte, section *imap.FetchItemBodySection) []byte { + item := *section + item.Partial = nil + data := imapserver.ExtractBodySection(bytes.NewReader(content), &item) + if partial := section.Partial; partial != nil { + if partial.Offset < 0 || partial.Size < 0 || partial.Offset >= int64(len(data)) { + return nil + } + // 先约束剩余长度,不对客户端提供的两个大整数直接求和。 + size := min(partial.Size, int64(len(data))-partial.Offset) + data = data[partial.Offset : partial.Offset+size] + } + return data +} + func writeBodySectionContent(bodyWriter io.WriteCloser, content []byte) error { written, writeErr := bodyWriter.Write(content) if writeErr == nil && written != len(content) { diff --git a/backend/listen/imap_server/session_idle.go b/backend/listen/imap_server/session_idle.go index 2f3b0417..32df6089 100644 --- a/backend/listen/imap_server/session_idle.go +++ b/backend/listen/imap_server/session_idle.go @@ -8,11 +8,13 @@ import ( log "github.com/sirupsen/logrus" "github.com/spf13/cast" "sync" + "time" ) type idleConnection struct { writer *imapserver.UpdateWriter mailbox string + session *serverSession } type idleConnections struct { @@ -36,13 +38,11 @@ func (s *serverSession) Idle(w *imapserver.UpdateWriter, stop <-chan struct{}) e connects := connectsAny.(*idleConnections) connects.mu.Lock() - connects.writers[logId] = idleConnection{writer: w, mailbox: s.currentMailbox} + connects.writers[logId] = idleConnection{writer: w, mailbox: s.currentMailbox, session: s} connects.mu.Unlock() userConnectsMu.Unlock() - go func() { - <-stop - + defer func() { userConnectsMu.Lock() connects.mu.Lock() delete(connects.writers, logId) @@ -52,8 +52,21 @@ func (s *serverSession) Idle(w *imapserver.UpdateWriter, stop <-chan struct{}) e connects.mu.Unlock() userConnectsMu.Unlock() }() - - return nil + if err := s.Poll(w, true); err != nil { + return err + } + ticker := time.NewTicker(time.Second) + defer ticker.Stop() + for { + select { + case <-stop: + return nil + case <-ticker.C: + if err := s.Poll(w, true); err != nil { + return err + } + } + } } func IdleNotice(ctx *context.Context, userId int, email *models.Email) error { @@ -70,14 +83,17 @@ func IdleNotice(ctx *context.Context, userId int, email *models.Email) error { connects := connectsAny.(*idleConnections) connects.mu.Lock() - idleConnects := make([]idleConnection, 0, len(connects.writers)) - for _, connect := range connects.writers { - idleConnects = append(idleConnects, connect) - } - connects.mu.Unlock() userConnectsMu.Unlock() + defer connects.mu.Unlock() - for _, connect := range idleConnects { + for _, connect := range connects.writers { + if connect.session != nil { + // 同步变化和显式总数在同一锁内只写一次,避免投递时重复通知。 + if err := connect.session.poll(connect.writer, true, true); err != nil { + return err + } + continue + } numMessages, err := idleNumMessages(ctx, userId, connect.mailbox) if err != nil { return err diff --git a/backend/listen/imap_server/session_messages.go b/backend/listen/imap_server/session_messages.go new file mode 100644 index 00000000..7b8822a0 --- /dev/null +++ b/backend/listen/imap_server/session_messages.go @@ -0,0 +1,86 @@ +package imap_server + +import ( + "git.pcux.cn/xuans/Smail/dto/response" + "git.pcux.cn/xuans/Smail/services/list" + "github.com/emersion/go-imap/v2" +) + +// 分批限制同时适配 SQLite 参数数量和底层 UID 列表校验,不限制邮箱总量。 +const imapUIDQueryBatchSize = 500 + +// selectedUIDs 返回客户端已经获知的序号快照,外部删除不能提前改变命令目标。 +func (s *serverSession) selectedUIDs() ([]int, error) { + s.mailboxMu.Lock() + defer s.mailboxMu.Unlock() + if s.mailboxUIDs == nil { + relations, err := list.GetUEListByUID(s.ctx, s.currentMailbox, 0, 0, nil) + if err != nil { + return nil, err + } + s.mailboxUIDs = make([]int, 0, len(relations)) + for _, relation := range relations { + s.mailboxUIDs = append(s.mailboxUIDs, relation.ID) + } + } + return append([]int{}, s.mailboxUIDs...), nil +} + +func numberInRange(value, start, stop, maximum uint32) bool { + if start == 0 { + start = maximum + } + if stop == 0 { + stop = maximum + } + if start > stop { + start, stop = stop, start + } + return value >= start && value <= stop +} + +func (s *serverSession) selectedMessages(numbers imap.NumSet) ([]*response.EmailResponseData, error) { + uids, err := s.selectedUIDs() + if err != nil { + return nil, err + } + var maximum uint32 + for _, uid := range uids { + maximum = max(maximum, uint32(uid)) + } + selected := make([]int, 0) + sequences := make(map[int]int, len(uids)) + for index, uid := range uids { + sequences[uid] = index + 1 + matched := false + switch set := numbers.(type) { + case imap.SeqSet: + for _, span := range set { + matched = matched || numberInRange(uint32(index+1), span.Start, span.Stop, uint32(len(uids))) + } + case imap.UIDSet: + for _, span := range set { + matched = matched || numberInRange(uint32(uid), uint32(span.Start), uint32(span.Stop), maximum) + } + } + if matched { + selected = append(selected, uid) + } + } + if len(selected) == 0 { + return nil, nil + } + rows := make([]*response.EmailResponseData, 0, len(selected)) + for start := 0; start < len(selected); start += imapUIDQueryBatchSize { + end := min(start+imapUIDQueryBatchSize, len(selected)) + batch, err := list.GetEmailListByGroup(s.ctx, s.currentMailbox, list.ImapListReq{UidList: selected[start:end]}, true) + if err != nil { + return nil, err + } + rows = append(rows, batch...) + } + for _, row := range rows { + row.SerialNumber = sequences[row.UeId] + } + return rows, nil +} diff --git a/backend/listen/imap_server/session_move.go b/backend/listen/imap_server/session_move.go index 73c5bee4..ef36744a 100644 --- a/backend/listen/imap_server/session_move.go +++ b/backend/listen/imap_server/session_move.go @@ -1,46 +1,20 @@ package imap_server import ( - "git.pcux.cn/xuans/Smail/dto/response" "git.pcux.cn/xuans/Smail/services/group" - "git.pcux.cn/xuans/Smail/services/list" "git.pcux.cn/xuans/Smail/utils/context" "github.com/emersion/go-imap/v2" "github.com/emersion/go-imap/v2/imapserver" log "github.com/sirupsen/logrus" - "github.com/spf13/cast" ) func (s *serverSession) Move(w *imapserver.MoveWriter, numSet imap.NumSet, dest string) error { - var emailList []*response.EmailResponseData - - switch numSet.(type) { - case imap.SeqSet: - seqSet := numSet.(imap.SeqSet) - for _, seq := range seqSet { - res, err := list.GetEmailListByGroup(s.ctx, s.currentMailbox, list.ImapListReq{ - Star: cast.ToInt(seq.Start), - End: cast.ToInt(seq.Stop), - }, false) - if err != nil { - log.WithContext(s.ctx).WithError(err).Error("query IMAP messages for move failed") - return moveFailedError() - } - emailList = append(emailList, res...) - } - case imap.UIDSet: - uidSet := numSet.(imap.UIDSet) - for _, uid := range uidSet { - res, err := list.GetEmailListByGroup(s.ctx, s.currentMailbox, list.ImapListReq{ - Star: cast.ToInt(uint32(uid.Start)), - End: cast.ToInt(uint32(uid.Stop)), - }, true) - if err != nil { - log.WithContext(s.ctx).WithError(err).Error("query IMAP messages for UID move failed") - return moveFailedError() - } - emailList = append(emailList, res...) - } + if s.readOnly { + return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Mailbox is read-only"} + } + emailList, err := s.selectedMessages(numSet) + if err != nil { + return moveFailedError() } relationIDs := make([]int, 0, len(emailList)) diff --git a/backend/listen/imap_server/session_poll.go b/backend/listen/imap_server/session_poll.go index 2bf66558..a391c572 100644 --- a/backend/listen/imap_server/session_poll.go +++ b/backend/listen/imap_server/session_poll.go @@ -1,27 +1,58 @@ package imap_server import ( - "git.pcux.cn/xuans/Smail/db" - "git.pcux.cn/xuans/Smail/models" + "git.pcux.cn/xuans/Smail/services/list" "github.com/emersion/go-imap/v2" "github.com/emersion/go-imap/v2/imapserver" log "github.com/sirupsen/logrus" - "github.com/spf13/cast" ) func (s *serverSession) Poll(w *imapserver.UpdateWriter, allowExpunge bool) error { - var ue []models.UserEmail - if err := db.Instance.Table("user_email").Where("user_id=? and `create` >=?", s.ctx.UserID, s.connectTime).Find(&ue); err != nil { + return s.poll(w, allowExpunge, false) +} + +func (s *serverSession) poll(w *imapserver.UpdateWriter, allowExpunge, forceExists bool) error { + s.mailboxMu.Lock() + defer s.mailboxMu.Unlock() + if s.currentMailbox == "" { + return nil + } + relations, err := list.GetUEListByUID(s.ctx, s.currentMailbox, 0, 0, nil) + if err != nil { log.WithContext(s.ctx).WithError(err).Error("query IMAP polling updates failed") return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Poll failed"} } - if len(ue) > 0 { - if err := w.WriteNumMessages(cast.ToUint32(len(ue))); err != nil { - log.WithContext(s.ctx).WithError(err).Error("write IMAP polling update failed") + present := make(map[int]bool, len(relations)) + for _, relation := range relations { + present[relation.ID] = true + } + // 不允许 EXPUNGE 的命令后保留旧序号,等到可通知时再同步整个变化。 + for _, uid := range s.mailboxUIDs { + if !present[uid] && !allowExpunge { + return nil + } + } + for index := len(s.mailboxUIDs) - 1; index >= 0; index-- { + if !present[s.mailboxUIDs[index]] { + if err := w.WriteExpunge(uint32(index + 1)); err != nil { + return err + } + s.mailboxUIDs = append(s.mailboxUIDs[:index], s.mailboxUIDs[index+1:]...) + } + } + if forceExists || len(relations) != len(s.mailboxUIDs) { + count, err := imapUint32(len(relations)) + if err != nil { + return err + } + if err := w.WriteNumMessages(count); err != nil { return err } } - + s.mailboxUIDs = s.mailboxUIDs[:0] + for _, relation := range relations { + s.mailboxUIDs = append(s.mailboxUIDs, relation.ID) + } return nil } diff --git a/backend/listen/imap_server/session_search.go b/backend/listen/imap_server/session_search.go index e2366c81..6c541a18 100644 --- a/backend/listen/imap_server/session_search.go +++ b/backend/listen/imap_server/session_search.go @@ -17,7 +17,11 @@ func (s *serverSession) Search(kind imapserver.NumKind, criteria *imap.SearchCri log.WithContext(s.ctx).Debugf("IMAP SEARCH started: kind=%v", kind) // Use the new comprehensive search function - retList, err := list.SearchEmails(s.ctx, s.currentMailbox, criteria) + uids, err := s.selectedUIDs() + if err != nil { + return nil, &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Search failed"} + } + retList, err := list.SearchEmails(s.ctx, s.currentMailbox, criteria, uids) if err != nil { log.WithContext(s.ctx).Errorf("IMAP SEARCH error: %v", err) return nil, &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Search failed"} diff --git a/backend/listen/imap_server/session_select.go b/backend/listen/imap_server/session_select.go index ec03871a..b33c3f63 100644 --- a/backend/listen/imap_server/session_select.go +++ b/backend/listen/imap_server/session_select.go @@ -2,6 +2,7 @@ package imap_server import ( "git.pcux.cn/xuans/Smail/services/group" + "git.pcux.cn/xuans/Smail/services/list" "github.com/emersion/go-imap/v2" log "github.com/sirupsen/logrus" "strings" @@ -22,10 +23,11 @@ func (s *serverSession) Select(mailbox string, options *imap.SelectOptions) (*im log.WithContext(s.ctx).WithError(err).Error("query selected mailbox status failed") return nil, &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Select failed"} } - s.currentMailbox = mailbox - s.readOnly = readOnly - - numMessages, err := imapUint32(data["MESSAGES"]) + relations, err := list.GetUEListByUID(s.ctx, mailbox, 0, 0, nil) + if err != nil { + return nil, &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Select failed"} + } + numMessages, err := imapUint32(len(relations)) if err != nil { return nil, err } @@ -37,6 +39,14 @@ func (s *serverSession) Select(mailbox string, options *imap.SelectOptions) (*im if err != nil { return nil, err } + s.mailboxMu.Lock() + s.currentMailbox = mailbox + s.readOnly = readOnly + s.mailboxUIDs = make([]int, 0, len(relations)) + for _, relation := range relations { + s.mailboxUIDs = append(s.mailboxUIDs, relation.ID) + } + s.mailboxMu.Unlock() ret := &imap.SelectData{ Flags: []imap.Flag{imap.FlagSeen, imap.FlagDeleted}, @@ -45,6 +55,9 @@ func (s *serverSession) Select(mailbox string, options *imap.SelectOptions) (*im UIDNext: uidNext, UIDValidity: uidValidity, } + if readOnly { + ret.PermanentFlags = nil + } return ret, nil diff --git a/backend/listen/imap_server/session_store.go b/backend/listen/imap_server/session_store.go index 2c9888dc..43a06146 100644 --- a/backend/listen/imap_server/session_store.go +++ b/backend/listen/imap_server/session_store.go @@ -4,14 +4,11 @@ import ( "fmt" "git.pcux.cn/xuans/Smail/db" - "git.pcux.cn/xuans/Smail/dto/response" "git.pcux.cn/xuans/Smail/models" - "git.pcux.cn/xuans/Smail/services/list" "git.pcux.cn/xuans/Smail/utils/array" "github.com/emersion/go-imap/v2" "github.com/emersion/go-imap/v2/imapserver" log "github.com/sirupsen/logrus" - "github.com/spf13/cast" ) func (s *serverSession) Store(w *imapserver.FetchWriter, numSet imap.NumSet, flags *imap.StoreFlags, options *imap.StoreOptions) error { @@ -22,36 +19,9 @@ func (s *serverSession) Store(w *imapserver.FetchWriter, numSet imap.NumSet, fla return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Mailbox is read-only"} } - var emailList []*response.EmailResponseData - - switch numSet.(type) { - case imap.SeqSet: - seqSet := numSet.(imap.SeqSet) - for _, seq := range seqSet { - res, err := list.GetEmailListByGroup(s.ctx, s.currentMailbox, list.ImapListReq{ - Star: cast.ToInt(seq.Start), - End: cast.ToInt(seq.Stop), - }, false) - if err != nil { - log.WithContext(s.ctx).WithError(err).Error("query IMAP messages for store failed") - return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Store failed"} - } - emailList = append(emailList, res...) - } - - case imap.UIDSet: - uidSet := numSet.(imap.UIDSet) - for _, uid := range uidSet { - res, err := list.GetEmailListByGroup(s.ctx, s.currentMailbox, list.ImapListReq{ - Star: cast.ToInt(uint32(uid.Start)), - End: cast.ToInt(uint32(uid.Stop)), - }, true) - if err != nil { - log.WithContext(s.ctx).WithError(err).Error("query IMAP messages for UID store failed") - return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Store failed"} - } - emailList = append(emailList, res...) - } + emailList, err := s.selectedMessages(numSet) + if err != nil { + return &imap.Error{Type: imap.StatusResponseTypeNo, Text: "Store failed"} } seenRequested := array.InArray(imap.FlagSeen, flags.Flags) diff --git a/backend/listen/pop3_server/action.go b/backend/listen/pop3_server/action.go index 985cc1af..2a730360 100644 --- a/backend/listen/pop3_server/action.go +++ b/backend/listen/pop3_server/action.go @@ -60,7 +60,7 @@ func (a action) Capa(session *gopop.Session) ([]string, error) { "LIST", "RETR", "DELE", - "REST", + "RSET", "NOOP", "QUIT", } @@ -109,13 +109,17 @@ func (a action) Pass(session *gopop.Session, pwd string) error { return errors2.New("password error") } - session.Status = gopop.TRANSACTION ctx := session.Ctx.(*context.Context) ctx.UserID = user.ID ctx.UserName = user.Name ctx.UserAccount = user.Account ctx.Role = user.EffectiveRole() ctx.IsAdmin = ctx.Role == access.RoleSuperAdmin || ctx.Role == access.RoleAdmin + if _, err := a.ensureMessageMap(session); err != nil { + ctx.UserID = 0 + return errors2.New("mailbox unavailable") + } + session.Status = gopop.TRANSACTION return nil } @@ -164,8 +168,8 @@ func (a action) Uidl(session *gopop.Session, msg string) ([]gopop.UidlItem, erro return nil, err } ret := make([]gopop.UidlItem, 0, len(emailList)) - for number, info := range emailList { - ret = append(ret, gopop.UidlItem{Id: int64(number + 1), UnionId: cast.ToString(info.Id)}) + for _, info := range emailList { + ret = append(ret, gopop.UidlItem{Id: int64(info.SerialNumber), UnionId: cast.ToString(info.Id)}) } log.WithContext(session.Ctx).Debugf("Uidl \n %+v", ret) @@ -207,13 +211,10 @@ func (a action) List(session *gopop.Session, msg string) ([]gopop.MailInfo, erro emailList = filtered } ret := make([]gopop.MailInfo, 0, len(emailList)) - for number, info := range emailList { + for _, info := range emailList { size := int64(info.Size) - if size == 0 { - size = 9999 - } ret = append(ret, gopop.MailInfo{ - Id: int64(number + 1), + Id: int64(info.SerialNumber), Size: size, }) } @@ -268,7 +269,7 @@ func (a action) Delete(session *gopop.Session, id int64) error { } func (a action) Rest(session *gopop.Session) error { - log.WithContext(session.Ctx).Debugf("POP3 CMD: REST ") + log.WithContext(session.Ctx).Debugf("POP3 CMD: RSET ") session.DeleteIds = []int64{} return nil } @@ -306,7 +307,8 @@ func (a action) Top(session *gopop.Session, id int64, n int) (string, error) { break } } - if len(res) <= headerEndLine+n+1 { + // 先比较剩余行数,避免极大的合法整数在加法中溢出。 + if n >= len(res)-headerEndLine-1 { return string(ret), nil } @@ -333,10 +335,13 @@ func (a action) ensureMessageMap(session *gopop.Session) ([]*response.EmailRespo return nil, err } session.MessageMap = make(map[int64]int64, len(emailList)) + session.MessageSizes = make(map[int64]int, len(emailList)) session.MessageIDs = make([]int64, 0, len(emailList)) for number, info := range emailList { id := cast.ToInt64(info.Id) session.MessageMap[int64(number+1)] = id + session.MessageSizes[id] = info.Size + info.SerialNumber = number + 1 session.MessageIDs = append(session.MessageIDs, id) } return emailList, nil @@ -362,19 +367,12 @@ func (a action) resolveMessageID(session *gopop.Session, number int64) (int64, e } func (a action) visibleMessages(session *gopop.Session) ([]*response.EmailResponseData, error) { - emailList, err := a.ensureMessageMap(session) - if err != nil { + if _, err := a.ensureMessageMap(session); err != nil { return nil, err } - if emailList == nil { - emailList, _, err = list.GetEmailList(session.Ctx.(*context.Context), dto.SearchTag{Type: consts.EmailTypeReceive, Status: -1, GroupId: -1}, "", true, 0, 99999) - if err != nil { - return nil, err - } - } - result := make([]*response.EmailResponseData, 0, len(emailList)) - for _, info := range emailList { - id := cast.ToInt64(info.Id) + // 列表和单封命令使用同一会话快照,删除标记不能压缩消息编号。 + result := make([]*response.EmailResponseData, 0, len(session.MessageIDs)) + for number, id := range session.MessageIDs { marked := false for _, deletedID := range session.DeleteIds { if deletedID == id { @@ -383,6 +381,9 @@ func (a action) visibleMessages(session *gopop.Session) ([]*response.EmailRespon } } if !marked { + info := &response.EmailResponseData{SerialNumber: number + 1} + info.Id = cast.ToInt(id) + info.Size = session.MessageSizes[id] result = append(result, info) } } diff --git a/backend/res_init/init.go b/backend/res_init/init.go index ca3c53b0..b806d4c2 100644 --- a/backend/res_init/init.go +++ b/backend/res_init/init.go @@ -251,15 +251,16 @@ func startService(serviceErrors chan<- error, name string, service func() error) func stopServices() error { restart.SetCurrent(nil) - session.Stop() stopAdminResetControl() - stopVerificationRuntime() smtp_server.Stop() http_server.HttpsStop() http_server.HttpStop() pop3_server.Stop() imap_server.Stop() hooks.Stop() + // 先关闭业务入口,再撤下安全服务及会话清理器,避免新请求读取停止中的依赖。 + stopVerificationRuntime() + session.Stop() // 关闭连接池后还要等待借出的连接归还,不能提前解除迁移源锁。 if db.Instance != nil { _ = db.Instance.Close() @@ -392,14 +393,14 @@ func startWebAuthnRuntime() error { } func stopVerificationRuntime() { - controllers.ConfigureWebAuthnRuntime(nil) + controllers.ClearSecurityRuntime() webAuthnRuntimeService = nil + credentialService = nil if verificationRuntime == nil { return } verificationRuntime.Stop() verificationRuntime = nil - credentialService = nil } // startAdminResetControl 启动受保护的本机超级管理员重置通道。 diff --git a/backend/services/list/imap_search.go b/backend/services/list/imap_search.go index de736ee3..c1034067 100644 --- a/backend/services/list/imap_search.go +++ b/backend/services/list/imap_search.go @@ -62,12 +62,26 @@ type IMAPSearchResult struct { } // SearchEmails performs IMAP search based on criteria -func SearchEmails(ctx *context.Context, groupName string, criteria *imap.SearchCriteria) ([]*response.UserEmailUIDData, error) { +func SearchEmails(ctx *context.Context, groupName string, criteria *imap.SearchCriteria, snapshot ...[]int) ([]*response.UserEmailUIDData, error) { // First get the base list for the mailbox baseList, err := GetUEListByUID(ctx, groupName, 0, 0, nil) if err != nil { return nil, err } + if len(snapshot) > 0 { + sequences := make(map[int]int, len(snapshot[0])) + for index, uid := range snapshot[0] { + sequences[uid] = index + 1 + } + filtered := make([]*response.UserEmailUIDData, 0, len(baseList)) + for _, item := range baseList { + if sequence := sequences[item.ID]; sequence > 0 { + item.SerialNumber = sequence + filtered = append(filtered, item) + } + } + baseList = filtered + } if len(baseList) == 0 { return baseList, nil } @@ -77,22 +91,34 @@ func SearchEmails(ctx *context.Context, groupName string, criteria *imap.SearchC return baseList, nil } - // Build UID to sequence number mapping - uidToSeq := make(map[int]int) - seqToUID := make(map[int]int) + // 通配上界来自完整会话快照,不能随其他搜索条件缩小。 + var maxUID, maxSequence uint32 for _, item := range baseList { - uidToSeq[item.ID] = item.SerialNumber - seqToUID[item.SerialNumber] = item.ID + if number, ok := validIMAPNumber(item.ID); ok { + maxUID = max(maxUID, number) + } + if number, ok := validIMAPNumber(item.SerialNumber); ok { + maxSequence = max(maxSequence, number) + } + } + if len(snapshot) > 0 { + maxUID = 0 + maxSequence = uint32(len(snapshot[0])) + for _, uid := range snapshot[0] { + if number, ok := validIMAPNumber(uid); ok { + maxUID = max(maxUID, number) + } + } } // Filter by UID sets if len(criteria.UID) > 0 { - baseList = filterByUIDSets(baseList, criteria.UID) + baseList = filterByUIDSets(baseList, criteria.UID, maxUID) } // Filter by sequence number sets if len(criteria.SeqNum) > 0 { - baseList = filterBySeqNumSets(baseList, criteria.SeqNum) + baseList = filterBySeqNumSets(baseList, criteria.SeqNum, maxSequence) } // For more complex filters, we need to fetch email data @@ -111,7 +137,7 @@ func SearchEmails(ctx *context.Context, groupName string, criteria *imap.SearchC // Handle NOT criteria if len(criteria.Not) > 0 { for _, notCriteria := range criteria.Not { - baseList, err = applyNotCriteria(ctx, groupName, baseList, ¬Criteria) + baseList, err = applyNotCriteria(ctx, groupName, baseList, ¬Criteria, snapshot...) if err != nil { return nil, fmt.Errorf("apply NOT criteria: %w", err) } @@ -120,7 +146,7 @@ func SearchEmails(ctx *context.Context, groupName string, criteria *imap.SearchC // Handle OR criteria if len(criteria.Or) > 0 { - baseList, err = applyOrCriteria(ctx, groupName, baseList, criteria.Or) + baseList, err = applyOrCriteria(ctx, groupName, baseList, criteria.Or, snapshot...) if err != nil { return nil, fmt.Errorf("apply OR criteria: %w", err) } @@ -169,14 +195,26 @@ func validIMAPNumber(value int) (uint32, bool) { } // filterByUIDSets filters the list by UID sets -func filterByUIDSets(list []*response.UserEmailUIDData, uidSets []imap.UIDSet) []*response.UserEmailUIDData { +func filterByUIDSets(list []*response.UserEmailUIDData, uidSets []imap.UIDSet, upper ...uint32) []*response.UserEmailUIDData { + var maximum uint32 + for _, item := range list { + if number, ok := validIMAPNumber(item.ID); ok { + maximum = max(maximum, number) + } + } + if len(upper) > 0 { + maximum = upper[0] + } result := list for _, uidSet := range uidSets { filtered := make([]*response.UserEmailUIDData, 0, len(result)) for _, item := range result { number, ok := validIMAPNumber(item.ID) - if ok && uidSet.Contains(imap.UID(number)) { - filtered = append(filtered, item) + for _, span := range uidSet { + if ok && searchNumberInRange(number, uint32(span.Start), uint32(span.Stop), maximum) { + filtered = append(filtered, item) + break + } } } result = filtered @@ -185,14 +223,26 @@ func filterByUIDSets(list []*response.UserEmailUIDData, uidSets []imap.UIDSet) [ } // filterBySeqNumSets filters the list by sequence number sets -func filterBySeqNumSets(list []*response.UserEmailUIDData, seqSets []imap.SeqSet) []*response.UserEmailUIDData { +func filterBySeqNumSets(list []*response.UserEmailUIDData, seqSets []imap.SeqSet, upper ...uint32) []*response.UserEmailUIDData { + var maximum uint32 + for _, item := range list { + if number, ok := validIMAPNumber(item.SerialNumber); ok { + maximum = max(maximum, number) + } + } + if len(upper) > 0 { + maximum = upper[0] + } result := list for _, seqSet := range seqSets { filtered := make([]*response.UserEmailUIDData, 0, len(result)) for _, item := range result { number, ok := validIMAPNumber(item.SerialNumber) - if ok && seqSet.Contains(number) { - filtered = append(filtered, item) + for _, span := range seqSet { + if ok && searchNumberInRange(number, span.Start, span.Stop, maximum) { + filtered = append(filtered, item) + break + } } } result = filtered @@ -200,6 +250,19 @@ func filterBySeqNumSets(list []*response.UserEmailUIDData, seqSets []imap.SeqSet return result } +func searchNumberInRange(number, start, stop, maximum uint32) bool { + if start == 0 { + start = maximum + } + if stop == 0 { + stop = maximum + } + if start > stop { + start, stop = stop, start + } + return number >= start && number <= stop +} + // filterByFlags filters by message flags func filterByFlags(list []*response.UserEmailUIDData, flags []imap.Flag, notFlags []imap.Flag) []*response.UserEmailUIDData { var result []*response.UserEmailUIDData @@ -285,7 +348,11 @@ func filterWithEmailData(ctx *context.Context, list []*response.UserEmailUIDData continue } - if matchesEmailCriteria(email, criteria) { + visible := *email + if visible.Type == 0 { + visible.Bcc = "" + } + if matchesEmailCriteria(&visible, criteria) { result = append(result, item) } } @@ -433,9 +500,9 @@ func matchesText(email *models.Email, pattern string) bool { } // applyNotCriteria applies NOT criteria -func applyNotCriteria(ctx *context.Context, groupName string, list []*response.UserEmailUIDData, notCriteria *imap.SearchCriteria) ([]*response.UserEmailUIDData, error) { +func applyNotCriteria(ctx *context.Context, groupName string, list []*response.UserEmailUIDData, notCriteria *imap.SearchCriteria, snapshot ...[]int) ([]*response.UserEmailUIDData, error) { // Get the list of items that match the NOT criteria - matchedList, err := SearchEmails(ctx, groupName, notCriteria) + matchedList, err := SearchEmails(ctx, groupName, notCriteria, snapshot...) if err != nil { return nil, err } @@ -458,7 +525,7 @@ func applyNotCriteria(ctx *context.Context, groupName string, list []*response.U } // applyOrCriteria applies OR criteria -func applyOrCriteria(ctx *context.Context, groupName string, list []*response.UserEmailUIDData, orCriteria [][2]imap.SearchCriteria) ([]*response.UserEmailUIDData, error) { +func applyOrCriteria(ctx *context.Context, groupName string, list []*response.UserEmailUIDData, orCriteria [][2]imap.SearchCriteria, snapshot ...[]int) ([]*response.UserEmailUIDData, error) { if len(orCriteria) == 0 { return list, nil } @@ -472,7 +539,7 @@ func applyOrCriteria(ctx *context.Context, groupName string, list []*response.Us for _, pair := range orCriteria { pairUIDs := make(map[int]bool) // Get matches for first condition - matches1, err := SearchEmails(ctx, groupName, &pair[0]) + matches1, err := SearchEmails(ctx, groupName, &pair[0], snapshot...) if err != nil { return nil, err } @@ -483,7 +550,7 @@ func applyOrCriteria(ctx *context.Context, groupName string, list []*response.Us } // Get matches for second condition - matches2, err := SearchEmails(ctx, groupName, &pair[1]) + matches2, err := SearchEmails(ctx, groupName, &pair[1], snapshot...) if err != nil { return nil, err } diff --git a/backend/services/list/list.go b/backend/services/list/list.go index bd7f0390..34f1a248 100644 --- a/backend/services/list/list.go +++ b/backend/services/list/list.go @@ -342,6 +342,9 @@ func hydrateIMAPEmailResponses(relations []*response.UserEmailUIDData) ([]*respo continue } copyMessage := *message + if copyMessage.Type == consts.EmailTypeReceive { + copyMessage.Bcc = "[]" + } copyMessage.IsRead = relation.IsRead copyMessage.IsDeleted = relation.IsDeleted copyMessage.SerialNumber = relation.SerialNumber diff --git a/backend/services/recovery/service.go b/backend/services/recovery/service.go index 0083e5c3..e169d967 100644 --- a/backend/services/recovery/service.go +++ b/backend/services/recovery/service.go @@ -100,6 +100,8 @@ func (s *Service) SetWebAuthnVerifier(verifier WebAuthnVerifier) { if s == nil { return } + s.mu.Lock() + defer s.mu.Unlock() s.webauthn = verifier } @@ -107,14 +109,36 @@ func (s *Service) SetSecurityStateInvalidator(invalidator func(int, string)) { if s == nil { return } + s.mu.Lock() + defer s.mu.Unlock() s.invalidate = invalidator } +func (s *Service) currentWebAuthnVerifier() WebAuthnVerifier { + if s == nil { + return nil + } + s.mu.Lock() + defer s.mu.Unlock() + return s.webauthn +} + +func (s *Service) invalidateSecurityState(userID int, account string) { + s.mu.Lock() + invalidator := s.invalidate + s.mu.Unlock() + // 回调可能重新进入本服务,必须在释放内部锁后调用。 + if invalidator != nil { + invalidator(userID, account) + } +} + func (s *Service) BeginWebAuthn(identifier string) (*protocol.CredentialAssertion, string, error) { - if s == nil || s.engine == nil || s.webauthn == nil { + verifier := s.currentWebAuthnVerifier() + if s == nil || s.engine == nil || verifier == nil { return nil, "", ErrUnavailable } - options, sessionID, err := s.webauthn.BeginDiscoverableLogin() + options, sessionID, err := verifier.BeginDiscoverableLogin() if err != nil { return nil, "", ErrRecoveryDenied } @@ -122,10 +146,11 @@ func (s *Service) BeginWebAuthn(identifier string) (*protocol.CredentialAssertio } func (s *Service) VerifyWebAuthn(identifier, sessionID string, request *http.Request) (string, error) { - if s == nil || s.engine == nil || s.webauthn == nil || request == nil || strings.TrimSpace(sessionID) == "" { + verifier := s.currentWebAuthnVerifier() + if s == nil || s.engine == nil || verifier == nil || request == nil || strings.TrimSpace(sessionID) == "" { return "", ErrInvalidRequest } - user, _, err := s.webauthn.FinishDiscoverableLogin(sessionID, request) + user, _, err := verifier.FinishDiscoverableLogin(sessionID, request) if err != nil || user == nil { return "", ErrRecoveryDenied } @@ -137,10 +162,11 @@ func (s *Service) VerifyWebAuthn(identifier, sessionID string, request *http.Req } func (s *Service) VerifyWebAuthnWithContext(identifier, sessionID string, request *http.Request, browserContext string) (string, error) { - if s == nil || s.engine == nil || s.webauthn == nil || request == nil || strings.TrimSpace(sessionID) == "" { + verifier := s.currentWebAuthnVerifier() + if s == nil || s.engine == nil || verifier == nil || request == nil || strings.TrimSpace(sessionID) == "" { return "", ErrInvalidRequest } - user, credential, err := s.webauthn.FinishDiscoverableLogin(sessionID, request) + user, credential, err := verifier.FinishDiscoverableLogin(sessionID, request) if err != nil || user == nil || credential == nil { return "", ErrRecoveryDenied } @@ -314,7 +340,7 @@ func (s *Service) CompleteWithContext(ticketID, newPassword, browserContext stri defer clearString(&hash) var user models.User has, err := s.engine.ID(item.UserID).Get(&user) - if err != nil || !has || user.EffectiveRole() == access.RoleSuperAdmin || user.SessionVersion != item.SessionVersion { + if err != nil || !has || user.Disabled != 0 || user.EffectiveRole() == access.RoleSuperAdmin || user.SessionVersion != item.SessionVersion { return ErrRecoveryDenied } if item.Method == "email" { @@ -350,7 +376,7 @@ func (s *Service) CompleteWithContext(ticketID, newPassword, browserContext stri if err := session.Begin(); err != nil { return err } - if _, err := session.ID(item.UserID).Cols("password", "session_version", "security_version", "must_change_password", "password_changed_at").Update(&models.User{ + if affected, err := session.ID(item.UserID).Where("session_version = ? AND security_version = ? AND disabled = 0", item.SessionVersion, user.SecurityVersion).Cols("password", "session_version", "security_version", "must_change_password", "password_changed_at").Update(&models.User{ Password: hash, SessionVersion: version, SecurityVersion: securityVersion, @@ -359,6 +385,9 @@ func (s *Service) CompleteWithContext(ticketID, newPassword, browserContext stri }); err != nil { _ = session.Rollback() return err + } else if affected != 1 { + _ = session.Rollback() + return ErrRecoveryDenied } if err := s.credentials.RevokeAllWithSession(session, item.UserID); err != nil { _ = session.Rollback() @@ -371,9 +400,7 @@ func (s *Service) CompleteWithContext(ticketID, newPassword, browserContext stri if s.verification != nil && s.verification.Store != nil { _ = s.verification.Store.InvalidateAccount(user.Account) } - if s.invalidate != nil { - s.invalidate(item.UserID, user.Account) - } + s.invalidateSecurityState(item.UserID, user.Account) return nil } diff --git a/backend/services/setup/ssl/ssl.go b/backend/services/setup/ssl/ssl.go index 82757412..75dd5510 100644 --- a/backend/services/setup/ssl/ssl.go +++ b/backend/services/setup/ssl/ssl.go @@ -98,6 +98,7 @@ func SetSSL(sslType, priKey, crtKey string) error { if err != nil { return errors.Wrap(err) } + expected := *cfg if sslType == config.SSLTypeAutoHTTP || sslType == config.SSLTypeUser || sslType == config.SSLTypeAutoDNS { cfg.SSLType = sslType } else { @@ -110,6 +111,14 @@ func SetSSL(sslType, priKey, crtKey string) error { Mode: config.ManagedSSLModeDefault, DefaultDirectory: "config/ssl", } + if cfg.SSLType == config.SSLTypeAutoDNS { + cfg.SSL.Challenge = config.SSLChallengeDNS + if err := validateWildcardConfiguration(cfg); err != nil { + return err + } + } else { + cfg.SSL.Challenge = config.SSLChallengeHTTP + } } if cfg.SSLType == config.SSLTypeUser { privatePath, publicPath, err := validateUserCertificatePaths(priKey, crtKey) @@ -124,7 +133,7 @@ func SetSSL(sslType, priKey, crtKey string) error { if err != nil { return newUserCertificateError("SSL 证书无法读取,请确认文件路径和 smail 用户权限") } - reference, err := config.ImportManagedSSL(config.ManagedSSLModeUpload, privatePEM, publicPEM, nil, sslDomains(cfg)) + reference, err := config.StageManagedSSL(config.ManagedSSLModeUpload, privatePEM, publicPEM, nil, sslDomains(cfg)) if err != nil { return normalizeUserCertificateImportError(err) } @@ -133,7 +142,7 @@ func SetSSL(sslType, priKey, crtKey string) error { cfg.HttpsEnabled = 1 } - err = config.WriteConfig(cfg) + err = config.WriteSSLConfig(&expected, cfg) if err != nil { return errors.Wrap(err) } @@ -164,6 +173,10 @@ func applyManagedSSLReference(cfg *config.Config, reference config.ManagedSSLRef } if reference.Mode == config.ManagedSSLModeUpload { cfg.SSL.CustomDirectory = "config/ssl/managed/upload" + } else if cfg.SSLType == config.SSLTypeAutoDNS { + cfg.SSL.Challenge = config.SSLChallengeDNS + } else { + cfg.SSL.Challenge = config.SSLChallengeHTTP } } @@ -257,14 +270,19 @@ func writeCertificateFiles(cfg *config.Config, privateKey, certificate, issuer [ if cfg == nil { return errors.New("SSL 配置不能为空") } - reference, err := config.ImportManagedSSL(config.ManagedSSLModeDefault, privateKey, certificate, issuer, certificateDomains(cfg)) + if err := validateWildcardConfiguration(cfg); err != nil { + return err + } + next := *cfg + reference, err := config.StageManagedSSL(config.ManagedSSLModeDefault, privateKey, certificate, issuer, certificateDomains(cfg)) if err != nil { return err } - applyManagedSSLReference(cfg, reference) - if err := config.WriteConfig(cfg); err != nil { + applyManagedSSLReference(&next, reference) + if err := config.WriteSSLConfig(cfg, &next); err != nil { return errors.Wrap(err) } + *cfg = next return nil } @@ -300,13 +318,43 @@ func certificateMatchesConfiguredDomains(cert *x509.Certificate, cfg *config.Con if cert == nil || cfg == nil { return false } - for _, domain := range sslDomains(cfg) { + if validateWildcardConfiguration(cfg) != nil { + return false + } + for _, domain := range certificateDomains(cfg) { if err := cert.VerifyHostname(domain); err != nil { return false } } return true } + +// 自动 DNS 只申请主域泛域名;不覆盖的服务地址是配置错误,不能靠重复续期修复。 +func validateWildcardConfiguration(cfg *config.Config) error { + if cfg == nil || cfg.SSLType != config.SSLTypeAutoDNS { + return nil + } + domains := wildcardCertificateDomains(cfg) + if len(domains) != 1 { + return newUserCertificateError("自动 DNS 证书需要有效的主域名") + } + base := strings.TrimPrefix(domains[0], "*.") + covered := func(host string) bool { + host = strings.ToLower(strings.TrimSuffix(strings.TrimSpace(host), ".")) + suffix := "." + strings.ToLower(base) + label := strings.TrimSuffix(host, suffix) + return strings.HasSuffix(host, suffix) && label != "" && !strings.Contains(label, ".") + } + if cfg.WebDomain != "" && !covered(cfg.WebDomain) { + return newUserCertificateError("Web 域名必须是主域名的一级子域名,泛域名证书不覆盖根域名或多级子域名") + } + for _, domain := range cfg.Domains { + if !covered("smtp."+domain) || !covered("imap."+domain) || !covered("pop."+domain) { + return newUserCertificateError("附加邮箱域名的服务地址不在主域泛域名证书覆盖范围内") + } + } + return nil +} func wildcardCertificateDomains(cfg *config.Config) []string { if cfg == nil { return nil @@ -475,6 +523,9 @@ func GenSSL(update bool) error { if err != nil { return errors.Wrap(err) } + if err := validateWildcardConfiguration(cfg); err != nil { + return err + } if !update { privatePath, publicPath, pathErr := config.ResolveRuntimeSSLPaths(cfg) diff --git a/deploy/install-windows.ps1 b/deploy/install-windows.ps1 index 263dc856..56368c42 100644 --- a/deploy/install-windows.ps1 +++ b/deploy/install-windows.ps1 @@ -129,13 +129,17 @@ $adminBackup = Join-Path $backupRoot "smail-admin.exe" $serviceCreated = $false try { + $mainInstalled = $false + $adminInstalled = $false Stop-SmailService Copy-Item -LiteralPath $mainSource -Destination (Join-Path $transactionRoot "smail.exe") Copy-Item -LiteralPath $adminSource -Destination (Join-Path $transactionRoot "smail-admin.exe") if (Test-Path -LiteralPath $mainTarget) { Move-Item -LiteralPath $mainTarget -Destination $mainBackup } if (Test-Path -LiteralPath $adminTarget) { Move-Item -LiteralPath $adminTarget -Destination $adminBackup } Move-Item -LiteralPath (Join-Path $transactionRoot "smail.exe") -Destination $mainTarget + $mainInstalled = $true Move-Item -LiteralPath (Join-Path $transactionRoot "smail-admin.exe") -Destination $adminTarget + $adminInstalled = $true $service = Get-Service -Name "Smail" -ErrorAction SilentlyContinue if ($null -eq $service) { $binaryPath = '"' + $mainTarget + '"' @@ -169,10 +173,10 @@ try { Write-Output "主服务保持停止状态,请完成配置后再手动启动。" } } catch { - if (Test-Path -LiteralPath $mainTarget) { + if ($mainInstalled -and (Test-Path -LiteralPath $mainTarget)) { Remove-Item -LiteralPath $mainTarget -Force } - if (Test-Path -LiteralPath $adminTarget) { + if ($adminInstalled -and (Test-Path -LiteralPath $adminTarget)) { Remove-Item -LiteralPath $adminTarget -Force } if (Test-Path -LiteralPath $mainBackup) { @@ -185,18 +189,19 @@ try { $deleteArguments = @("delete", "Smail") Invoke-Native -FilePath "sc.exe" -Arguments $deleteArguments } - if (Test-Path -LiteralPath $transactionRoot) { - Remove-Item -LiteralPath $transactionRoot -Recurse -Force - } - if (Test-Path -LiteralPath $backupRoot) { - Remove-Item -LiteralPath $backupRoot -Recurse -Force - } + # 失败时保留事务目录和备份,回滚再次失败时也不能丢失唯一恢复材料。 throw } -if (Test-Path -LiteralPath $transactionRoot) { - Remove-Item -LiteralPath $transactionRoot -Recurse -Force -} -if (Test-Path -LiteralPath $backupRoot) { - Remove-Item -LiteralPath $backupRoot -Recurse -Force +foreach ($temporaryRoot in @($transactionRoot, $backupRoot)) { + if (Test-Path -LiteralPath $temporaryRoot) { + $resolvedTemporaryRoot = (Resolve-Path -LiteralPath $temporaryRoot).Path + if ([IO.Path]::GetDirectoryName($resolvedTemporaryRoot) -ne $resolvedInstallRoot.TrimEnd('\')) { + throw "事务目录超出安装目录,拒绝清理:$resolvedTemporaryRoot" + } + if ((Get-Item -LiteralPath $resolvedTemporaryRoot).Attributes -band [IO.FileAttributes]::ReparsePoint) { + throw "事务目录不能是重解析点,拒绝清理:$resolvedTemporaryRoot" + } + Remove-Item -LiteralPath $resolvedTemporaryRoot -Recurse -Force + } } diff --git a/docs/代码逐行审查报告-20260906.md b/docs/代码逐行审查报告-20260906.md index 2f25f482..16834472 100644 --- a/docs/代码逐行审查报告-20260906.md +++ b/docs/代码逐行审查报告-20260906.md @@ -4,26 +4,77 @@ - 请求:对全部代码逐行审查,不能用搜索命中、编译成功或测试通过代替逐行检查。 - 开始时间:2026-09-06 01:46:05,时区 Asia/Shanghai。 -- 基线:`9849f8f11c9f49efd1bc877d7fedd8da265e32df`,分支 `main`。 +- 原始审查基线:`9849f8f11c9f49efd1bc877d7fedd8da265e32df`,分支 `main`。 - 审查前已建立中文提交检查点,远端 `smail/main` 已核验一致。 -- 当前结论:**审查进行中,尚未完成全仓逐行覆盖;发现的问题尚未修复。** -- 本轮只新增隔离审查测试、覆盖工具和报告,未修改业务源码,未清空真实数据库,未停止开发服务。 +- 修复阶段开始时间:2026-09-06 11:03:06;修改前检查点 `addfcea1cdb59f1fd28bd1639c54502cb05990de` 已提交并推送。 +- 当前结论:**已实施业务修复并通过本节所列自动化回归,仍有部分修复及待真实验收项;尚未完成全仓逐行覆盖。** +- 本文保留 R01–R51 原始发现、原位置与失败证据,它们是历史审查记录,不代表修复后的当前行为。当前状态以“修复状态”和“修复后最终回归与构建”两节为准。 +- 修复范围包含原始基线之后已有提交中的修复及本次补充;测试使用隔离数据库与配置目录,未清空真实运行数据库,未将新构建部署到用户正在使用的实例。 ## 需求确认与任务清单 按用户明确要求,审查范围包括业务源码、测试、构建部署脚本、配置模板及前端页面。锁文件、第三方资源、二进制资源单独分类,不伪装成已逐行审查的业务代码。 - [✓] 核验工作区及审查前 Git 检查点。 -- [x] 运行原有前后端测试基线,结果通过,等待结合新增场景复审。 +- [✓] 运行原有前后端测试基线,保留原始通过及新增失败证据。 - [~] 建立逐文件、行范围和哈希覆盖账本。 - [~] 检查认证恢复、迁移、邮件列表、IMAP、配置与前端交互。 -- [~] 用隔离 SQLite、真实本地协议和真实前端函数验证问题。 +- [✓] 用隔离 SQLite、真实本地协议和真实前端函数验证已登记问题并执行修复回归。 +- [x] 修复认证授权、配置事务、邮件协议、隐私、前端交互及部署回滚问题,等待适用环境复审。 +- [~] 补齐外部邮箱刷新或验证码过期后的绑定恢复流程。 +- [✓] 完成前端 55/55、独立前端审查 11/11、后端全量与 race、独立审查普通与 race 回归。 +- [✓] 完成 Windows 八阶段内存回滚验证、Telegram 隔离测试及前后端构建。 - [ ] 补齐全仓其余文件的逐行审查。 - [ ] 浏览器 MCP 验收完整业务流程。 - [ ] MySQL、PostgreSQL 实库及 Linux/Windows 部署验收。 -- [ ] 全部发现项修复后的独立复审;本轮未执行修复。 +- [ ] 部署新产物并完成真实证书续期、邮件投递及在途请求排空验收。 +- [~] 全部发现项修复后的独立复审;自动化场景已通过,但不等于完整功能验收。 -## 已确认问题 +## 修复状态 + +状态约定:本表 `[x]` 表示代码修改及列出的自动化场景已完成,仍需对应的浏览器、外部服务或部署复审;`[~]` 表示仍有明确未完成的代码流程。上方测试任务的 `[✓]` 仅指该项测试本身复核通过,不扩大为产品验收通过。 + +| 原始编号 | 当前状态与修复内容 | 自动化证据与仍需验收的边界 | +| --- | --- | --- | +| R01、R03、R28 | [x] 不支持的源索引明确拒绝;迁移排空响应取消;保留历史自增高水位。 | `dbmigration_findings_test.go`;SQLite 迁移回归通过,MySQL/PostgreSQL 实库待验。 | +| R02 | [x] 登录二次验证重发进入登录前安全路由,仍保留挑战、限频及 CSRF 边界。 | `TestAuditAnonymousLoginResendMustReachController`;真实验证码投递与浏览器待验。 | +| R04 | [x] 邮件计数与分页查询分离,不再把偏移带入计数。 | `TestAuditPaginationMustPreserveTotal`;普通列表页面待浏览器验收。 | +| R05 | [x] 恢复完成时复查绑定的 WebAuthn 凭据及账户安全状态,撤销后的旧票据不能改密。 | `recovery_findings_test.go`;真实安全密钥设备流程待验。 | +| R06 | [x] 直接使用响应拦截器返回的 Blob;错误 Blob 不生成下载,允许重试。 | `frontend-findings.test.mjs` 的 CSV/JSON 内容及错误响应断言;浏览器下载待验。 | +| R07、R35 | [x] 写信页接收 `fileRef`,邮件列表接收 `taskTableDataRef`,连接真实模板与组合函数。 | 真实 SFC 编译挂载的附件和列表测试;系统文件选择框、实际表格待浏览器验收。 | +| R08、R11 | [x] 区分内容身份和邮箱关系身份,为副本创建独立状态对象,保持 COPY 后移动、FETCH 的 UID 一致。 | `imap_findings_test.go` 的副本、移动与 FETCH 回归;未宣告原生 MOVE,客户端仍使用回退流程。 | +| R09 | [x] 安全服务使用加锁快照发布与读取;先停止入口,再撤下服务引用及会话清理器。 | `runtime_findings_test.go`、`lifecycle_findings_test.go` 普通与 race 通过;完整 HTTP 在途排空待验。 | +| R10、R12–R15 | [x] 保存只读选择状态,统一动态 UID 与序号解析,修复默认文件夹枚举、UIDNEXT 和有效邮件计数。 | `imap_findings_test.go`、`imap_wire_findings_test.go`;同时覆盖 Poll/IDLE、DONE 与清理。 | +| R16 | [x] 传递目标角色,并用安全版本条件更新及失效流程保护角色变更。 | `TestAuditAuthorizedRoleChangeMustSucceed`;角色变化后的真实 HTTP 会话失效待验。 | +| R17 | [x] 恢复 Cookie 的签发和清除采用覆盖新旧接口的 `/api/` 路径,保留上下文校验。 | `TestAuditRecoveryCookieMustReachVersionedAPI` 的 TLS/CookieJar 新旧接口对照;自签名 HTTPS 浏览器 Cookie/CSRF 待验。 | +| R18–R23 | [x] POP3 建立会话快照及连续编号,统一可见集合与删除视图,支持 RSET、正确单行响应和非负 TOP 参数。 | `pop3_action_findings_test.go` 及解析器测试;LIST/STAT 字节大小、dot-stuffing 和 RETR 额外空行另行专项验收。 | +| R24–R27 | [x] 收件人响应不暴露密送地址;保留标准 Reply-To、带逗号姓名和附件 Content-ID;IMAP 重建字节稳定。 | `message_findings_test.go` 与真实解析、存储、响应回归;网页 `cid:` 显示及外部客户端待验。 | +| R29 | [x] 配置重试前处理未决事务,成功重试不再被旧失败事务在重启时回滚。 | `TestAuditSuccessfulConfigRetryMustSurviveRestart`:重启后代次仍为 2,策略仍为 8 MiB。 | +| R30 | [x] 候选有唯一 ID,提供受授权的脱敏查询、恢复和取消;页面刷新恢复候选,垃圾邮件设置同步适配。 | `TestAuditPendingIdentityAndRecovery`、配置前端及审查测试;跨会话恢复保留近期身份验证,已落盘候选不能取消。 | +| R31 | [~] 同页面输错验证码或取消弹窗后保留原挑战,可再次输入,不重复绑定。 | 前端重试及后端约束测试通过;刷新、验证码过期后的挑战恢复与重发入口尚未完成,不能标为完全修复。 | +| R32、R33 | [x] 外部邮箱绑定执行近期认证,规范化后拒绝当前用户自己的主邮箱。 | `TestAuditExternalBindingMustRequireRecentAuthentication`、`TestAuditExternalBindingMustRejectOwnPrimaryMailbox`;真实邮件投递待验。 | +| R34 | [x] 修正正常附件下载路由解析,保留邮件归属及索引校验。 | `TestAuditAttachmentPageDownloadMustReturnStoredContent` 的真实 TLS 路由;浏览器点击下载待验。 | +| R36 | [x] Windows 升级按目标记录备份和安装状态,回滚只处理本次实际替换的文件。 | 最新八个失败阶段均保留旧主程序与旧管理程序;这是内存隔离控制流验证,不是正式服务部署。 | +| R37 | [x] 编辑规则使用独立草稿,新建和取消重置状态,新建提交 ID 为 0。 | `rule-frontend.test.mjs`;列表未被未保存条件污染,实际请求走新增接口。 | +| R38、R39、R41 | [x] 完整证书链去重,按 X.509 校验泛域名覆盖,迁移保留 DNS 挑战方式。 | `ssl_findings_test.go` 及原有 SSL 测试;不请求真实 CA。自动 DNS 只申请 `*.主域名`,不加入根域名 SAN。 | +| R40 | [x] 证书先写入独立托管版本,再通过分区配置事务提交引用;不提前替换现用目录。 | `TestAuditPartitionedRenewalMustCommitBeforeSwitch`、`TestAuditStagedSSLDoesNotReplaceCurrent`;发布失败保留已提交候选,超级管理员可受授权恢复,但自动恢复与真实 CA 续期尚未验收。 | +| R42 | [x] 旧配置迁移保留 SQLite 原路径,避免静默连接到新空库。 | `TestAuditLegacyMigrationMustPreserveSQLiteLocation`;原目录标记数据仍可读取。 | +| R43 | [x] 移除特殊域名的 DKIM 放行,只依据实际签名校验结果。 | `TestAuditInvalidDKIMMustNotPassForSpecialDomain`;普通域名与原特殊域名均拒绝无效签名。 | +| R44、R45 | [x] SEARCH 独立搜索键求交,OR 节点内部求并,删除搜索使用与 STORE/FETCH 一致的标记。 | `TestAuditSearchKeysMustIntersect`、`TestAuditSearchDeletedMustMatchStoredFlag` 的本地 TLS 协议测试。 | +| R46–R48 | [x] Telegram 附件发送遵守隐私开关,普通文本 HTML 转义,Unicode 截断保持有效字符和闭合标记。 | `telegram_findings_test.go`:关闭上传 0 个附件、开启对照 2 个;真实 Telegram API 待验。 | +| R49 | [x] 协议开关采用受控值,请求成功才提交新状态,失败恢复服务端一致状态。 | `protocol-frontend.test.mjs` 成功与失败对照通过;浏览器及真实协议连接关闭待验。 | +| R50 | [x] Telegram 设置改为主程序原生 `.vue/.js/.css` 页面,不依赖受限 iframe 存储或网络访问。 | `test/frontend/telegram-settings.test.mjs`;其他插件继续保留 `sandbox="allow-scripts"`,完整新页面部署待验。 | +| R51 | [x] 插件 RPC 用明确字符串映射传递失败信息,在适配层与 Go `error` 转换。 | `TestAuditSendAfterFailureMustReachPlugin` 的成功与失败回调对照;外部插件进程待验。 | + +### 修复期间追加的边界问题 + +1. **大邮箱 UID 查询上限**:`session_messages.go` 每批最多读取 500 个 UID,保持会话序号;EXPUNGE 分批读取后汇总并单事务删除。`TestAuditLargeSelectedMailboxMustBatchUIDQueries` 使用 10,001 封邮件,修复前 `count=0 err=invalid UID list`,最终普通测试 4.73 秒、race 179.29 秒通过。失败证据:`test-log/run-2026-09-06T04-25-22-809Z-39264/backend.log`。 +2. **SSL 提交覆盖签发期间的其他配置**:旧配置读写共用 `legacyConfigMu`,SSL 提交重读最新配置,只合并 SSL/HTTPS 字段;域名或证书策略改变时拒绝旧结果。`TestAuditLegacySSLCommitPreservesLatestUnrelatedSettings` 修复前把端口覆盖回 8080,最终断言保留 9080。失败证据:`test-log/run-2026-09-06T04-32-01-312Z-3768/backend.log`。本项不是通用旧版整份配置写入的完整 CAS,不宣称所有并发更新都已解决。 +3. **停止验证运行时后残留旧服务引用**:`ClearSecurityRuntime()` 在锁内清空五项服务,再清登录挑战;有无清理运行时均撤下引用,重复停止安全。`TestAuditStopVerificationWithdrawsPublishedServices` 先失败,最终两组均通过,并验证真实控制器返回 `NeedLogin`。失败证据:`test-log/run-2026-09-06T04-33-27-630Z-42076/backend.log`。HTTP 仍使用 `Close()`,这不等于完整在途请求排空已验收。 + +此外,微信推送增加业务响应码及响应解析检查,不能把 HTTP 成功误判为业务成功;`TestAuditWeChatBusinessResponses` 的隔离回归通过,未向真实微信端点投递。 + +## 原始已确认问题(历史发现) 优先级:P1 为安全授权、数据完整性或核心功能问题;P2 为特定场景故障及一致性问题。编号固定,便于后续复现和修复。 @@ -396,7 +447,7 @@ - 影响:本应保持原安装的普通升级失败会破坏可用安装,导致服务下次无法启动或本地管理程序丢失。 - 建议:按目标记录备份和替换状态,仅清理本次真正安装的新文件;成功备份后才能覆盖,回滚失败时必须保留恢复材料并明确报告。 - 边界:没有执行真实安装脚本主体、删除磁盘文件、停止服务、调用系统安装工具或改变 ACL;此处确认的是实际控制流在指定异常下的结果,双平台部署验收仍未完成。 -- 复现:`node test/review/run-windows.mjs`,六个失败阶段内存验证。 +- 原始复现:`node test/review/run-windows.mjs`,当时六个失败阶段内存验证;修复后已扩展为八阶段,结果见最终回归节。 ### R37 / P2:规则编辑取消后点击新建,提交的仍是对旧规则的修改 @@ -561,13 +612,15 @@ - Windows 正式模式插件隔离存在明确不可用分支,仍需核对功能开关、实际调用链和文档承诺。 - SMTP MX 回退、IMAP 动态序列其他命令、HTML 入库与插件路径等尚需继续检查;UIDNEXT 和 UID FETCH 通配符的已验证问题见 R13、R14。 -## 复现与验证记录 +## 原始复现与验证记录 + +以下命令和日志保留审查阶段的先失败证据;修复后的同名测试现已通过,不应将历史失败日志解释为当前程序仍出现相同故障。 ```powershell # 原有测试基线 node test/run.mjs all -count=1 ./... -# 独立审查测试:当前预期会失败,失败断言用于暴露尚未修复的业务问题 +# 独立审查测试:审查阶段曾失败,用于暴露当时尚未修复的问题 node test/review/run.mjs node test/review/run.mjs -race node test/review/run.mjs --pop3-only @@ -620,13 +673,68 @@ node test/review/coverage.mjs - R49 协议开关失败回滚:`test-log/review-protocol-20260906-041737.log`,失败请求状态一致性断言失败,成功请求对照通过。 - 包含候选配置与外部邮箱重试的五项前端失败复现:`test-log/review-frontend-2026-09-05T19-15-56-197Z/frontend.log`。 - 前端两项复现日志:`test-log/frontend-review-20260906-022032.log`。 -- 测试源码:`test/review/`;Go overlay 将测试注入对应包,不改业务源码;运行产物放在 `test-log/`。 +- 测试源码:`test/review/`;Go overlay 将测试注入对应包,不为执行测试改写业务源码;修复阶段的业务修改另由 Git 差异记录,运行产物放在 `test-log/`。 - `^TestAudit` 还会匹配项目原有审计接口测试,因此测试用例总数不等于新增问题数。 - 未运行带 `--fix` 的 lint,不用自动格式化或文本替换悄悄改动业务源码。 +## 修复后最终回归与构建 + +### 自动化回归 + +本批测试使用隔离库、临时配置及本地协议夹具,没有清空真实运行库,也没有执行用户要求的“清库后按生产环境重新初始化”验收。 + +| 验证 | 最终结果 | 本地证据 | +| --- | --- | --- | +| 前后端全量 | 前端 55/55,后端全量通过 | `test-log/run-2026-09-06T04-34-53-174Z-29120/` | +| 后端全量 race | 通过 | `test-log/run-2026-09-06T04-34-53-169Z-15200/backend.log` | +| 独立审查 race | 通过,包含最后三项补充修复 | `test-log/run-2026-09-06T04-34-53-289Z-17348/backend.log` | +| 独立审查普通 | 通过,包含最后三项补充修复 | `test-log/run-2026-09-06T04-40-27-845Z-16924/backend.log` | +| 独立前端审查 | 11/11 | `test-log/review-frontend-final-20260906-123539.log` | +| Windows 升级回滚 | 八个失败阶段,两份旧程序均保留 | `test-log/review-windows-1788669339410/windows.log` | +| Telegram | 隔离测试通过 | `test-log/review-telegram-2026-09-06T04-35-50-100Z/telegram.log` | + +主要复核命令: + +```powershell +node test/run.mjs all -count=1 -timeout=180s ./... +node test/run.mjs backend -race -count=1 -timeout=240s ./... +node test/review/run.mjs -race -timeout=240s +node test/review/run.mjs -timeout=240s +node --test test/review/frontend-findings.test.mjs test/review/rule-frontend.test.mjs test/review/protocol-frontend.test.mjs +node test/review/run-windows.mjs +node test/review/run-telegram.mjs +``` + +独立审查 race 日志中的隔离 IMAP 连接空闲超时不代表用户后端故障;相关用例和包最终通过。`^TestAudit` 也会运行项目原有审计接口测试,因此测试数量不等于问题数量。 + +### 构建产物 + +- 前端:`out/frontend`,已同步到 Go 嵌入目录 `backend/listen/http_server/dist`;1858 个模块转换,构建 9.03 秒,postbuild 成功。日志:`test-log/review-build-frontend-20260906-124143.log`。 +- 前端仍有超过 500 kB 的分块警告,属于待优化项;PowerShell 将标准错误流显示为 `NativeCommandError`,但本次构建及 postbuild 已成功,不能据此判为构建失败。 +- 后端:`out/review-fixes-20260906-124516/`,包含主程序、管理员工具及配置工具,均包含最后三项补充修复。日志:`test-log/review-build-backend-20260906-124516.log`。 +- 构建元数据:`Version=review-fixes-20260906-124516`,`GitHash=addfcea1cdb59f1fd28bd1639c54502cb05990de-dirty`,`BuildTime=2026-09-06T04:45:17Z`,`GoVersion=go1.26.6`。这是最终提交前工作树的验证产物,不冒充最终提交哈希构建。 + +| 文件 | SHA-256 | +| --- | --- | +| `smail.exe` | `67BA0822498E8724CC51E811793CFE2347D4AC642CE8EAB82B4062DBDC63AF52` | +| `smail-admin.exe` | `F1F2F7655224661276AF44A16947303405B275938D3C241CFC0A6DF32785EC6B` | +| `smail-config.exe` | `FB05E8673E2B807F46CC51D0366E6269841927D3AA82D6320249C6699371C228` | + +旧目录 `out/review-fixes-20260906-121853/` 不含最后三项修复,不用于本节结果对应的部署。旧产物保留,没有清理用户文件。根 `config/`、`ssl/`、`test-log/`、`out/` 及嵌入产物不纳入本次源码提交。 + +### 仍需继续的验收 + +- R31 的刷新、过期挑战恢复与重发入口尚需实现并测试。 +- R40 的已提交候选受授权恢复需要真实运行验证,自动恢复和真实 CA 签发、续期未闭环。 +- 登录刷新保持会话、setup、忘记密码、登录二次验证、强制改密、普通邮箱列表、审计 Blob 下载、自签名 HTTPS Cookie/CSRF 尚需浏览器 MCP 真实验收。 +- MySQL/PostgreSQL 的索引与自增迁移、角色变更后的 HTTP 会话失效、Linux/Windows 正式部署、真实邮件及插件投递待验。 +- HTTP 完整在途排空、POP3 字节大小及转义、网页内嵌图片显示等专项仍需补充。 +- **用户正在运行的实例尚未部署本批新程序;测试通过及构建成功不意味着页面或运行中的后端已更新。** + ## 覆盖说明 - 覆盖记录源:`test/review/reviewed-ranges.json`。 +- 账本基线仍为 `9849f8f11c9f49efd1bc877d7fedd8da265e32df`,只对应原始审查版本;没有修改基线来伪造修复后的覆盖率。覆盖工具会拒绝与该基线不一致的 HEAD,当前修复差异复核也不等于新版本全仓逐行覆盖。 - 生成账本包含每个受版本控制文件的路径、分类、行数、SHA-256、阅读行范围及发现编号。 - `read-not-accepted` 只表示全文已读,不表示无问题,也不表示复审通过。 - `partial-read` 表示只读取部分行;`not-recorded` 表示没有登记,接续摘要中模糊描述的已读内容不冒充全文覆盖。 diff --git a/frontend/src/views/Editer/Editer.vue b/frontend/src/views/Editer/Editer.vue index dd5a59cd..5b0ef619 100644 --- a/frontend/src/views/Editer/Editer.vue +++ b/frontend/src/views/Editer/Editer.vue @@ -118,6 +118,7 @@ const { upload, send, ruleFormRef, + fileRef, fileChange, rules, ruleForm, diff --git a/frontend/src/views/List/List.vue b/frontend/src/views/List/List.vue index 145e10fe..7bceb91a 100644 --- a/frontend/src/views/List/List.vue +++ b/frontend/src/views/List/List.vue @@ -98,6 +98,7 @@ import { usePage, CircleCheck, Switch, Delete, View, Folder, EpArrowDownBold, EditPen, Warning } from './List.js' const { + taskTableDataRef, groupStore, selectAll, data, diff --git a/frontend/src/views/components/ConfigSettings/ConfigEditor/ConfigEditor.vue b/frontend/src/views/components/ConfigSettings/ConfigEditor/ConfigEditor.vue index 15896c9d..b517a7a8 100644 --- a/frontend/src/views/components/ConfigSettings/ConfigEditor/ConfigEditor.vue +++ b/frontend/src/views/components/ConfigSettings/ConfigEditor/ConfigEditor.vue @@ -103,7 +103,7 @@ :section="selectedSection" :config="formConfig" :errors="formErrors" - :disabled="!canWrite || !!working" + :disabled="!canWrite || !!working || !!pendingCandidate" @change="updateField" />

{{ sensitiveHint }}。

@@ -119,7 +119,7 @@ type="password" show-password clearable - :disabled="!canWrite || sectionLoading || !!working || !!sectionError" + :disabled="!canWrite || sectionLoading || !!working || !!sectionError || !!pendingCandidate" :placeholder="secretField.placeholder" autocomplete="new-password" @update:model-value="updateSecret" @@ -127,21 +127,34 @@
{{ validationResult.valid ? '验证通过' : '验证未通过' }} @@ -182,6 +195,8 @@ const { secretValue, confirmed, savedGeneration, + pendingCandidate, + managePending, validationResult, canWrite, refreshPage, diff --git a/frontend/src/views/components/ConfigSettings/ConfigSettings.js b/frontend/src/views/components/ConfigSettings/ConfigSettings.js index 66af914f..ace4de83 100644 --- a/frontend/src/views/components/ConfigSettings/ConfigSettings.js +++ b/frontend/src/views/components/ConfigSettings/ConfigSettings.js @@ -60,6 +60,7 @@ export function usePage(pageKey = 'base') { const secretValue = ref('') const confirmed = ref(false) const savedGeneration = ref(0) + const pendingCandidate = ref(null) let savedInput = null const validationResult = ref(null) const backups = ref([]) @@ -134,7 +135,8 @@ export function usePage(pageKey = 'base') { validationResult.value = null confirmed.value = false } - const canEdit = () => !disposed && canWrite.value && !loading.value && !working.value && !sectionLoading.value && !sectionError.value + const canOperate = () => !disposed && canWrite.value && !loading.value && !working.value && !sectionLoading.value && !sectionError.value && !availabilityError.value + const canEdit = () => canOperate() && !pendingCandidate.value && !savedGeneration.value const updateField = (path, value) => { if (!canEdit()) return const updated = updateFormConfig(editorText.value, selectedSection.value, path, value) @@ -185,6 +187,50 @@ export function usePage(pageKey = 'base') { Object.assign(status, response.data || {}) } + const loadPending = async () => { + const response = await http.get('/api/v1/config/pending') + if (disposed) return + notifyResponseError(response, '候选配置读取失败') + pendingCandidate.value = response.data || null + savedGeneration.value = 0 + savedInput = null + const pending = pendingCandidate.value + const section = pending?.section === 'root' ? 'base' : pending?.section + if (pending && section === selectedSection.value) { + editorText.value = JSON.stringify(pending.payload, null, 2) + secretValue.value = '' + savedInput = { text: editorText.value, secret: '' } + savedGeneration.value = pending.sameSession ? Number(pending.generation) : 0 + } + } + + const managePending = async action => { + if (!canOperate() || !pendingCandidate.value) return + working.value = action + try { + await ElMessageBox.confirm( + action === 'cancel' ? '取消后保留当前生效配置,候选修改不会应用。确认取消吗?' : '恢复后将候选配置绑定到当前会话,请确认已核对候选内容。', + action === 'cancel' ? '取消候选配置' : '恢复候选配置', + { type: 'warning', confirmButtonText: '确认', cancelButtonText: '返回' } + ) + const response = await http.post('/api/v1/config/pending', { + generation: pendingCandidate.value.generation, + candidateId: pendingCandidate.value.candidateId, + action, + confirmation: { confirmed: confirmed.value, method: 'recent_auth' } + }) + notifyResponseError(response, '候选配置操作失败') + await loadStatus() + await loadSection() + await loadPending() + ElMessage.success(action === 'cancel' ? '候选已取消,可以继续编辑' : '候选已恢复,请核对后应用') + } catch (error) { + if (error !== 'cancel' && error !== 'close') ElMessage.error(error.message || '候选配置操作失败') + } finally { + working.value = '' + } + } + const loadSection = async () => { const requestId = ++sectionRequestId const section = selectedSection.value @@ -388,6 +434,7 @@ export function usePage(pageKey = 'base') { return } if (isConfigPage || (pageKey === 'migration' && canMigrate.value)) await loadSection() + if (isConfigPage) await loadPending() if (pageKey === 'backups') await loadBackups() if (pageKey === 'audit') await loadAudit() } catch (error) { @@ -473,9 +520,11 @@ export function usePage(pageKey = 'base') { const response = await http.put(`/api/v1/config/${selectedSection.value}`, request) notifyResponseError(response, '配置保存失败') savedGeneration.value = response.data?.generation || 0 + pendingCandidate.value = { ...response.data, section: selectedSection.value, sameSession: true } savedInput = { text: editorText.value, secret: secretValue.value } validationResult.value = { valid: true, generation: savedGeneration.value, issues: [] } await loadStatus() + await loadPending() ElMessage.success(`候选配置已保存,代次 ${savedGeneration.value} 等待应用`) } catch (error) { ElMessage.error(error.message || '配置保存失败') @@ -485,7 +534,7 @@ export function usePage(pageKey = 'base') { } const applyConfig = async () => { - if (!canEdit() || !savedGeneration.value) return + if (!canOperate() || !savedGeneration.value) return if (!savedInput || savedInput.text !== editorText.value || savedInput.secret !== secretValue.value) { invalidateCandidate() ElMessage.warning('填写内容已变更,请重新保存候选配置后再应用') @@ -493,10 +542,14 @@ export function usePage(pageKey = 'base') { } working.value = 'apply' try { - const response = await http.post(`/api/v1/config/${selectedSection.value}/apply`, { generation: savedGeneration.value }) + const response = await http.post(`/api/v1/config/${selectedSection.value}/apply`, { + generation: savedGeneration.value, + candidateId: pendingCandidate.value?.candidateId + }) notifyResponseError(response, '配置应用失败') savedGeneration.value = 0 await Promise.all([loadStatus(), loadSection()]) + await loadPending() ElMessage.success('配置已应用') } catch (error) { ElMessage.error(error.message || '配置应用失败') @@ -551,7 +604,17 @@ export function usePage(pageKey = 'base') { working.value = format === 'csv' ? 'export-csv' : 'export' try { const response = await http.post('/api/v1/audits/config', { format, limit: 500 }, { responseType: 'blob' }) - const blob = response.data instanceof Blob ? response.data : new Blob([response.data], { type: format === 'csv' ? 'text/csv' : 'application/json' }) + if (!(response instanceof Blob)) { + throw new Error(response?.errorMsg || '审计导出响应格式不正确') + } + // Blob 请求也可能返回 JSON 业务错误,不能把错误响应当作审计文件下载。 + if (response.type.includes('json') || format === 'json') { + const payload = JSON.parse(await response.text()) + if (payload && !Array.isArray(payload) && Object.hasOwn(payload, 'errorNo') && payload.errorNo !== 0) { + throw new Error(payload.errorMsg || '审计导出失败') + } + } + const blob = response const url = URL.createObjectURL(blob) const anchor = document.createElement('a') anchor.href = url @@ -604,7 +667,7 @@ export function usePage(pageKey = 'base') { loading, availabilityError, working, status, stateLabel, stateTagType, sectionOptions: sections, selectedSection, currentSection, sectionLoading, editorText, formConfig, formErrors, updateField, updateSecret, sectionError, sensitiveHint, secretField, secretValue, - confirmed, savedGeneration, validationResult, canWrite, canRollback, canAudit, canExportAudit, + confirmed, savedGeneration, pendingCandidate, managePending, validationResult, canWrite, canRollback, canAudit, canExportAudit, backupsLoading, backupsError, backups, rollbackGeneration, rollbackConfirmed, rollbackPreview, auditLoading, auditError, auditItems, loadAll, loadSection, validateConfig, saveConfig, applyConfig, loadBackups, previewRollback, executeRollback, loadAudit, exportAudit, formatTime, diff --git a/frontend/src/views/components/PluginSettings/PluginSettings.vue b/frontend/src/views/components/PluginSettings/PluginSettings.vue index a3cc7ced..395fee40 100644 --- a/frontend/src/views/components/PluginSettings/PluginSettings.vue +++ b/frontend/src/views/components/PluginSettings/PluginSettings.vue @@ -9,6 +9,7 @@ +