1 parent
20a6eea3b3
commit
f530ab0078
10 files changed
+269
-12
No files matched your search
@@ -71,6 +71,10 @@ func ValidateRuntimeConfig(runtime RuntimeConfig, options RuntimeValidationOptio
|
||||
if runtime.Root.HTTP.HTTPSEnabled && runtime.Root.HTTP.HTTPSPort == 0 {
|
||||
return ErrInvalidRuntimeConfig
|
||||
}
|
||||
// 在保存候选配置时拒绝无效的安全密钥来源,避免保存成功却在重启时失败。
|
||||
if _, err := ResolveWebAuthnSecurity(runtime.Root); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if options.Production && runtime.SetupState == SetupStateDegraded && !runtime.Root.HTTP.HTTPSEnabled && runtime.Root.HTTP.TLSMode != HTTPSTLSModeProxy {
|
||||
return ErrInvalidRuntimeConfig
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ResolveWebAuthnSecurity 为旧配置补足安全密钥站点信息,不修改持久化配置或用户开关。
|
||||
// 信赖方只能来自受控配置,不能根据请求 Host 或转发头自动扩展。
|
||||
func ResolveWebAuthnSecurity(root RootConfig) (SecurityConfig, error) {
|
||||
security := root.Security
|
||||
security.WebAuthnRPOrigins = append([]string(nil), security.WebAuthnRPOrigins...)
|
||||
web, err := normalizeConfigDomain(root.Domains.Web)
|
||||
if err != nil {
|
||||
return SecurityConfig{}, fmt.Errorf("安全密钥缺少有效的站点域名")
|
||||
}
|
||||
if security.WebAuthnRPID == "" {
|
||||
security.WebAuthnRPID = web
|
||||
}
|
||||
rpID, err := normalizeConfigDomain(security.WebAuthnRPID)
|
||||
if err != nil || rpID != security.WebAuthnRPID {
|
||||
return SecurityConfig{}, fmt.Errorf("安全密钥信赖方标识必须是规范域名")
|
||||
}
|
||||
if security.WebAuthnRPDisplayName == "" {
|
||||
security.WebAuthnRPDisplayName = "Smail"
|
||||
}
|
||||
if strings.TrimSpace(security.WebAuthnRPDisplayName) == "" {
|
||||
return SecurityConfig{}, fmt.Errorf("安全密钥站点显示名称不能为空")
|
||||
}
|
||||
if len(security.WebAuthnRPOrigins) == 0 {
|
||||
origin := "https://" + web
|
||||
// 反代的外部端口不等于内部 HTTP 端口,非标准外部端口需显式配置。
|
||||
if root.HTTP.TLSMode != HTTPSTLSModeProxy {
|
||||
port := ResolveHTTPPortDefaults(root.HTTP, 0).HTTPSPort
|
||||
if !validServicePort(port) {
|
||||
return SecurityConfig{}, fmt.Errorf("安全密钥站点 HTTPS 端口无效")
|
||||
}
|
||||
if port != 443 {
|
||||
origin += ":" + strconv.Itoa(port)
|
||||
}
|
||||
}
|
||||
security.WebAuthnRPOrigins = []string{origin}
|
||||
}
|
||||
for _, origin := range security.WebAuthnRPOrigins {
|
||||
parsed, err := url.Parse(origin)
|
||||
if err != nil || parsed.Hostname() == "" || parsed.User != nil || parsed.Opaque != "" ||
|
||||
parsed.Path != "" || parsed.RawQuery != "" || parsed.ForceQuery || parsed.Fragment != "" ||
|
||||
strings.ContainsAny(origin, "#?\\") {
|
||||
return SecurityConfig{}, fmt.Errorf("安全密钥来源必须是完整且不含路径的站点地址")
|
||||
}
|
||||
host := parsed.Hostname()
|
||||
normalizedHost, err := normalizeConfigDomain(host)
|
||||
if err != nil || normalizedHost != host || strings.HasSuffix(parsed.Host, ":") {
|
||||
return SecurityConfig{}, fmt.Errorf("安全密钥来源必须使用规范域名和有效端口")
|
||||
}
|
||||
if host != rpID && !strings.HasSuffix(host, "."+rpID) {
|
||||
return SecurityConfig{}, fmt.Errorf("安全密钥来源不属于配置信赖方")
|
||||
}
|
||||
if parsed.Scheme != "https" && !(parsed.Scheme == "http" && host == "localhost") {
|
||||
return SecurityConfig{}, fmt.Errorf("安全密钥来源必须使用 HTTPS,本机 localhost 除外")
|
||||
}
|
||||
if port := parsed.Port(); port != "" {
|
||||
number, err := strconv.Atoi(port)
|
||||
if err != nil || !validServicePort(number) {
|
||||
return SecurityConfig{}, fmt.Errorf("安全密钥来源端口无效")
|
||||
}
|
||||
}
|
||||
}
|
||||
return security, nil
|
||||
}
|
||||
@@ -219,7 +219,7 @@ func TestDefaultMigrationChecksumsAreImmutable(t *testing.T) {
|
||||
13: "c51604f27347d0a1b93acad1e33af53463fceb7b6573960f881f95f29c5628ed",
|
||||
14: "531398c2b8fc83a65753fae08666946bb253ac3da6596829e71cc8ccc4ddf62f",
|
||||
15: "59f0ff9acfed3619c17f078d21a1362ddd5f1feacb9a2d77626f69040d25de65",
|
||||
16: "",
|
||||
16: "00cc275529e522d93b41e608692fb6e2271f1aa9300ebe020fcaed4d68d0ff76",
|
||||
}
|
||||
migrations := DefaultMigrations()
|
||||
if len(migrations) != len(expected) {
|
||||
|
||||
@@ -356,7 +356,10 @@ func startWebAuthnRuntime() error {
|
||||
return nil
|
||||
}
|
||||
snapshot := manager.Snapshot()
|
||||
security := snapshot.Root.Security
|
||||
security, err := config.ResolveWebAuthnSecurity(snapshot.Root)
|
||||
if err != nil {
|
||||
return fmt.Errorf("加载安全密钥站点配置失败: %w", err)
|
||||
}
|
||||
service, err := webauthnservice.NewService(db.Instance, webauthnservice.Config{
|
||||
RPID: security.WebAuthnRPID,
|
||||
RPDisplayName: security.WebAuthnRPDisplayName,
|
||||
|
||||
Reference in new issue
Block a user