完善配置审计与数据库迁移安全链路
This commit is contained in:
1 parent
84440d8761
commit
3af1fc4147
117 files changed
+4703
-1110
No files matched your search
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/internal/app"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := app.RecoverPanic(app.Run); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func exportAuditCSV(events []ConfigAuditEvent) ([]byte, error) {
|
||||
var builder strings.Builder
|
||||
writer := csv.NewWriter(&builder)
|
||||
if err := writer.Write([]string{"id", "operation", "section", "actor_id", "actor_role", "from_generation", "to_generation", "target_generation", "changed_fields", "confirmation_method", "result", "created_at"}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, event := range events {
|
||||
if err := writer.Write([]string{
|
||||
event.ID,
|
||||
string(event.Operation),
|
||||
string(event.Section),
|
||||
event.ActorID,
|
||||
event.ActorRole,
|
||||
fmt.Sprintf("%d", event.FromGeneration),
|
||||
fmt.Sprintf("%d", event.ToGeneration),
|
||||
fmt.Sprintf("%d", event.TargetGeneration),
|
||||
strings.Join(event.ChangedFields, ","),
|
||||
event.ConfirmationMethod,
|
||||
event.Result,
|
||||
event.CreatedAt.UTC().Format(time.RFC3339Nano),
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
writer.Flush()
|
||||
if err := writer.Error(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []byte(builder.String()), nil
|
||||
}
|
||||
@@ -3,12 +3,44 @@ package config
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var ErrAuditQueryUnavailable = errors.New("configuration audit query unavailable")
|
||||
var (
|
||||
ErrAuditQueryUnavailable = errors.New("configuration audit query unavailable")
|
||||
ErrInvalidAuditQuery = errors.New("configuration audit query is invalid")
|
||||
ErrInvalidAuditFormat = errors.New("configuration audit export format is invalid")
|
||||
ErrAuditNotFound = errors.New("configuration audit event not found")
|
||||
)
|
||||
|
||||
// ValidateConfigAuditQuery 校验审计查询条件,拒绝未知或越界参数。
|
||||
func ValidateConfigAuditQuery(query ConfigAuditQuery) error {
|
||||
if query.ID != "" && !validAuditEventID(query.ID) {
|
||||
return ErrInvalidAuditQuery
|
||||
}
|
||||
if query.Limit < 0 || query.Limit > 1000 {
|
||||
return ErrInvalidAuditQuery
|
||||
}
|
||||
if !query.From.IsZero() && !query.To.IsZero() && query.From.After(query.To) {
|
||||
return ErrInvalidAuditQuery
|
||||
}
|
||||
if len(query.ActorID) > 128 || strings.ContainsAny(query.ActorID, "\r\n") {
|
||||
return ErrInvalidAuditQuery
|
||||
}
|
||||
if query.Operation != "" && query.Operation != ConfigOperationUpdate && query.Operation != ConfigOperationRollback && query.Operation != ConfigOperationMigration {
|
||||
return ErrInvalidAuditQuery
|
||||
}
|
||||
if query.Section != "" && query.Section != SectionRoot && query.Section != SectionSMTP && query.Section != SectionEmail && query.Section != SectionSSL && query.Section != SectionPlugins {
|
||||
return ErrInvalidAuditQuery
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AuditQuery 读取配置审计的只读接口,写入器不支持查询时明确拒绝。
|
||||
func (m *ConfigManager) AuditQuery(ctx context.Context, query ConfigAuditQuery) ([]ConfigAuditEvent, error) {
|
||||
if err := ValidateConfigAuditQuery(query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if m == nil || m.security.Audit == nil {
|
||||
return nil, ErrAuditQueryUnavailable
|
||||
}
|
||||
@@ -23,6 +55,9 @@ func (m *ConfigManager) AuditQuery(ctx context.Context, query ConfigAuditQuery)
|
||||
|
||||
// ExportAuditJSON 导出脱敏配置审计;导出内容由审计存储实现生成。
|
||||
func (m *ConfigManager) ExportAuditJSON(ctx context.Context, query ConfigAuditQuery) ([]byte, error) {
|
||||
if err := ValidateConfigAuditQuery(query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if m == nil || m.security.Audit == nil {
|
||||
return nil, ErrAuditQueryUnavailable
|
||||
}
|
||||
@@ -37,6 +72,9 @@ func (m *ConfigManager) ExportAuditJSON(ctx context.Context, query ConfigAuditQu
|
||||
|
||||
// ExportAuditCSV 导出脱敏配置审计;导出内容由审计存储实现生成。
|
||||
func (m *ConfigManager) ExportAuditCSV(ctx context.Context, query ConfigAuditQuery) ([]byte, error) {
|
||||
if err := ValidateConfigAuditQuery(query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if m == nil || m.security.Audit == nil {
|
||||
return nil, ErrAuditQueryUnavailable
|
||||
}
|
||||
@@ -48,3 +86,19 @@ func (m *ConfigManager) ExportAuditCSV(ctx context.Context, query ConfigAuditQue
|
||||
}
|
||||
return reader.ExportCSV(ctx, query)
|
||||
}
|
||||
|
||||
// AuditDetail 按稳定标识查询一条配置审计事件。
|
||||
func (m *ConfigManager) AuditDetail(ctx context.Context, id string) (ConfigAuditEvent, error) {
|
||||
id = strings.TrimSpace(id)
|
||||
if !validAuditEventID(id) {
|
||||
return ConfigAuditEvent{}, ErrInvalidAuditQuery
|
||||
}
|
||||
items, err := m.AuditQuery(ctx, ConfigAuditQuery{ID: id, Limit: 1})
|
||||
if err != nil {
|
||||
return ConfigAuditEvent{}, err
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return ConfigAuditEvent{}, ErrAuditNotFound
|
||||
}
|
||||
return items[0], nil
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestValidateConfigAuditQueryAcceptsSupportedFilters(t *testing.T) {
|
||||
query := ConfigAuditQuery{
|
||||
From: time.Date(2026, 8, 1, 0, 0, 0, 0, time.UTC),
|
||||
To: time.Date(2026, 8, 30, 0, 0, 0, 0, time.UTC),
|
||||
ActorID: "42",
|
||||
Operation: ConfigOperationUpdate,
|
||||
Section: SectionSMTP,
|
||||
Limit: 1000,
|
||||
}
|
||||
if err := ValidateConfigAuditQuery(query); err != nil {
|
||||
t.Fatalf("valid audit query rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateConfigAuditQueryRejectsAmbiguousFilters(t *testing.T) {
|
||||
tests := []ConfigAuditQuery{
|
||||
{Limit: -1},
|
||||
{Limit: 1001},
|
||||
{From: time.Date(2026, 8, 30, 0, 0, 0, 0, time.UTC), To: time.Date(2026, 8, 1, 0, 0, 0, 0, time.UTC)},
|
||||
{ActorID: "bad\nactor"},
|
||||
{Operation: ConfigOperation("unknown")},
|
||||
{Section: Section("unknown")},
|
||||
}
|
||||
for index, query := range tests {
|
||||
if err := ValidateConfigAuditQuery(query); !errors.Is(err, ErrInvalidAuditQuery) {
|
||||
t.Fatalf("case %d returned %v, want ErrInvalidAuditQuery", index, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigManagerRejectsInvalidAuditQueryBeforeStorageAccess(t *testing.T) {
|
||||
var manager *ConfigManager
|
||||
_, err := manager.AuditQuery(nil, ConfigAuditQuery{Limit: 1001})
|
||||
if !errors.Is(err, ErrInvalidAuditQuery) {
|
||||
t.Fatalf("AuditQuery returned %v, want ErrInvalidAuditQuery", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigManagerAuditDetailUsesStableIDAndRedactsExport(t *testing.T) {
|
||||
writer := &MemoryAuditWriter{}
|
||||
manager := &ConfigManager{security: ConfigSecurity{Audit: writer}}
|
||||
event := ConfigAuditEvent{
|
||||
Operation: ConfigOperationUpdate,
|
||||
Section: SectionSMTP,
|
||||
ActorID: "42",
|
||||
ActorRole: "super_admin",
|
||||
FromGeneration: 1,
|
||||
ToGeneration: 2,
|
||||
ChangedFields: []string{"relay.password"},
|
||||
ConfirmationMethod: "recent_auth",
|
||||
Result: "success",
|
||||
CreatedAt: time.Date(2026, 8, 30, 0, 0, 0, 0, time.UTC),
|
||||
}
|
||||
if err := writer.WriteConfigAudit(nil, event); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items := writer.Events()
|
||||
if len(items) != 1 || items[0].ID == "" || !strings.HasPrefix(items[0].ID, "audit-") {
|
||||
t.Fatalf("unexpected stable audit ID: %+v", items)
|
||||
}
|
||||
detail, err := manager.AuditDetail(nil, items[0].ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if detail.ID != items[0].ID || detail.ChangedFields[0] != "relay.password" {
|
||||
t.Fatalf("unexpected audit detail: %+v", detail)
|
||||
}
|
||||
if _, err := manager.AuditDetail(nil, "audit-not-valid"); !errors.Is(err, ErrInvalidAuditQuery) {
|
||||
t.Fatalf("invalid audit ID returned %v", err)
|
||||
}
|
||||
if _, err := manager.AuditDetail(nil, "audit-00000000000000000000000000000000"); !errors.Is(err, ErrAuditNotFound) {
|
||||
t.Fatalf("missing audit ID returned %v", err)
|
||||
}
|
||||
jsonData, err := manager.ExportAuditJSON(nil, ConfigAuditQuery{ID: items[0].ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(jsonData), "password-value") || !strings.Contains(string(jsonData), items[0].ID) {
|
||||
t.Fatalf("unexpected audit export: %s", jsonData)
|
||||
}
|
||||
csvData, err := manager.ExportAuditCSV(nil, ConfigAuditQuery{ID: items[0].ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(csvData), "id,operation") || !strings.Contains(string(csvData), items[0].ID) {
|
||||
t.Fatalf("CSV export does not include stable ID: %s", csvData)
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,10 @@ package config
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/csv"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -24,6 +27,7 @@ var (
|
||||
)
|
||||
|
||||
type ConfigAuditQuery struct {
|
||||
ID string
|
||||
From time.Time
|
||||
To time.Time
|
||||
ActorID string
|
||||
@@ -119,11 +123,16 @@ func (w *FileAuditWriter) Query(ctx context.Context, query ConfigAuditQuery) ([]
|
||||
result := make([]ConfigAuditEvent, 0, minAuditLimit(limit, 32))
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Buffer(make([]byte, 4096), 256*1024)
|
||||
lineNumber := 0
|
||||
for scanner.Scan() {
|
||||
lineNumber++
|
||||
var event ConfigAuditEvent
|
||||
if err := json.Unmarshal(scanner.Bytes(), &event); err != nil {
|
||||
return nil, ErrAuditStorageUnavailable
|
||||
}
|
||||
if event.ID == "" {
|
||||
event.ID = legacyAuditEventID(scanner.Bytes(), lineNumber)
|
||||
}
|
||||
if err := validateAuditEvent(event); err != nil {
|
||||
return nil, ErrAuditStorageUnavailable
|
||||
}
|
||||
@@ -156,12 +165,12 @@ func (w *FileAuditWriter) ExportCSV(ctx context.Context, query ConfigAuditQuery)
|
||||
}
|
||||
var builder strings.Builder
|
||||
writer := csv.NewWriter(&builder)
|
||||
if err := writer.Write([]string{"operation", "section", "actor_id", "actor_role", "from_generation", "to_generation", "target_generation", "changed_fields", "confirmation_method", "result", "created_at"}); err != nil {
|
||||
if err := writer.Write([]string{"id", "operation", "section", "actor_id", "actor_role", "from_generation", "to_generation", "target_generation", "changed_fields", "confirmation_method", "result", "created_at"}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, event := range events {
|
||||
if err := writer.Write([]string{
|
||||
string(event.Operation), string(event.Section), event.ActorID, event.ActorRole,
|
||||
event.ID, string(event.Operation), string(event.Section), event.ActorID, event.ActorRole,
|
||||
fmt.Sprintf("%d", event.FromGeneration), fmt.Sprintf("%d", event.ToGeneration),
|
||||
fmt.Sprintf("%d", event.TargetGeneration), strings.Join(event.ChangedFields, ","),
|
||||
event.ConfirmationMethod, event.Result, event.CreatedAt.UTC().Format(time.RFC3339Nano),
|
||||
@@ -203,16 +212,28 @@ func (w *FileAuditWriter) Cleanup(ctx context.Context, now time.Time) (int, erro
|
||||
deleted := 0
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Buffer(make([]byte, 4096), 256*1024)
|
||||
lineNumber := 0
|
||||
for scanner.Scan() {
|
||||
lineNumber++
|
||||
var event ConfigAuditEvent
|
||||
if err := json.Unmarshal(scanner.Bytes(), &event); err != nil || validateAuditEvent(event) != nil {
|
||||
if err := json.Unmarshal(scanner.Bytes(), &event); err != nil {
|
||||
return deleted, ErrAuditStorageUnavailable
|
||||
}
|
||||
if event.ID == "" {
|
||||
event.ID = legacyAuditEventID(scanner.Bytes(), lineNumber)
|
||||
}
|
||||
if err := validateAuditEvent(event); err != nil {
|
||||
return deleted, ErrAuditStorageUnavailable
|
||||
}
|
||||
if event.CreatedAt.Before(cutoff) {
|
||||
deleted++
|
||||
continue
|
||||
}
|
||||
kept = append(kept, append([]byte(nil), scanner.Bytes()...))
|
||||
data, marshalErr := json.Marshal(event)
|
||||
if marshalErr != nil {
|
||||
return deleted, ErrAuditStorageUnavailable
|
||||
}
|
||||
kept = append(kept, data)
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return deleted, ErrAuditStorageUnavailable
|
||||
@@ -250,21 +271,42 @@ func ensureAuditFile(path string) error {
|
||||
}
|
||||
|
||||
func normalizeAuditEvent(event ConfigAuditEvent) (ConfigAuditEvent, error) {
|
||||
if err := validateAuditEvent(event); err != nil {
|
||||
return ConfigAuditEvent{}, err
|
||||
if event.ID == "" {
|
||||
id, err := newAuditEventID()
|
||||
if err != nil {
|
||||
return ConfigAuditEvent{}, err
|
||||
}
|
||||
event.ID = id
|
||||
}
|
||||
event.ActorID = strings.TrimSpace(event.ActorID)
|
||||
event.ActorRole = strings.TrimSpace(event.ActorRole)
|
||||
event.ConfirmationMethod = strings.ToLower(strings.TrimSpace(event.ConfirmationMethod))
|
||||
event.ChangedFields = append([]string(nil), event.ChangedFields...)
|
||||
if err := validateAuditEvent(event); err != nil {
|
||||
return ConfigAuditEvent{}, err
|
||||
}
|
||||
return event, nil
|
||||
}
|
||||
|
||||
func newAuditEventID() (string, error) {
|
||||
value := make([]byte, 16)
|
||||
if _, err := rand.Read(value); err != nil {
|
||||
return "", ErrAuditEventInvalid
|
||||
}
|
||||
return "audit-" + hex.EncodeToString(value), nil
|
||||
}
|
||||
|
||||
func legacyAuditEventID(raw []byte, lineNumber int) string {
|
||||
hash := sha256.New()
|
||||
hash.Write(raw)
|
||||
hash.Write([]byte(fmt.Sprintf(":%d", lineNumber)))
|
||||
return "legacy-" + hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
func validateAuditEvent(event ConfigAuditEvent) error {
|
||||
if event.Operation != ConfigOperationUpdate && event.Operation != ConfigOperationRollback && event.Operation != ConfigOperationMigration {
|
||||
return ErrAuditEventInvalid
|
||||
}
|
||||
if strings.TrimSpace(event.ActorID) == "" || strings.TrimSpace(event.ActorRole) == "" || event.FromGeneration == 0 || event.ToGeneration == 0 || event.CreatedAt.IsZero() {
|
||||
if !validAuditEventID(event.ID) || strings.TrimSpace(event.ActorID) == "" || strings.TrimSpace(event.ActorRole) == "" || event.FromGeneration == 0 || event.ToGeneration == 0 || event.CreatedAt.IsZero() {
|
||||
return ErrAuditEventInvalid
|
||||
}
|
||||
if event.Result != "success" && event.Result != "failure" {
|
||||
@@ -282,6 +324,9 @@ func validateAuditEvent(event ConfigAuditEvent) error {
|
||||
}
|
||||
|
||||
func auditEventMatches(event ConfigAuditEvent, query ConfigAuditQuery) bool {
|
||||
if query.ID != "" && event.ID != query.ID {
|
||||
return false
|
||||
}
|
||||
if !query.From.IsZero() && event.CreatedAt.Before(query.From) {
|
||||
return false
|
||||
}
|
||||
@@ -300,6 +345,24 @@ func auditEventMatches(event ConfigAuditEvent, query ConfigAuditQuery) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func validAuditEventID(value string) bool {
|
||||
if len(value) != 38 && len(value) != 71 {
|
||||
return false
|
||||
}
|
||||
if strings.HasPrefix(value, "audit-") {
|
||||
value = strings.TrimPrefix(value, "audit-")
|
||||
} else if strings.HasPrefix(value, "legacy-") {
|
||||
value = strings.TrimPrefix(value, "legacy-")
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
for _, character := range value {
|
||||
if !(character >= '0' && character <= '9') && !(character >= 'a' && character <= 'f') {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
func auditContextError(ctx context.Context) error {
|
||||
if ctx == nil {
|
||||
return nil
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ListBackups 枚举合法备份的代次和时间,不返回内部路径。
|
||||
@@ -28,7 +29,10 @@ func (s *TransactionStore) ListBackups() ([]ConfigBackupInfo, error) {
|
||||
}
|
||||
result := make([]ConfigBackupInfo, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() {
|
||||
if strings.HasPrefix(entry.Name(), ".") {
|
||||
continue
|
||||
}
|
||||
if entry.Type()&os.ModeSymlink != 0 || !entry.IsDir() {
|
||||
return nil, ErrTransactionInvalid
|
||||
}
|
||||
runtime, manifest, loadErr := loadConfigBackup(filepath.Join(backupsDirectory, entry.Name()), s.validation)
|
||||
|
||||
@@ -324,7 +324,8 @@ func secureManagerConfigDirectory(root string) (string, error) {
|
||||
if err != nil || info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
return "", ErrManagerStorage
|
||||
}
|
||||
// 鐩綍鏉冮檺 0700 鏄厤缃枃浠剁殑鏈€灏忔潈闄愯姹傦紱G302 灏嗙洰褰曡鍒や负鏂囦欢銆? // #nosec G302
|
||||
// 目录权限 0700 是配置目录的最低要求;G302 将目录误判为文件。
|
||||
// #nosec G302
|
||||
if err := os.Chmod(directory, 0700); err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -344,7 +345,8 @@ func secureBackupDirectory(root string) (string, error) {
|
||||
if err != nil || info.Mode()&os.ModeSymlink != 0 || !info.IsDir() {
|
||||
return "", ErrManagerStorage
|
||||
}
|
||||
// 澶囦唤鐩綍鍙厑璁告湇鍔¤处鎴疯闂紱G302 灏嗙洰褰曡鍒や负鏂囦欢銆? // #nosec G302
|
||||
// 备份目录只允许服务账户访问;G302 将目录误判为文件。
|
||||
// #nosec G302
|
||||
if err := os.Chmod(directory, 0700); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -343,6 +343,12 @@ func (m *ConfigManager) apply(ctx context.Context, generation uint64, actor Audi
|
||||
if err := ValidateRuntimeConfig(candidate, m.options); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ValidateRuntimeSecrets(m.vault, current); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ValidateRuntimeSecrets(m.vault, candidate); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, _, err := m.transactions.CreateBackupIfDue(current, time.Now().UTC()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -405,7 +411,11 @@ func (m *ConfigManager) PreviewRollback(ctx context.Context, generation uint64,
|
||||
if pending || generation >= currentGeneration {
|
||||
return nil, ErrGenerationConflict
|
||||
}
|
||||
if _, err := m.transactions.LoadBackup(generation); err != nil {
|
||||
backup, err := m.transactions.LoadBackup(generation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ValidateRuntimeSecrets(m.vault, backup); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &RollbackPreview{
|
||||
@@ -436,7 +446,11 @@ func (m *ConfigManager) IssueRollbackTicket(ctx context.Context, generation uint
|
||||
if pending || generation >= currentGeneration {
|
||||
return "", ErrGenerationConflict
|
||||
}
|
||||
if _, err := m.transactions.LoadBackup(generation); err != nil {
|
||||
backup, err := m.transactions.LoadBackup(generation)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := ValidateRuntimeSecrets(m.vault, backup); err != nil {
|
||||
return "", err
|
||||
}
|
||||
token, err := newRollbackTicketID()
|
||||
@@ -501,6 +515,9 @@ func (m *ConfigManager) rollback(ctx context.Context, generation uint64, actor A
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ValidateRuntimeSecrets(m.vault, candidate); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
setCandidateGeneration(&candidate, current.Generation+1)
|
||||
candidate.Root.UpdatedAt = time.Now().UTC()
|
||||
if err := ValidateRuntimeConfig(candidate, m.options); err != nil {
|
||||
|
||||
@@ -264,6 +264,91 @@ func TestConfigBackupRejectsTamperedPartition(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigManagerRejectsUndecryptableRollbackBackup(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
current := validMySQLRuntimeConfig(t, root)
|
||||
writeRuntimeFixture(t, root, current)
|
||||
options := ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}
|
||||
manager, err := NewConfigManager(root, options)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
updatedEmail := manager.Snapshot().Email
|
||||
updatedEmail.Policy.MaxMessageBytes = 8 << 20
|
||||
payload, err := json.Marshal(updatedEmail)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
updated, err := manager.Update(context.Background(), UpdateRequest{
|
||||
Section: SectionEmail, ExpectedGeneration: 1, Payload: payload, Actor: authorizedConfigActor(),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := manager.Apply(context.Background(), updated.Generation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
entries, err := os.ReadDir(filepath.Join(root, "config", "backups-v2"))
|
||||
if err != nil || len(entries) != 1 {
|
||||
t.Fatalf("backup entries = %v, err = %v", entries, err)
|
||||
}
|
||||
backupDirectory := filepath.Join(root, "config", "backups-v2", entries[0].Name())
|
||||
rootPath := filepath.Join(backupDirectory, "files", "config.json")
|
||||
rootData, err := os.ReadFile(rootPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var backupRoot RootConfig
|
||||
if err := ParseStrict(rootData, &backupRoot); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
foreignRoot := t.TempDir()
|
||||
foreignVault, err := credential.NewVault(foreignRoot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
foreignCiphertext, err := foreignVault.Encrypt("foreign-database-secret")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
backupRoot.Database.Password.Ciphertext = foreignCiphertext
|
||||
rootData, err = json.Marshal(backupRoot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(rootPath, rootData, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
manifestPath := filepath.Join(backupDirectory, "backup.json")
|
||||
manifestData, err := os.ReadFile(manifestPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var manifest ConfigBackupManifest
|
||||
if err := ParseStrict(manifestData, &manifest); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
manifest.Files["config.json"] = SectionDigest(rootData)
|
||||
manifestData, err = json.Marshal(manifest)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(manifestPath, manifestData, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
actor := authorizedConfigActor()
|
||||
if _, err := manager.PreviewRollback(context.Background(), 1, actor); !errors.Is(err, ErrSecretUnavailable) {
|
||||
t.Fatalf("undecryptable backup preview error = %v, want secret unavailable", err)
|
||||
}
|
||||
if _, err := manager.RollbackAuthorized(context.Background(), 1, actor, StepUpConfirmation{Confirmed: true, Method: "webauthn"}); !errors.Is(err, ErrSecretUnavailable) {
|
||||
t.Fatalf("undecryptable backup rollback error = %v, want secret unavailable", err)
|
||||
}
|
||||
if manager.Snapshot().Generation != 2 {
|
||||
t.Fatalf("failed rollback changed active generation to %d", manager.Snapshot().Generation)
|
||||
}
|
||||
}
|
||||
func TestConfigBackupRetentionKeepsFourRecentBackups(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
runtime := validRuntimeConfig()
|
||||
@@ -782,6 +867,139 @@ func TestConfigBackupScheduleUsesSevenDayInterval(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigBackupScheduleCreatesSameGenerationBackupWhenDue(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
runtime := validRuntimeConfig()
|
||||
store := NewTransactionStore(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true})
|
||||
firstAt := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
firstPath, created, err := store.CreateBackupIfDue(runtime, firstAt)
|
||||
if err != nil || !created || firstPath == "" {
|
||||
t.Fatalf("first backup path=%q created=%v err=%v", firstPath, created, err)
|
||||
}
|
||||
secondPath, created, err := store.CreateBackupIfDue(runtime, firstAt.Add(configBackupInterval))
|
||||
if err != nil || !created || secondPath == "" || secondPath == firstPath {
|
||||
t.Fatalf("due backup path=%q created=%v err=%v, want a new backup", secondPath, created, err)
|
||||
}
|
||||
entries, err := os.ReadDir(filepath.Join(root, "config", "backups-v2"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 2 {
|
||||
t.Fatalf("same generation backup count=%d, want 2", len(entries))
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigBackupListingAndLoadingIgnoreTemporaryDirectories(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
runtime := validRuntimeConfig()
|
||||
store := NewTransactionStore(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true})
|
||||
if _, err := store.CreateBackup(runtime); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
backupDirectory := filepath.Join(root, "config", "backups-v2")
|
||||
if err := os.Mkdir(filepath.Join(backupDirectory, ".interrupted.tmp"), 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, err := store.ListBackups()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(items) != 1 || items[0].Generation != runtime.Generation {
|
||||
t.Fatalf("backup list = %+v, want generation %d", items, runtime.Generation)
|
||||
}
|
||||
loaded, err := store.LoadBackup(runtime.Generation)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if loaded.Generation != runtime.Generation {
|
||||
t.Fatalf("loaded generation=%d, want %d", loaded.Generation, runtime.Generation)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigBackupCleanupKeepsOnlyExpiredValidBackup(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
runtime := validRuntimeConfig()
|
||||
store := NewTransactionStore(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true})
|
||||
createdAt := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
if _, err := store.createBackupAt(runtime, createdAt); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
backupDirectory := filepath.Join(root, "config", "backups-v2")
|
||||
if err := cleanupConfigBackups(backupDirectory, createdAt.Add(configBackupMaxAge+time.Nanosecond), store.validation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries, err := os.ReadDir(backupDirectory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("backup count=%d, want the only valid backup retained", len(entries))
|
||||
}
|
||||
}
|
||||
func TestConfigBackupRetentionHonorsThirtyDayBoundary(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
store := NewTransactionStore(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true})
|
||||
base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||
first := validRuntimeConfig()
|
||||
if _, err := store.createBackupAt(first, base); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second := cloneRuntimeConfig(first)
|
||||
setCandidateGeneration(&second, 2)
|
||||
if _, err := store.createBackupAt(second, base.Add(configBackupMaxAge)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
backupDirectory := filepath.Join(root, "config", "backups-v2")
|
||||
entries, err := os.ReadDir(backupDirectory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 2 {
|
||||
t.Fatalf("backup count at exact age=%d, want 2", len(entries))
|
||||
}
|
||||
third := cloneRuntimeConfig(second)
|
||||
setCandidateGeneration(&third, 3)
|
||||
if _, err := store.createBackupAt(third, base.Add(configBackupMaxAge+time.Nanosecond)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries, err = os.ReadDir(backupDirectory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 2 {
|
||||
t.Fatalf("backup count after expiration=%d, want 2", len(entries))
|
||||
}
|
||||
if _, err := store.LoadBackup(1); !errors.Is(err, ErrNoBackup) {
|
||||
t.Fatalf("expired generation load error=%v, want no backup", err)
|
||||
}
|
||||
if _, err := store.LoadBackup(2); err != nil {
|
||||
t.Fatalf("boundary generation was removed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigBackupFailureDoesNotDeleteExistingValidBackup(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
runtime := validRuntimeConfig()
|
||||
store := NewTransactionStore(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true})
|
||||
if _, err := store.CreateBackup(runtime); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
invalid := cloneRuntimeConfig(runtime)
|
||||
invalid.Generation = 0
|
||||
if _, err := store.CreateBackup(invalid); err == nil {
|
||||
t.Fatal("invalid backup unexpectedly succeeded")
|
||||
}
|
||||
if _, err := store.LoadBackup(runtime.Generation); err != nil {
|
||||
t.Fatalf("existing valid backup was removed after failure: %v", err)
|
||||
}
|
||||
entries, err := os.ReadDir(filepath.Join(root, "config", "backups-v2"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("backup count after failed creation=%d, want 1", len(entries))
|
||||
}
|
||||
}
|
||||
func TestConfigManagerStatusReportsPendingRestartServices(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
writeRuntimeFixture(t, root, validRuntimeConfig())
|
||||
|
||||
@@ -29,6 +29,8 @@ var (
|
||||
ErrMigrationTargetExists = errors.New("partitioned configuration files already exist")
|
||||
)
|
||||
|
||||
const legacyMigrationSourceCopy = "config.json.legacy"
|
||||
|
||||
type LegacyMigrationPlan struct {
|
||||
SourceDigest string
|
||||
TargetGeneration uint64
|
||||
@@ -178,6 +180,9 @@ func MigrateLegacyConfig(root string, options RuntimeValidationOptions) (LegacyM
|
||||
if err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
if err := preserveLegacyMigrationSource(directory, legacyData); err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
}
|
||||
files, err := marshalRuntimeFilesWithPluginSecrets(runtime, true)
|
||||
if err != nil {
|
||||
return LegacyMigrationResult{}, err
|
||||
@@ -324,8 +329,24 @@ func equivalentLegacyMigrationSection(section Section, expected, existing []byte
|
||||
}
|
||||
}
|
||||
return reflect.DeepEqual(expectedConfig.Meta, existingConfig.Meta), nil
|
||||
case SectionEmail, SectionSSL:
|
||||
return false, nil
|
||||
case SectionEmail:
|
||||
var expectedConfig, existingConfig EmailSectionConfig
|
||||
if err := ParseStrict(expected, &expectedConfig); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := ParseStrict(existing, &existingConfig); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return reflect.DeepEqual(expectedConfig, existingConfig), nil
|
||||
case SectionSSL:
|
||||
var expectedConfig, existingConfig SSLSectionConfig
|
||||
if err := ParseStrict(expected, &expectedConfig); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := ParseStrict(existing, &existingConfig); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return reflect.DeepEqual(expectedConfig, existingConfig), nil
|
||||
default:
|
||||
return false, ErrInvalidSection
|
||||
}
|
||||
@@ -846,3 +867,17 @@ func writeLegacyMigrationBackup(directory string, data []byte, now time.Time) (s
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func preserveLegacyMigrationSource(directory string, data []byte) error {
|
||||
existing, err := readManagerConfigFile(directory, legacyMigrationSourceCopy)
|
||||
if err == nil {
|
||||
if bytes.Equal(existing, data) {
|
||||
return nil
|
||||
}
|
||||
return ErrMigrationTargetExists
|
||||
}
|
||||
if !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
return atomicfile.Write(filepath.Join(directory, legacyMigrationSourceCopy), data, 0600)
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
@@ -63,6 +64,13 @@ func TestMigrateLegacyConfigPreservesBackupAndLoadsPartitions(t *testing.T) {
|
||||
if !bytes.Equal(backup, original) {
|
||||
t.Fatal("legacy backup does not match the original configuration")
|
||||
}
|
||||
legacyCopy, err := os.ReadFile(filepath.Join(root, "config", legacyMigrationSourceCopy))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(legacyCopy, original) {
|
||||
t.Fatal("legacy source copy does not match the original configuration")
|
||||
}
|
||||
loader := NewConfigLoader(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}})
|
||||
runtime, err := loader.Load()
|
||||
if err != nil {
|
||||
@@ -71,6 +79,16 @@ func TestMigrateLegacyConfigPreservesBackupAndLoadsPartitions(t *testing.T) {
|
||||
if runtime.Root.Database.LocalRef != "smail.db" || runtime.Plugins.Plugins["wechat_push"].Secret.Ciphertext == "" {
|
||||
t.Fatalf("unexpected migrated runtime: %+v", runtime)
|
||||
}
|
||||
manager, err := NewConfigManager(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := manager.Load(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if manager.Snapshot().Generation != result.Generation {
|
||||
t.Fatalf("manager generation = %d, want %d", manager.Snapshot().Generation, result.Generation)
|
||||
}
|
||||
for _, name := range []string{"config.json", "smtp.json", "email.json", "ssl.json", "plugins.json"} {
|
||||
data, readErr := os.ReadFile(filepath.Join(root, "config", name))
|
||||
if readErr != nil {
|
||||
@@ -82,6 +100,62 @@ func TestMigrateLegacyConfigPreservesBackupAndLoadsPartitions(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateLegacyConfigResumesAfterNonSecretPartitionWrite(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
original := writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"domain": "mail.example.com",
|
||||
"domains": []string{"mail.example.com"},
|
||||
"webDomain": "mail.example.com",
|
||||
"sslType": "1",
|
||||
"SSLPrivateKeyPath": "./config/ssl/private.key",
|
||||
"SSLPublicKeyPath": "./config/ssl/public.crt",
|
||||
"isInit": true,
|
||||
"httpsEnabled": 1,
|
||||
"httpPort": 80,
|
||||
"httpsPort": 443,
|
||||
})
|
||||
var legacy legacyMigrationConfig
|
||||
if err := json.Unmarshal(original, &legacy); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runtime, manualFields, err := legacyRuntimeConfig(root, legacy)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(manualFields) != 0 {
|
||||
t.Fatalf("unexpected manual fields: %+v", manualFields)
|
||||
}
|
||||
files, err := marshalRuntimeFilesWithPluginSecrets(runtime, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
directory := filepath.Join(root, "config")
|
||||
for _, section := range []Section{SectionEmail, SectionSSL} {
|
||||
name, ok := FixedSectionFile(section)
|
||||
if !ok {
|
||||
t.Fatalf("missing fixed file for section %q", section)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(directory, name), files[name], 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := NewConfigManager(root, ConfigLoaderOptions{Validation: RuntimeValidationOptions{HasSuperAdmin: true, Production: true}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
legacyCopy, err := os.ReadFile(filepath.Join(directory, legacyMigrationSourceCopy))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(legacyCopy, original) {
|
||||
t.Fatal("resumed migration did not preserve the legacy source")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlanLegacyMigrationReportsUnknownFieldsWithoutOverwritingConfig(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
original := writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
@@ -177,6 +251,123 @@ func TestMigrateLegacyConfigReencryptsExistingLegacyCredential(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateLegacyConfigRejectsCorruptedLegacyCredential(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
original := writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"domain": "mail.example.com",
|
||||
"domains": []string{"mail.example.com"},
|
||||
"webDomain": "mail.example.com",
|
||||
"sslType": "0",
|
||||
"isInit": true,
|
||||
"httpsEnabled": 1,
|
||||
"httpPort": 80,
|
||||
"httpsPort": 443,
|
||||
"tgBotToken": "enc:v1:not-a-valid-ciphertext",
|
||||
})
|
||||
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); !errors.Is(err, ErrMigrationUnsafeValue) {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
current, err := os.ReadFile(filepath.Join(root, "config", "config.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(current, original) {
|
||||
t.Fatal("corrupted credential migration overwrote the legacy configuration")
|
||||
}
|
||||
for _, name := range []string{"smtp.json", "email.json", "ssl.json", "plugins.json"} {
|
||||
if _, err := os.Stat(filepath.Join(root, "config", name)); !os.IsNotExist(err) {
|
||||
t.Fatalf("corrupted credential migration created %s", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateLegacyConfigRejectsConflictingPreservedSource(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
original := writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"domain": "mail.example.com",
|
||||
"domains": []string{"mail.example.com"},
|
||||
"webDomain": "mail.example.com",
|
||||
"sslType": "0",
|
||||
"isInit": true,
|
||||
"httpsEnabled": 1,
|
||||
"httpPort": 80,
|
||||
"httpsPort": 443,
|
||||
})
|
||||
legacyCopyPath := filepath.Join(root, "config", legacyMigrationSourceCopy)
|
||||
conflictingCopy := []byte(`{"dbDSN":"different.db","dbType":"sqlite"}`)
|
||||
if err := os.WriteFile(legacyCopyPath, conflictingCopy, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); !errors.Is(err, ErrMigrationTargetExists) {
|
||||
t.Fatalf("error = %v", err)
|
||||
}
|
||||
current, err := os.ReadFile(filepath.Join(root, "config", "config.json"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(current, original) {
|
||||
t.Fatal("conflicting preserved source changed the legacy configuration")
|
||||
}
|
||||
storedCopy, err := os.ReadFile(legacyCopyPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(storedCopy, conflictingCopy) {
|
||||
t.Fatal("conflicting preserved source was overwritten")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateLegacyConfigRejectsRepeatedMigrationAfterSuccess(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
original := writeLegacyMigrationFixture(t, root, map[string]any{
|
||||
"dbDSN": "smail.db",
|
||||
"dbType": "sqlite",
|
||||
"domain": "mail.example.com",
|
||||
"domains": []string{"mail.example.com"},
|
||||
"webDomain": "mail.example.com",
|
||||
"sslType": "0",
|
||||
"isInit": true,
|
||||
"httpsEnabled": 1,
|
||||
"httpPort": 80,
|
||||
"httpsPort": 443,
|
||||
})
|
||||
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rootPath := filepath.Join(root, "config", "config.json")
|
||||
migrated, err := os.ReadFile(rootPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if bytes.Equal(migrated, original) {
|
||||
t.Fatal("successful migration did not replace the legacy root")
|
||||
}
|
||||
|
||||
if _, err := MigrateLegacyConfig(root, RuntimeValidationOptions{HasSuperAdmin: true, Production: true}); !errors.Is(err, ErrMigrationNotRequired) {
|
||||
t.Fatalf("repeated migration error = %v", err)
|
||||
}
|
||||
after, err := os.ReadFile(rootPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(after, migrated) {
|
||||
t.Fatal("repeated migration changed the partitioned root")
|
||||
}
|
||||
legacyCopy, err := os.ReadFile(filepath.Join(root, "config", legacyMigrationSourceCopy))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Equal(legacyCopy, original) {
|
||||
t.Fatal("repeated migration changed the preserved legacy source")
|
||||
}
|
||||
}
|
||||
func TestMigrateLegacyConfigResumesWithEquivalentExistingSecretSection(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
legacy := map[string]any{
|
||||
|
||||
@@ -143,6 +143,27 @@ func OpenStoredSecret(vault *credential.Vault, stored StoredSecret) (string, err
|
||||
return plaintext, nil
|
||||
}
|
||||
|
||||
// ValidateRuntimeSecrets 验证运行时配置中的持久化 Secret 可以由当前密钥解密。
|
||||
func ValidateRuntimeSecrets(vault *credential.Vault, runtime RuntimeConfig) error {
|
||||
for _, stored := range []StoredSecret{runtime.Root.Database.Password, runtime.SMTP.Relay.Password} {
|
||||
if !stored.Configured() {
|
||||
continue
|
||||
}
|
||||
if _, err := OpenStoredSecret(vault, stored); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, plugin := range runtime.Plugins.Plugins {
|
||||
if !plugin.Secret.Configured() {
|
||||
continue
|
||||
}
|
||||
if _, err := OpenStoredSecret(vault, plugin.Secret); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateStoredSecret(stored StoredSecret, required bool) error {
|
||||
if !stored.Configured() {
|
||||
if stored.Ciphertext != "" || stored.Version != 0 || !stored.UpdatedAt.IsZero() || required {
|
||||
|
||||
@@ -135,3 +135,41 @@ func TestSecretJSONOutputIsRedacted(t *testing.T) {
|
||||
t.Fatal("secret status exposed ciphertext")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateRuntimeSecretsChecksPluginSecrets(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
vault, err := credential.NewVault(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pluginSecret, err := ApplySecretChange(vault, StoredSecret{}, SecretChange{
|
||||
Operation: SecretReplace,
|
||||
Value: &SecretInput{Plaintext: "plugin-secret"},
|
||||
}, SecretPolicy{Required: true, MaxLength: 4096}, time.Now().UTC())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runtime := validRuntimeConfig()
|
||||
plugin := runtime.Plugins.Plugins["telegram_push"]
|
||||
plugin.Secret = pluginSecret
|
||||
runtime.Plugins.Plugins["telegram_push"] = plugin
|
||||
if err := ValidateRuntimeSecrets(vault, runtime); err != nil {
|
||||
t.Fatalf("valid plugin secret was rejected: %v", err)
|
||||
}
|
||||
|
||||
tampered := runtime
|
||||
tamperedPlugin := tampered.Plugins.Plugins["telegram_push"]
|
||||
last := len(tamperedPlugin.Secret.Ciphertext) - 1
|
||||
if last < 0 {
|
||||
t.Fatal("plugin secret ciphertext is empty")
|
||||
}
|
||||
replacement := byte('A')
|
||||
if tamperedPlugin.Secret.Ciphertext[last] == replacement {
|
||||
replacement = 'B'
|
||||
}
|
||||
tamperedPlugin.Secret.Ciphertext = tamperedPlugin.Secret.Ciphertext[:last] + string(replacement)
|
||||
tampered.Plugins.Plugins["telegram_push"] = tamperedPlugin
|
||||
if err := ValidateRuntimeSecrets(vault, tampered); !errors.Is(err, ErrSecretUnavailable) {
|
||||
t.Fatalf("tampered plugin secret error = %v, want ErrSecretUnavailable", err)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -25,6 +26,7 @@ const (
|
||||
)
|
||||
|
||||
type ConfigAuditEvent struct {
|
||||
ID string `json:"id"`
|
||||
Operation ConfigOperation `json:"operation"`
|
||||
Section Section `json:"section,omitempty"`
|
||||
ActorID string `json:"actorId"`
|
||||
@@ -37,7 +39,6 @@ type ConfigAuditEvent struct {
|
||||
Result string `json:"result"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
type ConfigAuditWriter interface {
|
||||
WriteConfigAudit(context.Context, ConfigAuditEvent) error
|
||||
}
|
||||
@@ -64,12 +65,63 @@ func (w *MemoryAuditWriter) WriteConfigAudit(_ context.Context, event ConfigAudi
|
||||
if w == nil {
|
||||
return ErrAuditWrite
|
||||
}
|
||||
var err error
|
||||
event, err = normalizeAuditEvent(event)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
event.ChangedFields = append([]string(nil), event.ChangedFields...)
|
||||
w.mu.Lock()
|
||||
w.events = append(w.events, event)
|
||||
w.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
func (w *MemoryAuditWriter) Query(ctx context.Context, query ConfigAuditQuery) ([]ConfigAuditEvent, error) {
|
||||
if err := auditContextError(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ValidateConfigAuditQuery(query); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if w == nil {
|
||||
return nil, ErrAuditQueryUnavailable
|
||||
}
|
||||
limit := query.Limit
|
||||
if limit <= 0 {
|
||||
limit = 1000
|
||||
}
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
result := make([]ConfigAuditEvent, 0, minAuditLimit(limit, len(w.events)))
|
||||
for _, event := range w.events {
|
||||
if !auditEventMatches(event, query) {
|
||||
continue
|
||||
}
|
||||
copyEvent := event
|
||||
copyEvent.ChangedFields = append([]string(nil), event.ChangedFields...)
|
||||
result = append(result, copyEvent)
|
||||
if len(result) >= limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (w *MemoryAuditWriter) ExportJSON(ctx context.Context, query ConfigAuditQuery) ([]byte, error) {
|
||||
events, err := w.Query(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return json.Marshal(events)
|
||||
}
|
||||
|
||||
func (w *MemoryAuditWriter) ExportCSV(ctx context.Context, query ConfigAuditQuery) ([]byte, error) {
|
||||
events, err := w.Query(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return exportAuditCSV(events)
|
||||
}
|
||||
|
||||
func (w *MemoryAuditWriter) Events() []ConfigAuditEvent {
|
||||
if w == nil {
|
||||
|
||||
@@ -594,7 +594,7 @@ func (s *TransactionStore) CreateBackupIfDue(runtime RuntimeConfig, now time.Tim
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
if found && (latest.Generation == runtime.Generation || now.Before(latest.CreatedAt.Add(configBackupInterval))) {
|
||||
if found && now.Before(latest.CreatedAt.Add(configBackupInterval)) {
|
||||
return filepath.Join(s.root, "config", "backups-v2", latest.ID), false, nil
|
||||
}
|
||||
path, err := s.createBackupAt(runtime, now)
|
||||
@@ -728,10 +728,10 @@ func (s *TransactionStore) LoadBackup(generation uint64) (RuntimeConfig, error)
|
||||
var selectedAt time.Time
|
||||
found := false
|
||||
for _, entry := range entries {
|
||||
if strings.HasPrefix(entry.Name(), ".") {
|
||||
continue
|
||||
}
|
||||
if entry.Type()&os.ModeSymlink != 0 || !entry.IsDir() {
|
||||
if strings.HasPrefix(entry.Name(), ".") {
|
||||
continue
|
||||
}
|
||||
return RuntimeConfig{}, ErrTransactionInvalid
|
||||
}
|
||||
if !transactionIDPattern.MatchString(entry.Name()) {
|
||||
|
||||
@@ -247,7 +247,11 @@ func ConfigAuditQuery(ctx *appcontext.Context, w http.ResponseWriter, req *http.
|
||||
writeConfigAccessError(w, err)
|
||||
return
|
||||
}
|
||||
query := parseAuditQuery(req)
|
||||
query, err := parseAuditQuery(req)
|
||||
if err != nil {
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
}
|
||||
items, err := manager.AuditQuery(req.Context(), query)
|
||||
if err != nil {
|
||||
writeConfigError(w, err)
|
||||
@@ -273,11 +277,26 @@ func ExportConfigAudit(ctx *appcontext.Context, w http.ResponseWriter, req *http
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
}
|
||||
query := parseAuditQuery(req)
|
||||
query, err := parseAuditQuery(req)
|
||||
if err != nil {
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
}
|
||||
query.Limit = input.Limit
|
||||
if err := config.ValidateConfigAuditQuery(query); err != nil {
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
}
|
||||
format := strings.ToLower(strings.TrimSpace(input.Format))
|
||||
if format == "" {
|
||||
format = "json"
|
||||
}
|
||||
if format != "json" && format != "csv" {
|
||||
writeConfigError(w, config.ErrInvalidAuditFormat)
|
||||
return
|
||||
}
|
||||
var data []byte
|
||||
var err error
|
||||
if strings.EqualFold(input.Format, "csv") {
|
||||
if format == "csv" {
|
||||
data, err = manager.ExportAuditCSV(req.Context(), query)
|
||||
} else {
|
||||
data, err = manager.ExportAuditJSON(req.Context(), query)
|
||||
@@ -287,7 +306,7 @@ func ExportConfigAudit(ctx *appcontext.Context, w http.ResponseWriter, req *http
|
||||
return
|
||||
}
|
||||
contentType := "application/json; charset=utf-8"
|
||||
if strings.EqualFold(input.Format, "csv") {
|
||||
if format == "csv" {
|
||||
contentType = "text/csv; charset=utf-8"
|
||||
}
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
@@ -348,17 +367,48 @@ func configActor(ctx *appcontext.Context, req *http.Request) config.AuditActor {
|
||||
return actor
|
||||
}
|
||||
|
||||
func parseAuditQuery(req *http.Request) config.ConfigAuditQuery {
|
||||
func parseAuditQuery(req *http.Request) (config.ConfigAuditQuery, error) {
|
||||
query := req.URL.Query()
|
||||
result := config.ConfigAuditQuery{ActorID: strings.TrimSpace(query.Get("actorId")), Operation: config.ConfigOperation(strings.TrimSpace(query.Get("operation"))), Section: config.Section(strings.TrimSpace(query.Get("section")))}
|
||||
result.Limit, _ = strconv.Atoi(query.Get("limit"))
|
||||
if value, err := time.Parse(time.RFC3339, query.Get("from")); err == nil {
|
||||
for key, values := range query {
|
||||
if len(values) != 1 {
|
||||
return config.ConfigAuditQuery{}, config.ErrInvalidAuditQuery
|
||||
}
|
||||
switch key {
|
||||
case "actorId", "operation", "section", "limit", "from", "to":
|
||||
default:
|
||||
return config.ConfigAuditQuery{}, config.ErrInvalidAuditQuery
|
||||
}
|
||||
}
|
||||
result := config.ConfigAuditQuery{
|
||||
ActorID: strings.TrimSpace(query.Get("actorId")),
|
||||
Operation: config.ConfigOperation(strings.TrimSpace(query.Get("operation"))),
|
||||
Section: config.Section(strings.TrimSpace(query.Get("section"))),
|
||||
}
|
||||
if rawLimit := strings.TrimSpace(query.Get("limit")); rawLimit != "" {
|
||||
limit, err := strconv.Atoi(rawLimit)
|
||||
if err != nil {
|
||||
return config.ConfigAuditQuery{}, config.ErrInvalidAuditQuery
|
||||
}
|
||||
result.Limit = limit
|
||||
}
|
||||
if rawFrom := strings.TrimSpace(query.Get("from")); rawFrom != "" {
|
||||
value, err := time.Parse(time.RFC3339, rawFrom)
|
||||
if err != nil {
|
||||
return config.ConfigAuditQuery{}, config.ErrInvalidAuditQuery
|
||||
}
|
||||
result.From = value
|
||||
}
|
||||
if value, err := time.Parse(time.RFC3339, query.Get("to")); err == nil {
|
||||
if rawTo := strings.TrimSpace(query.Get("to")); rawTo != "" {
|
||||
value, err := time.Parse(time.RFC3339, rawTo)
|
||||
if err != nil {
|
||||
return config.ConfigAuditQuery{}, config.ErrInvalidAuditQuery
|
||||
}
|
||||
result.To = value
|
||||
}
|
||||
return result
|
||||
if err := config.ValidateConfigAuditQuery(result); err != nil {
|
||||
return config.ConfigAuditQuery{}, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func publicConfigSection(runtime config.RuntimeConfig, section config.Section) (any, error) {
|
||||
@@ -407,11 +457,18 @@ func publicConfigSection(runtime config.RuntimeConfig, section config.Section) (
|
||||
case config.SectionPlugins:
|
||||
plugins := make(map[string]any, len(runtime.Plugins.Plugins))
|
||||
for id, item := range runtime.Plugins.Plugins {
|
||||
settings := any(nil)
|
||||
if len(item.Settings) > 0 {
|
||||
var value any
|
||||
if err := config.ParseStrict(item.Settings, &value); err == nil {
|
||||
settings = value
|
||||
}
|
||||
}
|
||||
plugins[id] = map[string]any{
|
||||
"enabled": item.Enabled,
|
||||
"executable": item.Executable,
|
||||
"permissions": item.Permissions,
|
||||
"settings": nil,
|
||||
"settings": settings,
|
||||
"secretStatus": publicSecret(item.Secret),
|
||||
}
|
||||
}
|
||||
@@ -420,23 +477,3 @@ func publicConfigSection(runtime config.RuntimeConfig, section config.Section) (
|
||||
return nil, errors.New("invalid configuration section")
|
||||
}
|
||||
}
|
||||
|
||||
func writeConfigAccessError(w http.ResponseWriter, err error) { writeConfigError(w, err) }
|
||||
func writeConfigError(w http.ResponseWriter, err error) {
|
||||
status := response.NoAccessPrivileges
|
||||
message := "无权访问或请求不满足安全条件"
|
||||
var data any = ""
|
||||
var denial *access.Denial
|
||||
if errors.As(err, &denial) {
|
||||
data = map[string]string{"code": string(denial.Code)}
|
||||
}
|
||||
if errors.Is(err, config.ErrManagerStorage) {
|
||||
status = response.ServerError
|
||||
message = "配置服务不可用"
|
||||
}
|
||||
if errors.Is(err, config.ErrGenerationConflict) {
|
||||
status = response.ParamsError
|
||||
message = "配置已发生变化,请刷新后重试"
|
||||
}
|
||||
response.NewErrorResponse(status, message, data).FPrint(w)
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// GetConfigAuditDetail 返回一条已脱敏的配置审计事件。
|
||||
func GetConfigAuditDetail(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) {
|
||||
manager, ok := configManagerOrError(w)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := authorizeCapability(ctx, access.CapabilityAuditRead, "", false); err != nil {
|
||||
writeConfigAccessError(w, err)
|
||||
return
|
||||
}
|
||||
parts := strings.Split(strings.Trim(req.URL.Path, "/"), "/")
|
||||
if len(parts) != 5 || parts[0] != "api" || parts[1] != "v1" || parts[2] != "audits" || parts[3] != "config" || strings.TrimSpace(parts[4]) == "" {
|
||||
writeConfigError(w, config.ErrInvalidAuditQuery)
|
||||
return
|
||||
}
|
||||
event, err := manager.AuditDetail(req.Context(), parts[4])
|
||||
if err != nil {
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(event).FPrint(w)
|
||||
}
|
||||
|
||||
type configAuditExportRequest struct {
|
||||
Format string `json:"format"`
|
||||
ID string `json:"id"`
|
||||
ActorID string `json:"actorId"`
|
||||
Operation string `json:"operation"`
|
||||
Section string `json:"section"`
|
||||
Limit int `json:"limit"`
|
||||
From string `json:"from"`
|
||||
To string `json:"to"`
|
||||
}
|
||||
|
||||
// ExportConfigAuditFormal 实现正式配置审计导出接口。
|
||||
func ExportConfigAuditFormal(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) {
|
||||
manager, ok := configManagerOrError(w)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := authorizeCapability(ctx, access.CapabilityAuditExport, "", false); err != nil {
|
||||
writeConfigAccessError(w, err)
|
||||
return
|
||||
}
|
||||
var input configAuditExportRequest
|
||||
if err := decodeJSONRequest(w, req, &input); err != nil {
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
}
|
||||
query, err := auditExportQuery(input)
|
||||
if err != nil {
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
}
|
||||
format := strings.ToLower(strings.TrimSpace(input.Format))
|
||||
if format == "" {
|
||||
format = "json"
|
||||
}
|
||||
if format != "json" && format != "csv" {
|
||||
writeConfigError(w, config.ErrInvalidAuditFormat)
|
||||
return
|
||||
}
|
||||
var data []byte
|
||||
if format == "csv" {
|
||||
data, err = manager.ExportAuditCSV(req.Context(), query)
|
||||
} else {
|
||||
data, err = manager.ExportAuditJSON(req.Context(), query)
|
||||
}
|
||||
if err != nil {
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.Header().Set("Content-Disposition", `attachment; filename="config-audit.`+format+`"`)
|
||||
if format == "csv" {
|
||||
w.Header().Set("Content-Type", "text/csv; charset=utf-8")
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
}
|
||||
_, _ = w.Write(data)
|
||||
}
|
||||
|
||||
func auditExportQuery(input configAuditExportRequest) (config.ConfigAuditQuery, error) {
|
||||
query := config.ConfigAuditQuery{
|
||||
ID: strings.TrimSpace(input.ID),
|
||||
ActorID: strings.TrimSpace(input.ActorID),
|
||||
Operation: config.ConfigOperation(strings.TrimSpace(input.Operation)),
|
||||
Section: config.Section(strings.TrimSpace(input.Section)),
|
||||
Limit: input.Limit,
|
||||
}
|
||||
if raw := strings.TrimSpace(input.From); raw != "" {
|
||||
value, err := time.Parse(time.RFC3339, raw)
|
||||
if err != nil {
|
||||
return config.ConfigAuditQuery{}, config.ErrInvalidAuditQuery
|
||||
}
|
||||
query.From = value
|
||||
}
|
||||
if raw := strings.TrimSpace(input.To); raw != "" {
|
||||
value, err := time.Parse(time.RFC3339, raw)
|
||||
if err != nil {
|
||||
return config.ConfigAuditQuery{}, config.ErrInvalidAuditQuery
|
||||
}
|
||||
query.To = value
|
||||
}
|
||||
if err := config.ValidateConfigAuditQuery(query); err != nil {
|
||||
return config.ConfigAuditQuery{}, err
|
||||
}
|
||||
return query, nil
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
)
|
||||
|
||||
func TestParseAuditQueryRejectsUnknownAndMalformedParameters(t *testing.T) {
|
||||
tests := []string{
|
||||
"/api/v1/audits/config?unknown=value",
|
||||
"/api/v1/audits/config?limit=not-a-number",
|
||||
"/api/v1/audits/config?from=not-a-time",
|
||||
"/api/v1/audits/config?operation=unknown",
|
||||
"/api/v1/audits/config?limit=10&limit=20",
|
||||
}
|
||||
for _, target := range tests {
|
||||
request := httptest.NewRequest("GET", target, nil)
|
||||
if _, err := parseAuditQuery(request); !errors.Is(err, config.ErrInvalidAuditQuery) {
|
||||
t.Fatalf("target %s returned %v, want invalid audit query", target, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseAuditQueryAcceptsSupportedParameters(t *testing.T) {
|
||||
request := httptest.NewRequest("GET", "/api/v1/audits/config?actorId=42&operation=update§ion=smtp&limit=10&from=2026-08-01T00:00:00Z&to=2026-08-30T00:00:00Z", nil)
|
||||
query, err := parseAuditQuery(request)
|
||||
if err != nil {
|
||||
t.Fatalf("valid audit query rejected: %v", err)
|
||||
}
|
||||
if query.ActorID != "42" || query.Limit != 10 || string(query.Operation) != "update" || string(query.Section) != "smtp" {
|
||||
t.Fatalf("unexpected parsed query: %+v", query)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuditExportQueryRejectsInvalidStableID(t *testing.T) {
|
||||
query, err := auditExportQuery(configAuditExportRequest{ID: "audit-not-valid"})
|
||||
if !errors.Is(err, config.ErrInvalidAuditQuery) {
|
||||
t.Fatalf("invalid export ID returned query=%+v error=%v", query, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuditExportQueryDefaultsLimitAndParsesTimeRange(t *testing.T) {
|
||||
query, err := auditExportQuery(configAuditExportRequest{
|
||||
ActorID: "42",
|
||||
From: "2026-08-01T00:00:00Z",
|
||||
To: "2026-08-30T00:00:00Z",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if query.ActorID != "42" || query.From.IsZero() || query.To.IsZero() || query.Limit != 0 {
|
||||
t.Fatalf("unexpected export query: %+v", query)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/dto/response"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
"git.pcux.cn/xuans/Smail/services/dbmigration"
|
||||
)
|
||||
|
||||
var requestIDPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
|
||||
// EnsureConfigRequestID 为配置 API 设置可回溯但不包含敏感信息的请求 ID.
|
||||
func EnsureConfigRequestID(w http.ResponseWriter, req *http.Request) string {
|
||||
requestID := ""
|
||||
if req != nil {
|
||||
requestID = strings.TrimSpace(req.Header.Get("X-Request-ID"))
|
||||
}
|
||||
if !requestIDPattern.MatchString(requestID) {
|
||||
requestID = newConfigRequestID()
|
||||
}
|
||||
w.Header().Set("X-Request-ID", requestID)
|
||||
return requestID
|
||||
}
|
||||
|
||||
func newConfigRequestID() string {
|
||||
value := make([]byte, 16)
|
||||
if _, err := rand.Read(value); err != nil {
|
||||
return "config-request"
|
||||
}
|
||||
return hex.EncodeToString(value)
|
||||
}
|
||||
|
||||
func configErrorDetails(err error) (string, int, string, map[string]string) {
|
||||
code := "CONFIG_REQUEST_FAILED"
|
||||
status := http.StatusBadRequest
|
||||
message := "配置请求未完成"
|
||||
fields := map[string]string(nil)
|
||||
|
||||
var denial *access.Denial
|
||||
switch {
|
||||
case errors.As(err, &denial):
|
||||
code = string(denial.Code)
|
||||
status = http.StatusForbidden
|
||||
message = "无权访问或请求不满足安全条件"
|
||||
case errors.Is(err, config.ErrManagerStorage):
|
||||
code = "CONFIG_MANAGER_UNAVAILABLE"
|
||||
status = http.StatusServiceUnavailable
|
||||
message = "配置服务暂时不可用,请稍后重试"
|
||||
case errors.Is(err, config.ErrGenerationConflict):
|
||||
code = "CONFIG_GENERATION_CONFLICT"
|
||||
status = http.StatusConflict
|
||||
message = "配置已发生变化,请刷新后重试"
|
||||
case errors.Is(err, config.ErrAuthorizationRequired):
|
||||
code = "CONFIG_AUTHORIZATION_REQUIRED"
|
||||
status = http.StatusForbidden
|
||||
message = "当前账户没有执行此配置操作的权限"
|
||||
case errors.Is(err, config.ErrStepUpRequired):
|
||||
code = "CONFIG_STEP_UP_REQUIRED"
|
||||
status = http.StatusPreconditionRequired
|
||||
message = "请完成近期重新认证或 WebAuthn 二次确认"
|
||||
case errors.Is(err, config.ErrAuditWrite):
|
||||
code = "CONFIG_AUDIT_WRITE_FAILED"
|
||||
status = http.StatusServiceUnavailable
|
||||
message = "配置审计暂时不可用,操作未完成"
|
||||
case errors.Is(err, config.ErrBackupListingUnavailable), errors.Is(err, config.ErrAuditQueryUnavailable), errors.Is(err, config.ErrAuditStorageUnavailable), errors.Is(err, config.ErrConfigRecoveryRequired):
|
||||
code = "CONFIG_READ_UNAVAILABLE"
|
||||
status = http.StatusServiceUnavailable
|
||||
message = "配置历史数据暂时不可用,请稍后重试"
|
||||
case errors.Is(err, config.ErrNoBackup):
|
||||
code = "CONFIG_BACKUP_NOT_FOUND"
|
||||
status = http.StatusNotFound
|
||||
message = "未找到可用的配置备份"
|
||||
case errors.Is(err, config.ErrAuditNotFound):
|
||||
code = "CONFIG_AUDIT_NOT_FOUND"
|
||||
status = http.StatusNotFound
|
||||
message = "未找到指定的配置审计记录"
|
||||
case errors.Is(err, dbmigration.ErrMigrationNotFound):
|
||||
code = "DATABASE_MIGRATION_NOT_FOUND"
|
||||
status = http.StatusNotFound
|
||||
message = "未找到指定的数据库迁移记录"
|
||||
case errors.Is(err, dbmigration.ErrPreviewExpired):
|
||||
code = "DATABASE_MIGRATION_PREVIEW_EXPIRED"
|
||||
status = http.StatusConflict
|
||||
message = "数据库迁移预览已过期,请重新生成预览"
|
||||
case errors.Is(err, dbmigration.ErrExecutorUnavailable):
|
||||
code = "DATABASE_MIGRATION_UNAVAILABLE"
|
||||
status = http.StatusServiceUnavailable
|
||||
message = "数据库迁移执行器暂未配置,请先完成服务部署"
|
||||
case errors.Is(err, dbmigration.ErrMigrationState):
|
||||
code = "DATABASE_MIGRATION_STATE_INVALID"
|
||||
status = http.StatusConflict
|
||||
message = "数据库迁移当前状态不允许执行该操作"
|
||||
case errors.Is(err, dbmigration.ErrPreviewInvalid):
|
||||
code = "DATABASE_MIGRATION_PREVIEW_INVALID"
|
||||
status = http.StatusForbidden
|
||||
message = "数据库迁移预览票据无效"
|
||||
case errors.Is(err, dbmigration.ErrInvalidRequest), errors.Is(err, dbmigration.ErrInvalidTargetName), errors.Is(err, dbmigration.ErrImmutableSetting), errors.Is(err, dbmigration.ErrSourceDatabase):
|
||||
code = "DATABASE_MIGRATION_VALIDATION_FAILED"
|
||||
status = http.StatusUnprocessableEntity
|
||||
message = "数据库迁移参数校验未通过"
|
||||
case errors.Is(err, config.ErrInvalidRuntimeConfig), errors.Is(err, config.ErrPortConflict), errors.Is(err, config.ErrInvalidDomainConfig), errors.Is(err, config.ErrPluginCapability), errors.Is(err, config.ErrInvalidSetupState), errors.Is(err, config.ErrInvalidDatabase), errors.Is(err, config.ErrInvalidSMTP), errors.Is(err, config.ErrInvalidSSL), errors.Is(err, config.ErrInvalidPlugin), errors.Is(err, config.ErrInvalidHTTP), errors.Is(err, config.ErrInvalidEmail), errors.Is(err, config.ErrInvalidLogLevel), errors.Is(err, config.ErrInvalidSecretChange), errors.Is(err, config.ErrSecretClearDenied), errors.Is(err, config.ErrSecretUnavailable), errors.Is(err, config.ErrInvalidSection), errors.Is(err, config.ErrInvalidSectionReference), errors.Is(err, config.ErrSectionGenerationMismatch), errors.Is(err, config.ErrInvalidSSLMode), errors.Is(err, config.ErrInvalidSSLMaterial), errors.Is(err, config.ErrInvalidSSLReference), errors.Is(err, config.ErrUnsafeSSLFile), errors.Is(err, config.ErrSSLCertificateMismatch), errors.Is(err, config.ErrSSLCertificateDomain), errors.Is(err, config.ErrSSLCertificateExpired), errors.Is(err, config.ErrSSLPrivateKeyPermission), errors.Is(err, config.ErrInvalidAuditQuery), errors.Is(err, config.ErrInvalidAuditFormat):
|
||||
code = "CONFIG_VALIDATION_FAILED"
|
||||
status = http.StatusUnprocessableEntity
|
||||
message = "配置校验未通过,请检查填写内容"
|
||||
case errors.Is(err, config.ErrTransactionInvalid), errors.Is(err, config.ErrTransactionPhase), errors.Is(err, config.ErrConfigDigestMismatch), errors.Is(err, config.ErrConfigGenerationMismatch):
|
||||
code = "CONFIG_TRANSACTION_FAILED"
|
||||
status = http.StatusServiceUnavailable
|
||||
message = "配置事务未完成,系统仍保持原有配置"
|
||||
default:
|
||||
fields = map[string]string{"request": "invalid"}
|
||||
}
|
||||
|
||||
return code, status, message, fields
|
||||
}
|
||||
|
||||
func writeConfigError(w http.ResponseWriter, err error) {
|
||||
requestID := strings.TrimSpace(w.Header().Get("X-Request-ID"))
|
||||
if !requestIDPattern.MatchString(requestID) {
|
||||
requestID = EnsureConfigRequestID(w, nil)
|
||||
}
|
||||
code, status, message, fields := configErrorDetails(err)
|
||||
data := response.ConfigErrorData{Code: code, RequestID: requestID, Fields: fields}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
response.NewErrorResponse(response.ConfigErrorNumber(status), message, data).FPrint(w)
|
||||
}
|
||||
|
||||
func writeConfigAccessError(w http.ResponseWriter, err error) {
|
||||
writeConfigError(w, err)
|
||||
}
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/dto/response"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
)
|
||||
@@ -40,3 +42,51 @@ func TestWriteConfigErrorDoesNotExposeInternalErrorText(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureConfigRequestIDPreservesSafeHeader(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest("GET", "/api/v1/config/status", nil)
|
||||
request.Header.Set("X-Request-ID", "ui-request-42")
|
||||
if got := EnsureConfigRequestID(recorder, request); got != "ui-request-42" {
|
||||
t.Fatalf("safe request id was not preserved: %q", got)
|
||||
}
|
||||
if recorder.Header().Get("X-Request-ID") != "ui-request-42" {
|
||||
t.Fatalf("response request id header was not preserved: %q", recorder.Header().Get("X-Request-ID"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureConfigRequestIDReplacesUnsafeHeader(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest("GET", "/api/v1/config/status", nil)
|
||||
request.Header.Set("X-Request-ID", "../../secret")
|
||||
requestID := EnsureConfigRequestID(recorder, request)
|
||||
if requestID == "../../secret" || !requestIDPattern.MatchString(requestID) {
|
||||
t.Fatalf("unsafe request id was not replaced: %q", requestID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteConfigErrorUsesStableCodeAndHTTPStatus(t *testing.T) {
|
||||
recorder := httptest.NewRecorder()
|
||||
recorder.Header().Set("X-Request-ID", "request-99")
|
||||
writeConfigError(recorder, config.ErrGenerationConflict)
|
||||
|
||||
if recorder.Code != 409 {
|
||||
t.Fatalf("unexpected HTTP status: %d", recorder.Code)
|
||||
}
|
||||
var payload struct {
|
||||
ErrorMsg string `json:"errorMsg"`
|
||||
Data response.ConfigErrorData `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatalf("failed to decode configuration error response: %v", err)
|
||||
}
|
||||
if payload.Data.Code != "CONFIG_GENERATION_CONFLICT" {
|
||||
t.Fatalf("unexpected stable error code: %q", payload.Data.Code)
|
||||
}
|
||||
if payload.Data.RequestID != "request-99" {
|
||||
t.Fatalf("unexpected request id: %q", payload.Data.RequestID)
|
||||
}
|
||||
if payload.ErrorMsg == "" || strings.Contains(payload.ErrorMsg, "generation conflict") {
|
||||
t.Fatalf("configuration error message is not a Chinese public message: %q", payload.ErrorMsg)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
)
|
||||
|
||||
func TestPublicPluginConfigIncludesNonSensitiveSettings(t *testing.T) {
|
||||
runtime := config.RuntimeConfig{
|
||||
Plugins: config.PluginsSectionConfig{
|
||||
Meta: config.SectionMeta{Schema: config.CurrentSectionSchema, Generation: 3},
|
||||
Plugins: map[string]config.PluginInstance{
|
||||
"spam_block": {
|
||||
Enabled: true,
|
||||
Executable: "spam_block.exe",
|
||||
Settings: json.RawMessage(`{"url":"https://model.example.com/v1/predict","timeout":3000,"threshold":20}`),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
public, err := publicConfigSection(runtime, config.SectionPlugins)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data, err := json.Marshal(public)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var decoded struct {
|
||||
Plugins map[string]struct {
|
||||
Settings map[string]any `json:"settings"`
|
||||
} `json:"plugins"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &decoded); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
settings, ok := decoded.Plugins["spam_block"].Settings["url"]
|
||||
if !ok || settings != "https://model.example.com/v1/predict" {
|
||||
t.Fatalf("public plugin settings were not returned: %s", data)
|
||||
}
|
||||
if _, ok := decoded.Plugins["spam_block"].Settings["password"]; ok {
|
||||
t.Fatalf("public plugin settings contain a password field: %s", data)
|
||||
}
|
||||
if _, ok := decoded.Plugins["spam_block"].Settings["secret"]; ok {
|
||||
t.Fatalf("public plugin settings contain a secret field: %s", data)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/dto/response"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
"git.pcux.cn/xuans/Smail/services/dbmigration"
|
||||
appcontext "git.pcux.cn/xuans/Smail/utils/context"
|
||||
)
|
||||
|
||||
type databaseMigrationPreviewRequest struct {
|
||||
TargetName string `json:"targetName"`
|
||||
ExpectedGeneration uint64 `json:"expectedGeneration"`
|
||||
}
|
||||
|
||||
type databaseMigrationExecuteRequest struct {
|
||||
ID string `json:"id"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
// PreviewDatabaseMigration 校验只改数据库名称的迁移计划,不连接或修改目标数据库。
|
||||
func PreviewDatabaseMigration(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if err := authorizeCapability(ctx, access.CapabilityDatabaseMigrate, "", false); err != nil {
|
||||
writeConfigAccessError(w, err)
|
||||
return
|
||||
}
|
||||
manager, ok := configManagerOrError(w)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
service := dbmigration.Current()
|
||||
if service == nil {
|
||||
writeConfigError(w, dbmigration.ErrExecutorUnavailable)
|
||||
return
|
||||
}
|
||||
var input databaseMigrationPreviewRequest
|
||||
if err := decodeJSONRequest(w, req, &input); err != nil || input.ExpectedGeneration == 0 {
|
||||
writeConfigError(w, dbmigration.ErrInvalidRequest)
|
||||
return
|
||||
}
|
||||
status := manager.Status()
|
||||
if input.ExpectedGeneration != status.ActiveGeneration {
|
||||
writeConfigError(w, config.ErrGenerationConflict)
|
||||
return
|
||||
}
|
||||
record, token, err := service.Preview(req.Context(), manager.Snapshot().Root.Database, status.ActiveGeneration, input.TargetName)
|
||||
if err != nil {
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(map[string]any{"migration": record, "token": token}).FPrint(w)
|
||||
}
|
||||
|
||||
// ExecuteDatabaseMigration 执行预览票据对应的固定迁移计划,不接受 DSN、SQL 或连接参数。
|
||||
func ExecuteDatabaseMigration(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if err := authorizeCapability(ctx, access.CapabilityDatabaseMigrate, "", false); err != nil {
|
||||
writeConfigAccessError(w, err)
|
||||
return
|
||||
}
|
||||
if dbmigration.Current() == nil {
|
||||
writeConfigError(w, dbmigration.ErrExecutorUnavailable)
|
||||
return
|
||||
}
|
||||
var input databaseMigrationExecuteRequest
|
||||
if err := decodeJSONRequest(w, req, &input); err != nil {
|
||||
writeConfigError(w, dbmigration.ErrInvalidRequest)
|
||||
return
|
||||
}
|
||||
record, err := dbmigration.Current().Execute(req.Context(), input.ID, input.Token)
|
||||
if err != nil {
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(record).FPrint(w)
|
||||
}
|
||||
|
||||
// GetDatabaseMigration 返回迁移状态,不返回密码、密钥或底层数据库错误。
|
||||
func GetDatabaseMigration(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if err := authorizeCapability(ctx, access.CapabilityDatabaseMigrate, "", false); err != nil {
|
||||
writeConfigAccessError(w, err)
|
||||
return
|
||||
}
|
||||
service := dbmigration.Current()
|
||||
if service == nil {
|
||||
writeConfigError(w, dbmigration.ErrExecutorUnavailable)
|
||||
return
|
||||
}
|
||||
parts := strings.Split(strings.Trim(req.URL.Path, "/"), "/")
|
||||
if len(parts) != 5 || parts[0] != "api" || parts[1] != "v1" || parts[2] != "database" || parts[3] != "migrations" || strings.TrimSpace(parts[4]) == "" {
|
||||
writeConfigError(w, dbmigration.ErrInvalidRequest)
|
||||
return
|
||||
}
|
||||
record, err := service.Get(parts[4])
|
||||
if err != nil {
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(record).FPrint(w)
|
||||
}
|
||||
@@ -54,10 +54,10 @@ func TestEditUserPasswordResetRoleMatrix(t *testing.T) {
|
||||
adminContext := &appcontext.Context{UserID: admin.ID, UserAccount: admin.Account, Role: access.RoleAdmin, RecentAuth: true, RecoveryCodeConfigured: true, Lang: "zhCn"}
|
||||
userContext := &appcontext.Context{UserID: user.ID, UserAccount: user.Account, Role: access.RoleUser, RecentAuth: true, RecoveryCodeConfigured: true, Lang: "zhCn"}
|
||||
|
||||
if result := editUserRequest(t, superContext, `{"id":`+itoa(admin.ID)+`,"password":"ResetAdmin123","disabled":0}`); result != 0 {
|
||||
if result := editUserRequest(t, superContext, `{"id":`+itoa(admin.ID)+`,"password":"ResetAdm12","disabled":0}`); result != 0 {
|
||||
t.Fatalf("super administrator could not reset administrator: %d", result)
|
||||
}
|
||||
assertUserPassword(t, admin.ID, "ResetAdmin123")
|
||||
assertUserPassword(t, admin.ID, "ResetAdm12")
|
||||
|
||||
if result := editUserRequest(t, adminContext, `{"id":`+itoa(user.ID)+`,"password":"ResetUser123","disabled":0}`); result != 0 {
|
||||
t.Fatalf("administrator could not reset regular user: %d", result)
|
||||
|
||||
@@ -67,6 +67,11 @@ func SettingsHtml(ctx *context.Context, w http.ResponseWriter, req *http.Request
|
||||
w.Header().Set("Allow", http.MethodGet+", "+http.MethodPost)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
} else {
|
||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||
w.WriteHeader(http.StatusGone)
|
||||
_, _ = w.Write([]byte("旧版插件配置保存接口已停用,请使用新版插件配置接口"))
|
||||
return
|
||||
}
|
||||
req.Body = http.MaxBytesReader(w, req.Body, 64<<10)
|
||||
requestData, err := io.ReadAll(req.Body)
|
||||
@@ -84,12 +89,7 @@ func SettingsHtml(ctx *context.Context, w http.ResponseWriter, req *http.Request
|
||||
response.NewErrorResponse(response.ServerError, "", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
if req.Method == http.MethodPost && content == "success" {
|
||||
if err := hooks.PersistPluginConfig(pluginName, string(requestData)); err != nil {
|
||||
response.NewErrorResponse(response.ServerError, "", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if req.Method == http.MethodGet {
|
||||
if strings.HasSuffix(resourcePath, ".js") {
|
||||
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
|
||||
|
||||
@@ -63,8 +63,11 @@ func TestSettingsHtmlSeparatesStaticAndSaveRequests(t *testing.T) {
|
||||
|
||||
postRecorder := httptest.NewRecorder()
|
||||
SettingsHtml(&context.Context{IsAdmin: true}, postRecorder, httptest.NewRequest(http.MethodPost, "/api/plugin/settings/PluginTest/save", strings.NewReader(`{"url":"https://model.example.com"}`)))
|
||||
if hook.resource != "PluginTest/save" || hook.body == "" || !strings.HasPrefix(postRecorder.Header().Get("Content-Type"), "text/plain") {
|
||||
t.Fatalf("plugin save request was not isolated: resource=%s body=%s type=%s", hook.resource, hook.body, postRecorder.Header().Get("Content-Type"))
|
||||
if postRecorder.Code != http.StatusGone {
|
||||
t.Fatalf("legacy plugin save status = %d, want %d", postRecorder.Code, http.StatusGone)
|
||||
}
|
||||
if hook.resource != "PluginTest/index.html" || hook.body != "" {
|
||||
t.Fatalf("stopped plugin save request reached the plugin: resource=%s body=%s", hook.resource, hook.body)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/dto/response"
|
||||
"git.pcux.cn/xuans/Smail/hooks"
|
||||
"git.pcux.cn/xuans/Smail/hooks/framework"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
pluginservice "git.pcux.cn/xuans/Smail/services/plugin"
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const maxTelegramUserSettingsBodyBytes int64 = 16 << 10
|
||||
|
||||
type telegramSettingsRequest struct {
|
||||
Settings pluginservice.TelegramUserSettings `json:"settings"`
|
||||
Version uint64 `json:"version"`
|
||||
}
|
||||
|
||||
func GetTelegramUserSettings(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if !authenticatedPluginUser(ctx) {
|
||||
response.NewErrorResponse(response.NeedLogin, "请先登录", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
service := hooks.PluginConfigService()
|
||||
if service == nil {
|
||||
response.NewErrorResponse(response.ServerError, "插件配置服务不可用", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
settings, err := service.GetOwnTelegramSettings(ctx.UserID)
|
||||
if err != nil {
|
||||
writeTelegramSettingsError(w, err)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(settings).FPrint(w)
|
||||
}
|
||||
|
||||
func UpdateTelegramUserSettings(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if !authenticatedPluginUser(ctx) {
|
||||
response.NewErrorResponse(response.NeedLogin, "请先登录", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
var input telegramSettingsRequest
|
||||
if !decodePluginJSONBody(w, req, &input) {
|
||||
return
|
||||
}
|
||||
if err := pluginservice.ValidateTelegramUserSettings(input.Settings); err != nil {
|
||||
writeTelegramSettingsError(w, err)
|
||||
return
|
||||
}
|
||||
service := hooks.PluginConfigService()
|
||||
if service == nil {
|
||||
response.NewErrorResponse(response.ServerError, "插件配置服务不可用", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
settings, err := service.UpdateOwnTelegramSettings(ctx.UserID, input.Settings, input.Version)
|
||||
if err != nil {
|
||||
writeTelegramSettingsError(w, err)
|
||||
return
|
||||
}
|
||||
if err := hooks.SyncTelegramUserSettings(ctx.UserID, settings.Settings); err != nil {
|
||||
log.WithError(err).Warnf("Telegram 用户设置同步失败,用户 ID=%d", ctx.UserID)
|
||||
}
|
||||
response.NewSuccessResponse(settings).FPrint(w)
|
||||
}
|
||||
|
||||
func GetTelegramBotInfo(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if !authenticatedPluginUser(ctx) {
|
||||
response.NewErrorResponse(response.NeedLogin, "请先登录", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
hook, ok := hooks.GetHook("telegram_push")
|
||||
if !ok {
|
||||
response.NewErrorResponse(response.ServerError, "Telegram 插件不可用", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
provider, ok := hook.(framework.BotInfoProvider)
|
||||
if !ok {
|
||||
response.NewErrorResponse(response.ServerError, "Telegram 插件不支持机器人信息", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
info, err := provider.GetBotInfo()
|
||||
if err != nil {
|
||||
log.WithError(err).Warn("获取 Telegram 机器人信息失败")
|
||||
response.NewErrorResponse(response.ServerError, "获取 Telegram 机器人信息失败", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(info).FPrint(w)
|
||||
}
|
||||
|
||||
func TestTelegramMessage(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if !authenticatedPluginUser(ctx) {
|
||||
response.NewErrorResponse(response.NeedLogin, "请先登录", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
var input telegramSettingsRequest
|
||||
if !decodePluginJSONBody(w, req, &input) {
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(input.Settings.ChatID) == "" {
|
||||
response.NewErrorResponse(response.ParamsError, "请输入 Telegram 聊天 ID", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
if err := pluginservice.ValidateTelegramUserSettings(input.Settings); err != nil {
|
||||
writeTelegramSettingsError(w, err)
|
||||
return
|
||||
}
|
||||
hook, ok := hooks.GetHook("telegram_push")
|
||||
if !ok {
|
||||
response.NewErrorResponse(response.ServerError, "Telegram 插件不可用", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
provider, ok := hook.(framework.TestMessageProvider)
|
||||
if !ok {
|
||||
response.NewErrorResponse(response.ServerError, "Telegram 插件不支持测试消息", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
payload, err := json.Marshal(input.Settings)
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, "测试消息参数处理失败", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
if err := provider.TestMessage(payload); err != nil {
|
||||
log.WithError(err).Warn("发送 Telegram 测试消息失败")
|
||||
response.NewErrorResponse(response.ServerError, "发送 Telegram 测试消息失败", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(map[string]string{"message": "测试消息已发送"}).FPrint(w)
|
||||
}
|
||||
|
||||
func authenticatedPluginUser(ctx *context.Context) bool {
|
||||
return ctx != nil && ctx.UserID > 0
|
||||
}
|
||||
|
||||
func decodePluginJSONBody(w http.ResponseWriter, req *http.Request, target any) bool {
|
||||
req.Body = http.MaxBytesReader(w, req.Body, maxTelegramUserSettingsBodyBytes)
|
||||
defer req.Body.Close()
|
||||
decoder := json.NewDecoder(req.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(target); err != nil {
|
||||
var maxBytesErr *http.MaxBytesError
|
||||
if errors.As(err, &maxBytesErr) {
|
||||
response.NewErrorResponse(response.ParamsError, "请求内容过大", "").FPrint(w)
|
||||
} else {
|
||||
response.NewErrorResponse(response.ParamsError, "请求参数格式不正确", "").FPrint(w)
|
||||
}
|
||||
return false
|
||||
}
|
||||
if err := decoder.Decode(&struct{}{}); !errors.Is(err, io.EOF) {
|
||||
response.NewErrorResponse(response.ParamsError, "请求参数格式不正确", "").FPrint(w)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func writeTelegramSettingsError(w http.ResponseWriter, err error) {
|
||||
if errors.Is(err, pluginservice.ErrInvalidTelegramChatID) {
|
||||
response.NewErrorResponse(response.ParamsError, "Telegram 聊天 ID 格式不正确", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
if errors.Is(err, pluginservice.ErrPluginSettingConflict) {
|
||||
response.NewErrorResponse(response.ParamsError, "配置已被其他操作更新,请刷新后重试", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
var denial *access.Denial
|
||||
if errors.As(err, &denial) {
|
||||
response.NewErrorResponse(response.NoAccessPrivileges, "无权修改插件设置", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
log.WithError(err).Error("Telegram 插件设置操作失败")
|
||||
response.NewErrorResponse(response.ServerError, "插件设置操作失败", "").FPrint(w)
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/db/migration"
|
||||
"git.pcux.cn/xuans/Smail/hooks"
|
||||
"git.pcux.cn/xuans/Smail/hooks/framework"
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
pluginservice "git.pcux.cn/xuans/Smail/services/plugin"
|
||||
appcontext "git.pcux.cn/xuans/Smail/utils/context"
|
||||
"git.pcux.cn/xuans/Smail/utils/credential"
|
||||
_ "modernc.org/sqlite"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type telegramControllerTestHook struct {
|
||||
pluginTestHook
|
||||
botInfo framework.BotInfo
|
||||
botInfoErr error
|
||||
testPayload json.RawMessage
|
||||
testMessageErr error
|
||||
userID int
|
||||
userSettings json.RawMessage
|
||||
}
|
||||
|
||||
func (h *telegramControllerTestHook) ConfigureUserSettings(userID int, settings json.RawMessage) error {
|
||||
h.userID = userID
|
||||
h.userSettings = append(h.userSettings[:0], settings...)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *telegramControllerTestHook) GetBotInfo() (framework.BotInfo, error) {
|
||||
return h.botInfo, h.botInfoErr
|
||||
}
|
||||
|
||||
func (h *telegramControllerTestHook) TestMessage(settings json.RawMessage) error {
|
||||
h.testPayload = append(h.testPayload[:0], settings...)
|
||||
return h.testMessageErr
|
||||
}
|
||||
|
||||
func TestTelegramUserSettingsRequiresLogin(t *testing.T) {
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/plugin/settings/telegram_push/user", nil)
|
||||
recorder := httptest.NewRecorder()
|
||||
GetTelegramUserSettings(nil, recorder, request)
|
||||
if recorder.Code != http.StatusOK || !strings.Contains(recorder.Body.String(), "请先登录") {
|
||||
t.Fatalf("unauthenticated GET status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
|
||||
request = httptest.NewRequest(http.MethodPut, "/api/v1/plugin/settings/telegram_push/user", strings.NewReader(`{}`))
|
||||
recorder = httptest.NewRecorder()
|
||||
UpdateTelegramUserSettings(&appcontext.Context{}, recorder, request)
|
||||
if recorder.Code != http.StatusOK || !strings.Contains(recorder.Body.String(), "请先登录") {
|
||||
t.Fatalf("unauthenticated PUT status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTelegramUserSettingsRejectsUnknownAndTrailingFields(t *testing.T) {
|
||||
hooks.SetPluginConfigService(nil)
|
||||
t.Cleanup(func() { hooks.SetPluginConfigService(nil) })
|
||||
|
||||
cases := []string{
|
||||
`{"settings":{"chatId":"123","userId":99},"version":0}`,
|
||||
`{"settings":{"chatId":"123"},"version":0} {}`,
|
||||
}
|
||||
for _, body := range cases {
|
||||
recorder := httptest.NewRecorder()
|
||||
request := httptest.NewRequest(http.MethodPut, "/api/v1/plugin/settings/telegram_push/user", bytes.NewBufferString(body))
|
||||
UpdateTelegramUserSettings(&appcontext.Context{UserID: 7}, recorder, request)
|
||||
if recorder.Code != http.StatusOK || !strings.Contains(recorder.Body.String(), "请求参数格式不正确") {
|
||||
t.Fatalf("body=%s status=%d response=%s", body, recorder.Code, recorder.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTelegramUserSettingsUsesSessionUserAndVersion(t *testing.T) {
|
||||
service, engine := newTelegramControllerConfigService(t)
|
||||
previousService := hooks.PluginConfigService()
|
||||
hooks.SetPluginConfigService(service)
|
||||
t.Cleanup(func() { hooks.SetPluginConfigService(previousService) })
|
||||
|
||||
user := models.User{Account: "controller-user", Name: "controller-user", Password: "hash"}
|
||||
if _, err := engine.Insert(&user); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
firstRecorder := httptest.NewRecorder()
|
||||
firstRequest := httptest.NewRequest(http.MethodPut, "/api/v1/plugin/settings/telegram_push/user", strings.NewReader(`{"settings":{"chatId":"-1001234567890","showContent":true},"version":0}`))
|
||||
UpdateTelegramUserSettings(&appcontext.Context{UserID: user.ID}, firstRecorder, firstRequest)
|
||||
if firstRecorder.Code != http.StatusOK || !strings.Contains(firstRecorder.Body.String(), `"version":1`) {
|
||||
t.Fatalf("first update status=%d body=%s", firstRecorder.Code, firstRecorder.Body.String())
|
||||
}
|
||||
if strings.Contains(firstRecorder.Body.String(), "bot-token") {
|
||||
t.Fatal("secret material appeared in settings response")
|
||||
}
|
||||
|
||||
staleRecorder := httptest.NewRecorder()
|
||||
staleRequest := httptest.NewRequest(http.MethodPut, "/api/v1/plugin/settings/telegram_push/user", strings.NewReader(`{"settings":{"chatId":"123"},"version":0}`))
|
||||
UpdateTelegramUserSettings(&appcontext.Context{UserID: user.ID}, staleRecorder, staleRequest)
|
||||
if staleRecorder.Code != http.StatusOK || !strings.Contains(staleRecorder.Body.String(), "配置已被其他操作更新") {
|
||||
t.Fatalf("stale update status=%d body=%s", staleRecorder.Code, staleRecorder.Body.String())
|
||||
}
|
||||
|
||||
getRecorder := httptest.NewRecorder()
|
||||
GetTelegramUserSettings(&appcontext.Context{UserID: user.ID}, getRecorder, httptest.NewRequest(http.MethodGet, "/api/v1/plugin/settings/telegram_push/user", nil))
|
||||
if getRecorder.Code != http.StatusOK || !strings.Contains(getRecorder.Body.String(), "-1001234567890") {
|
||||
t.Fatalf("GET status=%d body=%s", getRecorder.Code, getRecorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTelegramBotInfoDoesNotExposeSecret(t *testing.T) {
|
||||
previousHooks := hooks.HookList
|
||||
hook := &telegramControllerTestHook{botInfo: framework.BotInfo{Username: "smail_test_bot", BotLink: "https://t.me/smail_test_bot"}}
|
||||
hooks.HookList = map[string]framework.EmailHook{"telegram_push": hook}
|
||||
t.Cleanup(func() { hooks.HookList = previousHooks })
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
GetTelegramBotInfo(&appcontext.Context{UserID: 7}, recorder, httptest.NewRequest(http.MethodGet, "/api/v1/plugin/settings/telegram_push/bot", nil))
|
||||
if recorder.Code != http.StatusOK || !strings.Contains(recorder.Body.String(), "smail_test_bot") {
|
||||
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
||||
}
|
||||
if strings.Contains(recorder.Body.String(), "bot-token") || strings.Contains(recorder.Body.String(), "secret") {
|
||||
t.Fatalf("bot response exposed secret material: %s", recorder.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTelegramTestMessageValidatesChatIDAndDoesNotPersist(t *testing.T) {
|
||||
previousHooks := hooks.HookList
|
||||
hook := &telegramControllerTestHook{}
|
||||
hooks.HookList = map[string]framework.EmailHook{"telegram_push": hook}
|
||||
t.Cleanup(func() { hooks.HookList = previousHooks })
|
||||
|
||||
invalidRecorder := httptest.NewRecorder()
|
||||
invalidRequest := httptest.NewRequest(http.MethodPost, "/api/v1/plugin/settings/telegram_push/test", strings.NewReader(`{"settings":{"chatId":"not-a-chat"}}`))
|
||||
TestTelegramMessage(&appcontext.Context{UserID: 7}, invalidRecorder, invalidRequest)
|
||||
if invalidRecorder.Code != http.StatusOK || !strings.Contains(invalidRecorder.Body.String(), "聊天 ID 格式不正确") {
|
||||
t.Fatalf("invalid message status=%d body=%s", invalidRecorder.Code, invalidRecorder.Body.String())
|
||||
}
|
||||
if hook.testPayload != nil {
|
||||
t.Fatal("invalid test message reached plugin")
|
||||
}
|
||||
|
||||
validRecorder := httptest.NewRecorder()
|
||||
validRequest := httptest.NewRequest(http.MethodPost, "/api/v1/plugin/settings/telegram_push/test", strings.NewReader(`{"settings":{"chatId":"123"},"version":99}`))
|
||||
TestTelegramMessage(&appcontext.Context{UserID: 7}, validRecorder, validRequest)
|
||||
if validRecorder.Code != http.StatusOK || !strings.Contains(validRecorder.Body.String(), "测试消息已发送") {
|
||||
t.Fatalf("valid message status=%d body=%s", validRecorder.Code, validRecorder.Body.String())
|
||||
}
|
||||
if !strings.Contains(string(hook.testPayload), `"chatId":"123"`) {
|
||||
t.Fatalf("plugin did not receive validated settings: %s", hook.testPayload)
|
||||
}
|
||||
}
|
||||
|
||||
func newTelegramControllerConfigService(t *testing.T) (*pluginservice.ConfigService, *xorm.Engine) {
|
||||
t.Helper()
|
||||
engine, err := xorm.NewEngine("sqlite", filepath.Join(t.TempDir(), "telegram-controller.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = engine.Close() })
|
||||
runner := migration.Runner{DB: engine.DB().DB, Dialect: migration.DialectSQLite, Migrations: migration.DefaultMigrations()}
|
||||
if _, err = runner.Run(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
vault, err := credential.NewVault(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
service, err := pluginservice.NewConfigService(engine, vault)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return service, engine
|
||||
}
|
||||
@@ -3,12 +3,33 @@ package controllers
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/dto/response"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
"git.pcux.cn/xuans/Smail/services/restart"
|
||||
appcontext "git.pcux.cn/xuans/Smail/utils/context"
|
||||
)
|
||||
|
||||
// PreviewRestartSmail 返回固定的 Smail 重启预览,不执行重启动作。
|
||||
func PreviewRestartSmail(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if err := authorizeCapability(ctx, access.CapabilityServiceRestart, "", false); err != nil {
|
||||
response.NewErrorResponse(response.NoAccessPrivileges, "重启预览需要近期认证和有效离线恢复码", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
manager := config.CurrentManager()
|
||||
if manager == nil {
|
||||
response.NewErrorResponse(response.ServerError, "重启预览暂时不可用", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
status := manager.Status()
|
||||
pending := make([]string, 0, len(status.PendingRestart))
|
||||
for _, service := range status.PendingRestart {
|
||||
pending = append(pending, string(service))
|
||||
}
|
||||
preview := restart.BuildPreview(status.ActiveGeneration, pending, restart.Current() != nil)
|
||||
response.NewSuccessResponse(preview).FPrint(w)
|
||||
}
|
||||
|
||||
// RestartSmail 请求固定的 Smail 服务重启,不接受外部命令或服务参数。
|
||||
func RestartSmail(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if err := authorizeCapability(ctx, access.CapabilityServiceRestart, "", false); err != nil {
|
||||
|
||||
@@ -44,7 +44,7 @@ func newPublicSecurityFixture(t *testing.T) (*recovery.Service, *webauthnservice
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
passwordHash, err := auth.HashPassword("FixturePassword@123")
|
||||
passwordHash, err := auth.HashPassword("FixtureP12")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ func ModifyPassword(ctx *context.Context, w http.ResponseWriter, req *http.Reque
|
||||
}
|
||||
if err := auth.SetPassword(&user, retData.Password); err != nil {
|
||||
if errors.Is(err, password.ErrInvalidLength) {
|
||||
response.NewErrorResponse(response.ParamsError, "\u5bc6\u7801\u957f\u5ea6\u5fc5\u987b\u4e3a 8-256 \u5b57\u8282", "").FPrint(w)
|
||||
response.NewErrorResponse(response.ParamsError, "\u5bc6\u7801\u957f\u5ea6\u5fc5\u987b\u4e3a 8-12 \u5b57\u8282", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
log.WithContext(ctx).WithError(err).Error("password hash failed")
|
||||
|
||||
@@ -50,7 +50,7 @@ func TestModifyPasswordRequiresCurrentPassword(t *testing.T) {
|
||||
}
|
||||
|
||||
user := models.User{Account: "password-change-user", Name: "密码修改用户"}
|
||||
if err := auth.SetPassword(&user, "CorrectPass123"); err != nil {
|
||||
if err := auth.SetPassword(&user, "CorrectP12"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.Instance.Insert(&user); err != nil {
|
||||
@@ -77,11 +77,11 @@ func TestModifyPasswordRequiresCurrentPassword(t *testing.T) {
|
||||
if _, err := db.Instance.ID(user.ID).Get(&unchanged); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if unchanged.Password != originalHash || !password.Verify(unchanged.Password, "CorrectPass123") {
|
||||
if unchanged.Password != originalHash || !password.Verify(unchanged.Password, "CorrectP12") {
|
||||
t.Fatal("password was changed after current password verification failed")
|
||||
}
|
||||
|
||||
request = httptest.NewRequest(http.MethodPost, "/api/settings/modify_password", strings.NewReader(`{"current_password":"CorrectPass123","password":"NewPass123"}`))
|
||||
request = httptest.NewRequest(http.MethodPost, "/api/settings/modify_password", strings.NewReader(`{"current_password":"CorrectP12","password":"NewPass123"}`))
|
||||
loadedContext, err := session.Instance.Load(request.Context(), "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -86,7 +86,7 @@ func CreateUser(ctx *context.Context, w http.ResponseWriter, req *http.Request)
|
||||
user.ApplyRole(targetRole)
|
||||
if err := auth.SetPassword(&user, reqData.Password); err != nil {
|
||||
if errors.Is(err, password.ErrInvalidLength) {
|
||||
response.NewErrorResponse(response.ParamsError, "\u5bc6\u7801\u957f\u5ea6\u5fc5\u987b\u4e3a 8-256 \u5b57\u8282", "").FPrint(w)
|
||||
response.NewErrorResponse(response.ParamsError, "\u5bc6\u7801\u957f\u5ea6\u5fc5\u987b\u4e3a 8-12 \u5b57\u8282", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
log.WithContext(ctx).WithError(err).Error("password hash failed")
|
||||
@@ -262,7 +262,7 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if reqData.Password != "" {
|
||||
if err := auth.SetPassword(&user, reqData.Password); err != nil {
|
||||
if errors.Is(err, password.ErrInvalidLength) {
|
||||
response.NewErrorResponse(response.ParamsError, "\u5bc6\u7801\u957f\u5ea6\u5fc5\u987b\u4e3a 8-256 \u5b57\u8282", "").FPrint(w)
|
||||
response.NewErrorResponse(response.ParamsError, "\u5bc6\u7801\u957f\u5ea6\u5fc5\u987b\u4e3a 8-12 \u5b57\u8282", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
log.WithContext(ctx).WithError(err).Error("password hash failed")
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package response
|
||||
|
||||
// ConfigErrorData 是配置 API 的统一错误载荷,不包含底层错误、路径、密文或第三方响应正文。
|
||||
type ConfigErrorData struct {
|
||||
Code string `json:"code"`
|
||||
RequestID string `json:"requestId"`
|
||||
Fields map[string]string `json:"fields,omitempty"`
|
||||
}
|
||||
|
||||
// ConfigErrorNumber 保留旧 errorNo 外壳,同时让 HTTP 状态承担标准语义。
|
||||
func ConfigErrorNumber(httpStatus int) int {
|
||||
switch {
|
||||
case httpStatus >= 500:
|
||||
return ServerError
|
||||
case httpStatus == 401:
|
||||
return NeedLogin
|
||||
case httpStatus == 403:
|
||||
return NoAccessPrivileges
|
||||
default:
|
||||
return ParamsError
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,10 @@ func SetPluginConfigService(service *pluginservice.ConfigService) {
|
||||
pluginConfigService = service
|
||||
}
|
||||
|
||||
func PluginConfigService() *pluginservice.ConfigService {
|
||||
return pluginConfigService
|
||||
}
|
||||
|
||||
const (
|
||||
maxPluginRPCBytes int64 = 8 << 20
|
||||
maxPluginSettingsPageResponseBytes = 1 << 20
|
||||
@@ -178,6 +182,31 @@ func (h *HookSender) Configure(payload framework.PluginRuntimeConfig) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (h *HookSender) ConfigureUserSettings(userID int, settings json.RawMessage) error {
|
||||
_, err := h.call("UserSettings", framework.UserSettingsRequest{
|
||||
UserID: userID,
|
||||
Settings: append(json.RawMessage(nil), settings...),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (h *HookSender) GetBotInfo() (framework.BotInfo, error) {
|
||||
body, err := h.call("BotInfo", struct{}{})
|
||||
if err != nil {
|
||||
return framework.BotInfo{}, err
|
||||
}
|
||||
var info framework.BotInfo
|
||||
if err := json.Unmarshal(body, &info); err != nil {
|
||||
return framework.BotInfo{}, err
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
|
||||
func (h *HookSender) TestMessage(settings json.RawMessage) error {
|
||||
_, err := h.call("TestMessage", append(json.RawMessage(nil), settings...))
|
||||
return err
|
||||
}
|
||||
|
||||
func (h *HookSender) GetName(ctx *context.Context) string {
|
||||
body, err := h.call("GetName", framework.HookDTO{Ctx: ctx})
|
||||
if err != nil {
|
||||
@@ -351,6 +380,7 @@ func Init(serverVersion string) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
syncTelegramUserSettings(logicalID, hook)
|
||||
if healthErr := hook.Health(); healthErr != nil {
|
||||
log.Warnf("[%s] Plugin health check failed", logicalID)
|
||||
_ = process.Kill()
|
||||
@@ -367,6 +397,53 @@ func Init(serverVersion string) {
|
||||
}
|
||||
}
|
||||
|
||||
func SyncTelegramUserSettings(userID int, settings pluginservice.TelegramUserSettings) error {
|
||||
if userID <= 0 {
|
||||
return errors.New("invalid user ID")
|
||||
}
|
||||
hook, ok := GetHook("TelegramPush")
|
||||
if !ok {
|
||||
hook, ok = GetHook("telegram_push")
|
||||
}
|
||||
if !ok {
|
||||
return errors.New("telegram plugin is unavailable")
|
||||
}
|
||||
configurer, ok := hook.(framework.UserSettingsConfigurer)
|
||||
if !ok {
|
||||
return errors.New("telegram plugin does not support user settings")
|
||||
}
|
||||
payload, err := json.Marshal(settings)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return configurer.ConfigureUserSettings(userID, payload)
|
||||
}
|
||||
func syncTelegramUserSettings(logicalID string, hook *HookSender) {
|
||||
if logicalID != "telegram_push" || hook == nil || pluginConfigService == nil {
|
||||
return
|
||||
}
|
||||
entries, err := pluginConfigService.ListTelegramUserSettings()
|
||||
if err != nil {
|
||||
log.WithError(err).Warnf("[%s] Telegram user settings sync load failed", logicalID)
|
||||
return
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if syncErr := syncTelegramUserSetting(hook, entry.UserID, entry.Settings); syncErr != nil {
|
||||
log.WithError(syncErr).Warnf("[%s] Telegram user settings sync failed for user %d", logicalID, entry.UserID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func syncTelegramUserSetting(hook *HookSender, userID int, settings pluginservice.TelegramUserSettings) error {
|
||||
if hook == nil || userID <= 0 {
|
||||
return errors.New("invalid plugin user settings target")
|
||||
}
|
||||
payload, err := json.Marshal(settings)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return hook.ConfigureUserSettings(userID, payload)
|
||||
}
|
||||
func pluginInstanceForStartup(logicalID string) (config.PluginInstance, uint64, bool) {
|
||||
manager := config.CurrentManager()
|
||||
if manager == nil {
|
||||
|
||||
@@ -52,6 +52,10 @@ type SettingsHtmlRequest struct {
|
||||
URL string
|
||||
RequestData string
|
||||
}
|
||||
type UserSettingsRequest struct {
|
||||
UserID int `json:"userId"`
|
||||
Settings json.RawMessage `json:"settings"`
|
||||
}
|
||||
|
||||
// 插件能力宣告
|
||||
type Capabilities struct {
|
||||
@@ -88,6 +92,26 @@ type ConfigurableHook interface {
|
||||
Health() error
|
||||
}
|
||||
|
||||
// UserSettingsConfigurer 接收主程序下发的已授权用户设置。
|
||||
type UserSettingsConfigurer interface {
|
||||
ConfigureUserSettings(userID int, settings json.RawMessage) error
|
||||
}
|
||||
|
||||
// BotInfoProvider 提供不包含密钥的机器人公开信息。
|
||||
type BotInfoProvider interface {
|
||||
GetBotInfo() (BotInfo, error)
|
||||
}
|
||||
|
||||
// TestMessageProvider 仅负责发送主程序已校验的测试消息。
|
||||
type TestMessageProvider interface {
|
||||
TestMessage(json.RawMessage) error
|
||||
}
|
||||
|
||||
type BotInfo struct {
|
||||
Username string `json:"username"`
|
||||
BotLink string `json:"botLink"`
|
||||
}
|
||||
|
||||
type Plugin struct {
|
||||
name string
|
||||
hook EmailHook
|
||||
@@ -321,6 +345,56 @@ func (p *Plugin) Run() error {
|
||||
})
|
||||
}
|
||||
|
||||
if configurable, ok := p.hook.(UserSettingsConfigurer); ok {
|
||||
mux.HandleFunc("/UserSettings", func(w http.ResponseWriter, request *http.Request) {
|
||||
var payload UserSettingsRequest
|
||||
if !decodePluginRequest(w, request, &payload) {
|
||||
return
|
||||
}
|
||||
if payload.UserID <= 0 || len(payload.Settings) == 0 || int64(len(payload.Settings)) > maxPluginRPCBytes {
|
||||
http.Error(w, "用户设置请求无效", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := configurable.ConfigureUserSettings(payload.UserID, payload.Settings); err != nil {
|
||||
http.Error(w, "用户设置下发失败", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
_ = writePluginResponse(w, "application/json", []byte(`{"status":"ok"}`))
|
||||
})
|
||||
}
|
||||
if provider, ok := p.hook.(BotInfoProvider); ok {
|
||||
mux.HandleFunc("/BotInfo", func(w http.ResponseWriter, request *http.Request) {
|
||||
if request.Method != http.MethodPost {
|
||||
w.Header().Set("Allow", http.MethodPost)
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
info, err := provider.GetBotInfo()
|
||||
if err != nil {
|
||||
http.Error(w, "获取机器人信息失败", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
body, err := json.Marshal(info)
|
||||
if err != nil {
|
||||
http.Error(w, "机器人信息无效", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
_ = writePluginResponse(w, "application/json", body)
|
||||
})
|
||||
}
|
||||
if provider, ok := p.hook.(TestMessageProvider); ok {
|
||||
mux.HandleFunc("/TestMessage", func(w http.ResponseWriter, request *http.Request) {
|
||||
var payload json.RawMessage
|
||||
if !decodePluginRequest(w, request, &payload) {
|
||||
return
|
||||
}
|
||||
if err := provider.TestMessage(payload); err != nil {
|
||||
http.Error(w, "发送测试消息失败", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
_ = writePluginResponse(w, "application/json", []byte(`{"status":"ok"}`))
|
||||
})
|
||||
}
|
||||
server := http.Server{
|
||||
ReadTimeout: 5 * time.Second,
|
||||
WriteTimeout: 5 * time.Second,
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/internal/buildinfo"
|
||||
"git.pcux.cn/xuans/Smail/listen/cron_server"
|
||||
"git.pcux.cn/xuans/Smail/res_init"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Run 启动 Smail 主服务。
|
||||
func Run() error {
|
||||
if err := config.Init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
build := buildinfo.Current()
|
||||
if build.Version == "dev" {
|
||||
build.Version = "TestVersion"
|
||||
}
|
||||
|
||||
log.Infoln("*******************************************************************")
|
||||
log.Infof("***\tServer Start Success \n")
|
||||
log.Infof("***\tServer Version: %s \n", build.Version)
|
||||
log.Infof("***\tGit Commit Hash: %s ", build.GitHash)
|
||||
log.Infof("***\tBuild Date: %s ", build.BuildTime)
|
||||
log.Infof("***\tBuild GoLang Version: %s ", build.GoVersion)
|
||||
log.Infoln("*******************************************************************")
|
||||
|
||||
// 启动定时任务。
|
||||
go cron_server.Start()
|
||||
|
||||
// 启动核心服务。
|
||||
if err := res_init.Init(build.Version); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Warnf("Server Stoped \n")
|
||||
return nil
|
||||
}
|
||||
|
||||
// RunAndExit 执行主服务并在发生致命错误时退出。
|
||||
func RunAndExit() {
|
||||
if err := Run(); err != nil {
|
||||
log.WithError(err).Error("Server stopped because of a fatal error")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// RecoverPanic 将启动过程中的 panic 转换为可记录的错误。
|
||||
func RecoverPanic(run func() error) (err error) {
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
err = fmt.Errorf("server initialization panic: %v", recovered)
|
||||
}
|
||||
}()
|
||||
return run()
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package http_server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/controllers"
|
||||
"git.pcux.cn/xuans/Smail/controllers/email"
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
)
|
||||
|
||||
// registerV1Routes 注册业务 API 的稳定版本入口。旧入口仍然保留,便于已有客户端平滑迁移。
|
||||
func registerV1Routes(mux *http.ServeMux) {
|
||||
registerV1MethodRoute(mux, "/api/v1/login", "/api/login", http.MethodPost, 64<<10, controllers.Login)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/verification/request", "/api/security/verification/request", http.MethodPost, 16<<10, controllers.RequestVerification)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/verification/verify", "/api/security/verification/verify", http.MethodPost, 16<<10, controllers.VerifyVerification)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/login/verify", "/api/security/login/verify", http.MethodPost, 16<<10, controllers.CompleteLoginVerification)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/login/resend", "/api/security/login/resend", http.MethodPost, 16<<10, controllers.ResendLoginVerification)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/webauthn/registration/begin", "/api/security/webauthn/registration/begin", http.MethodPost, 16<<10, controllers.BeginWebAuthnRegistration)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/webauthn/registration/finish", "/api/security/webauthn/registration/finish", http.MethodPost, 64<<10, controllers.FinishWebAuthnRegistration)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/webauthn/credentials", "/api/security/webauthn/credentials", http.MethodGet, 0, controllers.ListWebAuthnCredentials)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/webauthn/credentials/rename", "/api/security/webauthn/credentials/rename", http.MethodPost, 16<<10, controllers.RenameWebAuthnCredential)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/webauthn/credentials/revoke", "/api/security/webauthn/credentials/revoke", http.MethodPost, 16<<10, controllers.RevokeWebAuthnCredential)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/webauthn/login/begin", "/api/security/webauthn/login/begin", http.MethodPost, 16<<10, controllers.BeginWebAuthnLogin)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/webauthn/login/finish", "/api/security/webauthn/login/finish", http.MethodPost, 64<<10, controllers.FinishWebAuthnLogin)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/recovery/webauthn/begin", "/api/security/recovery/webauthn/begin", http.MethodPost, 16<<10, controllers.BeginWebAuthnRecovery)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/recovery/webauthn/verify", "/api/security/recovery/webauthn/verify", http.MethodPost, 64<<10, controllers.VerifyWebAuthnRecovery)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/recovery/begin", "/api/security/recovery/begin", http.MethodPost, 16<<10, controllers.BeginPasswordRecovery)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/recovery/verify", "/api/security/recovery/verify", http.MethodPost, 16<<10, controllers.VerifyPasswordRecovery)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/recovery/complete", "/api/security/recovery/complete", http.MethodPost, 16<<10, controllers.CompletePasswordRecovery)
|
||||
registerV1CredentialsQuestionRoute(mux)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/credentials/recovery-codes", "/api/security/credentials/recovery-codes", http.MethodGet, 0, controllers.GetRecoveryCodeStatus)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/credentials/recovery-codes/generate", "/api/security/credentials/recovery-codes/generate", http.MethodPost, 16<<10, controllers.GenerateRecoveryCodes)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/credentials/recovery-codes/revoke", "/api/security/credentials/recovery-codes/revoke", http.MethodPost, 4<<10, controllers.RevokeRecoveryCodes)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/external-email/list", "/api/security/external-email/list", http.MethodGet, 0, controllers.ListExternalEmails)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/external-email/bind", "/api/security/external-email/bind", http.MethodPost, 16<<10, controllers.BindExternalEmail)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/external-email/verify", "/api/security/external-email/verify", http.MethodPost, 16<<10, controllers.VerifyExternalEmail)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/external-email/disable", "/api/security/external-email/disable", http.MethodPost, 16<<10, controllers.DisableExternalEmail)
|
||||
registerV1MethodRoute(mux, "/api/v1/logout", "/api/logout", http.MethodPost, 4<<10, controllers.Logout)
|
||||
registerV1MethodRoute(mux, "/api/v1/system/restart", "/api/v1/system/restart", http.MethodPost, 4<<10, controllers.RestartSmail)
|
||||
registerV1MethodRoute(mux, "/api/v1/system/restart/preview", "/api/v1/system/restart/preview", http.MethodPost, 4<<10, controllers.PreviewRestartSmail)
|
||||
|
||||
registerV1MethodRoute(mux, "/api/v1/group", "/api/group", http.MethodGet, 0, controllers.GetUserGroup)
|
||||
registerV1MethodRoute(mux, "/api/v1/group/list", "/api/group/list", http.MethodGet, 0, controllers.GetUserGroupList)
|
||||
registerV1MethodRoute(mux, "/api/v1/group/add", "/api/group/add", http.MethodPost, 64<<10, controllers.AddGroup)
|
||||
registerV1MethodRoute(mux, "/api/v1/group/del", "/api/group/del", http.MethodPost, 64<<10, controllers.DelGroup)
|
||||
registerV1MethodRoute(mux, "/api/v1/email/list", "/api/email/list", http.MethodPost, 256<<10, email.EmailList)
|
||||
registerV1MethodRoute(mux, "/api/v1/email/del", "/api/email/del", http.MethodPost, 256<<10, email.EmailDelete)
|
||||
registerV1MethodRoute(mux, "/api/v1/email/read", "/api/email/read", http.MethodPost, 256<<10, email.MarkRead)
|
||||
registerV1MethodRoute(mux, "/api/v1/email/detail", "/api/email/detail", http.MethodPost, 256<<10, email.EmailDetail)
|
||||
registerV1MethodRoute(mux, "/api/v1/email/move", "/api/email/move", http.MethodPost, 256<<10, email.Move)
|
||||
registerV1MethodRoute(mux, "/api/v1/email/send", "/api/email/send", http.MethodPost, 40<<20, email.Send)
|
||||
registerV1MethodRoute(mux, "/api/v1/settings/modify_password", "/api/settings/modify_password", http.MethodPost, 64<<10, controllers.ModifyPassword)
|
||||
registerV1MethodRoute(mux, "/api/v1/rule/get", "/api/rule/get", http.MethodPost, 4<<10, controllers.GetRule)
|
||||
registerV1MethodRoute(mux, "/api/v1/rule/add", "/api/rule/add", http.MethodPost, 64<<10, controllers.UpsertRule)
|
||||
registerV1MethodRoute(mux, "/api/v1/rule/update", "/api/rule/update", http.MethodPost, 64<<10, controllers.UpsertRule)
|
||||
registerV1MethodRoute(mux, "/api/v1/rule/del", "/api/rule/del", http.MethodPost, 64<<10, controllers.DelRule)
|
||||
registerV1MethodRoute(mux, "/api/v1/user/create", "/api/user/create", http.MethodPost, 64<<10, controllers.CreateUser)
|
||||
registerV1MethodRoute(mux, "/api/v1/user/edit", "/api/user/edit", http.MethodPost, 64<<10, controllers.EditUser)
|
||||
registerV1MethodRoute(mux, "/api/v1/user/info", "/api/user/info", http.MethodPost, 64<<10, controllers.Info)
|
||||
registerV1MethodRoute(mux, "/api/v1/user/list", "/api/user/list", http.MethodPost, 64<<10, controllers.UserList)
|
||||
registerV1PluginRoutes(mux)
|
||||
}
|
||||
|
||||
func registerV1MethodRoute(mux *http.ServeMux, path string, legacyPath string, method string, maxBody int64, handler controllers.HandlerFunc) {
|
||||
wrapped := contextIterceptor(rewriteV1Path(legacyPath, handler))
|
||||
if maxBody > 0 {
|
||||
wrapped = limitBody(maxBody, wrapped)
|
||||
}
|
||||
mux.HandleFunc(path, onlyMethod(method, wrapped))
|
||||
}
|
||||
|
||||
func registerV1CredentialsQuestionRoute(mux *http.ServeMux) {
|
||||
get := contextIterceptor(rewriteV1Path("/api/security/credentials/question", controllers.GetSecurityQuestion))
|
||||
post := limitBody(16<<10, contextIterceptor(rewriteV1Path("/api/security/credentials/question", controllers.SetSecurityQuestion)))
|
||||
mux.HandleFunc("/api/v1/security/credentials/question", func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
get.ServeHTTP(w, r)
|
||||
case http.MethodPost:
|
||||
post.ServeHTTP(w, r)
|
||||
default:
|
||||
w.Header().Set("Allow", "GET, POST")
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func registerV1PluginRoutes(mux *http.ServeMux) {
|
||||
getUserSettings := contextIterceptor(controllers.GetTelegramUserSettings)
|
||||
updateUserSettings := limitBody(16<<10, contextIterceptor(controllers.UpdateTelegramUserSettings))
|
||||
mux.HandleFunc("/api/v1/plugin/settings/telegram_push/user", func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
getUserSettings.ServeHTTP(w, r)
|
||||
case http.MethodPut:
|
||||
updateUserSettings.ServeHTTP(w, r)
|
||||
default:
|
||||
w.Header().Set("Allow", "GET, PUT")
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
})
|
||||
mux.HandleFunc("/api/v1/plugin/settings/telegram_push/bot", onlyMethod(http.MethodGet, contextIterceptor(controllers.GetTelegramBotInfo)))
|
||||
mux.HandleFunc("/api/v1/plugin/settings/telegram_push/test", onlyMethod(http.MethodPost, limitBody(16<<10, contextIterceptor(controllers.TestTelegramMessage))))
|
||||
mux.HandleFunc("/api/v1/plugin/settings/", contextIterceptor(rewriteV1PathPrefix("/api/plugin/settings/", controllers.SettingsHtml)))
|
||||
registerV1MethodRoute(mux, "/api/v1/plugin/list", "/api/plugin/list", http.MethodGet, 0, controllers.GetPluginList)
|
||||
}
|
||||
|
||||
func normalizedAPIPath(path string) string {
|
||||
if path == "/api/v1" {
|
||||
return "/api"
|
||||
}
|
||||
if strings.HasPrefix(path, "/api/v1/") {
|
||||
return "/api/" + strings.TrimPrefix(path, "/api/v1/")
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func setLegacyAPIHeaders(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasPrefix(r.URL.Path, "/api/") && !strings.HasPrefix(r.URL.Path, "/api/v1/") {
|
||||
w.Header().Set("Deprecation", "true")
|
||||
w.Header().Set("X-Smail-API-Version", "legacy")
|
||||
w.Header().Set("Link", "</api/v1>; rel=\"successor-version\"")
|
||||
}
|
||||
}
|
||||
|
||||
func rewriteV1Path(legacyPath string, handler controllers.HandlerFunc) controllers.HandlerFunc {
|
||||
return func(ctx *context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
originalPath := r.URL.Path
|
||||
originalRawPath := r.URL.RawPath
|
||||
r.URL.Path = legacyPath
|
||||
r.URL.RawPath = ""
|
||||
defer func() {
|
||||
r.URL.Path = originalPath
|
||||
r.URL.RawPath = originalRawPath
|
||||
}()
|
||||
handler(ctx, w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func rewriteV1PathPrefix(legacyPrefix string, handler controllers.HandlerFunc) controllers.HandlerFunc {
|
||||
return func(ctx *context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
originalPath := r.URL.Path
|
||||
originalRawPath := r.URL.RawPath
|
||||
path := strings.TrimPrefix(originalPath, "/api/v1/plugin/settings/")
|
||||
r.URL.Path = strings.TrimSuffix(legacyPrefix, "/") + "/" + path
|
||||
r.URL.RawPath = ""
|
||||
defer func() {
|
||||
r.URL.Path = originalPath
|
||||
r.URL.RawPath = originalRawPath
|
||||
}()
|
||||
handler(ctx, w, r)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
package http_server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/controllers"
|
||||
appcontext "git.pcux.cn/xuans/Smail/utils/context"
|
||||
)
|
||||
|
||||
func TestNormalizedAPIPath(t *testing.T) {
|
||||
tests := []struct {
|
||||
path string
|
||||
want string
|
||||
}{
|
||||
{path: "/api/login", want: "/api/login"},
|
||||
{path: "/api/v1/login", want: "/api/login"},
|
||||
{path: "/api/v1/security/recovery/begin", want: "/api/security/recovery/begin"},
|
||||
{path: "/api/v1", want: "/api"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
if got := normalizedAPIPath(test.path); got != test.want {
|
||||
t.Fatalf("normalizedAPIPath(%q) = %q, want %q", test.path, got, test.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestV1RouteRewritesControllerPath(t *testing.T) {
|
||||
var got string
|
||||
handler := rewriteV1PathPrefix("/api/plugin/settings/", func(_ *appcontext.Context, w http.ResponseWriter, r *http.Request) {
|
||||
got = r.URL.Path
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
})
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/plugin/settings/TestPlugin/index.html", nil)
|
||||
recorder := httptest.NewRecorder()
|
||||
handler(&appcontext.Context{}, recorder, request)
|
||||
if recorder.Code != http.StatusNoContent {
|
||||
t.Fatalf("rewritten handler returned %d", recorder.Code)
|
||||
}
|
||||
if got != "/api/plugin/settings/TestPlugin/index.html" {
|
||||
t.Fatalf("controller path = %q", got)
|
||||
}
|
||||
if request.URL.Path != "/api/v1/plugin/settings/TestPlugin/index.html" {
|
||||
t.Fatalf("request path was not restored: %q", request.URL.Path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestV1PostOnlyRoutesRejectGet(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
if err := router(mux); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, endpoint := range []string{
|
||||
"/api/v1/email/detail",
|
||||
"/api/v1/email/list",
|
||||
"/api/v1/user/info",
|
||||
"/api/v1/user/list",
|
||||
"/api/v1/rule/get",
|
||||
} {
|
||||
recorder := httptest.NewRecorder()
|
||||
mux.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, endpoint, nil))
|
||||
if recorder.Code != http.StatusMethodNotAllowed {
|
||||
t.Fatalf("GET %s returned %d, want %d", endpoint, recorder.Code, http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLegacyRouteIncludesDeprecationHeaders(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
if err := router(mux); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
recorder := httptest.NewRecorder()
|
||||
mux.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/user/info", nil))
|
||||
if recorder.Header().Get("Deprecation") != "true" {
|
||||
t.Fatalf("legacy route missing Deprecation header: %v", recorder.Header())
|
||||
}
|
||||
if recorder.Header().Get("X-Smail-API-Version") != "legacy" {
|
||||
t.Fatalf("legacy route missing version header: %v", recorder.Header())
|
||||
}
|
||||
if recorder.Header().Get("Link") == "" {
|
||||
t.Fatalf("legacy route missing successor link: %v", recorder.Header())
|
||||
}
|
||||
}
|
||||
|
||||
func TestV1RouteDoesNotReceiveLegacyDeprecationHeaders(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
if err := router(mux); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
recorder := httptest.NewRecorder()
|
||||
mux.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/api/v1/user/info", nil))
|
||||
if recorder.Header().Get("Deprecation") != "" {
|
||||
t.Fatalf("V1 route unexpectedly marked deprecated: %v", recorder.Header())
|
||||
}
|
||||
}
|
||||
|
||||
func TestV1LoginUsesSameRequestOriginGuard(t *testing.T) {
|
||||
oldInit := config.IsInit
|
||||
t.Cleanup(func() { config.IsInit = oldInit })
|
||||
config.IsInit = false
|
||||
|
||||
handler := contextIterceptor(controllers.Login)
|
||||
request := httptest.NewRequest(http.MethodPost, "http://mail.example.com/api/v1/login", nil)
|
||||
request.Host = "mail.example.com"
|
||||
request.Header.Set("Content-Type", "text/plain")
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, request)
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("V1 login guard returned HTTP %d", recorder.Code)
|
||||
}
|
||||
if recorder.Body.Len() == 0 {
|
||||
t.Fatal("V1 login guard returned an empty response")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLegacyCredentialsQuestionMethodErrorIncludesDeprecationHeaders(t *testing.T) {
|
||||
legacy := credentialsQuestionHandler()
|
||||
legacyRecorder := httptest.NewRecorder()
|
||||
legacyRequest := httptest.NewRequest(http.MethodDelete, "/api/security/credentials/question", nil)
|
||||
legacy.ServeHTTP(legacyRecorder, legacyRequest)
|
||||
if legacyRecorder.Code != http.StatusMethodNotAllowed {
|
||||
t.Fatalf("legacy method returned %d, want %d", legacyRecorder.Code, http.StatusMethodNotAllowed)
|
||||
}
|
||||
if legacyRecorder.Header().Get("Deprecation") != "true" {
|
||||
t.Fatalf("legacy method error missing Deprecation header: %v", legacyRecorder.Header())
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
registerV1CredentialsQuestionRoute(mux)
|
||||
v1Recorder := httptest.NewRecorder()
|
||||
v1Request := httptest.NewRequest(http.MethodDelete, "/api/v1/security/credentials/question", nil)
|
||||
mux.ServeHTTP(v1Recorder, v1Request)
|
||||
if v1Recorder.Code != http.StatusMethodNotAllowed {
|
||||
t.Fatalf("V1 method returned %d, want %d", v1Recorder.Code, http.StatusMethodNotAllowed)
|
||||
}
|
||||
if v1Recorder.Header().Get("Deprecation") != "" {
|
||||
t.Fatalf("V1 method error unexpectedly marked deprecated: %v", v1Recorder.Header())
|
||||
}
|
||||
}
|
||||
|
||||
func TestFrontendV1RoutesAreRegistered(t *testing.T) {
|
||||
mux := http.NewServeMux()
|
||||
if err := router(mux); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
paths := []string{
|
||||
"/api/v1/audits/config",
|
||||
"/api/v1/audits/config/export",
|
||||
"/api/v1/audits/config/audit-00000000000000000000000000000000",
|
||||
"/api/v1/database/migrations/preview",
|
||||
"/api/v1/database/migrations/dbmig-00000000000000000000000000000000",
|
||||
"/api/v1/system/restart/preview",
|
||||
"/api/v1/config/status",
|
||||
"/api/v1/config/backups",
|
||||
"/api/v1/config/rollback",
|
||||
"/api/v1/config/rollback/preview",
|
||||
"/api/v1/config/rollback/ticket",
|
||||
"/api/v1/config/mail",
|
||||
"/api/v1/config/mail/validate",
|
||||
"/api/v1/config/mail/apply",
|
||||
"/api/v1/email/del",
|
||||
"/api/v1/email/detail",
|
||||
"/api/v1/email/list",
|
||||
"/api/v1/email/move",
|
||||
"/api/v1/email/read",
|
||||
"/api/v1/email/send",
|
||||
"/api/v1/group",
|
||||
"/api/v1/group/add",
|
||||
"/api/v1/group/del",
|
||||
"/api/v1/group/list",
|
||||
"/api/v1/login",
|
||||
"/api/v1/logout",
|
||||
"/api/v1/plugin/list",
|
||||
"/api/v1/plugin/settings/example/index.html",
|
||||
"/api/v1/plugin/settings/telegram_push/user",
|
||||
"/api/v1/plugin/settings/telegram_push/bot",
|
||||
"/api/v1/plugin/settings/telegram_push/test",
|
||||
"/api/v1/rule/add",
|
||||
"/api/v1/rule/del",
|
||||
"/api/v1/rule/get",
|
||||
"/api/v1/rule/update",
|
||||
"/api/v1/security/credentials/question",
|
||||
"/api/v1/security/credentials/recovery-codes",
|
||||
"/api/v1/security/credentials/recovery-codes/generate",
|
||||
"/api/v1/security/credentials/recovery-codes/revoke",
|
||||
"/api/v1/security/external-email/bind",
|
||||
"/api/v1/security/external-email/disable",
|
||||
"/api/v1/security/external-email/list",
|
||||
"/api/v1/security/external-email/verify",
|
||||
"/api/v1/security/login/resend",
|
||||
"/api/v1/security/login/verify",
|
||||
"/api/v1/security/recovery/begin",
|
||||
"/api/v1/security/recovery/complete",
|
||||
"/api/v1/security/recovery/verify",
|
||||
"/api/v1/security/recovery/webauthn/begin",
|
||||
"/api/v1/security/recovery/webauthn/verify",
|
||||
"/api/v1/security/verification/request",
|
||||
"/api/v1/security/verification/verify",
|
||||
"/api/v1/security/webauthn/credentials",
|
||||
"/api/v1/security/webauthn/credentials/rename",
|
||||
"/api/v1/security/webauthn/credentials/revoke",
|
||||
"/api/v1/security/webauthn/login/begin",
|
||||
"/api/v1/security/webauthn/login/finish",
|
||||
"/api/v1/security/webauthn/registration/begin",
|
||||
"/api/v1/security/webauthn/registration/finish",
|
||||
"/api/v1/settings/modify_password",
|
||||
"/api/v1/system/restart",
|
||||
"/api/v1/user/create",
|
||||
"/api/v1/user/edit",
|
||||
"/api/v1/user/info",
|
||||
"/api/v1/user/list",
|
||||
}
|
||||
for _, path := range paths {
|
||||
handler, pattern := mux.Handler(httptest.NewRequest(http.MethodGet, path, nil))
|
||||
if handler == nil || pattern == "" || pattern == "/" {
|
||||
t.Errorf("V1 route %s fell through to pattern %q", path, pattern)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,6 +95,12 @@ func router(mux *http.ServeMux) error {
|
||||
mux.HandleFunc("/api/plugin/list", onlyMethod(http.MethodGet, contextIterceptor(controllers.GetPluginList)))
|
||||
mux.HandleFunc("/api/v1/config/", limitBody(4<<20, contextIterceptor(configRoute)))
|
||||
mux.HandleFunc("/api/v1/audits/config", limitBody(1<<20, contextIterceptor(configAuditRoute)))
|
||||
mux.HandleFunc("/api/v1/audits/config/", limitBody(1<<20, contextIterceptor(configAuditSubRoute)))
|
||||
mux.HandleFunc("/api/v1/database/migrations/preview", onlyMethod(http.MethodPost, limitBody(16<<10, contextIterceptor(controllers.PreviewDatabaseMigration))))
|
||||
mux.HandleFunc("/api/v1/database/migrations", onlyMethod(http.MethodPost, limitBody(16<<10, contextIterceptor(controllers.ExecuteDatabaseMigration))))
|
||||
mux.HandleFunc("/api/v1/database/migrations/", contextIterceptor(databaseMigrationSubRoute))
|
||||
mux.HandleFunc("/api/v1/ping", controllers.Ping)
|
||||
registerV1Routes(mux)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -132,6 +138,7 @@ func isReservedResetPath(path string) bool {
|
||||
}
|
||||
|
||||
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/status":
|
||||
@@ -191,6 +198,7 @@ func configSectionRoute(path, operation string) bool {
|
||||
return len(parts) == 5 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "config" && parts[4] == operation
|
||||
}
|
||||
func configAuditRoute(ctx *context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
controllers.EnsureConfigRequestID(w, r)
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
controllers.ConfigAuditQuery(ctx, w, r)
|
||||
@@ -201,6 +209,31 @@ func configAuditRoute(ctx *context.Context, w http.ResponseWriter, r *http.Reque
|
||||
}
|
||||
}
|
||||
|
||||
func configAuditSubRoute(ctx *context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
controllers.EnsureConfigRequestID(w, r)
|
||||
path := strings.TrimSuffix(r.URL.Path, "/")
|
||||
switch path {
|
||||
case "/api/v1/audits/config/export":
|
||||
if !allowRouteMethod(w, r, http.MethodPost) {
|
||||
return
|
||||
}
|
||||
controllers.ExportConfigAuditFormal(ctx, w, r)
|
||||
default:
|
||||
if !allowRouteMethod(w, r, http.MethodGet) {
|
||||
return
|
||||
}
|
||||
controllers.GetConfigAuditDetail(ctx, w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func databaseMigrationSubRoute(ctx *context.Context, w http.ResponseWriter, r *http.Request) {
|
||||
controllers.EnsureConfigRequestID(w, r)
|
||||
if !allowRouteMethod(w, r, http.MethodGet) {
|
||||
return
|
||||
}
|
||||
controllers.GetDatabaseMigration(ctx, w, r)
|
||||
}
|
||||
|
||||
func allowRouteMethod(w http.ResponseWriter, r *http.Request, allowed ...string) bool {
|
||||
for _, method := range allowed {
|
||||
if r.Method == method {
|
||||
|
||||
@@ -96,13 +96,14 @@ func HttpsStop() {
|
||||
func contextIterceptor(h controllers.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
securityHeaders(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
setLegacyAPIHeaders(w, r)
|
||||
if w.Header().Get("Content-Type") == "" {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 64<<20)
|
||||
|
||||
if r.URL.Path == "/api/login" && !loginRequestAllowed(r) {
|
||||
if normalizedAPIPath(r.URL.Path) == "/api/login" && !loginRequestAllowed(r) {
|
||||
response.NewErrorResponse(response.ParamsError, "\u8bf7\u6c42\u6765\u6e90\u6821\u9a8c\u5931\u8d25", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
@@ -160,7 +161,7 @@ func contextIterceptor(h controllers.HandlerFunc) http.HandlerFunc {
|
||||
response.NewErrorResponse(response.NoAccessPrivileges, "请先修改临时密码", "PASSWORD_CHANGE_REQUIRED").FPrint(w)
|
||||
return
|
||||
}
|
||||
} else if r.URL.Path != "/api/setup" {
|
||||
} else if normalizedAPIPath(r.URL.Path) != "/api/setup" {
|
||||
response.NewErrorResponse(response.NeedSetup, "", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
@@ -170,7 +171,7 @@ func contextIterceptor(h controllers.HandlerFunc) http.HandlerFunc {
|
||||
}
|
||||
|
||||
func mustChangePasswordAllowedPath(path string) bool {
|
||||
switch path {
|
||||
switch normalizedAPIPath(path) {
|
||||
case "/api/settings/modify_password", "/api/logout", "/api/user/info":
|
||||
return true
|
||||
default:
|
||||
@@ -179,7 +180,7 @@ func mustChangePasswordAllowedPath(path string) bool {
|
||||
}
|
||||
|
||||
func publicSecurityPath(path string) bool {
|
||||
switch path {
|
||||
switch normalizedAPIPath(path) {
|
||||
case "/api/ping", "/api/login", "/api/security/login/verify", "/api/security/recovery/begin", "/api/security/recovery/verify", "/api/security/recovery/complete", "/api/security/recovery/webauthn/begin", "/api/security/recovery/webauthn/verify", "/api/security/webauthn/login/begin", "/api/security/webauthn/login/finish":
|
||||
return true
|
||||
default:
|
||||
@@ -190,6 +191,7 @@ func credentialsQuestionHandler() http.HandlerFunc {
|
||||
get := contextIterceptor(controllers.GetSecurityQuestion)
|
||||
post := limitBody(16<<10, contextIterceptor(controllers.SetSecurityQuestion))
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
setLegacyAPIHeaders(w, r)
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
get.ServeHTTP(w, r)
|
||||
@@ -204,6 +206,7 @@ func credentialsQuestionHandler() http.HandlerFunc {
|
||||
|
||||
func onlyMethod(method string, next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
setLegacyAPIHeaders(w, r)
|
||||
if r.Method != method {
|
||||
w.Header().Set("Allow", method)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
@@ -222,10 +225,11 @@ func limitBody(maxBytes int64, next http.HandlerFunc) http.HandlerFunc {
|
||||
|
||||
func securityHeaders(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
setLegacyAPIHeaders(w, r)
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Referrer-Policy", "no-referrer")
|
||||
w.Header().Set("Permissions-Policy", "camera=(), microphone=(), geolocation=()")
|
||||
if strings.HasPrefix(r.URL.Path, "/api/plugin/settings/") {
|
||||
if strings.HasPrefix(normalizedAPIPath(r.URL.Path), "/api/plugin/settings/") {
|
||||
w.Header().Set("X-Frame-Options", "SAMEORIGIN")
|
||||
w.Header().Set("Content-Security-Policy", "default-src 'none'; base-uri 'none'; object-src 'none'; frame-ancestors 'self'; form-action 'none'; img-src data:; style-src 'unsafe-inline'; script-src 'unsafe-inline'; connect-src 'none'")
|
||||
} else if strings.HasPrefix(r.URL.Path, "/attachments/") {
|
||||
|
||||
@@ -194,6 +194,11 @@ func TestSecurityHeadersRespectPluginFrameBoundary(t *testing.T) {
|
||||
t.Fatalf("plugin settings CSP is unsafe: %s", got)
|
||||
}
|
||||
|
||||
v1PluginRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(v1PluginRecorder, httptest.NewRequest(http.MethodGet, "/api/v1/plugin/settings/SpamBlock/index.html", nil))
|
||||
if v1PluginRecorder.Header().Get("X-Frame-Options") != "SAMEORIGIN" || !contains(v1PluginRecorder.Header().Get("Content-Security-Policy"), "frame-ancestors 'self'") {
|
||||
t.Fatalf("V1 plugin settings frame boundary is unsafe: headers=%v", v1PluginRecorder.Header())
|
||||
}
|
||||
pageRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(pageRecorder, httptest.NewRequest(http.MethodGet, "/", nil))
|
||||
if pageRecorder.Header().Get("X-Frame-Options") != "DENY" || !contains(pageRecorder.Header().Get("Content-Security-Policy"), "frame-ancestors 'none'") {
|
||||
|
||||
@@ -37,7 +37,7 @@ func TestModifyPasswordRouteRejectsUnauthenticatedRequest(t *testing.T) {
|
||||
if err := router(mux); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/settings/modify_password", strings.NewReader(`{"current_password":"CurrentPass123","password":"NewPass123"}`))
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/settings/modify_password", strings.NewReader(`{"current_password":"CurrentP12","password":"NewPass123"}`))
|
||||
loaded, err := session.Instance.Load(request.Context(), "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -81,7 +81,7 @@ func TestSuperAdminCannotChangeOwnPasswordAfterLogin(t *testing.T) {
|
||||
}
|
||||
|
||||
superAdmin := models.User{Account: "super-admin", Name: "root", IsAdmin: 1, SessionVersion: 1}
|
||||
if err := auth.SetPassword(&superAdmin, "CurrentPass123"); err != nil {
|
||||
if err := auth.SetPassword(&superAdmin, "CurrentP12"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.Instance.Insert(&superAdmin); err != nil {
|
||||
@@ -96,7 +96,7 @@ func TestSuperAdminCannotChangeOwnPasswordAfterLogin(t *testing.T) {
|
||||
if err := router(mux); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/settings/modify_password", strings.NewReader(`{"current_password":"CurrentPass123","password":"NewPass123"}`)).WithContext(currentSession)
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/settings/modify_password", strings.NewReader(`{"current_password":"CurrentP12","password":"NewPass123"}`)).WithContext(currentSession)
|
||||
recorder := httptest.NewRecorder()
|
||||
mux.ServeHTTP(recorder, request)
|
||||
|
||||
@@ -113,7 +113,7 @@ func TestSuperAdminCannotChangeOwnPasswordAfterLogin(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !found || !password.Verify(unchanged.Password, "CurrentPass123") {
|
||||
if !found || !password.Verify(unchanged.Password, "CurrentP12") {
|
||||
t.Fatal("super admin password changed through the web route")
|
||||
}
|
||||
if password.Verify(unchanged.Password, "NewPass123") {
|
||||
|
||||
@@ -113,7 +113,7 @@ func (s *setupSecurity) bootstrap(next http.Handler) http.Handler {
|
||||
}
|
||||
if setupRemoteIP(r).IsLoopback() {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: setupCookieName, Value: s.token, Path: "/api/setup",
|
||||
Name: setupCookieName, Value: s.token, Path: "/api/",
|
||||
HttpOnly: true, Secure: false, SameSite: http.SameSiteStrictMode,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -134,7 +134,96 @@ func TestSetupSecurityBootstrapCookie(t *testing.T) {
|
||||
t.Fatalf("bootstrap returned %d", recorder.Code)
|
||||
}
|
||||
cookies := recorder.Result().Cookies()
|
||||
if len(cookies) != 1 || cookies[0].Name != setupCookieName || !cookies[0].HttpOnly || cookies[0].SameSite != http.SameSiteStrictMode || cookies[0].Path != "/api/setup" {
|
||||
if len(cookies) != 1 || cookies[0].Name != setupCookieName || !cookies[0].HttpOnly || cookies[0].SameSite != http.SameSiteStrictMode || cookies[0].Path != "/api/" {
|
||||
t.Fatalf("unsafe setup cookie: %+v", cookies)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupAPIRoutesUseEquivalentAuthorization(t *testing.T) {
|
||||
const token = "0123456789abcdefghijklmnop"
|
||||
for _, endpoint := range []string{"/api/setup", "/api/v1/setup"} {
|
||||
t.Run(endpoint, func(t *testing.T) {
|
||||
security := withSetupSecurity(t, token)
|
||||
mux := http.NewServeMux()
|
||||
calls := 0
|
||||
registerSetupAPIRoutes(mux, security, setupOnlyPost(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls++
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
|
||||
request := httptest.NewRequest(http.MethodPost, endpoint, nil)
|
||||
request.RemoteAddr = "127.0.0.1:12345"
|
||||
recorder := httptest.NewRecorder()
|
||||
mux.ServeHTTP(recorder, request)
|
||||
if recorder.Code != http.StatusForbidden || calls != 0 {
|
||||
t.Fatalf("missing token returned code=%d calls=%d", recorder.Code, calls)
|
||||
}
|
||||
|
||||
request = httptest.NewRequest(http.MethodPost, endpoint, nil)
|
||||
request.RemoteAddr = "127.0.0.1:12345"
|
||||
request.Header.Set("X-Setup-Token", token)
|
||||
recorder = httptest.NewRecorder()
|
||||
mux.ServeHTTP(recorder, request)
|
||||
if recorder.Code != http.StatusNoContent || calls != 1 {
|
||||
t.Fatalf("valid token returned code=%d calls=%d", recorder.Code, calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupAPIRoutesRequirePostAndToken(t *testing.T) {
|
||||
const token = "0123456789abcdefghijklmnop"
|
||||
for _, endpoint := range []string{"/api/setup", "/api/v1/setup"} {
|
||||
t.Run(endpoint, func(t *testing.T) {
|
||||
security := withSetupSecurity(t, token)
|
||||
mux := http.NewServeMux()
|
||||
calls := 0
|
||||
registerSetupAPIRoutes(mux, security, setupOnlyPost(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls++
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, endpoint, nil)
|
||||
request.RemoteAddr = "127.0.0.1:12345"
|
||||
request.Header.Set("X-Setup-Token", token)
|
||||
recorder := httptest.NewRecorder()
|
||||
mux.ServeHTTP(recorder, request)
|
||||
if recorder.Code != http.StatusMethodNotAllowed || recorder.Header().Get("Allow") != http.MethodPost || calls != 0 {
|
||||
t.Fatalf("GET returned code=%d allow=%q calls=%d", recorder.Code, recorder.Header().Get("Allow"), calls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupAPIRoutesBecomeInvalidAfterSetupCompletes(t *testing.T) {
|
||||
const token = "0123456789abcdefghijklmnop"
|
||||
security := withSetupSecurity(t, token)
|
||||
mux := http.NewServeMux()
|
||||
calls := 0
|
||||
registerSetupAPIRoutes(mux, security, setupOnlyPost(func(w http.ResponseWriter, r *http.Request) {
|
||||
calls++
|
||||
config.IsInit = true
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/setup", nil)
|
||||
request.RemoteAddr = "127.0.0.1:12345"
|
||||
request.Header.Set("X-Setup-Token", token)
|
||||
recorder := httptest.NewRecorder()
|
||||
mux.ServeHTTP(recorder, request)
|
||||
if recorder.Code != http.StatusNoContent || calls != 1 {
|
||||
t.Fatalf("first setup returned code=%d calls=%d", recorder.Code, calls)
|
||||
}
|
||||
|
||||
config.IsInit = false
|
||||
for _, endpoint := range []string{"/api/setup", "/api/v1/setup"} {
|
||||
request = httptest.NewRequest(http.MethodPost, endpoint, nil)
|
||||
request.RemoteAddr = "127.0.0.1:12345"
|
||||
request.Header.Set("X-Setup-Token", token)
|
||||
recorder = httptest.NewRecorder()
|
||||
mux.ServeHTTP(recorder, request)
|
||||
if recorder.Code != http.StatusForbidden || calls != 1 {
|
||||
t.Fatalf("replayed setup %s returned code=%d calls=%d", endpoint, recorder.Code, calls)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,7 @@ func SetupStart() error {
|
||||
return fmt.Errorf("load embedded setup frontend: %w", err)
|
||||
}
|
||||
mux.Handle("/", setupSecurity.bootstrap(http.FileServer(http.FS(fe))))
|
||||
mux.HandleFunc("/api/setup", setupSecurity.authorize(setupOnlyPost(contextIterceptor(controllers.Setup))))
|
||||
registerSetupAPIRoutes(mux, setupSecurity, setupOnlyPost(contextIterceptor(controllers.Setup)))
|
||||
// 挑战请求类似这样 /.well-known/acme-challenge/QPyMAyaWw9s5JvV1oruyqWHG7OqkHMJEHPoUz2046KM
|
||||
mux.HandleFunc("/.well-known/", controllers.AcmeChallenge)
|
||||
|
||||
@@ -80,6 +80,17 @@ func SetupStart() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// registerSetupAPIRoutes 为旧初始化入口和 V1 初始化入口安装完全相同的安全链路。
|
||||
// 两个入口只承担兼容关系,不允许因为路径版本不同而出现授权差异。
|
||||
func registerSetupAPIRoutes(mux *http.ServeMux, security *setupSecurity, next http.HandlerFunc) {
|
||||
if mux == nil || security == nil || next == nil {
|
||||
return
|
||||
}
|
||||
authorized := security.authorize(next)
|
||||
mux.HandleFunc("/api/setup", authorized)
|
||||
mux.HandleFunc("/api/v1/setup", authorized)
|
||||
}
|
||||
|
||||
func setupOnlyPost(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
|
||||
+3
-43
@@ -4,52 +4,12 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/internal/buildinfo"
|
||||
"git.pcux.cn/xuans/Smail/listen/cron_server"
|
||||
"git.pcux.cn/xuans/Smail/res_init"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"git.pcux.cn/xuans/Smail/internal/app"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
log.WithError(err).Error("Server stopped because of a fatal error")
|
||||
if err := app.RecoverPanic(app.Run); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run() (err error) {
|
||||
defer func() {
|
||||
if recovered := recover(); recovered != nil {
|
||||
err = fmt.Errorf("server initialization panic: %v", recovered)
|
||||
}
|
||||
}()
|
||||
|
||||
if err := config.Init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
build := buildinfo.Current()
|
||||
if build.Version == "dev" {
|
||||
build.Version = "TestVersion"
|
||||
}
|
||||
|
||||
log.Infoln("*******************************************************************")
|
||||
log.Infof("***\tServer Start Success \n")
|
||||
log.Infof("***\tServer Version: %s \n", build.Version)
|
||||
log.Infof("***\tGit Commit Hash: %s ", build.GitHash)
|
||||
log.Infof("***\tBuild Date: %s ", build.BuildTime)
|
||||
log.Infof("***\tBuild GoLang Version: %s ", build.GoVersion)
|
||||
log.Infoln("*******************************************************************")
|
||||
|
||||
// 定时任务启动
|
||||
go cron_server.Start()
|
||||
|
||||
// 核心服务启动
|
||||
if err := res_init.Init(build.Version); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Warnf("Server Stoped \n")
|
||||
return nil
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/services/externalemail"
|
||||
pluginservice "git.pcux.cn/xuans/Smail/services/plugin"
|
||||
"git.pcux.cn/xuans/Smail/services/recovery"
|
||||
"git.pcux.cn/xuans/Smail/services/restart"
|
||||
"git.pcux.cn/xuans/Smail/services/setup/ssl"
|
||||
"git.pcux.cn/xuans/Smail/services/verification"
|
||||
webauthnservice "git.pcux.cn/xuans/Smail/services/webauthn"
|
||||
@@ -194,6 +195,7 @@ func Init(serverVersion string) error {
|
||||
startService(serviceErrors, "IMAP TLS", imap_server.StarTLS)
|
||||
|
||||
log.Info("管理员重置通道已启动")
|
||||
restart.SetCurrent(restart.NewSignalTrigger(signal.RestartChan))
|
||||
|
||||
select {
|
||||
case err := <-serviceErrors:
|
||||
@@ -224,6 +226,7 @@ func startService(serviceErrors chan<- error, name string, service func() error)
|
||||
}
|
||||
|
||||
func stopServices() {
|
||||
restart.SetCurrent(nil)
|
||||
stopConfigManager()
|
||||
stopAdminResetControl()
|
||||
stopVerificationRuntime()
|
||||
@@ -413,7 +416,7 @@ func stopAdminResetControl() {
|
||||
}
|
||||
if adminResetControl != nil {
|
||||
if err := adminResetControl.Close(); err != nil {
|
||||
log.WithError(err).Warn("闁稿繑濞婂Λ瀵告惥閸涱垶鐛撶紒鐙呯磿閹﹪宕ㄥΟ缁樻嫳闁哄牊妞介崳鍝ョ磾椤曗偓閳ь剚宀告禍鐐緞鏉堫偉袝")
|
||||
log.WithError(err).Warn("关闭管理员重置通道失败")
|
||||
}
|
||||
adminResetControl = nil
|
||||
adminResetServeErrors = nil
|
||||
|
||||
@@ -90,7 +90,7 @@ func TestAdminPluginRestartScope(t *testing.T) {
|
||||
|
||||
func TestManagementEndpointCatalog(t *testing.T) {
|
||||
endpoints := ManagementEndpoints()
|
||||
if len(endpoints) != 18 {
|
||||
if len(endpoints) != 32 {
|
||||
t.Fatalf("unexpected endpoint count: %d", len(endpoints))
|
||||
}
|
||||
seen := map[string]struct{}{}
|
||||
@@ -112,6 +112,10 @@ func TestManagementEndpointCatalog(t *testing.T) {
|
||||
if !ok || endpoint.Capability != CapabilityConfigWriteGlobal {
|
||||
t.Fatal("configuration validation endpoint was not matched")
|
||||
}
|
||||
endpoint, ok = LookupEndpoint("POST", "/api/v1/system/restart")
|
||||
if !ok || endpoint.Capability != CapabilityServiceRestart {
|
||||
t.Fatal("system restart endpoint was not matched")
|
||||
}
|
||||
endpoint, ok = LookupEndpoint("GET", "/api/v1/config/email")
|
||||
if !ok || endpoint.Capability != CapabilityConfigRead {
|
||||
t.Fatal("configuration section endpoint was not matched")
|
||||
|
||||
@@ -26,7 +26,21 @@ var managementEndpoints = []Endpoint{
|
||||
{Method: "GET", Path: "/api/v1/config/:section", Capability: CapabilityConfigRead},
|
||||
{Method: "PUT", Path: "/api/v1/config/:section", Capability: CapabilityConfigWriteGlobal},
|
||||
{Method: "GET", Path: "/api/v1/audits/config", Capability: CapabilityAuditRead},
|
||||
{Method: "GET", Path: "/api/v1/audits/config/:id", Capability: CapabilityAuditRead},
|
||||
{Method: "POST", Path: "/api/v1/audits/config", Capability: CapabilityAuditExport},
|
||||
{Method: "POST", Path: "/api/v1/audits/config/export", Capability: CapabilityAuditExport},
|
||||
{Method: "POST", Path: "/api/v1/settings/modify_password", Capability: CapabilityPasswordChangeOwn},
|
||||
{Method: "POST", Path: "/api/v1/system/restart", Capability: CapabilityServiceRestart},
|
||||
{Method: "POST", Path: "/api/v1/system/restart/preview", Capability: CapabilityServiceRestart},
|
||||
{Method: "POST", Path: "/api/v1/database/migrations/preview", Capability: CapabilityDatabaseMigrate},
|
||||
{Method: "POST", Path: "/api/v1/database/migrations", Capability: CapabilityDatabaseMigrate},
|
||||
{Method: "GET", Path: "/api/v1/database/migrations/:id", Capability: CapabilityDatabaseMigrate},
|
||||
{Method: "POST", Path: "/api/v1/user/create", Capability: CapabilityUserManage},
|
||||
{Method: "POST", Path: "/api/v1/user/edit", Capability: CapabilityUserManage},
|
||||
{Method: "POST", Path: "/api/v1/user/info", Capability: CapabilityUserRead},
|
||||
{Method: "POST", Path: "/api/v1/user/list", Capability: CapabilityUserRead},
|
||||
{Method: "GET", Path: "/api/v1/plugin/list", Capability: CapabilityPluginRead},
|
||||
{Method: "*", Path: "/api/v1/plugin/settings/", Capability: CapabilityPluginWriteOwn},
|
||||
}
|
||||
|
||||
func ManagementEndpoints() []Endpoint {
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
package adminreset
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/password"
|
||||
)
|
||||
|
||||
func ReadProtectedPassword() (string, error) {
|
||||
@@ -36,9 +36,27 @@ func askPassword(prompt string) (string, error) {
|
||||
if err != nil {
|
||||
return "", errors.New("受保护密码代理不可用")
|
||||
}
|
||||
output = bytes.TrimSpace(output)
|
||||
if len(output) == 0 || len(output) > 256 {
|
||||
output = stripPasswordLineEnding(output)
|
||||
value := string(output)
|
||||
defer clearBytes(output)
|
||||
if password.ValidateUserPassword(value) != nil {
|
||||
return "", errors.New("密码长度无效")
|
||||
}
|
||||
return strings.TrimSpace(string(output)), nil
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func stripPasswordLineEnding(value []byte) []byte {
|
||||
if len(value) > 0 && value[len(value)-1] == '\n' {
|
||||
value = value[:len(value)-1]
|
||||
}
|
||||
if len(value) > 0 && value[len(value)-1] == '\r' {
|
||||
value = value[:len(value)-1]
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func clearBytes(value []byte) {
|
||||
for index := range value {
|
||||
value[index] = 0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
//go:build linux
|
||||
|
||||
package adminreset
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStripPasswordLineEndingPreservesPasswordWhitespace(t *testing.T) {
|
||||
value := []byte(" Pass123 \r\n")
|
||||
got := string(stripPasswordLineEnding(value))
|
||||
if got != " Pass123 " {
|
||||
t.Fatalf("password output = %q, want surrounding spaces preserved", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStripPasswordLineEndingDoesNotTrimOtherWhitespace(t *testing.T) {
|
||||
value := []byte("\tPass123\t")
|
||||
got := string(stripPasswordLineEnding(value))
|
||||
if got != "\tPass123\t" {
|
||||
t.Fatalf("password output = %q, want non-line-ending whitespace preserved", got)
|
||||
}
|
||||
}
|
||||
@@ -6,20 +6,21 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/password"
|
||||
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
func ReadProtectedPassword() (string, error) {
|
||||
if !term.IsTerminal(int(os.Stdin.Fd())) {
|
||||
return "", errors.New("\u53d7\u4fdd\u62a4\u7684\u5bc6\u7801\u8f93\u5165\u9700\u8981\u4ea4\u4e92\u5f0f\u63a7\u5236\u53f0")
|
||||
return "", errors.New("受保护的密码输入需要交互式控制台")
|
||||
}
|
||||
first, err := readWindowsPassword("\u8bf7\u8f93\u5165\u65b0\u7684\u8d85\u7ea7\u7ba1\u7406\u5458\u5bc6\u7801")
|
||||
first, err := readWindowsPassword("请输入新的超级管理员密码")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
second, err := readWindowsPassword("\u8bf7\u518d\u6b21\u8f93\u5165\u65b0\u7684\u8d85\u7ea7\u7ba1\u7406\u5458\u5bc6\u7801")
|
||||
second, err := readWindowsPassword("请再次输入新的超级管理员密码")
|
||||
if err != nil {
|
||||
clearPassword(&first)
|
||||
return "", err
|
||||
@@ -27,7 +28,7 @@ func ReadProtectedPassword() (string, error) {
|
||||
if first == "" || first != second {
|
||||
clearPassword(&first)
|
||||
clearPassword(&second)
|
||||
return "", errors.New("\u5bc6\u7801\u4e3a\u7a7a\u6216\u4e24\u6b21\u8f93\u5165\u4e0d\u4e00\u81f4")
|
||||
return "", errors.New("密码为空或两次输入不一致")
|
||||
}
|
||||
clearPassword(&second)
|
||||
return first, nil
|
||||
@@ -38,14 +39,14 @@ func readWindowsPassword(prompt string) (string, error) {
|
||||
value, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
fmt.Fprintln(os.Stderr)
|
||||
if err != nil {
|
||||
return "", errors.New("\u53d7\u4fdd\u62a4\u7684\u5bc6\u7801\u8f93\u5165\u5931\u8d25")
|
||||
return "", errors.New("受保护的密码输入失败")
|
||||
}
|
||||
normalized := strings.TrimSpace(string(value))
|
||||
normalized := string(value)
|
||||
for i := range value {
|
||||
value[i] = 0
|
||||
}
|
||||
if len([]byte(normalized)) < 8 || len([]byte(normalized)) > 256 {
|
||||
return "", errors.New("\u5bc6\u7801\u957f\u5ea6\u5fc5\u987b\u4e3a 8 \u5230 256 \u4e2a\u5b57\u7b26")
|
||||
if password.ValidateUserPassword(normalized) != nil {
|
||||
return "", errors.New("\u5bc6\u7801\u957f\u5ea6\u5fc5\u987b\u4e3a 8 \u5230 12 \u4e2a\u5b57\u7b26")
|
||||
}
|
||||
return normalized, nil
|
||||
}
|
||||
@@ -12,12 +12,15 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/password"
|
||||
)
|
||||
|
||||
const (
|
||||
ProtocolVersion = 1
|
||||
maxControlMessageSize = 16 << 10
|
||||
controlRequestTimeout = 15 * time.Second
|
||||
ProtocolVersion = 1
|
||||
maxControlMessageSize = 16 << 10
|
||||
controlRequestTimeout = 15 * time.Second
|
||||
defaultSessionCapacity = 128
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -26,6 +29,7 @@ var (
|
||||
ErrReplay = errors.New("reset request replayed")
|
||||
ErrExpiredSession = errors.New("reset session expired")
|
||||
ErrUnsupported = errors.New("local reset channel is unsupported")
|
||||
ErrSessionCapacity = errors.New("reset session capacity exceeded")
|
||||
)
|
||||
|
||||
type Request struct {
|
||||
@@ -44,17 +48,31 @@ type Response struct {
|
||||
type ResetHandler func(newPassword string) error
|
||||
|
||||
type SessionGuard struct {
|
||||
mu sync.Mutex
|
||||
pending map[string]time.Time
|
||||
ttl time.Duration
|
||||
now func() time.Time
|
||||
mu sync.Mutex
|
||||
pending map[string]time.Time
|
||||
ttl time.Duration
|
||||
capacity int
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func NewSessionGuard(ttl time.Duration) *SessionGuard {
|
||||
return NewSessionGuardWithCapacity(ttl, defaultSessionCapacity)
|
||||
}
|
||||
|
||||
// NewSessionGuardWithCapacity 创建带有并发会话上限的短时会话守卫。
|
||||
func NewSessionGuardWithCapacity(ttl time.Duration, capacity int) *SessionGuard {
|
||||
if ttl <= 0 {
|
||||
ttl = 2 * time.Minute
|
||||
}
|
||||
return &SessionGuard{pending: make(map[string]time.Time), ttl: ttl, now: time.Now}
|
||||
if capacity <= 0 {
|
||||
capacity = defaultSessionCapacity
|
||||
}
|
||||
return &SessionGuard{
|
||||
pending: make(map[string]time.Time, capacity),
|
||||
ttl: ttl,
|
||||
capacity: capacity,
|
||||
now: time.Now,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *SessionGuard) Issue() (string, error) {
|
||||
@@ -65,6 +83,9 @@ func (g *SessionGuard) Issue() (string, error) {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
g.pruneLocked(now)
|
||||
if len(g.pending) >= g.capacity {
|
||||
return "", ErrSessionCapacity
|
||||
}
|
||||
for attempt := 0; attempt < 3; attempt++ {
|
||||
nonce, err := NewNonce()
|
||||
if err != nil {
|
||||
@@ -133,7 +154,7 @@ func DecodeRequest(r io.Reader) (Request, error) {
|
||||
if err := decodeControlJSON(data, &request); err != nil {
|
||||
return Request{}, ErrInvalidRequest
|
||||
}
|
||||
if request.Version != ProtocolVersion || strings.TrimSpace(request.Nonce) == "" || len([]byte(request.NewPassword)) < 8 || len([]byte(request.NewPassword)) > 256 {
|
||||
if request.Version != ProtocolVersion || strings.TrimSpace(request.Nonce) == "" || password.ValidateUserPassword(request.NewPassword) != nil {
|
||||
return Request{}, ErrInvalidRequest
|
||||
}
|
||||
return request, nil
|
||||
@@ -196,7 +217,7 @@ func handleResetConnection(conn io.ReadWriter, setDeadline func(time.Time) error
|
||||
}
|
||||
nonce, err := guard.Issue()
|
||||
if err != nil {
|
||||
_ = EncodeResponse(conn, Response{Code: "CHANNEL_UNAVAILABLE", Message: "\u91cd\u7f6e\u901a\u9053\u4e0d\u53ef\u7528"})
|
||||
_ = EncodeResponse(conn, Response{Code: "CHANNEL_UNAVAILABLE", Message: "重置通道不可用"})
|
||||
return
|
||||
}
|
||||
defer guard.Discard(nonce)
|
||||
@@ -205,20 +226,20 @@ func handleResetConnection(conn io.ReadWriter, setDeadline func(time.Time) error
|
||||
}
|
||||
request, err := DecodeRequest(conn)
|
||||
if err != nil || request.Nonce != nonce {
|
||||
_ = EncodeResponse(conn, Response{Code: "INVALID_REQUEST", Message: "\u91cd\u7f6e\u8bf7\u6c42\u65e0\u6548"})
|
||||
_ = EncodeResponse(conn, Response{Code: "INVALID_REQUEST", Message: "重置请求无效"})
|
||||
return
|
||||
}
|
||||
if err := guard.Consume(request.Nonce); err != nil {
|
||||
_ = EncodeResponse(conn, Response{Code: "REPLAY_OR_EXPIRED", Message: "\u91cd\u7f6e\u4f1a\u8bdd\u5df2\u5931\u6548\u6216\u5df2\u4f7f\u7528"})
|
||||
_ = EncodeResponse(conn, Response{Code: "REPLAY_OR_EXPIRED", Message: "重置会话已失效或已使用"})
|
||||
return
|
||||
}
|
||||
password := request.NewPassword
|
||||
defer clearPassword(&password)
|
||||
if err := handler(password); err != nil {
|
||||
_ = EncodeResponse(conn, Response{Code: "RESET_FAILED", Message: "\u8d85\u7ea7\u7ba1\u7406\u5458\u5bc6\u7801\u91cd\u7f6e\u5931\u8d25"})
|
||||
_ = EncodeResponse(conn, Response{Code: "RESET_FAILED", Message: "超级管理员密码重置失败"})
|
||||
return
|
||||
}
|
||||
_ = EncodeResponse(conn, Response{OK: true, Message: "\u8d85\u7ea7\u7ba1\u7406\u5458\u5bc6\u7801\u5df2\u91cd\u7f6e"})
|
||||
_ = EncodeResponse(conn, Response{OK: true, Message: "超级管理员密码已重置"})
|
||||
}
|
||||
|
||||
func clearPassword(password *string) {
|
||||
|
||||
@@ -39,6 +39,32 @@ func TestSessionGuardRejectsExpiredNonce(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionGuardRejectsNewSessionsWhenCapacityIsFull(t *testing.T) {
|
||||
guard := NewSessionGuardWithCapacity(time.Minute, 2)
|
||||
if _, err := guard.Issue(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := guard.Issue(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := guard.Issue(); !errors.Is(err, ErrSessionCapacity) {
|
||||
t.Fatalf("capacity error = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionGuardPrunesExpiredSessionsBeforeCapacityCheck(t *testing.T) {
|
||||
guard := NewSessionGuardWithCapacity(time.Second, 1)
|
||||
now := time.Unix(100, 0)
|
||||
guard.now = func() time.Time { return now }
|
||||
if _, err := guard.Issue(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
now = now.Add(2 * time.Second)
|
||||
if _, err := guard.Issue(); err != nil {
|
||||
t.Fatalf("expired session blocked a new session: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProtocolRejectsUnknownFieldsInvalidVersionAndTrailingData(t *testing.T) {
|
||||
cases := []string{
|
||||
`{"version":1,"nonce":"n","new_password":"Password123","extra":true}`,
|
||||
@@ -52,6 +78,30 @@ func TestProtocolRejectsUnknownFieldsInvalidVersionAndTrailingData(t *testing.T)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProtocolEnforcesUnifiedPasswordLength(t *testing.T) {
|
||||
valid := []string{
|
||||
`{"version":1,"nonce":"n","new_password":"12345678"}`,
|
||||
`{"version":1,"nonce":"n","new_password":"123456789012"}`,
|
||||
`{"version":1,"nonce":"n","new_password":"\u5bc6\u7801aa"}`,
|
||||
}
|
||||
for _, input := range valid {
|
||||
if _, err := DecodeRequest(strings.NewReader(input)); err != nil {
|
||||
t.Fatalf("valid password request was rejected: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
invalid := []string{
|
||||
`{"version":1,"nonce":"n","new_password":"1234567"}`,
|
||||
`{"version":1,"nonce":"n","new_password":"1234567890123"}`,
|
||||
`{"version":1,"nonce":"n","new_password":"\u5bc6\u7801\u5bc6\u7801a"}`,
|
||||
}
|
||||
for _, input := range invalid {
|
||||
if _, err := DecodeRequest(strings.NewReader(input)); !errors.Is(err, ErrInvalidRequest) {
|
||||
t.Fatalf("invalid password request returned %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestProtocolRejectsOversizedFrame(t *testing.T) {
|
||||
input := bytes.Repeat([]byte{'x'}, maxControlMessageSize+1)
|
||||
if _, err := DecodeRequest(bytes.NewReader(input)); err == nil {
|
||||
|
||||
@@ -120,7 +120,7 @@ func (s *UnixServer) handle(conn net.Conn, handler ResetHandler, guard *SessionG
|
||||
defer conn.SetDeadline(time.Time{})
|
||||
nonce, err := guard.Issue()
|
||||
if err != nil {
|
||||
_ = EncodeResponse(conn, Response{Code: "CHANNEL_UNAVAILABLE", Message: "\u91cd\u7f6e\u901a\u9053\u4e0d\u53ef\u7528"})
|
||||
_ = EncodeResponse(conn, Response{Code: "CHANNEL_UNAVAILABLE", Message: "重置通道不可用"})
|
||||
return
|
||||
}
|
||||
defer guard.Discard(nonce)
|
||||
@@ -133,7 +133,7 @@ func (s *UnixServer) handle(conn net.Conn, handler ResetHandler, guard *SessionG
|
||||
return
|
||||
}
|
||||
if request.Nonce != nonce {
|
||||
_ = EncodeResponse(conn, Response{Code: "INVALID_REQUEST", Message: "\u91cd\u7f6e\u8bf7\u6c42\u65e0\u6548"})
|
||||
_ = EncodeResponse(conn, Response{Code: "INVALID_REQUEST", Message: "重置请求无效"})
|
||||
return
|
||||
}
|
||||
if err := guard.Consume(request.Nonce); err != nil {
|
||||
|
||||
@@ -66,7 +66,7 @@ func (s *WindowsServer) Serve(ctx context.Context, handler ResetHandler, guard *
|
||||
func handleWindowsConnection(conn net.Conn, handler ResetHandler, guard *SessionGuard) {
|
||||
defer conn.Close()
|
||||
if err := authorizeWindowsConnection(conn); err != nil {
|
||||
_ = EncodeResponse(conn, Response{Code: "UNAUTHORIZED", Message: "\u672c\u673a\u8c03\u7528\u8005\u8eab\u4efd\u4e0d\u53d7\u4fe1\u4efb"})
|
||||
_ = EncodeResponse(conn, Response{Code: "UNAUTHORIZED", Message: "本机调用者身份不受信任"})
|
||||
return
|
||||
}
|
||||
handleResetConnection(conn, conn.SetDeadline, handler, guard)
|
||||
|
||||
@@ -114,7 +114,7 @@ func Authenticate(account, plain string) (*models.User, error) {
|
||||
|
||||
// HashPassword 统一生成新密码哈希。
|
||||
func HashPassword(plain string) (string, error) {
|
||||
return password.Hash(plain)
|
||||
return password.HashUserPassword(plain)
|
||||
}
|
||||
|
||||
// SetPassword 为用户设置新密码。
|
||||
|
||||
@@ -25,10 +25,10 @@ func TestAuthenticateUsesArgon2idOnly(t *testing.T) {
|
||||
})
|
||||
|
||||
user := models.User{Account: "test-user", Name: "测试用户", IsAdmin: 1}
|
||||
if err := SetPassword(&user, "correct-password"); err != nil {
|
||||
if err := SetPassword(&user, "CorrectP12"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasPrefix(user.Password, "$argon2id$") || !password.Verify(user.Password, "correct-password") {
|
||||
if !strings.HasPrefix(user.Password, "$argon2id$") || !password.Verify(user.Password, "CorrectP12") {
|
||||
t.Fatal("测试密码哈希格式或校验失败")
|
||||
}
|
||||
if _, err := db.Instance.Insert(&user); err != nil {
|
||||
@@ -42,10 +42,10 @@ func TestAuthenticateUsesArgon2idOnly(t *testing.T) {
|
||||
if loaded.ID == 0 || !strings.HasPrefix(loaded.Password, "$argon2id$") {
|
||||
t.Fatalf("测试用户未正确写入")
|
||||
}
|
||||
if _, err := Authenticate("test-user", "correct-password"); err != nil {
|
||||
if _, err := Authenticate("test-user", "CorrectP12"); err != nil {
|
||||
t.Fatalf("新密码认证失败: %v", err)
|
||||
}
|
||||
if _, err := Authenticate("test-user", "wrong-password"); err == nil {
|
||||
if _, err := Authenticate("test-user", "WrongPass12"); err == nil {
|
||||
t.Fatal("错误密码不应认证成功")
|
||||
}
|
||||
if _, err := Authenticate("test-user", ""); err == nil {
|
||||
|
||||
@@ -29,20 +29,20 @@ func TestAuthenticateUsesUniformCredentialErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
active := models.User{Account: "active-user", Name: "active"}
|
||||
if err := SetPassword(&active, "correct-password"); err != nil {
|
||||
if err := SetPassword(&active, "CorrectP12"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
disabled := models.User{Account: "disabled-user", Name: "disabled", Disabled: 1}
|
||||
if err := SetPassword(&disabled, "correct-password"); err != nil {
|
||||
if err := SetPassword(&disabled, "CorrectP12"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := db.Instance.Insert(&active, &disabled); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, wrongErr := Authenticate("active-user", "wrong-password")
|
||||
_, missingErr := Authenticate("missing-user", "wrong-password")
|
||||
_, disabledErr := Authenticate("disabled-user", "wrong-password")
|
||||
_, wrongErr := Authenticate("active-user", "WrongPass12")
|
||||
_, missingErr := Authenticate("missing-user", "WrongPass12")
|
||||
_, disabledErr := Authenticate("disabled-user", "WrongPass12")
|
||||
if wrongErr == nil || missingErr == nil || disabledErr == nil {
|
||||
t.Fatal("credential failure unexpectedly authenticated")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,300 @@
|
||||
package dbmigration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
)
|
||||
|
||||
type Status string
|
||||
|
||||
const (
|
||||
StatusPreviewed Status = "previewed"
|
||||
StatusCreated Status = "created"
|
||||
StatusCopying Status = "copying"
|
||||
StatusVerifying Status = "verifying"
|
||||
StatusSwitching Status = "switching"
|
||||
StatusSucceeded Status = "succeeded"
|
||||
StatusFailed Status = "failed"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidRequest = errors.New("database migration request is invalid")
|
||||
ErrInvalidTargetName = errors.New("database migration target name is invalid")
|
||||
ErrImmutableSetting = errors.New("database migration may change only the database name")
|
||||
ErrMigrationNotFound = errors.New("database migration not found")
|
||||
ErrPreviewInvalid = errors.New("database migration preview is invalid")
|
||||
ErrPreviewExpired = errors.New("database migration preview expired")
|
||||
ErrMigrationState = errors.New("database migration state is invalid")
|
||||
ErrExecutorUnavailable = errors.New("database migration executor unavailable")
|
||||
ErrSourceDatabase = errors.New("source database configuration is invalid")
|
||||
)
|
||||
|
||||
const previewLifetime = 10 * time.Minute
|
||||
|
||||
var targetNamePattern = regexp.MustCompile(`^[A-Za-z][A-Za-z0-9_-]{0,62}$`)
|
||||
var sqliteTargetNamePattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$`)
|
||||
|
||||
type DatabaseIdentity struct {
|
||||
Type config.DatabaseType `json:"type"`
|
||||
Host string `json:"host,omitempty"`
|
||||
Port uint16 `json:"port,omitempty"`
|
||||
Username string `json:"username,omitempty"`
|
||||
Name string `json:"name,omitempty"`
|
||||
LocalRef string `json:"localRef,omitempty"`
|
||||
}
|
||||
|
||||
type Plan struct {
|
||||
Source DatabaseIdentity
|
||||
Target DatabaseIdentity
|
||||
}
|
||||
|
||||
type Record struct {
|
||||
ID string `json:"id"`
|
||||
Status Status `json:"status"`
|
||||
Source DatabaseIdentity `json:"source"`
|
||||
Target DatabaseIdentity `json:"target"`
|
||||
CurrentGeneration uint64 `json:"currentGeneration"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
ErrorCode string `json:"errorCode,omitempty"`
|
||||
}
|
||||
|
||||
type Runner interface {
|
||||
Run(context.Context, Plan, func(Status) error) error
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
mu sync.RWMutex
|
||||
records map[string]*record
|
||||
runner Runner
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
type record struct {
|
||||
Record
|
||||
tokenHash [32]byte
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
var current struct {
|
||||
sync.RWMutex
|
||||
value *Service
|
||||
}
|
||||
|
||||
func NewService(runner Runner) *Service {
|
||||
return &Service{records: make(map[string]*record), runner: runner, now: func() time.Time { return time.Now().UTC() }}
|
||||
}
|
||||
|
||||
func SetCurrent(service *Service) {
|
||||
current.Lock()
|
||||
current.value = service
|
||||
current.Unlock()
|
||||
}
|
||||
|
||||
func Current() *Service {
|
||||
current.RLock()
|
||||
defer current.RUnlock()
|
||||
return current.value
|
||||
}
|
||||
|
||||
func (s *Service) Preview(_ context.Context, source config.DatabaseConfig, currentGeneration uint64, targetName string) (Record, string, error) {
|
||||
if s == nil || currentGeneration == 0 {
|
||||
return Record{}, "", ErrInvalidRequest
|
||||
}
|
||||
if err := config.ValidateDatabaseConfig(source); err != nil {
|
||||
return Record{}, "", ErrSourceDatabase
|
||||
}
|
||||
targetName = strings.TrimSpace(targetName)
|
||||
if (source.Type == config.DatabaseTypeSQLite && !sqliteTargetNamePattern.MatchString(targetName)) || (source.Type != config.DatabaseTypeSQLite && !targetNamePattern.MatchString(targetName)) {
|
||||
return Record{}, "", ErrInvalidTargetName
|
||||
}
|
||||
plan, err := buildPlan(source, targetName)
|
||||
if err != nil {
|
||||
return Record{}, "", err
|
||||
}
|
||||
id, err := randomID("dbmig-")
|
||||
if err != nil {
|
||||
return Record{}, "", ErrInvalidRequest
|
||||
}
|
||||
token, err := randomID("preview-")
|
||||
if err != nil {
|
||||
return Record{}, "", ErrInvalidRequest
|
||||
}
|
||||
now := s.currentTime()
|
||||
item := &record{Record: Record{ID: id, Status: StatusPreviewed, Source: plan.Source, Target: plan.Target, CurrentGeneration: currentGeneration, CreatedAt: now, UpdatedAt: now}, tokenHash: sha256.Sum256([]byte(token)), expiresAt: now.Add(previewLifetime)}
|
||||
s.mu.Lock()
|
||||
s.records[id] = item
|
||||
s.mu.Unlock()
|
||||
return cloneRecord(item.Record), token, nil
|
||||
}
|
||||
|
||||
func (s *Service) Execute(ctx context.Context, id, token string) (Record, error) {
|
||||
if s == nil || strings.TrimSpace(id) == "" || strings.TrimSpace(token) == "" {
|
||||
return Record{}, ErrInvalidRequest
|
||||
}
|
||||
now := s.currentTime()
|
||||
s.mu.Lock()
|
||||
item, ok := s.records[id]
|
||||
if !ok {
|
||||
s.mu.Unlock()
|
||||
return Record{}, ErrMigrationNotFound
|
||||
}
|
||||
if item.Status != StatusPreviewed {
|
||||
snapshot := cloneRecord(item.Record)
|
||||
s.mu.Unlock()
|
||||
return snapshot, ErrMigrationState
|
||||
}
|
||||
if now.After(item.expiresAt) {
|
||||
item.Status = StatusFailed
|
||||
item.ErrorCode = "PREVIEW_EXPIRED"
|
||||
item.UpdatedAt = now
|
||||
s.mu.Unlock()
|
||||
return cloneRecord(item.Record), ErrPreviewExpired
|
||||
}
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
if subtle.ConstantTimeCompare(hash[:], item.tokenHash[:]) != 1 {
|
||||
s.mu.Unlock()
|
||||
return Record{}, ErrPreviewInvalid
|
||||
}
|
||||
item.tokenHash = [32]byte{}
|
||||
item.Status = StatusCreated
|
||||
item.UpdatedAt = now
|
||||
plan := Plan{Source: item.Source, Target: item.Target}
|
||||
s.mu.Unlock()
|
||||
|
||||
if err := s.setStatus(id, StatusCopying); err != nil {
|
||||
return Record{}, err
|
||||
}
|
||||
if s.runner == nil {
|
||||
return s.fail(id, ErrExecutorUnavailable)
|
||||
}
|
||||
if err := s.runner.Run(ctx, plan, func(status Status) error { return s.setStatus(id, status) }); err != nil {
|
||||
return s.fail(id, err)
|
||||
}
|
||||
if err := s.setStatus(id, StatusSucceeded); err != nil {
|
||||
return Record{}, err
|
||||
}
|
||||
return s.Get(id)
|
||||
}
|
||||
|
||||
func (s *Service) Get(id string) (Record, error) {
|
||||
if s == nil || strings.TrimSpace(id) == "" {
|
||||
return Record{}, ErrInvalidRequest
|
||||
}
|
||||
s.mu.RLock()
|
||||
item, ok := s.records[id]
|
||||
if !ok {
|
||||
s.mu.RUnlock()
|
||||
return Record{}, ErrMigrationNotFound
|
||||
}
|
||||
value := cloneRecord(item.Record)
|
||||
s.mu.RUnlock()
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (s *Service) setStatus(id string, status Status) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
item, ok := s.records[id]
|
||||
if !ok {
|
||||
return ErrMigrationNotFound
|
||||
}
|
||||
if !validStatusTransition(item.Status, status) {
|
||||
return ErrMigrationState
|
||||
}
|
||||
item.Status = status
|
||||
item.UpdatedAt = s.currentTime()
|
||||
return nil
|
||||
}
|
||||
|
||||
func validStatusTransition(current, next Status) bool {
|
||||
if current == StatusFailed || current == StatusSucceeded || next == StatusPreviewed || next == StatusFailed {
|
||||
return false
|
||||
}
|
||||
return statusRank(next) >= statusRank(current)
|
||||
}
|
||||
|
||||
func statusRank(status Status) int {
|
||||
switch status {
|
||||
case StatusCreated:
|
||||
return 1
|
||||
case StatusCopying:
|
||||
return 2
|
||||
case StatusVerifying:
|
||||
return 3
|
||||
case StatusSwitching:
|
||||
return 4
|
||||
case StatusSucceeded:
|
||||
return 5
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) fail(id string, err error) (Record, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
item, ok := s.records[id]
|
||||
if !ok {
|
||||
return Record{}, ErrMigrationNotFound
|
||||
}
|
||||
item.Status = StatusFailed
|
||||
item.ErrorCode = migrationErrorCode(err)
|
||||
item.UpdatedAt = s.currentTime()
|
||||
return cloneRecord(item.Record), err
|
||||
}
|
||||
|
||||
func (s *Service) currentTime() time.Time {
|
||||
if s.now == nil {
|
||||
return time.Now().UTC()
|
||||
}
|
||||
return s.now().UTC()
|
||||
}
|
||||
|
||||
func buildPlan(source config.DatabaseConfig, targetName string) (Plan, error) {
|
||||
source = config.NormalizeDatabaseConfig(source)
|
||||
identity := DatabaseIdentity{Type: source.Type, Host: source.Host, Port: source.Port, Username: source.Username, Name: source.Name, LocalRef: source.LocalRef}
|
||||
target := identity
|
||||
if source.Type == config.DatabaseTypeSQLite {
|
||||
target.LocalRef = targetName
|
||||
} else {
|
||||
target.Name = targetName
|
||||
}
|
||||
if target.Type != source.Type || target.Host != source.Host || target.Port != source.Port || target.Username != source.Username {
|
||||
return Plan{}, ErrImmutableSetting
|
||||
}
|
||||
return Plan{Source: identity, Target: target}, nil
|
||||
}
|
||||
|
||||
func randomID(prefix string) (string, error) {
|
||||
value := make([]byte, 16)
|
||||
if _, err := rand.Read(value); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return prefix + hex.EncodeToString(value), nil
|
||||
}
|
||||
|
||||
func cloneRecord(value Record) Record {
|
||||
return value
|
||||
}
|
||||
|
||||
func migrationErrorCode(err error) string {
|
||||
switch {
|
||||
case errors.Is(err, ErrExecutorUnavailable):
|
||||
return "EXECUTOR_UNAVAILABLE"
|
||||
case errors.Is(err, ErrPreviewExpired):
|
||||
return "PREVIEW_EXPIRED"
|
||||
default:
|
||||
return "MIGRATION_FAILED"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
package dbmigration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/utils/credential"
|
||||
)
|
||||
|
||||
type testRunner struct{}
|
||||
|
||||
func (testRunner) Run(_ context.Context, _ Plan, update func(Status) error) error {
|
||||
if err := update(StatusCopying); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := update(StatusVerifying); err != nil {
|
||||
return err
|
||||
}
|
||||
return update(StatusSwitching)
|
||||
}
|
||||
|
||||
func testDatabase(t *testing.T) config.DatabaseConfig {
|
||||
t.Helper()
|
||||
vault, err := credential.NewVault(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ciphertext, err := vault.Encrypt("password")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return config.DatabaseConfig{
|
||||
Type: config.DatabaseTypeMySQL,
|
||||
Host: "db.internal",
|
||||
Port: 3306,
|
||||
Username: "smail",
|
||||
Name: "smail",
|
||||
Password: config.StoredSecret{Ciphertext: ciphertext, Version: vault.Version(), UpdatedAt: time.Unix(1, 0).UTC()},
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreviewAndExecuteUseOnlyDatabaseName(t *testing.T) {
|
||||
service := NewService(testRunner{})
|
||||
service.now = func() time.Time { return time.Date(2026, 8, 30, 0, 0, 0, 0, time.UTC) }
|
||||
record, token, err := service.Preview(context.Background(), testDatabase(t), 7, "smail_next")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if record.Status != StatusPreviewed || record.Source.Name != "smail" || record.Target.Name != "smail_next" {
|
||||
t.Fatalf("unexpected preview: %+v", record)
|
||||
}
|
||||
if record.Source.Host != record.Target.Host || record.Source.Port != record.Target.Port || record.Source.Username != record.Target.Username {
|
||||
t.Fatalf("immutable database settings changed: %+v", record)
|
||||
}
|
||||
if token == "" {
|
||||
t.Fatal("preview token is empty")
|
||||
}
|
||||
completed, err := service.Execute(context.Background(), record.ID, token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if completed.Status != StatusSucceeded {
|
||||
t.Fatalf("migration status = %s, want succeeded", completed.Status)
|
||||
}
|
||||
if _, err := service.Execute(context.Background(), record.ID, token); !errors.Is(err, ErrMigrationState) {
|
||||
t.Fatalf("replayed token returned %v, want state error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreviewRejectsUnsafeTargetName(t *testing.T) {
|
||||
service := NewService(testRunner{})
|
||||
if _, _, err := service.Preview(context.Background(), testDatabase(t), 1, "smail;drop"); !errors.Is(err, ErrInvalidTargetName) {
|
||||
t.Fatalf("unsafe target returned %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFailedExecutionKeepsSafeState(t *testing.T) {
|
||||
service := NewService(nil)
|
||||
record, token, err := service.Preview(context.Background(), testDatabase(t), 1, "smail_next")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
failed, err := service.Execute(context.Background(), record.ID, token)
|
||||
if !errors.Is(err, ErrExecutorUnavailable) {
|
||||
t.Fatalf("execution returned %v, want executor unavailable", err)
|
||||
}
|
||||
if failed.Status != StatusFailed || failed.ErrorCode != "EXECUTOR_UNAVAILABLE" {
|
||||
t.Fatalf("unexpected failed state: %+v", failed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSQLitePreviewChangesOnlyLocalReference(t *testing.T) {
|
||||
service := NewService(testRunner{})
|
||||
source := config.DatabaseConfig{Type: config.DatabaseTypeSQLite, LocalRef: "smail.db"}
|
||||
record, _, err := service.Preview(context.Background(), source, 1, "smail-next.db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if record.Target.LocalRef != "smail-next.db" || record.Target.Type != config.DatabaseTypeSQLite || record.Target.Name != "" {
|
||||
t.Fatalf("unexpected sqlite target: %+v", record.Target)
|
||||
}
|
||||
}
|
||||
|
||||
type invalidTransitionRunner struct{}
|
||||
|
||||
func (invalidTransitionRunner) Run(_ context.Context, _ Plan, update func(Status) error) error {
|
||||
return update(StatusCreated)
|
||||
}
|
||||
|
||||
func TestMigrationRejectsBackwardStatusTransition(t *testing.T) {
|
||||
service := NewService(invalidTransitionRunner{})
|
||||
record, token, err := service.Preview(context.Background(), testDatabase(t), 1, "smail_next")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
failed, err := service.Execute(context.Background(), record.ID, token)
|
||||
if !errors.Is(err, ErrMigrationState) {
|
||||
t.Fatalf("execution returned %v, want state error", err)
|
||||
}
|
||||
if failed.Status != StatusFailed || failed.ErrorCode != "MIGRATION_FAILED" {
|
||||
t.Fatalf("unexpected failed migration: %+v", failed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrationRecordsRemainIndependentDuringConcurrentExecution(t *testing.T) {
|
||||
service := NewService(testRunner{})
|
||||
first, firstToken, err := service.Preview(context.Background(), testDatabase(t), 1, "smail_first")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, secondToken, err := service.Preview(context.Background(), testDatabase(t), 1, "smail_second")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
results := make(chan struct {
|
||||
record Record
|
||||
err error
|
||||
}, 2)
|
||||
go func() {
|
||||
record, err := service.Execute(context.Background(), first.ID, firstToken)
|
||||
results <- struct {
|
||||
record Record
|
||||
err error
|
||||
}{record: record, err: err}
|
||||
}()
|
||||
go func() {
|
||||
record, err := service.Execute(context.Background(), second.ID, secondToken)
|
||||
results <- struct {
|
||||
record Record
|
||||
err error
|
||||
}{record: record, err: err}
|
||||
}()
|
||||
for range 2 {
|
||||
result := <-results
|
||||
if result.err != nil {
|
||||
t.Fatalf("concurrent execution failed: %v", result.err)
|
||||
}
|
||||
if result.record.Status != StatusSucceeded {
|
||||
t.Fatalf("unexpected concurrent status: %+v", result.record)
|
||||
}
|
||||
if result.record.Target.Name != "smail_first" && result.record.Target.Name != "smail_second" {
|
||||
t.Fatalf("unexpected concurrent target: %+v", result.record.Target)
|
||||
}
|
||||
}
|
||||
firstState, err := service.Get(first.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
secondState, err := service.Get(second.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if firstState.Target.Name != "smail_first" || secondState.Target.Name != "smail_second" {
|
||||
t.Fatalf("migration records crossed: first=%+v second=%+v", firstState.Target, secondState.Target)
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -49,7 +50,17 @@ type SecretStatus struct {
|
||||
|
||||
// TelegramUserSettings 是 Telegram 插件唯一允许由用户维护的设置。
|
||||
type TelegramUserSettings struct {
|
||||
ChatID string `json:"chatId"`
|
||||
ChatID string `json:"chatId"`
|
||||
ShowContent bool `json:"showContent"`
|
||||
SpoilerContent bool `json:"spoilerContent"`
|
||||
SendAttachments bool `json:"sendAttachments"`
|
||||
DisableLinkPreview bool `json:"disableLinkPreview"`
|
||||
}
|
||||
|
||||
// TelegramUserSettingsEntry 用于在插件启动或配置更新后同步用户设置。
|
||||
type TelegramUserSettingsEntry struct {
|
||||
UserID int
|
||||
Settings TelegramUserSettings
|
||||
}
|
||||
|
||||
// UserSettingsStatus 返回用户本人可见的设置与乐观锁版本。
|
||||
@@ -300,6 +311,38 @@ func (s *ConfigService) GetOwnTelegramSettings(actorUserID int) (UserSettingsSta
|
||||
return s.getTelegramSettings(actorUserID)
|
||||
}
|
||||
|
||||
// ListTelegramUserSettings 返回已配置的 Telegram 用户设置,不返回任何密钥。
|
||||
func (s *ConfigService) ListTelegramUserSettings() ([]TelegramUserSettingsEntry, error) {
|
||||
if s == nil || s.engine == nil || s.vault == nil {
|
||||
return nil, ErrPluginStoreUnavailable
|
||||
}
|
||||
var records []models.PluginUserSetting
|
||||
if err := s.engine.Where("plugin_id = ? AND enabled = ?", telegramPluginID, true).Find(&records); err != nil {
|
||||
return nil, fmt.Errorf("load Telegram user settings: %w", err)
|
||||
}
|
||||
entries := make([]TelegramUserSettingsEntry, 0, len(records))
|
||||
for _, record := range records {
|
||||
if record.SchemaVersion != userSettingsSchemaVersion {
|
||||
return nil, ErrPluginStoreUnavailable
|
||||
}
|
||||
plaintext, err := s.vault.Decrypt(credential.CurrentVersion, record.SettingsCiphertext)
|
||||
if err != nil {
|
||||
return nil, ErrPluginStoreUnavailable
|
||||
}
|
||||
fingerprint, err := s.vault.Fingerprint(plaintext)
|
||||
if err != nil || fingerprint != record.SettingsHash {
|
||||
return nil, ErrPluginStoreUnavailable
|
||||
}
|
||||
settings, err := decodeTelegramSettings(plaintext)
|
||||
if err != nil {
|
||||
return nil, ErrPluginStoreUnavailable
|
||||
}
|
||||
entries = append(entries, TelegramUserSettingsEntry{UserID: record.UserID, Settings: settings})
|
||||
}
|
||||
sort.Slice(entries, func(left, right int) bool { return entries[left].UserID < entries[right].UserID })
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
// UpdateOwnTelegramSettings 的 actorUserID 必须来自已验证 Session,调用方不得传入目标用户 ID。
|
||||
func (s *ConfigService) UpdateOwnTelegramSettings(actorUserID int, settings TelegramUserSettings, expectedVersion uint64) (UserSettingsStatus, error) {
|
||||
|
||||
@@ -680,8 +723,13 @@ func validateSecretField(pluginID, fieldName string) error {
|
||||
return ErrUnknownPluginField
|
||||
}
|
||||
|
||||
func ValidateTelegramUserSettings(settings TelegramUserSettings) error {
|
||||
_, err := encodeTelegramSettings(settings)
|
||||
return err
|
||||
}
|
||||
func encodeTelegramSettings(settings TelegramUserSettings) (string, error) {
|
||||
if !telegramChatIDPattern.MatchString(strings.TrimSpace(settings.ChatID)) || settings.ChatID != strings.TrimSpace(settings.ChatID) {
|
||||
settings.ChatID = strings.TrimSpace(settings.ChatID)
|
||||
if settings.ChatID != "" && !telegramChatIDPattern.MatchString(settings.ChatID) {
|
||||
return "", ErrInvalidTelegramChatID
|
||||
}
|
||||
encoded, err := json.Marshal(settings)
|
||||
@@ -698,7 +746,7 @@ func decodeTelegramSettings(raw string) (TelegramUserSettings, error) {
|
||||
if err := decoder.Decode(&settings); err != nil {
|
||||
return TelegramUserSettings{}, err
|
||||
}
|
||||
if decoder.More() {
|
||||
if err := decoder.Decode(&struct{}{}); !errors.Is(err, io.EOF) {
|
||||
return TelegramUserSettings{}, ErrInvalidTelegramChatID
|
||||
}
|
||||
_, err := encodeTelegramSettings(settings)
|
||||
|
||||
@@ -74,6 +74,58 @@ func TestConfigServiceEncryptsTelegramUserSettingsAndRejectsTampering(t *testing
|
||||
t.Fatalf("tampered user setting error = %v", err)
|
||||
}
|
||||
}
|
||||
func TestConfigServiceAllowsEmptyTelegramChatIDToDisablePush(t *testing.T) {
|
||||
service, engine := newPluginConfigService(t)
|
||||
user := insertPluginTestUser(t, engine, "disabled-push", access.RoleUser)
|
||||
|
||||
status, err := service.UpdateOwnTelegramSettings(user.ID, TelegramUserSettings{}, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("empty chat ID update failed: %v", err)
|
||||
}
|
||||
if status.Settings.ChatID != "" || status.Version != 1 {
|
||||
t.Fatalf("unexpected empty chat ID status: %+v", status)
|
||||
}
|
||||
entries, err := service.ListTelegramUserSettings()
|
||||
if err != nil || len(entries) != 1 || entries[0].Settings.ChatID != "" {
|
||||
t.Fatalf("stored empty chat ID settings = %+v, err=%v", entries, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigServiceRejectsTrailingTelegramSettingsJSON(t *testing.T) {
|
||||
service, engine := newPluginConfigService(t)
|
||||
user := insertPluginTestUser(t, engine, "trailing-json", access.RoleUser)
|
||||
if _, err := service.UpdateOwnTelegramSettings(user.ID, TelegramUserSettings{ChatID: "123"}, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
record := models.PluginUserSetting{}
|
||||
found, err := engine.Where("plugin_id = ? AND user_id = ?", telegramPluginID, user.ID).Get(&record)
|
||||
if err != nil || !found {
|
||||
t.Fatalf("load setting record: found=%v err=%v", found, err)
|
||||
}
|
||||
plaintext, err := service.vault.Decrypt(credential.CurrentVersion, record.SettingsCiphertext)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
badPlaintext := plaintext + " {}"
|
||||
ciphertext, err := service.vault.Encrypt(badPlaintext)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fingerprint, err := service.vault.Fingerprint(badPlaintext)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = engine.ID(record.ID).Cols("settings_ciphertext", "settings_hash").Update(&models.PluginUserSetting{
|
||||
SettingsCiphertext: ciphertext,
|
||||
SettingsHash: fingerprint,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = service.GetOwnTelegramSettings(user.ID); !errors.Is(err, ErrPluginStoreUnavailable) {
|
||||
t.Fatalf("trailing JSON was accepted: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigServiceEnforcesUserSettingRolesAndVersions(t *testing.T) {
|
||||
service, engine := newPluginConfigService(t)
|
||||
super := insertPluginTestUser(t, engine, "root", access.RoleSuperAdmin)
|
||||
|
||||
@@ -284,7 +284,7 @@ func (s *Service) CompleteWithContext(ticketID, newPassword, browserContext stri
|
||||
if s == nil || s.engine == nil || s.credentials == nil || strings.TrimSpace(ticketID) == "" {
|
||||
return ErrInvalidRequest
|
||||
}
|
||||
if len([]byte(newPassword)) < 8 || len([]byte(newPassword)) > 256 {
|
||||
if password.ValidateUserPassword(newPassword) != nil {
|
||||
return ErrInvalidNewPassword
|
||||
}
|
||||
s.mu.Lock()
|
||||
|
||||
@@ -28,7 +28,7 @@ func newRecoveryFixture(t *testing.T) (*Service, *verification.Service, *xorm.En
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
password, err := auth.HashPassword("OldPassword@123")
|
||||
password, err := auth.HashPassword("OldPass123")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -85,10 +85,10 @@ func TestRecoveryRejectsCrossUserVerificationAndConsumesTicketOnce(t *testing.T)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := recoveryService.Complete(ticketID, "NewPassword@123"); err != nil {
|
||||
if err := recoveryService.Complete(ticketID, "NewPass123"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := recoveryService.Complete(ticketID, "AnotherPassword@123"); !errors.Is(err, ErrRecoveryExpired) {
|
||||
if err := recoveryService.Complete(ticketID, "NextPass123"); !errors.Is(err, ErrRecoveryExpired) {
|
||||
t.Fatalf("replayed recovery ticket error = %v", err)
|
||||
}
|
||||
var sessions []models.Sessions
|
||||
@@ -118,7 +118,7 @@ func TestRecoveryTicketConcurrentConsumptionHasOneWinner(t *testing.T) {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
results <- recoveryService.Complete(ticketID, "ConcurrentPassword@123")
|
||||
results <- recoveryService.Complete(ticketID, "RacePass123")
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
@@ -145,7 +145,7 @@ func TestRecoveryDeniesSuperAdministrator(t *testing.T) {
|
||||
deliveries++
|
||||
return nil
|
||||
}
|
||||
password, err := auth.HashPassword("SuperPassword@123")
|
||||
password, err := auth.HashPassword("SuperPass12")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -352,7 +352,7 @@ func TestRecoveryRevokesExistingStrongRecoveryCredentials(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := service.Complete(ticketID, "RecoveredPassword@123"); err != nil {
|
||||
if err := service.Complete(ticketID, "RecoverP123"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if configured, err := service.credentials.HasValidRecoveryCode(user.ID); err != nil || configured {
|
||||
@@ -379,7 +379,7 @@ func TestRecoveryTicketRejectsChangedSessionVersion(t *testing.T) {
|
||||
if _, err := engine.ID(user.ID).Cols("session_version").Update(&models.User{SessionVersion: user.SessionVersion + 1}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := service.Complete(ticketID, "ChangedVersion@123"); !errors.Is(err, ErrRecoveryDenied) {
|
||||
if err := service.Complete(ticketID, "VersionP123"); !errors.Is(err, ErrRecoveryDenied) {
|
||||
t.Fatalf("版本变化后的票据错误 = %v", err)
|
||||
}
|
||||
}
|
||||
@@ -406,10 +406,10 @@ func TestRecoveryBindsChallengeAndTicketToBrowserContext(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := service.CompleteWithContext(ticketID, "ContextPassword@123", "browser-b"); !errors.Is(err, ErrRecoveryDenied) {
|
||||
if err := service.CompleteWithContext(ticketID, "ContextP123", "browser-b"); !errors.Is(err, ErrRecoveryDenied) {
|
||||
t.Fatalf("different browser ticket result = %v", err)
|
||||
}
|
||||
if err := service.CompleteWithContext(ticketID, "ContextPassword@123", "browser-a"); err != nil {
|
||||
if err := service.CompleteWithContext(ticketID, "ContextP123", "browser-a"); err != nil {
|
||||
t.Fatalf("matching browser ticket result = %v", err)
|
||||
}
|
||||
}
|
||||
@@ -61,10 +61,10 @@ func TestWebAuthnRecoveryIssuesOneTimeTicketAndRevokesCredentials(t *testing.T)
|
||||
if err != nil || ticketID == "" || verifier.finishCalls != 1 {
|
||||
t.Fatalf("verify webauthn recovery: ticket=%q calls=%d err=%v", ticketID, verifier.finishCalls, err)
|
||||
}
|
||||
if err := service.Complete(ticketID, "WebAuthnPassword@123"); err != nil {
|
||||
if err := service.Complete(ticketID, "WebAuthnP12"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := service.Complete(ticketID, "ReplayPassword@123"); !errors.Is(err, ErrRecoveryExpired) {
|
||||
if err := service.Complete(ticketID, "ReplayP123"); !errors.Is(err, ErrRecoveryExpired) {
|
||||
t.Fatalf("replayed ticket error = %v", err)
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ func TestWebAuthnRecoveryTicketExpiresAndSuperAdminIsDenied(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
service.now = func() time.Time { return time.Now().Add(authorizationTTL + time.Second) }
|
||||
if err := service.Complete(ticketID, "ExpiredPassword@123"); !errors.Is(err, ErrRecoveryExpired) {
|
||||
if err := service.Complete(ticketID, "ExpireP123"); !errors.Is(err, ErrRecoveryExpired) {
|
||||
t.Fatalf("expired ticket error = %v", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,27 @@ type Trigger interface {
|
||||
Request(context.Context) error
|
||||
}
|
||||
|
||||
// Preview 描述固定的 Smail 重启目标,但不执行重启动作。
|
||||
type Preview struct {
|
||||
Target string `json:"target"`
|
||||
Action string `json:"action"`
|
||||
ActiveGeneration uint64 `json:"activeGeneration"`
|
||||
PendingServices []string `json:"pendingServices,omitempty"`
|
||||
ServiceManager string `json:"serviceManager"`
|
||||
TriggerAvailable bool `json:"triggerAvailable"`
|
||||
}
|
||||
|
||||
func BuildPreview(activeGeneration uint64, pendingServices []string, triggerAvailable bool) Preview {
|
||||
return Preview{
|
||||
Target: "smail",
|
||||
Action: "restart",
|
||||
ActiveGeneration: activeGeneration,
|
||||
PendingServices: append([]string(nil), pendingServices...),
|
||||
ServiceManager: "固定服务管理器",
|
||||
TriggerAvailable: triggerAvailable,
|
||||
}
|
||||
}
|
||||
|
||||
type SignalTrigger struct {
|
||||
channel chan<- bool
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package restart
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSignalTriggerRequest(t *testing.T) {
|
||||
channel := make(chan bool, 1)
|
||||
trigger := NewSignalTrigger(channel)
|
||||
if err := trigger.Request(context.Background()); err != nil {
|
||||
t.Fatalf("request failed: %v", err)
|
||||
}
|
||||
select {
|
||||
case value := <-channel:
|
||||
if !value {
|
||||
t.Fatal("restart signal was false")
|
||||
}
|
||||
default:
|
||||
t.Fatal("restart signal was not sent")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignalTriggerUnavailable(t *testing.T) {
|
||||
if err := (*SignalTrigger)(nil).Request(context.Background()); !errors.Is(err, ErrUnavailable) {
|
||||
t.Fatalf("expected unavailable error, got %v", err)
|
||||
}
|
||||
if err := NewSignalTrigger(nil).Request(context.Background()); !errors.Is(err, ErrUnavailable) {
|
||||
t.Fatalf("expected unavailable error for nil channel, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignalTriggerCanceled(t *testing.T) {
|
||||
channel := make(chan bool)
|
||||
trigger := NewSignalTrigger(channel)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if err := trigger.Request(ctx); !errors.Is(err, ErrCanceled) {
|
||||
t.Fatalf("expected canceled error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCurrentTriggerRegistry(t *testing.T) {
|
||||
SetCurrent(nil)
|
||||
if Current() != nil {
|
||||
t.Fatal("expected empty current trigger")
|
||||
}
|
||||
first := NewSignalTrigger(make(chan bool, 1))
|
||||
second := NewSignalTrigger(make(chan bool, 1))
|
||||
SetCurrent(first)
|
||||
if Current() != first {
|
||||
t.Fatal("first trigger was not registered")
|
||||
}
|
||||
SetCurrent(second)
|
||||
if Current() != second {
|
||||
t.Fatal("current trigger was not replaced")
|
||||
}
|
||||
SetCurrent(nil)
|
||||
if Current() != nil {
|
||||
t.Fatal("current trigger was not cleared")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildPreviewUsesFixedRestartTarget(t *testing.T) {
|
||||
pending := []string{"smtp", "imap"}
|
||||
preview := BuildPreview(9, pending, false)
|
||||
if preview.Target != "smail" || preview.Action != "restart" || preview.ActiveGeneration != 9 || preview.ServiceManager != "固定服务管理器" {
|
||||
t.Fatalf("unexpected restart preview: %+v", preview)
|
||||
}
|
||||
if preview.TriggerAvailable || len(preview.PendingServices) != 2 {
|
||||
t.Fatalf("unexpected restart preview availability: %+v", preview)
|
||||
}
|
||||
preview.PendingServices[0] = "changed"
|
||||
if pending[0] == "changed" {
|
||||
t.Fatal("preview shares pending service slice")
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/utils/array"
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
"git.pcux.cn/xuans/Smail/utils/errors"
|
||||
"git.pcux.cn/xuans/Smail/utils/password"
|
||||
"git.pcux.cn/xuans/Smail/utils/validation"
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"github.com/lib/pq"
|
||||
@@ -182,8 +183,8 @@ func SetAdminPassword(ctx *context.Context, account, pwd string) error {
|
||||
}
|
||||
|
||||
func validateAdminPassword(pwd string) error {
|
||||
passwordSize := len([]byte(pwd))
|
||||
if passwordSize < 8 || passwordSize > 12 {
|
||||
if err := password.ValidateUserPassword(pwd); err != nil {
|
||||
|
||||
return databaseError("管理员密码长度必须为 8 到 12 个字节", nil)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
const DefaultOperationalCleanupInterval = time.Hour
|
||||
|
||||
type OperationalCleanupRunner struct {
|
||||
mu sync.Mutex
|
||||
store *EventStore
|
||||
rateLimits *PersistentRateLimiter
|
||||
interval time.Duration
|
||||
@@ -31,18 +32,23 @@ func (r *OperationalCleanupRunner) Start() {
|
||||
return
|
||||
}
|
||||
r.once.Do(func() {
|
||||
r.mu.Lock()
|
||||
r.stop = make(chan struct{})
|
||||
r.done = make(chan struct{})
|
||||
stop, done := r.stop, r.done
|
||||
interval := r.interval
|
||||
r.mu.Unlock()
|
||||
|
||||
go func() {
|
||||
defer close(r.done)
|
||||
defer close(done)
|
||||
r.runOnce()
|
||||
ticker := time.NewTicker(r.interval)
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
r.runOnce()
|
||||
case <-r.stop:
|
||||
case <-stop:
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -51,13 +57,20 @@ func (r *OperationalCleanupRunner) Start() {
|
||||
}
|
||||
|
||||
func (r *OperationalCleanupRunner) Stop() {
|
||||
if r == nil || r.stop == nil {
|
||||
if r == nil {
|
||||
return
|
||||
}
|
||||
close(r.stop)
|
||||
<-r.done
|
||||
r.stop = nil
|
||||
r.done = nil
|
||||
r.mu.Lock()
|
||||
stop, done := r.stop, r.done
|
||||
if stop == nil {
|
||||
r.mu.Unlock()
|
||||
return
|
||||
}
|
||||
r.stop, r.done = nil, nil
|
||||
r.mu.Unlock()
|
||||
|
||||
close(stop)
|
||||
<-done
|
||||
}
|
||||
|
||||
func (r *OperationalCleanupRunner) runOnce() {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -84,3 +85,25 @@ func TestCleanupStateInsertQueryUsesPostgresPlaceholders(t *testing.T) {
|
||||
t.Fatalf("postgres cleanup query contains empty placeholders: %q", query)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOperationalCleanupRunnerStopIsIdempotentAndConcurrentSafe(t *testing.T) {
|
||||
engine := newCleanupEngine(t)
|
||||
defer engine.Close()
|
||||
store, err := NewEventStore(engine)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runner := NewOperationalCleanupRunner(store)
|
||||
runner.Start()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 8; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
runner.Stop()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
runner.Stop()
|
||||
}
|
||||
@@ -34,7 +34,7 @@ const (
|
||||
maxSessions = 1024
|
||||
)
|
||||
|
||||
// Config 闁诲氦顫夐惌顔剧不?WebAuthn 婵炴挻纰嶇换鍡欑矉閸℃稒鍎?RP 闂佸憡鐟ラ崐褰掑汲閻旂厧妞介悘鐐舵濞堝爼鏌熺拠鈥虫灈闁伙附鍨块弻锕傚Ψ瑜庣粣妤冩喐閻楀牊纾搁柍?
|
||||
// Config 保存 WebAuthn 信赖方和凭据加密存储配置。
|
||||
type Config struct {
|
||||
RPID string
|
||||
RPDisplayName string
|
||||
@@ -42,7 +42,7 @@ type Config struct {
|
||||
VaultRoot string
|
||||
}
|
||||
|
||||
// Service 闁荤姵鍔楅崰鏇㈡儗?WebAuthn 濠电偛顦崝宀勫船娴犲违濞达絿鍎ら銏㈡喐閻楀牊纾搁柍褜鍏涚粈渚€宕甸悢鐓庣妞ゆ梻鍘ч~锝夋倵闂堟稒顥滈柣掳鍔戝畷鎺楀Ω閵夛箑顏梺閫涘嫎閸婃繈寮ㄩ姀銈呂?
|
||||
// Service 管理 WebAuthn 注册、登录和凭据生命周期。
|
||||
type Service struct {
|
||||
engine *xorm.Engine
|
||||
vault string
|
||||
@@ -69,7 +69,7 @@ const (
|
||||
discoverableLoginCeremony
|
||||
)
|
||||
|
||||
// NewService 闂佸憡甯楃粙鎴犵磽?WebAuthn 闂佸搫鐗嗙粔瀛樻叏閻旂厧违闁稿本绋戦鐘绘煙鐎涙◤顏堝箯?SessionData 闂佸憡鐟禍娆戞崲濮樻墎鍋撳☉娅亜锕㈤鍛氦婵炴垶鍩冮弻銈夋煕閹邦剚鍣归柣掳鍔戞俊?
|
||||
// NewService 创建 WebAuthn 服务并校验必要配置。
|
||||
func NewService(engine *xorm.Engine, cfg Config) (*Service, error) {
|
||||
if engine == nil || strings.TrimSpace(cfg.RPID) == "" || strings.TrimSpace(cfg.RPDisplayName) == "" || len(cfg.RPOrigins) == 0 || strings.TrimSpace(cfg.VaultRoot) == "" {
|
||||
return nil, ErrUnavailable
|
||||
@@ -86,7 +86,7 @@ func NewService(engine *xorm.Engine, cfg Config) (*Service, error) {
|
||||
return &Service{engine: engine, vault: cfg.VaultRoot, authn: authn, sessions: make(map[string]ceremony), now: time.Now}, nil
|
||||
}
|
||||
|
||||
// BeginRegistration 閻庢鍠掗崑鎾斥攽椤旂⒈鍎愰柡浣哄仱瀹曟浠﹂崜褋鍋婄紓浣割儏椤戝懘鎳熼悢鍛婁氦闁哄倹瀵х粈鈧繛鎴炴尭缁夊綊鍩€椤掆偓缁绘垵危閹达附鍎嶉柛鏇ㄥ亜閺佸爼鎮楀☉娅偐妲愰幍顔藉珰?ID闂?
|
||||
// BeginRegistration 开始指定用户的 WebAuthn 注册流程。
|
||||
func (s *Service) BeginRegistration(userID int) (*protocol.CredentialCreation, string, error) {
|
||||
user, err := s.loadUser(userID)
|
||||
if err != nil {
|
||||
@@ -107,7 +107,7 @@ func (s *Service) BeginRegistration(userID int) (*protocol.CredentialCreation, s
|
||||
return creation, sessionID, nil
|
||||
}
|
||||
|
||||
// FinishRegistration 闂佸搫绋勭换婵嬫偘濞嗗緷纭呯疀濮樺吋缍岄梺闈╃祷閸斿矂骞栭柨瀣劅闁哄倽鍎荤€氭瑥菐閸ャ劎绠撻柣掳鍔戝畷婵嬫偐閼碱剚顫氶梺鍛婂焾閸樿棄鐣垫笟鈧俊?
|
||||
// FinishRegistration 完成指定用户的 WebAuthn 注册流程。
|
||||
func (s *Service) FinishRegistration(userID int, sessionID string, request *http.Request, displayName string) (*models.WebAuthnCredential, error) {
|
||||
item, err := s.takeSession(sessionID, registrationCeremony, userID)
|
||||
if err != nil {
|
||||
@@ -163,7 +163,7 @@ func (s *Service) FinishRegistration(userID int, sessionID string, request *http
|
||||
return record, nil
|
||||
}
|
||||
|
||||
// BeginLogin 閻庢鍠掗崑鎾斥攽椤旂⒈鍎忛柛鎴節閹矂濡疯閺嗗繘鏌熼幘顕呮婵?WebAuthn 闂佽皫鍡╁殭缂傚秴绉撮湁濞达綀銆€閺屻倝鏌?
|
||||
// BeginLogin 开始指定用户的 WebAuthn 登录流程。
|
||||
func (s *Service) BeginLogin(userID int) (*protocol.CredentialAssertion, string, error) {
|
||||
user, err := s.loadUser(userID)
|
||||
if err != nil {
|
||||
@@ -184,7 +184,7 @@ func (s *Service) BeginLogin(userID int) (*protocol.CredentialAssertion, string,
|
||||
return assertion, sessionID, nil
|
||||
}
|
||||
|
||||
// FinishLogin 闂佸搫绋勭换婵嬫偘濞嗘挸妫樻い鎾閺嬪懘鏌曢崱鏇犲妽婵炴彃娼″顒勫箥椤曞懎濮伴梺鍛婅壘缁夌兘顢橀幖浣告瀬闁哄鍎荤€氭瑩姊洪幓鎺斝㈤柛鐔告崌瀹曟繈鎮╅懠顒侇潥闂佸憡鍩堥崢钘夌暤娓氣偓婵?
|
||||
// FinishLogin 完成指定用户的 WebAuthn 登录流程。
|
||||
func (s *Service) FinishLogin(userID int, sessionID string, request *http.Request) (*models.WebAuthnCredential, error) {
|
||||
item, err := s.takeSession(sessionID, loginCeremony, userID)
|
||||
if err != nil {
|
||||
@@ -291,7 +291,7 @@ func (s *Service) FinishDiscoverableLogin(sessionID string, request *http.Reques
|
||||
return &user, record, nil
|
||||
}
|
||||
|
||||
// ListCredentials 闁哄鏅滈弻銊ッ洪弽顓熷殗閺夊牄鍔嶅▍娑㈡煕閹搭厼骞楃€规挷鑳跺Σ鎰板閻樼數鈹涢梺鎸庣☉婵傛梻绮径瀣氦闁哄倹瀵х粈鈧梺绋挎禋閸撶喖骞愬鍫濈闁哄诞鍐炬奖闁诲酣娼уΛ娆忣焽濡ゅ懎妫樻繛鍡楃М閸?
|
||||
// ListCredentials 列出指定用户的非敏感 WebAuthn 凭据摘要。
|
||||
func (s *Service) ListCredentials(userID int) ([]models.WebAuthnCredential, error) {
|
||||
if s == nil || s.engine == nil || userID <= 0 {
|
||||
return nil, ErrInvalidRequest
|
||||
@@ -306,7 +306,7 @@ func (s *Service) ListCredentials(userID int) ([]models.WebAuthnCredential, erro
|
||||
return records, nil
|
||||
}
|
||||
|
||||
// RenameCredential 闂佸憡鐟禍婊堝储閹寸姵濯肩紒瀣仢濞堝爼鏌熺拠鈥虫灍濠⒀冪Ф娴狅箓鎮欓崹顐f闂佸湱顥愬〒褰掑箞閵娾晛缁╅柣鐔哄閸婂磭绱掓径搴$劷闁?
|
||||
// RenameCredential 修改指定用户凭据的显示名称。
|
||||
func (s *Service) RenameCredential(userID, credentialID int, displayName string) error {
|
||||
if userID <= 0 || credentialID <= 0 || strings.TrimSpace(displayName) == "" || len([]byte(displayName)) > 128 {
|
||||
return ErrInvalidRequest
|
||||
@@ -319,7 +319,7 @@ func (s *Service) RenameCredential(userID, credentialID int, displayName string)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RevokeCredential 闂侀€涘嫎閸婃繈寮ㄩ姀锝冧汗闁规儳鍟块·鍛存煟椤剙濡介柛鈺傜洴閹啴宕熼鍡╀紘婵$偑鍊楅幊鎾诲吹閻旂厧绠叉い鏇炴缁€澶愭煙娣囧崬鈧繈寮ㄩ姀锛勨枖鐎广儱鎳庣拋鏌ユ⒑椤愩垺顥撻柍?
|
||||
// RevokeCredential 撤销指定用户的一项 WebAuthn 凭据。
|
||||
func (s *Service) RevokeCredential(userID, credentialID int) error {
|
||||
if userID <= 0 || credentialID <= 0 {
|
||||
return ErrInvalidRequest
|
||||
@@ -333,7 +333,7 @@ func (s *Service) RevokeCredential(userID, credentialID int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// RevokeAllCredentials 闂侀€涘嫎閸婃繈寮ㄩ姀銈嗗仺闁靛绠戦悡鏇㈡煟閵娿儱顏╅柛娆忕箻閺屽牓濡搁敂鐣岀暢闂?WebAuthn 闂佸憡鍩堥崢钘夌暤娓氣偓婵?
|
||||
// RevokeAllCredentials 撤销指定用户的全部 WebAuthn 凭据。
|
||||
func (s *Service) RevokeAllCredentials(userID int) error {
|
||||
if userID <= 0 {
|
||||
return ErrInvalidRequest
|
||||
|
||||
@@ -22,14 +22,26 @@ const (
|
||||
saltLength = 16
|
||||
keyLength = 32
|
||||
maxHashSize = 255
|
||||
minSecretSize = 8
|
||||
maxSecretSize = 256
|
||||
minPasswordSize = 8
|
||||
maxPasswordSize = 256
|
||||
maxPasswordSize = 12
|
||||
)
|
||||
|
||||
// ValidateUserPassword 校验登录密码的统一长度边界。
|
||||
// 长度按 UTF-8 编码后的字节数计算,避免不同入口采用不同规则。
|
||||
func ValidateUserPassword(plain string) error {
|
||||
passwordSize := len([]byte(plain))
|
||||
if passwordSize < minPasswordSize || passwordSize > maxPasswordSize {
|
||||
return ErrInvalidLength
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Hash 生成 Argon2id 密码哈希。
|
||||
func Hash(plain string) (string, error) {
|
||||
plainSize := len([]byte(plain))
|
||||
if plainSize < minPasswordSize || plainSize > maxPasswordSize {
|
||||
if plainSize < minSecretSize || plainSize > maxSecretSize {
|
||||
return "", ErrInvalidLength
|
||||
}
|
||||
salt := make([]byte, saltLength)
|
||||
@@ -61,9 +73,17 @@ func Encode(plain string) string {
|
||||
return hash
|
||||
}
|
||||
|
||||
// HashUserPassword 生成符合登录密码策略的 Argon2id 哈希。
|
||||
func HashUserPassword(plain string) (string, error) {
|
||||
if err := ValidateUserPassword(plain); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return Hash(plain)
|
||||
}
|
||||
|
||||
// Verify 校验 Argon2id 密码哈希,不接受旧算法格式。
|
||||
func Verify(encoded, plain string) bool {
|
||||
if len([]byte(plain)) > maxPasswordSize {
|
||||
if len([]byte(plain)) > maxSecretSize {
|
||||
return false
|
||||
}
|
||||
parts := strings.Split(encoded, "$")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package password
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -16,16 +17,35 @@ func TestHashAndVerify(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHashRejectsInvalidLengths(t *testing.T) {
|
||||
for _, plain := range []string{"short", strings.Repeat("a", maxPasswordSize+1)} {
|
||||
for _, plain := range []string{"short", strings.Repeat("a", maxSecretSize+1)} {
|
||||
if _, err := Hash(plain); err == nil {
|
||||
t.Fatalf("invalid password length was accepted: %d", len([]byte(plain)))
|
||||
t.Fatalf("invalid secret length was accepted: %d", len([]byte(plain)))
|
||||
}
|
||||
}
|
||||
if Verify(mustHash(t, "StrongPass123"), strings.Repeat("a", maxPasswordSize+1)) {
|
||||
if Verify(mustHash(t, "StrongPass123"), strings.Repeat("a", maxSecretSize+1)) {
|
||||
t.Fatal("oversized verification input was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserPasswordLengthBoundaries(t *testing.T) {
|
||||
for _, plain := range []string{"12345678", "123456789012", "密码aa"} {
|
||||
if err := ValidateUserPassword(plain); err != nil {
|
||||
t.Fatalf("valid password length %d was rejected: %v", len([]byte(plain)), err)
|
||||
}
|
||||
if _, err := HashUserPassword(plain); err != nil {
|
||||
t.Fatalf("valid user password was not hashed: %v", err)
|
||||
}
|
||||
}
|
||||
for _, plain := range []string{"1234567", "1234567890123", "密码密码密码"} {
|
||||
if err := ValidateUserPassword(plain); !errors.Is(err, ErrInvalidLength) {
|
||||
t.Fatalf("invalid password length %d returned %v", len([]byte(plain)), err)
|
||||
}
|
||||
if _, err := HashUserPassword(plain); !errors.Is(err, ErrInvalidLength) {
|
||||
t.Fatalf("invalid user password length %d returned %v", len([]byte(plain)), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyRejectsNonCanonicalArgon2Parameters(t *testing.T) {
|
||||
encoded := mustHash(t, "StrongPass123")
|
||||
for _, modified := range []string{
|
||||
|
||||
Reference in new issue
Block a user