perf(files): 优化私有文件鉴权与高成本查询隔离

This commit is contained in:
yml
2026-11-20 20:12:00 +08:00
parent e01a01c1aa
commit 9d542af51f
26 changed files with 2068 additions and 42 deletions
+14
View File
@@ -33,6 +33,7 @@ type Config struct {
FrontendShellURL string
Storage StorageConfig
StorageMirror StorageConfig
FileBindingMode string
SMS SMSConfig
Realname RealnameConfig
Log LogConfig
@@ -138,6 +139,7 @@ func Load() Config {
Region: getEnv("STORAGE_MIRROR_REGION", ""),
BucketLookup: getEnv("STORAGE_MIRROR_BUCKET_LOOKUP", "auto"),
},
FileBindingMode: strings.ToLower(strings.TrimSpace(getEnv("PRIVATE_FILE_BINDING_MODE", "prefer"))),
SMS: SMSConfig{
Provider: getEnv("SMS_PROVIDER", "mock"),
AliyunAccessKeyID: getEnv("ALIYUN_ACCESS_KEY_ID", ""),
@@ -204,6 +206,9 @@ func (c Config) ValidateProductionSecurity() error {
if !IsProductionEnv(c.AppEnv) {
return nil
}
if !validFileBindingMode(c.FileBindingMode) {
return errors.New("PRIVATE_FILE_BINDING_MODE must be legacy, prefer, or strict")
}
if !strings.EqualFold(strings.TrimSpace(c.SMS.Provider), "aliyun") {
return errors.New("SMS_PROVIDER must be aliyun in production")
}
@@ -235,6 +240,15 @@ func (c Config) ValidateProductionSecurity() error {
return nil
}
func validFileBindingMode(mode string) bool {
switch strings.ToLower(strings.TrimSpace(mode)) {
case "legacy", "prefer", "strict":
return true
default:
return false
}
}
func getEnv(key, fallback string) string {
value := os.Getenv(key)
if value == "" {
+15
View File
@@ -118,6 +118,21 @@ func TestLoadOCRServiceURL(t *testing.T) {
}
}
func TestFileBindingMode(t *testing.T) {
t.Setenv("PRIVATE_FILE_BINDING_MODE", " STRICT ")
if got := Load().FileBindingMode; got != "strict" {
t.Fatalf("FileBindingMode = %q, want strict", got)
}
for _, mode := range []string{"legacy", "prefer", "strict"} {
if !validFileBindingMode(mode) {
t.Fatalf("validFileBindingMode(%q) = false", mode)
}
}
if validFileBindingMode("invalid") {
t.Fatal("invalid file binding mode accepted")
}
}
// TestFieldEncryptionLegacyKeyNonProductionDefaultsToHistoric 验证非生产环境未设置 legacy 时
// 回退到历史硬编码密钥(开发态零配置兼容旧密文)。
func TestFieldEncryptionLegacyKeyNonProductionDefaultsToHistoric(t *testing.T) {