完成安全整改与配置系统重构阶段开发
Security Checks / security (push) Waiting to run

This commit is contained in:
xuans committed 2026-08-28 00:42:28 +08:00
1 parent 6ec462895f
commit 18b6dfbdf7
26 files changed
+1913 -169

No files matched your search

+9 -4
View File
@@ -261,8 +261,8 @@ func ExportConfigAudit(ctx *appcontext.Context, w http.ResponseWriter, req *http
if !ok {
return
}
if ctx.EffectiveRole() != access.RoleSuperAdmin || !ctx.RecentAuth {
writeConfigAccessError(w, errors.New("audit export requires recent super administrator authentication"))
if err := authorizeCapability(ctx, access.CapabilityAuditExport, "", false); err != nil {
writeConfigAccessError(w, err)
return
}
var input struct {
@@ -365,7 +365,7 @@ func publicConfigSection(runtime config.RuntimeConfig, section config.Section) (
publicSecret := func(value config.StoredSecret) config.SecretStatus { return value.Status() }
switch section {
case config.SectionRoot:
return map[string]any{"configVersion": runtime.Root.ConfigVersion, "schemaVersion": runtime.Root.SchemaVersion, "generation": runtime.Root.Generation, "setupState": runtime.Root.SetupState, "logLevel": runtime.Root.LogLevel, "domains": runtime.Root.Domains, "http": runtime.Root.HTTP, "database": map[string]any{"type": runtime.Root.Database.Type, "host": runtime.Root.Database.Host, "port": runtime.Root.Database.Port, "username": runtime.Root.Database.Username, "name": runtime.Root.Database.Name, "sqliteLocalRef": runtime.Root.Database.SQLiteLocalRef, "password": publicSecret(runtime.Root.Database.Password)}, "security": runtime.Root.Security}, nil
return map[string]any{"configVersion": runtime.Root.ConfigVersion, "schemaVersion": runtime.Root.SchemaVersion, "generation": runtime.Root.Generation, "setupState": runtime.Root.SetupState, "logLevel": runtime.Root.LogLevel, "domains": runtime.Root.Domains, "http": runtime.Root.HTTP, "database": map[string]any{"type": runtime.Root.Database.Type, "host": runtime.Root.Database.Host, "port": runtime.Root.Database.Port, "username": runtime.Root.Database.Username, "name": runtime.Root.Database.Name, "localRef": runtime.Root.Database.LocalRef, "password": publicSecret(runtime.Root.Database.Password)}, "security": runtime.Root.Security}, nil
case config.SectionSMTP:
return map[string]any{"meta": runtime.SMTP.Meta, "relay": map[string]any{"relayHost": runtime.SMTP.Relay.RelayHost, "relayPort": runtime.SMTP.Relay.RelayPort, "username": runtime.SMTP.Relay.Username, "security": runtime.SMTP.Relay.Security, "fromDomain": runtime.SMTP.Relay.FromDomain, "verificationEnabled": runtime.SMTP.Relay.VerificationEnabled, "loginPolicy": runtime.SMTP.Relay.LoginPolicy, "password": publicSecret(runtime.SMTP.Relay.Password)}}, nil
case config.SectionEmail:
@@ -387,6 +387,11 @@ func writeConfigAccessError(w http.ResponseWriter, err error) { writeConfigError
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 = "配置服务不可用"
@@ -395,5 +400,5 @@ func writeConfigError(w http.ResponseWriter, err error) {
status = response.ParamsError
message = "配置已发生变化,请刷新后重试"
}
response.NewErrorResponse(status, message, "").FPrint(w)
response.NewErrorResponse(status, message, data).FPrint(w)
}
+42
View File
@@ -0,0 +1,42 @@
package controllers
import (
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"git.pcux.cn/xuans/Smail/dto/response"
"git.pcux.cn/xuans/Smail/services/access"
)
func TestWriteConfigErrorKeepsStableAuthorizationCode(t *testing.T) {
recorder := httptest.NewRecorder()
writeConfigError(recorder, &access.Denial{Code: access.CodeRecoveryCodeRequired})
var payload struct {
ErrorNo int `json:"errorNo"`
Data map[string]any `json:"data"`
}
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil {
t.Fatalf("failed to decode authorization error response: %v", err)
}
if payload.ErrorNo != response.NoAccessPrivileges {
t.Fatalf("unexpected authorization error number: %d", payload.ErrorNo)
}
if payload.Data["code"] != string(access.CodeRecoveryCodeRequired) {
t.Fatalf("stable authorization code was not exposed: %#v", payload.Data)
}
}
func TestWriteConfigErrorDoesNotExposeInternalErrorText(t *testing.T) {
recorder := httptest.NewRecorder()
writeConfigError(recorder, &access.Denial{Code: access.CodeRecentAuthRequired})
body := strings.ToLower(recorder.Body.String())
for _, forbidden := range []string{"password", "secret", "token", "nonce", "ticket", "challenge"} {
if strings.Contains(body, forbidden) {
t.Fatalf("authorization response contains sensitive response text: %s", body)
}
}
}