修复外部证书签发与续期文件命名不一致
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-10-02 15:15:01 +08:00
1 parent 73753ac4e1
commit 9465b4fee9
5 files changed
+21 -16

No files matched your search

+1 -1
View File
@@ -26,7 +26,7 @@ func run(args []string) error {
historySource := flags.String("deployment-history", "", "停服复制完成后,从原部署导入配置历史并转换证书引用")
exportTLS := flags.String("export-project-tls", "", "将项目托管证书导出为本机 TLS 入口使用的合并 PEM 文件")
publishTLS := flags.String("publish-external-tls", "", "将外部续期证书校验并原子发布为 Nginx 使用的合并 PEM 文件")
externalTLSDirectory := flags.String("external-tls-dir", "", "外部续期目录,包含权限受保护的 smail.crt 和 smail.key")
externalTLSDirectory := flags.String("external-tls-dir", "", "外部续期目录,包含以邮件主域名命名且权限受保护的证书与私钥")
if err := flags.Parse(args); err != nil {
return err
}
+7 -2
View File
@@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"git.pcux.cn/xuans/Smail/utils/atomicfile"
)
@@ -38,7 +39,7 @@ func ExportProjectTLS(root, destination string) error {
}
// PublishExternalTLS 仅由部署管理员显式提供外部续期目录,不从网页配置读取高权限源路径。
// 外部工具统一使用 smail 文件名;校验失败时不替换入口的有效证书。
// 外部工具使用邮件主域名作为文件名,与续期读取规则一致;校验失败时保留入口证书。
func PublishExternalTLS(root, sourceDirectory, destination string) error {
if runtime.GOOS != "linux" {
return errors.New("本机 Nginx 证书同步仅支持 Linux")
@@ -57,7 +58,11 @@ func PublishExternalTLS(root, sourceDirectory, destination string) error {
if filepath.Dir(destination) == sourceDirectory {
return ErrUnsafeSSLFile
}
return exportTLSBundle(sourceDirectory, "smail.crt", "smail.key", destination, validationCertificateDomains(&runtime))
domain := runtime.Root.Domains.Primary
if domain == "" || domain == "." || domain == ".." || strings.ContainsAny(domain, `/\:`) {
return ErrUnsafeSSLFile
}
return exportTLSBundle(sourceDirectory, domain+".crt", domain+".key", destination, validationCertificateDomains(&runtime))
}
func exportTLSBundle(sourceDirectory, certificateName, privateKeyName, destination string, domains []string) error {
+10 -10
View File
@@ -35,8 +35,8 @@ func TestPublishExternalTLSValidatesBeforeReplacingBundle(t *testing.T) {
t.Fatal(err)
}
}
write("smail.crt", certificate)
write("smail.key", key)
write("example.com.crt", certificate)
write("example.com.key", key)
destination := filepath.Join(output, "tls.pem")
if err := PublishExternalTLS(root, source, destination); err != nil {
t.Fatal(err)
@@ -69,22 +69,22 @@ func TestPublishExternalTLSValidatesBeforeReplacingBundle(t *testing.T) {
t.Fatal("failed publication replaced the previous bundle")
}
}
write("smail.key", []byte("invalid"))
write("example.com.key", []byte("invalid"))
assertRejected()
wrongKey, wrongCertificate := testManagedCertificate(t, "other.example", time.Now().Add(-time.Hour), time.Now().Add(time.Hour), 2048)
write("smail.key", wrongKey)
write("smail.crt", wrongCertificate)
write("example.com.key", wrongKey)
write("example.com.crt", wrongCertificate)
assertRejected()
write("smail.key", key)
write("smail.crt", certificate)
if err := os.Chmod(filepath.Join(source, "smail.key"), 0644); err != nil {
write("example.com.key", key)
write("example.com.crt", certificate)
if err := os.Chmod(filepath.Join(source, "example.com.key"), 0644); err != nil {
t.Fatal(err)
}
assertRejected()
if err := os.Chmod(filepath.Join(source, "smail.key"), 0600); err != nil {
if err := os.Chmod(filepath.Join(source, "example.com.key"), 0600); err != nil {
t.Fatal(err)
}
if err := PublishExternalTLS(root, source, filepath.Join(source, "smail.crt")); err == nil {
if err := PublishExternalTLS(root, source, filepath.Join(source, "example.com.crt")); err == nil {
t.Fatal("source overwrite allowed")
}
candidate.Root.HTTP.TLSMode = HTTPSTLSModeDirect