保存模拟部署迁移与最新构建校验阶段成果
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-09-08 18:33:21 +08:00
1 parent 519e2b4a5f
commit d7837de561
45 files changed
+1588 -109

No files matched your search

+6 -2
View File
@@ -7,6 +7,7 @@ import (
"encoding/base64"
"errors"
"git.pcux.cn/xuans/Smail/utils/atomicfile"
"git.pcux.cn/xuans/Smail/utils/runtimepath"
"io"
"os"
"path/filepath"
@@ -85,7 +86,7 @@ func loadOrCreateKey(root string) ([]byte, error) {
if !os.IsNotExist(err) {
return nil, err
}
directory := filepath.Join(root, "data")
directory := runtimepath.KeyDirectory(root)
if err := os.MkdirAll(directory, 0700); err != nil {
return nil, err
}
@@ -158,13 +159,16 @@ func newCredentialKeyTemporaryName() (string, error) {
}
func readKey(root string) ([]byte, error) {
if err := runtimepath.CheckKeyLayout(root); err != nil {
return nil, err
}
// 旧密钥尚未迁移时拒绝生成新密钥,避免现有密文永久失去读取能力。
if _, err := os.Lstat(filepath.Join(root, "config", "credential.key")); err == nil {
return nil, errors.New("凭据密钥尚未迁移到 data")
} else if !os.IsNotExist(err) {
return nil, err
}
directory := filepath.Join(root, "data")
directory := runtimepath.KeyDirectory(root)
if err := validateKeyDirectory(directory); err != nil {
return nil, err
}
+84
View File
@@ -0,0 +1,84 @@
package runtimepath
import (
"errors"
"os"
"path/filepath"
)
// CheckKeyLayout 拒绝同时存在新旧密钥,避免误用另一套加密材料。
func CheckKeyLayout(root string) error {
legacy, modern := false, false
for _, name := range []string{"credential.key", "external-email-encryption.key", "external-email-fingerprint.key"} {
if _, err := os.Lstat(filepath.Join(root, "data", name)); err == nil {
legacy = true
} else if !os.IsNotExist(err) {
return err
}
if _, err := os.Lstat(filepath.Join(root, "ssl", name)); err == nil {
modern = true
} else if !os.IsNotExist(err) {
return err
}
}
if legacy && modern {
return errors.New("新旧目录同时存在密钥,必须先人工确认迁移,不能自动选择")
}
return nil
}
// DeploymentLayout 识别已迁移部署,旧部署继续只读原路径,不能静默生成替代密钥。
func DeploymentLayout(root string) bool {
if _, err := os.Lstat(filepath.Join(root, "data", "credential.key")); err == nil {
return false
}
for _, name := range []string{"data/smail-state.db", "ssl/credential.key"} {
info, err := os.Lstat(filepath.Join(root, filepath.FromSlash(name)))
if err == nil && info.Mode().IsRegular() {
return true
}
}
return false
}
func SecureDirectory(root, name string) (string, error) {
directory := filepath.Join(root, name)
if err := os.Mkdir(directory, 0700); err != nil && !os.IsExist(err) {
return "", err
}
if err := safeDirectory(directory); err != nil {
return "", err
}
return directory, os.Chmod(directory, 0700)
}
func KeyDirectory(root string) string {
if DeploymentLayout(root) {
return filepath.Join(root, "ssl")
}
return filepath.Join(root, "data")
}
func SSLReference(root, value string) string {
value = LegacyReference(value)
if !DeploymentLayout(root) {
return value
}
for _, prefix := range []string{"data/ssl", "data/dkim"} {
if value == prefix || len(value) > len(prefix) && value[:len(prefix)+1] == prefix+"/" {
if prefix == "data/dkim" {
return "ssl/dkim" + value[len(prefix):]
}
return "ssl" + value[len(prefix):]
}
}
return value
}
func SSLDirectory(root string) string {
return filepath.Join(root, filepath.FromSlash(SSLReference(root, "data/ssl")))
}
func DKIMDirectory(root string) string {
return filepath.Join(root, filepath.FromSlash(SSLReference(root, "data/dkim")))
}
+12 -2
View File
@@ -150,6 +150,9 @@ func safeDirectory(path string) error {
// CheckReady 防止遗漏迁移时创建空账号库或重新生成加密密钥。
func CheckReady(root string) error {
if err := CheckKeyLayout(root); err != nil {
return err
}
if _, err := os.Lstat(filepath.Join(root, "data", migrationLock)); err == nil || !os.IsNotExist(err) {
return errors.New("运行目录迁移尚未结束,请保留现场并核对迁移锁")
}
@@ -189,14 +192,21 @@ func CheckReady(root string) error {
required = append(required, config.Database.LocalRef)
}
for _, name := range required {
base := filepath.Join(root, "data")
if strings.HasSuffix(name, ".key") {
base = KeyDirectory(root)
}
if err := safeDirectory(base); err != nil {
return err
}
parent := filepath.Dir(name)
for parent != "." {
if err := safeDirectory(filepath.Join(root, "data", parent)); err != nil {
if err := safeDirectory(filepath.Join(base, parent)); err != nil {
return err
}
parent = filepath.Dir(parent)
}
info, err := os.Lstat(filepath.Join(root, "data", name))
info, err := os.Lstat(filepath.Join(base, name))
if err != nil || !info.Mode().IsRegular() || info.Size() == 0 {
return fmt.Errorf("已初始化部署缺少必要运行文件,拒绝重新创建:%s", name)
}