diff --git a/backend/.env.example b/backend/.env.example index bf479f8c..dd4678a4 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -69,6 +69,10 @@ STORAGE_MIRROR_SECRET_ACCESS_KEY= STORAGE_MIRROR_REGION= STORAGE_MIRROR_BUCKET_LOOKUP=auto +# 私有文件对象鉴权:legacy=仅旧上传记录/业务 JSON 回退,prefer=绑定优先并允许回退,strict=只信绑定表。 +# 生产切换 strict 前必须先完成历史回填,并确认慢日志中的 legacy_reference 接近 0。 +PRIVATE_FILE_BINDING_MODE=prefer + # 短信服务:本地开发使用 mock。 SMS_PROVIDER=mock ALIYUN_ACCESS_KEY_ID= diff --git a/backend/Dockerfile b/backend/Dockerfile index b37f029e..0c159355 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -27,6 +27,10 @@ RUN --mount=type=cache,target=/go/pkg/mod \ RUN --mount=type=cache,target=/go/pkg/mod \ --mount=type=cache,target=/root/.cache/go-build \ CGO_ENABLED=0 GOOS=linux go build -o /out/hfb-pickup-points-backfill ./cmd/pickup-points-backfill +# 一次性运维脚本:回填私有文件对象绑定(默认预演,-dry-run=false 才写库) +RUN --mount=type=cache,target=/go/pkg/mod \ + --mount=type=cache,target=/root/.cache/go-build \ + CGO_ENABLED=0 GOOS=linux go build -o /out/hfb-file-object-bindings-backfill ./cmd/file-object-bindings-backfill FROM alpine:3.22 @@ -44,6 +48,7 @@ COPY --from=build /out/hfb-api /app/hfb-api COPY --from=build /out/hfb-storage-migrate /app/hfb-storage-migrate COPY --from=build /out/hfb-rebate-backfill /app/hfb-rebate-backfill COPY --from=build /out/hfb-pickup-points-backfill /app/hfb-pickup-points-backfill +COPY --from=build /out/hfb-file-object-bindings-backfill /app/hfb-file-object-bindings-backfill COPY --from=build /go/bin/goose /app/goose COPY --from=build /src/migrations /app/migrations USER app:app diff --git a/backend/cmd/api/main.go b/backend/cmd/api/main.go index e47258a9..43e255cd 100644 --- a/backend/cmd/api/main.go +++ b/backend/cmd/api/main.go @@ -110,6 +110,7 @@ func main() { jobCtx, stopJobs := context.WithCancel(context.Background()) defer stopJobs() if deps.DB != nil { + database.StartPoolStatsLogger(jobCtx, deps.DB, logger, 30*time.Second) var paymentRepo *payment.Repository if paymentConfigRepo := newPaymentConfigRepositoryForJobs(cfg, deps.DB, logger); paymentConfigRepo != nil { // 超时取消前的渠道核对要把收款推进到订单上,所以这里必须带真实的订单仓库, diff --git a/backend/cmd/file-object-bindings-backfill/main.go b/backend/cmd/file-object-bindings-backfill/main.go new file mode 100644 index 00000000..7ffb3f3e --- /dev/null +++ b/backend/cmd/file-object-bindings-backfill/main.go @@ -0,0 +1,391 @@ +// file-object-bindings-backfill 回填私有文件对象与业务资源的绑定关系。 +// +// 默认只预演。实际写入时按主键游标分批提交,可重复执行;指定 -checkpoint +// 后会记录每个来源已完成的最后主键,进程中断后可从断点继续。 +package main + +import ( + "context" + "encoding/json" + "flag" + "fmt" + "log" + "os" + "path/filepath" + "sort" + "strings" + "time" + + "hfb_sys/backend/internal/config" + "hfb_sys/backend/internal/database" + "hfb_sys/backend/internal/filekey" + "hfb_sys/backend/internal/logging" + "hfb_sys/backend/internal/model" + + "gorm.io/datatypes" + "gorm.io/gorm" + "gorm.io/gorm/clause" +) + +type sourceSpec struct { + Name string + SQL string +} + +type sourceRow struct { + ID uint64 `gorm:"column:id"` + TenantID uint64 `gorm:"column:tenant_id"` + OwnerUserID uint64 `gorm:"column:owner_user_id"` + ResourceID uint64 `gorm:"column:resource_id"` + ConversationID *uint64 `gorm:"column:conversation_id"` + Keys datatypes.JSON `gorm:"column:object_keys"` + ScalarKey string `gorm:"column:scalar_key"` +} + +type sourceStats struct { + Rows int64 `json:"rows"` + References int64 `json:"references"` + InvalidKeys int64 `json:"invalid_keys"` + MalformedJSON int64 `json:"malformed_json"` + NoResource int64 `json:"no_resource"` + InsertedBindings int64 `json:"inserted_bindings"` + ExistingBindings int64 `json:"existing_bindings"` +} + +type checkpointFile struct { + UpdatedAt string `json:"updated_at"` + Sources map[string]uint64 `json:"sources"` +} + +var sources = []sourceSpec{ + { + Name: "game_accounts", + SQL: `SELECT id, source_tenant_id AS tenant_id, owner_id AS owner_user_id, + id AS resource_id, NULL AS conversation_id, screenshot_urls AS object_keys, + '' AS scalar_key + FROM game_accounts WHERE id > ? ORDER BY id ASC LIMIT ?`, + }, + { + Name: "chat_messages", + SQL: `SELECT id, 0 AS tenant_id, 0 AS owner_user_id, conversation_id AS resource_id, + NULL AS conversation_id, attachment_urls AS object_keys, '' AS scalar_key + FROM chat_messages WHERE id > ? ORDER BY id ASC LIMIT ?`, + }, + { + Name: "handoff_records", + SQL: `SELECT id, 0 AS tenant_id, 0 AS owner_user_id, order_id AS resource_id, + NULL AS conversation_id, attachment_urls AS object_keys, '' AS scalar_key + FROM handoff_records WHERE id > ? ORDER BY id ASC LIMIT ?`, + }, + { + Name: "disputes", + SQL: `SELECT id, 0 AS tenant_id, 0 AS owner_user_id, order_id AS resource_id, + NULL AS conversation_id, evidence_urls AS object_keys, '' AS scalar_key + FROM disputes WHERE id > ? ORDER BY id ASC LIMIT ?`, + }, + { + Name: "order_checkouts", + SQL: `SELECT id, 0 AS tenant_id, 0 AS owner_user_id, order_id AS resource_id, + NULL AS conversation_id, evidence_urls AS object_keys, '' AS scalar_key + FROM order_checkouts WHERE id > ? ORDER BY id ASC LIMIT ?`, + }, + { + Name: "user_payment_accounts", + SQL: `SELECT id, tenant_id, user_id AS owner_user_id, id AS resource_id, + NULL AS conversation_id, certificate_urls AS object_keys, '' AS scalar_key + FROM user_payment_accounts WHERE id > ? ORDER BY id ASC LIMIT ?`, + }, + { + Name: "withdrawal_requests", + SQL: `SELECT id, tenant_id, user_id AS owner_user_id, id AS resource_id, + NULL AS conversation_id, NULL AS object_keys, payment_proof_url AS scalar_key + FROM withdrawal_requests WHERE id > ? ORDER BY id ASC LIMIT ?`, + }, + { + Name: "chat_qrcode_pool", + SQL: `SELECT id, tenant_id, 0 AS owner_user_id, id AS resource_id, + conversation_id, NULL AS object_keys, image_url AS scalar_key + FROM chat_qrcode_pool WHERE id > ? ORDER BY id ASC LIMIT ?`, + }, + { + Name: "operating_expenses", + SQL: `SELECT id, 0 AS tenant_id, 0 AS owner_user_id, id AS resource_id, + NULL AS conversation_id, NULL AS object_keys, voucher_url AS scalar_key + FROM operating_expenses WHERE id > ? ORDER BY id ASC LIMIT ?`, + }, + { + Name: "manual_disbursements", + SQL: `SELECT id, 0 AS tenant_id, 0 AS owner_user_id, id AS resource_id, + NULL AS conversation_id, NULL AS object_keys, voucher_url AS scalar_key + FROM manual_disbursements WHERE id > ? ORDER BY id ASC LIMIT ?`, + }, +} + +func main() { + dryRun := flag.Bool("dry-run", true, "只统计不写库;实际回填时传 -dry-run=false") + batchSize := flag.Int("batch-size", 500, "每批主键记录数") + onlySource := flag.String("source", "", "只处理指定来源,例如 chat_messages") + afterID := flag.Uint64("after-id", 0, "所有来源从该主键之后开始,仅用于手动续跑") + checkpointPath := flag.String("checkpoint", "", "断点 JSON 文件路径;为空时不持久化断点") + flag.Parse() + + if *batchSize < 1 || *batchSize > 5000 { + log.Fatal("-batch-size 必须在 1 到 5000 之间") + } + selected := selectSources(*onlySource) + if len(selected) == 0 { + log.Fatalf("未知来源 %q", *onlySource) + } + + cfg := config.Load() + logger, err := logging.New(cfg.Log) + if err != nil { + log.Fatalf("初始化日志失败: %v", err) + } + defer func() { + _ = logger.Sync() + }() + db, err := database.OpenMySQLWithOptions(cfg.MySQLDSN, cfg.Log.Level, logger, database.MySQLOptions{}) + if err != nil { + log.Fatalf("连接数据库失败: %v", err) + } + + checkpoint := loadCheckpoint(*checkpointPath) + mode := "预演" + if !*dryRun { + mode = "实际回填" + } + fmt.Printf("模式=%s 批次=%d 来源=%d 断点=%s\n", mode, *batchSize, len(selected), emptyAsNone(*checkpointPath)) + + for _, source := range selected { + after := checkpoint.Sources[source.Name] + if *afterID > after { + after = *afterID + } + stats, lastID, err := runSource(db, source, after, *batchSize, *dryRun, func(next uint64) error { + if *dryRun || *checkpointPath == "" { + return nil + } + checkpoint.Sources[source.Name] = next + return saveCheckpoint(*checkpointPath, checkpoint) + }) + if err != nil { + log.Fatalf("来源 %s 回填失败(最后主键 %d): %v", source.Name, lastID, err) + } + fmt.Printf( + "[%s] 扫描记录=%d 对象引用=%d 新增绑定=%d 已存在=%d 无效key=%d JSON解析失败=%d 无资源关联=%d 最后主键=%d\n", + source.Name, + stats.Rows, + stats.References, + stats.InsertedBindings, + stats.ExistingBindings, + stats.InvalidKeys, + stats.MalformedJSON, + stats.NoResource, + lastID, + ) + } +} + +func selectSources(name string) []sourceSpec { + name = strings.TrimSpace(name) + if name == "" { + return sources + } + for _, source := range sources { + if source.Name == name { + return []sourceSpec{source} + } + } + return nil +} + +func runSource( + db *gorm.DB, + source sourceSpec, + afterID uint64, + batchSize int, + dryRun bool, + checkpoint func(uint64) error, +) (sourceStats, uint64, error) { + var stats sourceStats + cursor := afterID + for { + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + var rows []sourceRow + err := db.WithContext(ctx).Raw(source.SQL, cursor, batchSize).Scan(&rows).Error + cancel() + if err != nil { + return stats, cursor, err + } + if len(rows) == 0 { + return stats, cursor, nil + } + + bindings := make([]model.FileObjectBinding, 0, len(rows)*2) + seen := make(map[string]struct{}) + for _, row := range rows { + stats.Rows++ + cursor = row.ID + resourceType, resourceID, scope, ownerUserID, tenantID := bindingTarget(source.Name, row) + keys, malformed, invalid := canonicalKeys(row) + stats.MalformedJSON += int64(malformed) + stats.InvalidKeys += int64(invalid) + if resourceID == 0 { + if len(keys) > 0 || malformed > 0 || invalid > 0 { + stats.NoResource++ + } + continue + } + for _, key := range keys { + stats.References++ + identity := key + "\x00" + resourceType + "\x00" + scope + if _, exists := seen[identity]; exists { + stats.ExistingBindings++ + continue + } + seen[identity] = struct{}{} + bindings = append(bindings, model.FileObjectBinding{ + TenantID: tenantID, + CanonicalKey: key, + ResourceType: resourceType, + ResourceID: resourceID, + AccessScope: scope, + OwnerUserID: ownerUserID, + }) + } + } + + if !dryRun && len(bindings) > 0 { + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + err := db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { + result := tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&bindings) + if result.Error != nil { + return result.Error + } + stats.InsertedBindings += result.RowsAffected + stats.ExistingBindings += int64(len(bindings)) - result.RowsAffected + return nil + }) + cancel() + if err != nil { + return stats, cursor, err + } + } else { + stats.InsertedBindings += int64(len(bindings)) + } + if err := checkpoint(cursor); err != nil { + return stats, cursor, err + } + if len(rows) < batchSize { + return stats, cursor, nil + } + } +} + +func bindingTarget(source string, row sourceRow) (resourceType string, resourceID uint64, scope string, ownerUserID, tenantID uint64) { + switch source { + case "game_accounts": + return model.FileResourceGameAccount, row.ResourceID, model.FileScopeListingAsset, row.OwnerUserID, row.TenantID + case "chat_messages": + return model.FileResourceChatConversation, row.ResourceID, model.FileScopeChatParticipant, 0, 0 + case "handoff_records", "disputes", "order_checkouts": + return model.FileResourceOrder, row.ResourceID, model.FileScopeOrderParticipant, 0, 0 + case "user_payment_accounts": + return model.FileResourcePaymentAccount, row.ResourceID, model.FileScopeOwner, row.OwnerUserID, row.TenantID + case "withdrawal_requests": + return model.FileResourceWithdrawal, row.ResourceID, model.FileScopeOwner, row.OwnerUserID, row.TenantID + case "chat_qrcode_pool": + if row.ConversationID != nil && *row.ConversationID > 0 { + return model.FileResourceChatConversation, *row.ConversationID, model.FileScopeQrCodeParticipant, 0, row.TenantID + } + return model.FileResourceChatQrCode, row.ResourceID, model.FileScopeQrCodeTenant, 0, row.TenantID + case "operating_expenses": + return model.FileResourceOperatingExpense, row.ResourceID, model.FileScopePlatformSuperAdmin, 0, 0 + case "manual_disbursements": + return model.FileResourceManualDisbursement, row.ResourceID, model.FileScopePlatformSuperAdmin, 0, 0 + default: + return "", 0, "", 0, 0 + } +} + +func canonicalKeys(row sourceRow) ([]string, int, int) { + keys := make([]string, 0, 2) + malformed := 0 + invalid := 0 + if len(row.Keys) > 0 { + var rawKeys []string + if err := json.Unmarshal(row.Keys, &rawKeys); err != nil { + malformed++ + } else { + keys = append(keys, rawKeys...) + } + } + if strings.TrimSpace(row.ScalarKey) != "" { + keys = append(keys, row.ScalarKey) + } + + seen := make(map[string]struct{}, len(keys)) + canonical := make([]string, 0, len(keys)) + for _, raw := range keys { + key := filekey.Canonical(raw) + if key == "" { + invalid++ + continue + } + if _, exists := seen[key]; exists { + continue + } + seen[key] = struct{}{} + canonical = append(canonical, key) + } + return canonical, malformed, invalid +} + +func loadCheckpoint(path string) checkpointFile { + checkpoint := checkpointFile{Sources: make(map[string]uint64)} + if strings.TrimSpace(path) == "" { + return checkpoint + } + payload, err := os.ReadFile(path) + if err != nil { + return checkpoint + } + if err := json.Unmarshal(payload, &checkpoint); err != nil || checkpoint.Sources == nil { + checkpoint.Sources = make(map[string]uint64) + } + return checkpoint +} + +func saveCheckpoint(path string, checkpoint checkpointFile) error { + checkpoint.UpdatedAt = time.Now().Format(time.RFC3339) + payload, err := json.MarshalIndent(checkpoint, "", " ") + if err != nil { + return err + } + dir := filepath.Dir(path) + if dir != "." { + if err := os.MkdirAll(dir, 0o755); err != nil { + return err + } + } + temp := path + ".tmp" + if err := os.WriteFile(temp, payload, 0o600); err != nil { + return err + } + return os.Rename(temp, path) +} + +func emptyAsNone(value string) string { + if strings.TrimSpace(value) == "" { + return "未启用" + } + return value +} + +func init() { + // 保证来源顺序稳定,便于运维日志对比。 + sort.SliceStable(sources, func(i, j int) bool { + return sources[i].Name < sources[j].Name + }) +} diff --git a/backend/cmd/file-object-bindings-backfill/main_test.go b/backend/cmd/file-object-bindings-backfill/main_test.go new file mode 100644 index 00000000..f27c5f1f --- /dev/null +++ b/backend/cmd/file-object-bindings-backfill/main_test.go @@ -0,0 +1,52 @@ +package main + +import ( + "testing" + + "hfb_sys/backend/internal/model" + + "gorm.io/datatypes" +) + +func TestCanonicalKeysHandlesJSONScalarAndInvalidReferences(t *testing.T) { + row := sourceRow{ + Keys: datatypes.JSON([]byte(`[ + "/api/files/object?key=listing%2Fa.webp", + "listing/a.webp", + "https://external.example.com/file.webp" + ]`)), + ScalarKey: "/api/files/object?key=listing%2Fb.webp", + } + keys, malformed, invalid := canonicalKeys(row) + if malformed != 0 || invalid != 1 { + t.Fatalf("malformed=%d invalid=%d, want 0/1", malformed, invalid) + } + if len(keys) != 2 || keys[0] != "listing/a.webp" || keys[1] != "listing/b.webp" { + t.Fatalf("keys = %#v", keys) + } +} + +func TestCanonicalKeysReportsMalformedJSON(t *testing.T) { + keys, malformed, invalid := canonicalKeys(sourceRow{ + Keys: datatypes.JSON([]byte(`{"bad":true}`)), + }) + if len(keys) != 0 || malformed != 1 || invalid != 0 { + t.Fatalf("keys=%#v malformed=%d invalid=%d", keys, malformed, invalid) + } +} + +func TestBindingTargetForConversationQrCode(t *testing.T) { + conversationID := uint64(88) + resourceType, resourceID, scope, ownerUserID, tenantID := bindingTarget("chat_qrcode_pool", sourceRow{ + ResourceID: 9, + TenantID: 101, + ConversationID: &conversationID, + }) + if resourceType != model.FileResourceChatConversation || + resourceID != conversationID || + scope != model.FileScopeQrCodeParticipant || + ownerUserID != 0 || + tenantID != 101 { + t.Fatalf("target = %s/%d/%s owner=%d tenant=%d", resourceType, resourceID, scope, ownerUserID, tenantID) + } +} diff --git a/backend/internal/config/config.go b/backend/internal/config/config.go index fe108d81..d681e61d 100644 --- a/backend/internal/config/config.go +++ b/backend/internal/config/config.go @@ -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 == "" { diff --git a/backend/internal/config/config_test.go b/backend/internal/config/config_test.go index 7252c5fd..67fe409e 100644 --- a/backend/internal/config/config_test.go +++ b/backend/internal/config/config_test.go @@ -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) { diff --git a/backend/internal/database/pool_stats.go b/backend/internal/database/pool_stats.go new file mode 100644 index 00000000..ca479200 --- /dev/null +++ b/backend/internal/database/pool_stats.go @@ -0,0 +1,60 @@ +package database + +import ( + "context" + "time" + + "go.uber.org/zap" + "gorm.io/gorm" +) + +// StartPoolStatsLogger 周期性记录连接池压力,便于区分 SQL 慢和连接排队。 +func StartPoolStatsLogger(ctx context.Context, db *gorm.DB, logger *zap.Logger, interval time.Duration) { + if db == nil || logger == nil { + return + } + sqlDB, err := db.DB() + if err != nil { + return + } + if interval <= 0 { + interval = 30 * time.Second + } + var lastWaitCount int64 + logStats := func() { + stats := sqlDB.Stats() + waitDelta := stats.WaitCount - lastWaitCount + lastWaitCount = stats.WaitCount + fields := []zap.Field{ + zap.Int("open_connections", stats.OpenConnections), + zap.Int("in_use", stats.InUse), + zap.Int("idle", stats.Idle), + zap.Int("max_open_connections", stats.MaxOpenConnections), + zap.Int64("wait_count", stats.WaitCount), + zap.Float64("wait_duration_ms", float64(stats.WaitDuration.Microseconds())/1000), + zap.Int64("max_idle_closed", stats.MaxIdleClosed), + zap.Int64("max_idle_time_closed", stats.MaxIdleTimeClosed), + zap.Int64("max_lifetime_closed", stats.MaxLifetimeClosed), + } + highPressure := waitDelta > 0 || + (stats.MaxOpenConnections > 0 && stats.InUse >= stats.MaxOpenConnections*4/5) + if highPressure { + logger.Warn("数据库连接池压力", fields...) + return + } + logger.Info("数据库连接池状态", fields...) + } + logStats() + go func() { + ticker := time.NewTicker(interval) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + logStats() + } + } + }() +} diff --git a/backend/internal/filekey/filekey.go b/backend/internal/filekey/filekey.go new file mode 100644 index 00000000..c38ca0d3 --- /dev/null +++ b/backend/internal/filekey/filekey.go @@ -0,0 +1,54 @@ +package filekey + +import ( + "net/url" + "strings" +) + +const objectEndpointSuffix = "/files/object" + +// Canonical returns the stored object key for current and legacy file URLs. +func Canonical(raw string) string { + value := strings.TrimSpace(strings.Trim(raw, `"'`)) + if value == "" || strings.Contains(value, "..") { + return "" + } + + if parsed, err := url.Parse(value); err == nil { + if key := parsed.Query().Get("key"); key != "" && strings.HasSuffix(parsed.Path, objectEndpointSuffix) { + return normalizeKey(key) + } + if parsed.Scheme != "" || parsed.Host != "" { + return "" + } + } + + if index := strings.Index(value, "?key="); index >= 0 { + query, err := url.ParseQuery(value[index+1:]) + if err == nil && query.Get("key") != "" { + return normalizeKey(query.Get("key")) + } + } + return normalizeKey(value) +} + +func normalizeKey(raw string) string { + key := strings.TrimSpace(raw) + for attempt := 0; attempt < 2; attempt++ { + decoded, err := url.QueryUnescape(key) + if err != nil || decoded == key { + break + } + key = decoded + } + key = strings.TrimLeft(strings.TrimSpace(key), "/") + if key == "" || strings.Contains(key, "..") { + return "" + } + for _, suffix := range []string{".medium.jpg", ".thumb.jpg"} { + if strings.HasSuffix(key, suffix) { + return strings.TrimSuffix(key, suffix) + } + } + return key +} diff --git a/backend/internal/filekey/filekey_test.go b/backend/internal/filekey/filekey_test.go new file mode 100644 index 00000000..4aba9802 --- /dev/null +++ b/backend/internal/filekey/filekey_test.go @@ -0,0 +1,21 @@ +package filekey + +import "testing" + +func TestCanonical(t *testing.T) { + tests := map[string]string{ + "listing/2026/11/a.webp": "listing/2026/11/a.webp", + "/api/files/object?key=listing%2F2026%2F11%2Fa.webp": "listing/2026/11/a.webp", + "/api/admin/files/object?key=listing/2026/11/a.webp": "listing/2026/11/a.webp", + "/api/files/object?key=listing%2F2026%2F11%2Fa.webp.medium.jpg": "listing/2026/11/a.webp", + "https://example.com/external.png": "", + "../secret": "", + } + for raw, want := range tests { + t.Run(raw, func(t *testing.T) { + if got := Canonical(raw); got != want { + t.Fatalf("Canonical(%q) = %q, want %q", raw, got, want) + } + }) + } +} diff --git a/backend/internal/middleware/request_guard.go b/backend/internal/middleware/request_guard.go new file mode 100644 index 00000000..92bc5735 --- /dev/null +++ b/backend/internal/middleware/request_guard.go @@ -0,0 +1,197 @@ +package middleware + +import ( + "context" + "errors" + "net/http" + "strings" + "sync" + "time" + + "hfb_sys/backend/internal/tenancy" + "hfb_sys/backend/pkg/response" + + "github.com/gin-gonic/gin" +) + +const defaultAPIQueryBudget = 15 * time.Second + +// QueryBudget 为普通 API 设置请求级数据库上下文预算。 +// SSE、上传和对象流不设置总时限,避免长连接或客户端下载时间被误算成 SQL 超时。 +func QueryBudget() gin.HandlerFunc { + return func(c *gin.Context) { + timeout := queryBudgetForRequest(c.Request.Method, c.Request.URL.Path, c.FullPath()) + if timeout <= 0 { + c.Next() + return + } + ctx, cancel := context.WithTimeout(c.Request.Context(), timeout) + defer cancel() + c.Request = c.Request.WithContext(ctx) + c.Next() + if errors.Is(ctx.Err(), context.DeadlineExceeded) && !c.Writer.Written() { + response.Error(c, http.StatusServiceUnavailable, "query_timeout", "请求处理超时,请稍后重试") + } + } +} + +func queryBudgetForRequest(method, path, route string) time.Duration { + if isUnboundedRequest(path, route) { + return 0 + } + if isPaymentNotifyPath(path) { + return 30 * time.Second + } + if route == "/api/files/access/batch" || route == "/api/admin/files/access/batch" { + return 5 * time.Second + } + if strings.Contains(route, "/qrcodes/ocr-group-name") || route == "/api/open/listing-uploads" { + return 60 * time.Second + } + if strings.Contains(route, "/payment-configs") || strings.HasSuffix(route, "/sync-accounts") { + return 30 * time.Second + } + if method == http.MethodGet { + switch { + case route == "/api/admin/dashboard", + strings.HasPrefix(route, "/api/admin/chats"), + strings.HasPrefix(route, "/api/admin/finance"), + strings.HasPrefix(route, "/api/admin/orders"), + strings.HasPrefix(route, "/api/admin/listings"), + strings.HasPrefix(route, "/api/admin/withdrawals"), + strings.HasPrefix(route, "/api/admin/manual-entries"): + return 8 * time.Second + } + } + return defaultAPIQueryBudget +} + +func isUnboundedRequest(path, route string) bool { + if isSSEPath(path, route) { + return true + } + switch path { + case "/api/files/object", + "/api/admin/files/object", + "/api/public/files/object", + "/api/files/upload", + "/api/admin/files/upload": + return true + } + return false +} + +type tenantConcurrencyState struct { + slots chan struct{} + refs int +} + +type tenantConcurrencyLimiter struct { + mu sync.Mutex + sites map[uint64]*tenantConcurrencyState + limit int + wait time.Duration +} + +// TenantConcurrencyLimit 隔离高成本后台读请求,避免单租户拖垮全站数据库连接池。 +func TenantConcurrencyLimit(limit int, wait time.Duration) gin.HandlerFunc { + if limit < 1 { + limit = 1 + } + limiter := &tenantConcurrencyLimiter{ + sites: make(map[uint64]*tenantConcurrencyState), + limit: limit, + wait: wait, + } + return func(c *gin.Context) { + if c.Request.Method != http.MethodGet || !isHighCostAdminRead(c.Request.URL.Path, c.FullPath()) { + c.Next() + return + } + tenantID := tenancy.IDFromContext(c.Request.Context()) + release, ok := limiter.acquire(c.Request.Context(), tenantID) + if !ok { + c.Header("Retry-After", "1") + response.Error(c, http.StatusTooManyRequests, "high_cost_query_busy", "当前查询繁忙,请稍后重试") + c.Abort() + return + } + defer release() + c.Next() + } +} + +func isHighCostAdminRead(path, route string) bool { + if path == "/api/admin/chats/events" { + return false + } + switch { + case route == "/api/admin/dashboard", + strings.HasPrefix(route, "/api/admin/chats"), + strings.HasPrefix(route, "/api/admin/finance"), + strings.HasPrefix(route, "/api/admin/orders"), + strings.HasPrefix(route, "/api/admin/listings"), + strings.HasPrefix(route, "/api/admin/withdrawals"), + strings.HasPrefix(route, "/api/admin/manual-entries"): + return true + default: + return false + } +} + +func (l *tenantConcurrencyLimiter) acquire(ctx context.Context, tenantID uint64) (func(), bool) { + l.mu.Lock() + state := l.sites[tenantID] + if state == nil { + state = &tenantConcurrencyState{slots: make(chan struct{}, l.limit)} + l.sites[tenantID] = state + } + state.refs++ + l.mu.Unlock() + + if !tryAcquireTenantSlot(ctx, state.slots, l.wait) { + l.releaseRef(tenantID) + return nil, false + } + var once sync.Once + return func() { + once.Do(func() { + <-state.slots + l.releaseRef(tenantID) + }) + }, true +} + +func (l *tenantConcurrencyLimiter) releaseRef(tenantID uint64) { + l.mu.Lock() + defer l.mu.Unlock() + state := l.sites[tenantID] + if state == nil { + return + } + state.refs-- + if state.refs == 0 { + delete(l.sites, tenantID) + } +} + +func tryAcquireTenantSlot(ctx context.Context, slots chan struct{}, wait time.Duration) bool { + select { + case slots <- struct{}{}: + return true + default: + } + if wait <= 0 { + return false + } + timer := time.NewTimer(wait) + defer timer.Stop() + select { + case slots <- struct{}{}: + return true + case <-ctx.Done(): + return false + case <-timer.C: + return false + } +} diff --git a/backend/internal/middleware/request_guard_test.go b/backend/internal/middleware/request_guard_test.go new file mode 100644 index 00000000..2f9825fa --- /dev/null +++ b/backend/internal/middleware/request_guard_test.go @@ -0,0 +1,105 @@ +package middleware + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/gin-gonic/gin" +) + +func TestQueryBudgetExcludesLongLivedAndStreamingRequests(t *testing.T) { + cases := []struct { + method string + path string + route string + want time.Duration + }{ + {method: http.MethodGet, path: "/api/orders", route: "/api/orders", want: defaultAPIQueryBudget}, + {method: http.MethodGet, path: "/api/admin/dashboard", route: "/api/admin/dashboard", want: 8 * time.Second}, + {method: http.MethodGet, path: "/api/admin/chats", route: "/api/admin/chats", want: 8 * time.Second}, + {method: http.MethodPost, path: "/api/files/access/batch", route: "/api/files/access/batch", want: 5 * time.Second}, + {method: http.MethodPost, path: "/api/payments/lakala/notify", route: "/api/payments/lakala/notify", want: 30 * time.Second}, + {method: http.MethodPost, path: "/api/admin/chats/qrcodes/ocr-group-name", route: "/api/admin/chats/qrcodes/ocr-group-name", want: 60 * time.Second}, + {method: http.MethodPost, path: "/api/open/listing-uploads", route: "/api/open/listing-uploads", want: 60 * time.Second}, + {method: http.MethodPost, path: "/api/admin/payment-configs/1/test-orders", route: "/api/admin/payment-configs/:id/test-orders", want: 30 * time.Second}, + {method: http.MethodGet, path: "/api/files/object", route: "/api/files/object", want: 0}, + {method: http.MethodPost, path: "/api/admin/files/upload", route: "/api/admin/files/upload", want: 0}, + {method: http.MethodGet, path: "/api/chats/events", route: "/api/chats/events", want: 0}, + } + for _, tc := range cases { + if got := queryBudgetForRequest(tc.method, tc.path, tc.route); got != tc.want { + t.Fatalf("queryBudgetForRequest(%s, %s) = %s, want %s", tc.method, tc.path, got, tc.want) + } + } +} + +func TestHighCostAdminReadClassification(t *testing.T) { + cases := []struct { + path string + route string + want bool + }{ + {path: "/api/admin/dashboard", route: "/api/admin/dashboard", want: true}, + {path: "/api/admin/chats/counts", route: "/api/admin/chats/counts", want: true}, + {path: "/api/admin/chats/events", route: "/api/admin/chats/events", want: false}, + {path: "/api/admin/finance/details", route: "/api/admin/finance/details", want: true}, + {path: "/api/admin/orders/123", route: "/api/admin/orders/:id", want: true}, + {path: "/api/admin/users", route: "/api/admin/users", want: false}, + } + for _, tc := range cases { + if got := isHighCostAdminRead(tc.path, tc.route); got != tc.want { + t.Fatalf("isHighCostAdminRead(%s) = %v, want %v", tc.path, got, tc.want) + } + } +} + +func TestTenantConcurrencyLimiterRejectsWhenSaturated(t *testing.T) { + limiter := &tenantConcurrencyLimiter{ + sites: make(map[uint64]*tenantConcurrencyState), + limit: 1, + wait: 10 * time.Millisecond, + } + release, ok := limiter.acquire(context.Background(), 101) + if !ok { + t.Fatal("first acquire failed") + } + if _, ok := limiter.acquire(context.Background(), 101); ok { + t.Fatal("second acquire unexpectedly succeeded") + } + release() + releaseAgain, ok := limiter.acquire(context.Background(), 101) + if !ok { + t.Fatal("acquire after release failed") + } + releaseAgain() +} + +func TestQueryBudgetUsesResolvedRouteBeforeHandler(t *testing.T) { + gin.SetMode(gin.TestMode) + engine := gin.New() + engine.Use(QueryBudget()) + engine.GET("/api/admin/dashboard", func(c *gin.Context) { + if _, ok := c.Request.Context().Deadline(); !ok { + t.Fatal("high-cost route missing query deadline") + } + c.Status(http.StatusNoContent) + }) + engine.GET("/api/files/object", func(c *gin.Context) { + if _, ok := c.Request.Context().Deadline(); ok { + t.Fatal("streaming route unexpectedly has query deadline") + } + c.Status(http.StatusNoContent) + }) + + for _, path := range []string{"/api/admin/dashboard", "/api/files/object"} { + recorder := httptest.NewRecorder() + request := httptest.NewRequest(http.MethodGet, path, nil) + engine.ServeHTTP(recorder, request) + if recorder.Code != http.StatusNoContent { + t.Fatalf("%s status = %d, want 204", path, recorder.Code) + } + } +} diff --git a/backend/internal/model/file_binding.go b/backend/internal/model/file_binding.go new file mode 100644 index 00000000..b3e02886 --- /dev/null +++ b/backend/internal/model/file_binding.go @@ -0,0 +1,138 @@ +package model + +import ( + "encoding/json" + "strings" + "time" + + "hfb_sys/backend/internal/filekey" + + "gorm.io/datatypes" + "gorm.io/gorm" + "gorm.io/gorm/clause" +) + +const ( + FileResourceGameAccount = "game_account" + FileResourceChatConversation = "chat_conversation" + FileResourceOrder = "order" + FileResourcePaymentAccount = "user_payment_account" + FileResourceWithdrawal = "withdrawal" + FileResourceChatQrCode = "chat_qrcode" + FileResourceOperatingExpense = "operating_expense" + FileResourceManualDisbursement = "manual_disbursement" + FileScopeListingAsset = "listing_asset" + FileScopeChatParticipant = "chat_participant" + FileScopeOrderParticipant = "order_participant" + FileScopeOwner = "owner" + FileScopeQrCodeParticipant = "chat_qrcode_participant" + FileScopeQrCodeTenant = "chat_qrcode_tenant" + FileScopePlatformSuperAdmin = "platform_super_admin" +) + +type FileObjectBinding struct { + ID uint64 `gorm:"primaryKey" json:"id"` + TenantID uint64 `gorm:"not null;default:0;index:idx_file_object_binding_key,priority:2" json:"tenant_id"` + CanonicalKey string `gorm:"size:512;not null;uniqueIndex:uk_file_object_binding,priority:1;index:idx_file_object_binding_key,priority:1" json:"canonical_key"` + ResourceType string `gorm:"size:32;not null;uniqueIndex:uk_file_object_binding,priority:2;index:idx_file_object_binding_resource,priority:1" json:"resource_type"` + ResourceID uint64 `gorm:"not null;uniqueIndex:uk_file_object_binding,priority:3;index:idx_file_object_binding_resource,priority:2" json:"resource_id"` + AccessScope string `gorm:"size:32;not null;uniqueIndex:uk_file_object_binding,priority:4" json:"access_scope"` + OwnerUserID uint64 `gorm:"not null;default:0" json:"owner_user_id"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` +} + +func (FileObjectBinding) TableName() string { + return "file_object_bindings" +} + +func UpsertFileBindings(tx *gorm.DB, tenantID uint64, resourceType string, resourceID uint64, accessScope string, ownerUserID uint64, rawKeys []string) error { + if tx == nil || resourceID == 0 || len(rawKeys) == 0 { + return nil + } + rows := make([]FileObjectBinding, 0, len(rawKeys)) + seen := make(map[string]struct{}, len(rawKeys)) + for _, raw := range rawKeys { + canonical := filekey.Canonical(raw) + if canonical == "" { + continue + } + key := canonical + "\x00" + resourceType + "\x00" + accessScope + if _, ok := seen[key]; ok { + continue + } + seen[key] = struct{}{} + rows = append(rows, FileObjectBinding{ + TenantID: tenantID, + CanonicalKey: canonical, + ResourceType: resourceType, + ResourceID: resourceID, + AccessScope: accessScope, + OwnerUserID: ownerUserID, + }) + } + if len(rows) == 0 { + return nil + } + return tx.Clauses(clause.OnConflict{ + Columns: []clause.Column{ + {Name: "canonical_key"}, + {Name: "resource_type"}, + {Name: "resource_id"}, + {Name: "access_scope"}, + }, + DoUpdates: clause.AssignmentColumns([]string{"tenant_id", "owner_user_id", "updated_at"}), + }).Create(&rows).Error +} + +func UpsertJSONFileBindings(tx *gorm.DB, tenantID uint64, resourceType string, resourceID uint64, accessScope string, ownerUserID uint64, raw datatypes.JSON) error { + var keys []string + if len(raw) > 0 { + if err := json.Unmarshal(raw, &keys); err != nil { + return nil + } + } + return UpsertFileBindings(tx, tenantID, resourceType, resourceID, accessScope, ownerUserID, keys) +} + +func UpsertScalarFileBinding(tx *gorm.DB, tenantID uint64, resourceType string, resourceID uint64, accessScope string, ownerUserID uint64, raw string) error { + if strings.TrimSpace(raw) == "" { + return nil + } + return UpsertFileBindings(tx, tenantID, resourceType, resourceID, accessScope, ownerUserID, []string{raw}) +} + +func (m *GameAccount) AfterSave(tx *gorm.DB) error { + return UpsertJSONFileBindings(tx, m.SourceTenantID, FileResourceGameAccount, m.ID, FileScopeListingAsset, m.OwnerID, m.ScreenshotURLS) +} + +func (m *ChatMessage) AfterSave(tx *gorm.DB) error { + return UpsertJSONFileBindings(tx, 0, FileResourceChatConversation, m.ConversationID, FileScopeChatParticipant, 0, m.AttachmentURLS) +} + +func (m *HandoffRecord) AfterSave(tx *gorm.DB) error { + return UpsertJSONFileBindings(tx, 0, FileResourceOrder, m.OrderID, FileScopeOrderParticipant, 0, m.AttachmentURLS) +} + +func (m *Dispute) AfterSave(tx *gorm.DB) error { + return UpsertJSONFileBindings(tx, 0, FileResourceOrder, m.OrderID, FileScopeOrderParticipant, 0, m.EvidenceURLS) +} + +func (m *OrderCheckout) AfterSave(tx *gorm.DB) error { + return UpsertJSONFileBindings(tx, 0, FileResourceOrder, m.OrderID, FileScopeOrderParticipant, 0, m.EvidenceURLS) +} + +func (m *UserPaymentAccount) AfterSave(tx *gorm.DB) error { + return UpsertJSONFileBindings(tx, m.TenantID, FileResourcePaymentAccount, m.ID, FileScopeOwner, m.UserID, m.CertificateURLs) +} + +func (m *WithdrawalRequest) AfterSave(tx *gorm.DB) error { + return UpsertScalarFileBinding(tx, m.TenantID, FileResourceWithdrawal, m.ID, FileScopeOwner, m.UserID, m.PaymentProofURL) +} + +func (m *ChatQrCode) AfterSave(tx *gorm.DB) error { + if m.ConversationID != nil { + return UpsertScalarFileBinding(tx, m.TenantID, FileResourceChatConversation, *m.ConversationID, FileScopeQrCodeParticipant, 0, m.ImageURL) + } + return UpsertScalarFileBinding(tx, m.TenantID, FileResourceChatQrCode, m.ID, FileScopeQrCodeTenant, 0, m.ImageURL) +} diff --git a/backend/internal/modules/adminfinance/manual_disbursement.go b/backend/internal/modules/adminfinance/manual_disbursement.go index 1c83c24b..131964b7 100644 --- a/backend/internal/modules/adminfinance/manual_disbursement.go +++ b/backend/internal/modules/adminfinance/manual_disbursement.go @@ -9,6 +9,7 @@ import ( "time" "hfb_sys/backend/internal/auditlog" + "hfb_sys/backend/internal/model" "hfb_sys/backend/internal/timeutil" "gorm.io/gorm" @@ -64,6 +65,11 @@ func (r *Repository) CreateManualDisbursement( if err := tx.Create(&record).Error; err != nil { return err } + if err := model.UpsertScalarFileBinding( + tx, 0, model.FileResourceManualDisbursement, record.ID, model.FileScopePlatformSuperAdmin, 0, record.VoucherURL, + ); err != nil { + return err + } id := record.ID return auditlog.Append(tx, auditlog.Entry{ ActorType: "admin", diff --git a/backend/internal/modules/adminfinance/operating_expense.go b/backend/internal/modules/adminfinance/operating_expense.go index 41e196ca..4e3c3ee0 100644 --- a/backend/internal/modules/adminfinance/operating_expense.go +++ b/backend/internal/modules/adminfinance/operating_expense.go @@ -10,6 +10,7 @@ import ( "time" "hfb_sys/backend/internal/auditlog" + "hfb_sys/backend/internal/model" "hfb_sys/backend/internal/timeutil" "gorm.io/gorm" @@ -58,6 +59,11 @@ func (r *Repository) CreateOperatingExpense(ctx context.Context, req CreateOpera if err := tx.Create(&record).Error; err != nil { return err } + if err := model.UpsertScalarFileBinding( + tx, 0, model.FileResourceOperatingExpense, record.ID, model.FileScopePlatformSuperAdmin, 0, record.VoucherURL, + ); err != nil { + return err + } id := record.ID return auditlog.Append(tx, auditlog.Entry{ ActorType: "admin", diff --git a/backend/internal/modules/file/access_ticket.go b/backend/internal/modules/file/access_ticket.go new file mode 100644 index 00000000..13cb3c37 --- /dev/null +++ b/backend/internal/modules/file/access_ticket.go @@ -0,0 +1,97 @@ +package file + +import ( + "crypto/hmac" + "crypto/sha256" + "encoding/base64" + "encoding/json" + "strings" + "time" + + "hfb_sys/backend/internal/filekey" +) + +const accessTicketTTL = 5 * time.Minute + +type accessTicketPayload struct { + Version int `json:"v"` + SubjectType string `json:"sub_type"` + SubjectID uint64 `json:"sub_id"` + TenantID uint64 `json:"tenant_id"` + ObjectKey string `json:"key"` + ExpiresAt int64 `json:"exp"` +} + +// AccessTicketManager 签发短期对象访问票据,避免相册逐图重复查询资源归属。 +type AccessTicketManager struct { + secret []byte + ttl time.Duration + now func() time.Time +} + +func NewAccessTicketManager(secret string) *AccessTicketManager { + sum := sha256.Sum256([]byte("hfb-file-access-ticket:" + secret)) + return &AccessTicketManager{ + secret: sum[:], + ttl: accessTicketTTL, + now: time.Now, + } +} + +func (m *AccessTicketManager) Issue(subjectType string, subjectID, tenantID uint64, objectKey string) string { + if m == nil || subjectID == 0 { + return "" + } + canonical := filekey.Canonical(objectKey) + if canonical == "" { + return "" + } + payload, err := json.Marshal(accessTicketPayload{ + Version: 1, + SubjectType: strings.TrimSpace(subjectType), + SubjectID: subjectID, + TenantID: tenantID, + ObjectKey: canonical, + ExpiresAt: m.now().Add(m.ttl).Unix(), + }) + if err != nil { + return "" + } + signature := m.sign(payload) + return base64.RawURLEncoding.EncodeToString(payload) + "." + base64.RawURLEncoding.EncodeToString(signature) +} + +func (m *AccessTicketManager) Valid(ticketText, subjectType string, subjectID, tenantID uint64, objectKey string) bool { + if m == nil || subjectID == 0 || len(ticketText) == 0 || len(ticketText) > 4096 { + return false + } + parts := strings.Split(ticketText, ".") + if len(parts) != 2 { + return false + } + payloadJSON, err := base64.RawURLEncoding.DecodeString(parts[0]) + if err != nil { + return false + } + signature, err := base64.RawURLEncoding.DecodeString(parts[1]) + if err != nil || !hmac.Equal(signature, m.sign(payloadJSON)) { + return false + } + var payload accessTicketPayload + if err := json.Unmarshal(payloadJSON, &payload); err != nil { + return false + } + canonical := filekey.Canonical(objectKey) + return payload.Version == 1 && + payload.SubjectType == strings.TrimSpace(subjectType) && + payload.SubjectID == subjectID && + payload.TenantID == tenantID && + payload.ObjectKey == canonical && + payload.ExpiresAt > m.now().Unix() +} + +func (m *AccessTicketManager) sign(payload []byte) []byte { + mac := hmac.New(sha256.New, m.secret) + _, _ = mac.Write(payload) + return mac.Sum(nil) +} diff --git a/backend/internal/modules/file/access_ticket_test.go b/backend/internal/modules/file/access_ticket_test.go new file mode 100644 index 00000000..72ddb0ec --- /dev/null +++ b/backend/internal/modules/file/access_ticket_test.go @@ -0,0 +1,49 @@ +package file + +import ( + "testing" + "time" +) + +func TestAccessTicketBindsSubjectTenantObjectAndExpiry(t *testing.T) { + manager := NewAccessTicketManager("test-secret") + now := time.Date(2026, 11, 20, 12, 0, 0, 0, time.Local) + manager.now = func() time.Time { return now } + + ticket := manager.Issue("user", 11, 101, "/api/files/object?key=listing%2Faccount.webp") + if ticket == "" { + t.Fatal("Issue() returned empty ticket") + } + if !manager.Valid(ticket, "user", 11, 101, "listing/account.webp.medium.jpg") { + t.Fatal("valid ticket rejected for permitted variant") + } + if manager.Valid(ticket, "user", 12, 101, "listing/account.webp") { + t.Fatal("ticket accepted for another user") + } + if manager.Valid(ticket, "admin", 11, 101, "listing/account.webp") { + t.Fatal("ticket accepted for another subject type") + } + if manager.Valid(ticket, "user", 11, 202, "listing/account.webp") { + t.Fatal("ticket accepted for another tenant") + } + if manager.Valid(ticket, "user", 11, 101, "listing/another.webp") { + t.Fatal("ticket accepted for another object") + } + + now = now.Add(accessTicketTTL + time.Second) + if manager.Valid(ticket, "user", 11, 101, "listing/account.webp") { + t.Fatal("expired ticket accepted") + } +} + +func TestAccessTicketRejectsTampering(t *testing.T) { + manager := NewAccessTicketManager("test-secret") + ticket := manager.Issue("admin", 7, 101, "chat/message.webp") + if ticket == "" { + t.Fatal("Issue() returned empty ticket") + } + tampered := "A" + ticket[1:] + if manager.Valid(tampered, "admin", 7, 101, "chat/message.webp") { + t.Fatal("tampered ticket accepted") + } +} diff --git a/backend/internal/modules/file/handler.go b/backend/internal/modules/file/handler.go index e6b5be81..a0cfa4d5 100644 --- a/backend/internal/modules/file/handler.go +++ b/backend/internal/modules/file/handler.go @@ -3,10 +3,16 @@ package file import ( "context" "errors" + "io" "net/http" + "strconv" "strings" + "time" + "hfb_sys/backend/internal/filekey" "hfb_sys/backend/internal/logging" + "hfb_sys/backend/internal/middleware" + "hfb_sys/backend/internal/tenancy" "hfb_sys/backend/pkg/response" "github.com/gin-gonic/gin" @@ -19,6 +25,8 @@ type Handler struct { objectAuthorizer ObjectAuthorizer adminObjectAuthorizer AdminObjectAuthorizer uploadOwnerRecorder UploadOwnerRecorder + accessTickets *AccessTicketManager + authGate chan struct{} } // ObjectAuthorizer 校验用户是否可读取指定私有对象。 @@ -30,6 +38,8 @@ type AdminObjectAuthorizer func(ctx context.Context, adminID uint64, key string) // UploadOwnerRecorder 保存用户或管理员上传私有文件的归属。 type UploadOwnerRecorder func(ctx context.Context, userID, adminID uint64, objectKeys []string) error +const maxConcurrentFileAuthorizations = 8 + func NewHandler(service *Service, storage *Storage, objectAuthorizer ObjectAuthorizer, uploadOwnerRecorder UploadOwnerRecorder, adminObjectAuthorizers ...AdminObjectAuthorizer) *Handler { var adminObjectAuthorizer AdminObjectAuthorizer if len(adminObjectAuthorizers) > 0 { @@ -41,9 +51,14 @@ func NewHandler(service *Service, storage *Storage, objectAuthorizer ObjectAutho objectAuthorizer: objectAuthorizer, adminObjectAuthorizer: adminObjectAuthorizer, uploadOwnerRecorder: uploadOwnerRecorder, + authGate: make(chan struct{}, maxConcurrentFileAuthorizations), } } +func (h *Handler) SetAccessTicketManager(manager *AccessTicketManager) { + h.accessTickets = manager +} + func (h *Handler) Upload(c *gin.Context) { header, err := c.FormFile("file") if err != nil { @@ -103,6 +118,77 @@ func (h *Handler) Upload(c *gin.Context) { response.Created(c, item) } +type batchAccessRequest struct { + Keys []string `json:"keys"` +} + +type batchAccessResponse struct { + Tickets map[string]string `json:"tickets"` + ExpiresInSeconds int64 `json:"expires_in_seconds"` +} + +// BatchAccess 在同一请求内校验相册图片,并签发短期对象访问票据。 +func (h *Handler) BatchAccess(c *gin.Context) { + if h.accessTickets == nil { + response.ServiceUnavailable(c, "文件访问票据未启用") + return + } + var req batchAccessRequest + if err := c.ShouldBindJSON(&req); err != nil { + response.BadRequest(c, "文件列表格式不正确") + return + } + if len(req.Keys) == 0 || len(req.Keys) > 100 { + response.BadRequest(c, "每次最多校验 100 个文件") + return + } + subjectType, subjectID := fileAccessSubject(c) + if subjectID == 0 { + response.Unauthorized(c, "缺少文件访问身份") + return + } + if !h.acquireAuthorizationSlot(c.Request.Context(), 500*time.Millisecond) { + c.Header("Retry-After", "1") + response.Error(c, http.StatusTooManyRequests, "file_auth_busy", "图片鉴权繁忙,请稍后重试") + return + } + defer h.releaseAuthorizationSlot() + + ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second) + defer cancel() + tenantID := tenancy.IDFromContext(ctx) + tickets := make(map[string]string, len(req.Keys)) + seen := make(map[string]struct{}, len(req.Keys)) + for _, rawKey := range req.Keys { + rawKey = strings.TrimSpace(rawKey) + canonical := filekey.Canonical(rawKey) + if rawKey == "" || canonical == "" { + continue + } + identity := rawKey + "\x00" + canonical + if _, ok := seen[identity]; ok { + continue + } + seen[identity] = struct{}{} + + allowed, err := h.authorizeObject(ctx, subjectType, subjectID, rawKey) + if err != nil { + response.ServiceUnavailable(c, "文件权限校验失败") + return + } + if !allowed { + continue + } + if ticket := h.accessTickets.Issue(subjectType, subjectID, tenantID, canonical); ticket != "" { + tickets[rawKey] = ticket + } + } + response.OK(c, batchAccessResponse{ + Tickets: tickets, + ExpiresInSeconds: int64(accessTicketTTL.Seconds()), + }) +} + func (h *Handler) Object(c *gin.Context) { h.writeObject(c, false) } @@ -112,6 +198,7 @@ func (h *Handler) PublicObject(c *gin.Context) { } func (h *Handler) writeObject(c *gin.Context, publicOnly bool) { + totalStart := time.Now() if h.storage == nil { response.ServiceUnavailable(c, "文件存储未连接") return @@ -125,34 +212,28 @@ func (h *Handler) writeObject(c *gin.Context, publicOnly bool) { response.Error(c, http.StatusNotFound, "not_found", "文件不存在或暂不可访问") return } + authMS := 0.0 + accessSource := "public" if !publicOnly { - if adminIDValue, isAdmin := c.Get("admin_id"); isAdmin { - adminID, ok := adminIDValue.(uint64) - if !ok || h.adminObjectAuthorizer == nil { - response.Error(c, http.StatusForbidden, "forbidden", "无权访问该文件") - return - } - allowed, err := h.adminObjectAuthorizer(c.Request.Context(), adminID, key) - if err != nil { - response.ServiceUnavailable(c, "文件权限校验失败") - return - } - if !allowed { - response.Error(c, http.StatusForbidden, "forbidden", "无权访问该文件") - return - } + authStart := time.Now() + subjectType, subjectID := fileAccessSubject(c) + tenantID := tenancy.IDFromContext(c.Request.Context()) + if ticket := strings.TrimSpace(c.Query("ticket")); ticket != "" && + h.accessTickets != nil && + h.accessTickets.Valid(ticket, subjectType, subjectID, tenantID, key) { + accessSource = "ticket" } else { - userID, ok := c.Get("user_id") - if !ok { - response.Unauthorized(c, "缺少用户上下文") + accessSource = "database" + if !h.acquireAuthorizationSlot(c.Request.Context(), 500*time.Millisecond) { + c.Header("Retry-After", "1") + response.Error(c, http.StatusTooManyRequests, "file_auth_busy", "图片鉴权繁忙,请稍后重试") return } - id, ok := userID.(uint64) - if !ok || h.objectAuthorizer == nil { - response.Error(c, http.StatusForbidden, "forbidden", "无权访问该文件") - return - } - allowed, err := h.objectAuthorizer(c.Request.Context(), id, key) + defer h.releaseAuthorizationSlot() + + authCtx, cancel := context.WithTimeout(c.Request.Context(), 3*time.Second) + allowed, err := h.authorizeObject(authCtx, subjectType, subjectID, key) + cancel() if err != nil { response.ServiceUnavailable(c, "文件权限校验失败") return @@ -162,9 +243,25 @@ func (h *Handler) writeObject(c *gin.Context, publicOnly bool) { return } } + authMS = float64(time.Since(authStart).Microseconds()) / 1000 } + + storageStart := time.Now() object, err := h.storage.Get(c.Request.Context(), key) + storageMS := float64(time.Since(storageStart).Microseconds()) / 1000 if err != nil { + fields := []zap.Field{ + zap.String("access_source", accessSource), + zap.String("object_prefix", fileObjectPrefix(key)), + zap.Float64("auth_ms", authMS), + zap.Float64("storage_open_ms", storageMS), + zap.Error(err), + } + logger := logging.FromContext(c.Request.Context()) + logger.Debug("文件存储打开失败", fields...) + if storageMS >= 500 { + logger.Warn("文件存储打开慢请求", fields...) + } response.Error(c, http.StatusNotFound, "not_found", "文件不存在或暂不可访问") return } @@ -181,7 +278,34 @@ func (h *Handler) writeObject(c *gin.Context, publicOnly bool) { } else { c.Header("Cache-Control", "private, max-age=300") } - c.DataFromReader(http.StatusOK, object.Size, contentType, object.Reader, nil) + c.Header("Content-Length", strconv.FormatInt(object.Size, 10)) + c.Status(http.StatusOK) + streamStart := time.Now() + _, streamErr := io.Copy(c.Writer, object.Reader) + streamMS := float64(time.Since(streamStart).Microseconds()) / 1000 + totalMS := float64(time.Since(totalStart).Microseconds()) / 1000 + if streamErr != nil { + response.RecordError(c, streamErr) + } + fields := []zap.Field{ + zap.Bool("public", publicOnly), + zap.String("access_source", accessSource), + zap.String("object_prefix", fileObjectPrefix(key)), + zap.Int64("object_size", object.Size), + zap.Int("status", c.Writer.Status()), + zap.Float64("auth_ms", authMS), + zap.Float64("storage_open_ms", storageMS), + zap.Float64("stream_ms", streamMS), + zap.Float64("total_ms", totalMS), + } + if streamErr != nil { + fields = append(fields, zap.Error(streamErr)) + } + logger := logging.FromContext(c.Request.Context()) + logger.Debug("私有文件读取", fields...) + if totalMS >= 1000 { + logger.Warn("文件读取慢请求", fields...) + } } func isPublicObjectKey(key string) bool { @@ -203,6 +327,74 @@ func isPublicObjectKey(key string) bool { return false } +func fileAccessSubject(c *gin.Context) (string, uint64) { + if value, exists := c.Get(middleware.ContextAdminID); exists { + if adminID, ok := value.(uint64); ok && adminID > 0 { + return "admin", adminID + } + } + if value, exists := c.Get(middleware.ContextUserID); exists { + if userID, ok := value.(uint64); ok && userID > 0 { + return "user", userID + } + } + return "", 0 +} + +func (h *Handler) authorizeObject(ctx context.Context, subjectType string, subjectID uint64, key string) (bool, error) { + if subjectID == 0 { + return false, nil + } + if subjectType == "admin" { + if h.adminObjectAuthorizer == nil { + return false, nil + } + return h.adminObjectAuthorizer(ctx, subjectID, key) + } + if h.objectAuthorizer == nil { + return false, nil + } + return h.objectAuthorizer(ctx, subjectID, key) +} + +func (h *Handler) acquireAuthorizationSlot(ctx context.Context, wait time.Duration) bool { + if h.authGate == nil { + return true + } + select { + case h.authGate <- struct{}{}: + return true + default: + } + timer := time.NewTimer(wait) + defer timer.Stop() + select { + case h.authGate <- struct{}{}: + return true + case <-ctx.Done(): + return false + case <-timer.C: + return false + } +} + +func (h *Handler) releaseAuthorizationSlot() { + if h.authGate == nil { + return + } + select { + case <-h.authGate: + default: + } +} + +func fileObjectPrefix(key string) string { + if index := strings.IndexByte(key, '/'); index > 0 { + return key[:index] + } + return key +} + func writeFileError(c *gin.Context, err error) { response.RecordError(c, err) switch { diff --git a/backend/internal/router/dependencies.go b/backend/internal/router/dependencies.go index 045d118f..99df356f 100644 --- a/backend/internal/router/dependencies.go +++ b/backend/internal/router/dependencies.go @@ -3,11 +3,15 @@ package router import ( "context" "strings" + "time" + "hfb_sys/backend/internal/filekey" + "hfb_sys/backend/internal/logging" "hfb_sys/backend/internal/model" "hfb_sys/backend/internal/tenancy" "github.com/redis/go-redis/v9" + "go.uber.org/zap" "gorm.io/gorm" "gorm.io/gorm/clause" ) @@ -19,11 +23,13 @@ type Dependencies struct { // privateFileAuthorizer 仅允许对象所属用户、订单参与者或聊天成员读取私有文件。 // 未能关联到业务记录的对象默认拒绝,避免随机 key 成为访问凭证。 -func privateFileAuthorizer(db *gorm.DB) func(context.Context, uint64, string) (bool, error) { +func privateFileAuthorizer(db *gorm.DB, modes ...string) func(context.Context, uint64, string) (bool, error) { + mode := fileBindingMode(modes...) return func(ctx context.Context, userID uint64, key string) (bool, error) { if db == nil || userID == 0 || key == "" { return false, nil } + start := time.Now() // 若请求的是变体图片(.medium.jpg 或 .thumb.jpg),提取原始前缀,便于在存有原图的业务表中匹配 basePrefix := key @@ -35,50 +41,293 @@ func privateFileAuthorizer(db *gorm.DB) func(context.Context, uint64, string) (b } tenantID := tenancy.IDFromContext(ctx) + canonical := filekey.Canonical(key) var ownerCount int64 if err := db.WithContext(ctx).Raw( - "SELECT COUNT(1) FROM file_upload_owners WHERE user_id = ? AND object_key = ? AND (? = 0 OR tenant_id = ?)", - userID, key, tenantID, tenantID, + "SELECT COUNT(1) FROM file_upload_owners WHERE user_id = ? AND object_key IN (?, ?) AND (? = 0 OR tenant_id = ?)", + userID, key, canonical, tenantID, tenantID, ).Scan(&ownerCount).Error; err != nil { return false, err } if ownerCount > 0 { + logFileAuthorization(ctx, "user", "upload_owner", canonical, start, nil) return true, nil } - if knownQueries, known := knownUserFileQueries(userID, tenantID, fileReferenceValues(key, basePrefix)); known { - return runFileAccessQueries(ctx, db, knownQueries) + if mode != "legacy" { + bindings, err := loadFileObjectBindings(ctx, db, canonical) + if err != nil { + return false, err + } + if len(bindings) > 0 { + for _, binding := range bindings { + allowed, err := userCanAccessFileBinding(ctx, db, userID, tenantID, binding) + if err != nil { + return false, err + } + if allowed { + logFileAuthorization(ctx, "user", "binding", canonical, start, nil) + return true, nil + } + } + if mode == "strict" { + logFileAuthorization(ctx, "user", "binding_denied", canonical, start, nil) + return false, nil + } + } } + if mode == "strict" { + logFileAuthorization(ctx, "user", "binding_missing", canonical, start, nil) + return false, nil + } + if knownQueries, known := knownUserFileQueries(userID, tenantID, fileReferenceValues(key, basePrefix)); known { + allowed, err := runFileAccessQueries(ctx, db, knownQueries) + if err == nil && allowed { + logFileAuthorization(ctx, "user", "legacy_reference", canonical, start, nil) + return true, nil + } + logFileAuthorization(ctx, "user", "legacy_denied", canonical, start, err) + return allowed, err + } + logFileAuthorization(ctx, "user", "unmatched", canonical, start, nil) return false, nil } } // adminPrivateFileAuthorizer 将经营站点后台的私有文件访问限制在当前租户。 // 历史分销后台(tenant_id=0)保留原有平台级访问语义。 -func adminPrivateFileAuthorizer(db *gorm.DB) func(context.Context, uint64, string) (bool, error) { +func adminPrivateFileAuthorizer(db *gorm.DB, modes ...string) func(context.Context, uint64, string) (bool, error) { + mode := fileBindingMode(modes...) return func(ctx context.Context, adminID uint64, key string) (bool, error) { if db == nil || key == "" { return false, nil } + start := time.Now() basePrefix := strings.TrimSuffix(strings.TrimSuffix(key, ".medium.jpg"), ".thumb.jpg") tenantID := tenancy.IDFromContext(ctx) + canonical := filekey.Canonical(key) var ownerCount int64 if err := db.WithContext(ctx).Raw( - "SELECT COUNT(1) FROM file_upload_owners WHERE admin_id = ? AND object_key = ? AND (? = 0 OR tenant_id = ?)", - adminID, key, tenantID, tenantID, + "SELECT COUNT(1) FROM file_upload_owners WHERE admin_id = ? AND object_key IN (?, ?) AND (? = 0 OR tenant_id = ?)", + adminID, key, canonical, tenantID, tenantID, ).Scan(&ownerCount).Error; err != nil { return false, err } if ownerCount > 0 { + logFileAuthorization(ctx, "admin", "upload_owner", canonical, start, nil) return true, nil } - if knownQueries, known := knownAdminFileQueries(ctx, db, adminID, tenantID, fileReferenceValues(key, basePrefix)); known { - return runFileAccessQueries(ctx, db, knownQueries) + if mode != "legacy" { + bindings, err := loadFileObjectBindings(ctx, db, canonical) + if err != nil { + return false, err + } + if len(bindings) > 0 { + for _, binding := range bindings { + allowed, err := adminCanAccessFileBinding(ctx, db, adminID, tenantID, binding) + if err != nil { + return false, err + } + if allowed { + logFileAuthorization(ctx, "admin", "binding", canonical, start, nil) + return true, nil + } + } + if mode == "strict" { + logFileAuthorization(ctx, "admin", "binding_denied", canonical, start, nil) + return false, nil + } + } } + if mode == "strict" { + logFileAuthorization(ctx, "admin", "binding_missing", canonical, start, nil) + return false, nil + } + if knownQueries, known := knownAdminFileQueries(ctx, db, adminID, tenantID, fileReferenceValues(key, basePrefix)); known { + allowed, err := runFileAccessQueries(ctx, db, knownQueries) + if err == nil && allowed { + logFileAuthorization(ctx, "admin", "legacy_reference", canonical, start, nil) + return true, nil + } + logFileAuthorization(ctx, "admin", "legacy_denied", canonical, start, err) + return allowed, err + } + logFileAuthorization(ctx, "admin", "unmatched", canonical, start, nil) return false, nil } } +func fileBindingMode(modes ...string) string { + if len(modes) == 0 { + return "prefer" + } + switch strings.ToLower(strings.TrimSpace(modes[0])) { + case "strict", "legacy": + return strings.ToLower(strings.TrimSpace(modes[0])) + default: + return "prefer" + } +} + +func loadFileObjectBindings(ctx context.Context, db *gorm.DB, canonical string) ([]model.FileObjectBinding, error) { + if canonical == "" { + return nil, nil + } + var bindings []model.FileObjectBinding + err := db.WithContext(ctx). + Where("canonical_key = ?", canonical). + Order("id ASC"). + Find(&bindings).Error + return bindings, err +} + +func userCanAccessFileBinding(ctx context.Context, db *gorm.DB, userID, tenantID uint64, binding model.FileObjectBinding) (bool, error) { + if binding.OwnerUserID > 0 && binding.OwnerUserID == userID { + return bindingTenantMatches(binding.TenantID, tenantID), nil + } + switch binding.ResourceType { + case model.FileResourceGameAccount: + return userCanAccessGameAccountFiles(ctx, db, userID, tenantID, binding.ResourceID) + case model.FileResourceChatConversation: + return userCanAccessConversationFiles(ctx, db, userID, tenantID, binding.ResourceID) + case model.FileResourceOrder: + return userCanAccessOrderFiles(ctx, db, userID, tenantID, binding.ResourceID) + case model.FileResourcePaymentAccount: + var count int64 + err := db.WithContext(ctx).Table("user_payment_accounts"). + Where("id = ? AND user_id = ? AND (? = 0 OR tenant_id = ?)", binding.ResourceID, userID, tenantID, tenantID). + Count(&count).Error + return count > 0, err + case model.FileResourceWithdrawal: + var count int64 + err := db.WithContext(ctx).Table("withdrawal_requests"). + Where("id = ? AND user_id = ? AND (? = 0 OR tenant_id = ?)", binding.ResourceID, userID, tenantID, tenantID). + Count(&count).Error + return count > 0, err + case model.FileResourceChatQrCode: + var count int64 + err := db.WithContext(ctx).Table("chat_qrcode_pool AS q"). + Joins("JOIN chat_participants AS p ON p.conversation_id = q.conversation_id AND p.participant_type = 'user' AND p.participant_id = ?", userID). + Where("q.id = ?", binding.ResourceID). + Count(&count).Error + return count > 0, err + default: + return false, nil + } +} + +func adminCanAccessFileBinding(ctx context.Context, db *gorm.DB, adminID, tenantID uint64, binding model.FileObjectBinding) (bool, error) { + if tenantID == 0 { + return true, nil + } + switch binding.ResourceType { + case model.FileResourceGameAccount: + var count int64 + if err := db.WithContext(ctx).Table("game_accounts"). + Where("id = ? AND source_tenant_id = ?", binding.ResourceID, tenantID). + Count(&count).Error; err != nil || count > 0 { + return count > 0, err + } + err := db.WithContext(ctx).Table("rental_listings"). + Where("account_id = ? AND source_tenant_id = ?", binding.ResourceID, tenantID). + Where("distribution_site_id IS NULL OR distribution_site_id = 0"). + Count(&count).Error + return count > 0, err + case model.FileResourceChatConversation: + return adminCanAccessConversationFiles(ctx, db, tenantID, binding.ResourceID) + case model.FileResourceOrder: + var count int64 + err := db.WithContext(ctx).Table("rental_orders"). + Where("id = ? AND (buyer_tenant_id = ? OR source_tenant_id = ?)", binding.ResourceID, tenantID, tenantID). + Count(&count).Error + return count > 0, err + case model.FileResourcePaymentAccount: + var count int64 + err := db.WithContext(ctx).Table("user_payment_accounts AS p"). + Joins("JOIN users AS u ON u.id = p.user_id"). + Where("p.id = ? AND p.tenant_id = ? AND u.tenant_id = ?", binding.ResourceID, tenantID, tenantID). + Where("u.distribution_site_id = 0"). + Count(&count).Error + return count > 0, err + case model.FileResourceWithdrawal: + var count int64 + err := db.WithContext(ctx).Table("withdrawal_requests"). + Where("id = ? AND tenant_id = ? AND distribution_site_id = 0", binding.ResourceID, tenantID). + Count(&count).Error + return count > 0, err + case model.FileResourceChatQrCode: + var count int64 + query := db.WithContext(ctx).Table("chat_qrcode_pool"). + Where("id = ? AND tenant_id = ?", binding.ResourceID, tenantID) + if !isPlatformSuperAdmin(ctx, db, adminID) { + query = query.Where("distribution_site_id IS NULL OR distribution_site_id = 0") + } + err := query.Count(&count).Error + return count > 0, err + case model.FileResourceOperatingExpense, model.FileResourceManualDisbursement: + return isPlatformSuperAdmin(ctx, db, adminID), nil + default: + return false, nil + } +} + +func bindingTenantMatches(bindingTenantID, tenantID uint64) bool { + return bindingTenantID == 0 || tenantID == 0 || bindingTenantID == tenantID +} + +func userCanAccessGameAccountFiles(ctx context.Context, db *gorm.DB, userID, tenantID, accountID uint64) (bool, error) { + var count int64 + if err := db.WithContext(ctx).Table("game_accounts"). + Where("id = ? AND owner_id = ?", accountID, userID). + Where("? = 0 OR source_tenant_id = ?", tenantID, tenantID). + Count(&count).Error; err != nil || count > 0 { + return count > 0, err + } + if err := db.WithContext(ctx).Table("rental_listings AS l"). + Where("l.account_id = ? AND l.status = ? AND l.review_status = ?", accountID, "published", "approved"). + Where("? = 0 OR l.source_tenant_id = ?", tenantID, tenantID). + Count(&count).Error; err != nil || count > 0 { + return count > 0, err + } + err := db.WithContext(ctx).Table("rental_orders"). + Where("account_id = ? AND (owner_id = ? OR renter_id = ?)", accountID, userID, userID). + Where("? = 0 OR buyer_tenant_id = ? OR source_tenant_id = ?", tenantID, tenantID, tenantID). + Count(&count).Error + return count > 0, err +} + +func userCanAccessConversationFiles(ctx context.Context, db *gorm.DB, userID, tenantID, conversationID uint64) (bool, error) { + var count int64 + condition := conversationTenantAccessClause("c") + args := conversationTenantAccessArgs(tenantID) + err := db.WithContext(ctx).Table("chat_participants AS p"). + Joins("JOIN chat_conversations AS c ON c.id = p.conversation_id"). + Where("p.conversation_id = ? AND p.participant_type = ? AND p.participant_id = ?", conversationID, "user", userID). + Where(condition, args...). + Count(&count).Error + return count > 0, err +} + +func userCanAccessOrderFiles(ctx context.Context, db *gorm.DB, userID, tenantID, orderID uint64) (bool, error) { + var count int64 + err := db.WithContext(ctx).Table("rental_orders"). + Where("id = ? AND (owner_id = ? OR renter_id = ?)", orderID, userID, userID). + Where("? = 0 OR buyer_tenant_id = ? OR source_tenant_id = ?", tenantID, tenantID, tenantID). + Count(&count).Error + return count > 0, err +} + +func adminCanAccessConversationFiles(ctx context.Context, db *gorm.DB, tenantID, conversationID uint64) (bool, error) { + var count int64 + condition := conversationTenantAccessClause("c") + args := conversationTenantAccessArgs(tenantID) + err := db.WithContext(ctx).Table("chat_conversations AS c"). + Where("c.id = ?", conversationID). + Where(condition, args...). + Count(&count).Error + return count > 0, err +} + // recordPrivateFileUpload 记录用户或管理员上传的原图和变体,保证业务记录保存前仅上传者可预览。 func recordPrivateFileUpload(db *gorm.DB) func(context.Context, uint64, uint64, []string) error { return func(ctx context.Context, userID, adminID uint64, objectKeys []string) error { @@ -117,3 +366,28 @@ func isPlatformSuperAdmin(ctx context.Context, db *gorm.DB, adminID uint64) bool Count(&count).Error return err == nil && count > 0 } + +func logFileAuthorization(ctx context.Context, subject, outcome, canonical string, start time.Time, err error) { + elapsed := time.Since(start) + fields := []zap.Field{ + zap.String("subject", subject), + zap.String("outcome", outcome), + zap.String("object_prefix", fileObjectPrefix(canonical)), + zap.Float64("auth_ms", float64(elapsed.Microseconds())/1000), + } + if err != nil { + fields = append(fields, zap.Error(err)) + } + logger := logging.FromContext(ctx) + logger.Debug("私有文件鉴权", fields...) + if elapsed >= 250*time.Millisecond { + logger.Warn("私有文件鉴权慢查询", fields...) + } +} + +func fileObjectPrefix(key string) string { + if index := strings.IndexByte(key, '/'); index > 0 { + return key[:index] + } + return key +} diff --git a/backend/internal/router/dependencies_test.go b/backend/internal/router/dependencies_test.go index f4d8e4e2..4109f6c1 100644 --- a/backend/internal/router/dependencies_test.go +++ b/backend/internal/router/dependencies_test.go @@ -9,6 +9,7 @@ import ( "hfb_sys/backend/internal/model" "hfb_sys/backend/internal/tenancy" + "gorm.io/datatypes" "gorm.io/gorm" ) @@ -86,6 +87,50 @@ func TestPrivateFileAuthorizer(t *testing.T) { } } +func TestPrivateFileAuthorizerSupportsBindingModes(t *testing.T) { + db := openAuthorizerTestDB(t) + if err := db.Exec("INSERT INTO game_accounts (id, owner_id, server_region, login_platform, title, screenshot_urls) "+ + "VALUES (101, 201, '国服', 'steam', '绑定账号', '[]'), "+ + "(102, 202, '国服', 'steam', '旧数据账号', ?)", + `["/api/files/object?key=listing%2Fmode.jpg"]`).Error; err != nil { + t.Fatalf("create game accounts: %v", err) + } + if err := db.Create(&model.FileObjectBinding{ + CanonicalKey: "listing/mode.jpg", + ResourceType: model.FileResourceGameAccount, + ResourceID: 101, + AccessScope: model.FileScopeListingAsset, + OwnerUserID: 201, + }).Error; err != nil { + t.Fatalf("create file binding: %v", err) + } + + prefer := privateFileAuthorizer(db, "prefer") + strict := privateFileAuthorizer(db, "strict") + legacy := privateFileAuthorizer(db, "legacy") + key := "listing/mode.jpg" + + if allowed, err := prefer(context.Background(), 201, key); err != nil || !allowed { + t.Fatalf("prefer binding owner allowed=%v err=%v", allowed, err) + } + if allowed, err := prefer(context.Background(), 202, key); err != nil || !allowed { + t.Fatalf("prefer legacy fallback allowed=%v err=%v", allowed, err) + } + if allowed, err := strict(context.Background(), 202, key); err != nil { + t.Fatal(err) + } else if allowed { + t.Fatal("strict mode must not fall back to JSON references") + } + if allowed, err := legacy(context.Background(), 201, key); err != nil { + t.Fatal(err) + } else if allowed { + t.Fatal("legacy mode must skip binding table") + } + if allowed, err := legacy(context.Background(), 202, key); err != nil || !allowed { + t.Fatalf("legacy JSON owner allowed=%v err=%v", allowed, err) + } +} + func TestRecordPrivateFileUpload(t *testing.T) { db := openAuthorizerTestDB(t) if err := database.AutoMigrateIfMissing(db, &model.FileUploadOwner{}); err != nil { @@ -142,6 +187,31 @@ func TestPrivateFileAuthorizerRejectsCrossTenantOwner(t *testing.T) { } } +func TestPrivateFileAuthorizerRejectsCrossTenantBindingOwner(t *testing.T) { + db := openAuthorizerTestDB(t) + if err := db.Create(&model.FileObjectBinding{ + TenantID: 101, + CanonicalKey: "listing/binding-tenant-a.jpg", + ResourceType: model.FileResourceGameAccount, + ResourceID: 999, + AccessScope: model.FileScopeListingAsset, + OwnerUserID: 11, + }).Error; err != nil { + t.Fatalf("create binding: %v", err) + } + authorize := privateFileAuthorizer(db, "strict") + ctxA := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 101}) + ctxB := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 202}) + if allowed, err := authorize(ctxA, 11, "listing/binding-tenant-a.jpg"); err != nil || !allowed { + t.Fatalf("same tenant allowed=%v err=%v", allowed, err) + } + if allowed, err := authorize(ctxB, 11, "listing/binding-tenant-a.jpg"); err != nil { + t.Fatal(err) + } else if allowed { + t.Fatal("cross tenant binding owner must not be readable") + } +} + func TestAdminPrivateFileAuthorizerScopesOperatingTenant(t *testing.T) { db := openAuthorizerTestDB(t) if err := database.AutoMigrateIfMissing(db, &model.FileUploadOwner{}); err != nil { @@ -174,6 +244,54 @@ func TestAdminPrivateFileAuthorizerScopesOperatingTenant(t *testing.T) { } } +func TestAdminBindingAuthorizerExcludesDistributionFinanceFiles(t *testing.T) { + db := openAuthorizerTestDB(t) + user := model.User{ + TenantID: 101, + DistributionSiteID: 77, + Phone: "file-distribution-user", + Status: "active", + } + if err := db.Create(&user).Error; err != nil { + t.Fatal(err) + } + account := model.UserPaymentAccount{ + TenantID: 101, + UserID: user.ID, + AccountType: "alipay", + AccountName: "测试", + AccountNo: "test@example.com", + CertificateURLs: datatypes.JSON([]byte(`["/api/files/object?key=payment-cert%2Fdistribution.webp"]`)), + } + if err := db.Create(&account).Error; err != nil { + t.Fatal(err) + } + withdrawal := model.WithdrawalRequest{ + WithdrawNo: "W-FILE-DIST", + TenantID: 101, + UserID: user.ID, + DistributionSiteID: 77, + AccountType: "alipay", + AccountName: "测试", + AccountNo: "test@example.com", + Status: "processing", + PaymentProofURL: "/api/files/object?key=payment-proof%2Fdistribution.webp", + } + if err := db.Create(&withdrawal).Error; err != nil { + t.Fatal(err) + } + + authorize := adminPrivateFileAuthorizer(db, "strict") + ctx := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 101}) + for _, key := range []string{"payment-cert/distribution.webp", "payment-proof/distribution.webp"} { + if allowed, err := authorize(ctx, 9, key); err != nil { + t.Fatalf("%s authorize error: %v", key, err) + } else if allowed { + t.Fatalf("tenant admin must not read distribution finance file %s", key) + } + } +} + func TestPrivateFileAuthorizerAllowsDeliveredQrCodeParticipants(t *testing.T) { db := openAuthorizerTestDB(t) tenants := []model.Tenant{ diff --git a/backend/internal/router/router.go b/backend/internal/router/router.go index a8b683ee..507294cd 100644 --- a/backend/internal/router/router.go +++ b/backend/internal/router/router.go @@ -4,6 +4,7 @@ import ( "context" "net/http" "strings" + "time" "hfb_sys/backend/internal/config" "hfb_sys/backend/internal/handler" @@ -70,6 +71,7 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine { engine.Use(middleware.RequestID()) engine.Use(middleware.RequestLogger(logger)) engine.Use(middleware.Recovery(logger)) + engine.Use(middleware.QueryBudget()) var tenantRepo *tenancy.Repository if deps.DB != nil { tenantRepo = tenancy.NewRepository(deps.DB) @@ -398,6 +400,7 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine { } engine.Use(middleware.RequireKnownApplicationHost(cfg.AppEnv, cfg.DistributionAdminDomain)) engine.Use(middleware.PlatformOpsReadOnlyGuard()) + engine.Use(middleware.TenantConcurrencyLimit(2, 200*time.Millisecond)) distributionHandler := distribution.NewHandler(distributionService) if invitationHandler != nil && distributionService != nil { invitationHandler.SetSiteScoper(distributionService) @@ -453,10 +456,11 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine { fileHandler := filemodule.NewHandler( fileService, fileStorage, - privateFileAuthorizer(deps.DB), + privateFileAuthorizer(deps.DB, cfg.FileBindingMode), recordPrivateFileUpload(deps.DB), - adminPrivateFileAuthorizer(deps.DB), + adminPrivateFileAuthorizer(deps.DB, cfg.FileBindingMode), ) + fileHandler.SetAccessTicketManager(filemodule.NewAccessTicketManager(cfg.JWTSecret)) listingService := listing.NewService(listingRepo, systemConfigRepo) listingHandler := listing.NewHandler(listingService, fileStorage, listing.HandlerOptions{ ExternalUploadSecret: cfg.ExternalUploadSecret, @@ -727,6 +731,7 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine { fileRoutes := api.Group("/files", requireFileAuth) { fileRoutes.POST("/upload", fileHandler.Upload) + fileRoutes.POST("/access/batch", fileHandler.BatchAccess) fileRoutes.GET("/object", fileHandler.Object) } @@ -830,6 +835,7 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine { adminRoutes.GET("/distribution/auth/me", distributionHandler.Me) adminRoutes.POST("/distribution/auth/logout", distributionHandler.Logout) adminRoutes.POST("/files/upload", fileHandler.Upload) + adminRoutes.POST("/files/access/batch", fileHandler.BatchAccess) adminRoutes.GET("/dashboard", requirePerm("dashboard:view"), adminDashboardHandler.Summary) adminRoutes.GET("/manual-entries/today-stats", requirePerm("cs_manual_entry:view"), manualEntryHandler.TodayAgentStats) adminRoutes.GET("/manual-entries/options", requirePerm("cs_manual_entry:view"), manualEntryHandler.ListOptions) diff --git a/backend/migrations/000114_file_object_bindings.sql b/backend/migrations/000114_file_object_bindings.sql new file mode 100644 index 00000000..38a71e81 --- /dev/null +++ b/backend/migrations/000114_file_object_bindings.sql @@ -0,0 +1,18 @@ +-- +goose Up +CREATE TABLE IF NOT EXISTS file_object_bindings ( + id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, + tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0, + canonical_key VARCHAR(512) NOT NULL, + resource_type VARCHAR(32) NOT NULL, + resource_id BIGINT UNSIGNED NOT NULL, + access_scope VARCHAR(32) NOT NULL, + owner_user_id BIGINT UNSIGNED NOT NULL DEFAULT 0, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + UNIQUE KEY uk_file_object_binding (canonical_key, resource_type, resource_id, access_scope), + KEY idx_file_object_binding_key (canonical_key, tenant_id), + KEY idx_file_object_binding_resource (resource_type, resource_id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- +goose Down +DROP TABLE IF EXISTS file_object_bindings; diff --git a/frontend/src/features/admin/utils/__tests__/adminOrderPresentation.spec.ts b/frontend/src/features/admin/utils/__tests__/adminOrderPresentation.spec.ts index f0240642..7a137c0f 100644 --- a/frontend/src/features/admin/utils/__tests__/adminOrderPresentation.spec.ts +++ b/frontend/src/features/admin/utils/__tests__/adminOrderPresentation.spec.ts @@ -1,12 +1,16 @@ import { describe, expect, it } from 'vitest' import { + accountSourceLabel, estimateOrderEndAt, + formatHandoffRecordType, + handoffModeLabel, orderPlatformFeeBreakdown, paymentBizTypeLabel, paymentPaidAt, paymentStatusLabel, paymentStatusType, + settlementModeLabel, } from '../adminOrderPresentation' describe('admin order presentation', () => { @@ -25,11 +29,27 @@ describe('admin order presentation', () => { it('maps known payment states and preserves unknown channel values', () => { expect(paymentStatusLabel('refunded')).toBe('已退款') + expect(paymentStatusType('paid')).toBe('success') expect(paymentStatusType('failed')).toBe('danger') + expect(paymentStatusType('refunding')).toBe('warning') + expect(paymentStatusType('created')).toBe('info') expect(paymentBizTypeLabel('order_pay')).toBe('订单支付') expect(paymentBizTypeLabel('future_type')).toBe('future_type') }) + it('maps handoff, settlement, and account source labels with safe fallbacks', () => { + expect(formatHandoffRecordType('owner_handoff')).toBe('卖家交接') + expect(formatHandoffRecordType('future_type')).toBe('future_type') + expect(handoffModeLabel()).toBe('号主交接') + expect(handoffModeLabel('platform')).toBe('平台代管') + expect(handoffModeLabel('future')).toBe('future') + expect(settlementModeLabel()).toBe('号主钱包') + expect(settlementModeLabel('platform_managed')).toBe('线下结算') + expect(settlementModeLabel('future')).toBe('future') + expect(accountSourceLabel('external')).toBe('外部上传') + expect(accountSourceLabel('internal')).toBe('站内上传') + }) + it('selects the most meaningful payment completion time', () => { expect( paymentPaidAt({ @@ -81,4 +101,32 @@ describe('admin order presentation', () => { totalFeeCent: 4000, }) }) + + it('normalizes missing and invalid fee snapshots', () => { + expect(orderPlatformFeeBreakdown(null)).toEqual({ + baseFeeCent: 0, + insuranceFeeCent: 0, + insuranceFeeBps: 0, + totalFeeCent: 0, + }) + expect( + orderPlatformFeeBreakdown({ + status: 'completed', + platform_fee_cent: -1, + insurance_fee_cent: Number.NaN, + insurance_fee_bps: 500, + checkout: { + platform_fee_cent: 1200, + insurance_fee_cent: 200, + insurance_fee_bps: 800, + }, + }) + ).toEqual({ + baseFeeCent: 1000, + insuranceFeeCent: 200, + insuranceFeeBps: 800, + totalFeeCent: 1200, + }) + expect(estimateOrderEndAt(undefined, '2026-09-02T12:00:00Z', 0)).toBeUndefined() + }) }) diff --git a/frontend/src/shared/api/files.spec.ts b/frontend/src/shared/api/files.spec.ts new file mode 100644 index 00000000..5eaf7b3b --- /dev/null +++ b/frontend/src/shared/api/files.spec.ts @@ -0,0 +1,85 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +const mocks = vi.hoisted(() => ({ + get: vi.fn(), + post: vi.fn(), +})) + +vi.mock('./client', () => ({ + apiClient: { + get: mocks.get, + post: mocks.post, + }, +})) + +vi.mock('@/shared/utils/imageUpload', () => ({ + optimizeImageForUpload: vi.fn(), +})) + +import { fetchAdminFileBlob, fetchFileAccessTicket, fetchFileBlobByURL } from './files' + +describe('file access tickets', () => { + beforeEach(() => { + mocks.get.mockReset() + mocks.post.mockReset() + }) + + it('batches same-tick user image keys and reuses in-flight tickets', async () => { + mocks.post.mockResolvedValue({ + data: { + data: { + tickets: { + 'listing/a.webp': 'ticket-a', + 'listing/b.webp': 'ticket-b', + }, + }, + }, + }) + + const first = fetchFileAccessTicket('listing/a.webp') + const duplicate = fetchFileAccessTicket('listing/a.webp') + const second = fetchFileAccessTicket('listing/b.webp') + + await expect(Promise.all([first, duplicate, second])).resolves.toEqual([ + 'ticket-a', + 'ticket-a', + 'ticket-b', + ]) + expect(mocks.post).toHaveBeenCalledTimes(1) + expect(mocks.post).toHaveBeenCalledWith('/files/access/batch', { + keys: ['listing/a.webp', 'listing/b.webp'], + }) + }) + + it('uses the admin batch endpoint and appends the ticket to object requests', async () => { + mocks.post.mockResolvedValue({ + data: { + data: { + tickets: { + 'chat/message.webp': 'admin-ticket', + }, + }, + }, + }) + mocks.get.mockResolvedValue({ data: new Blob(['image']) }) + + const ticket = await fetchFileAccessTicket('chat/message.webp', true) + await fetchAdminFileBlob('chat/message.webp', ticket) + await fetchFileBlobByURL('/api/files/object?key=chat%2Fmessage.webp', ticket) + + expect(mocks.post).toHaveBeenCalledWith('/admin/files/access/batch', { + keys: ['chat/message.webp'], + }) + expect(mocks.get).toHaveBeenNthCalledWith(1, '/admin/files/object', { + params: { key: 'chat/message.webp', ticket: 'admin-ticket' }, + responseType: 'blob', + }) + expect(mocks.get).toHaveBeenNthCalledWith( + 2, + '/files/object?key=chat%2Fmessage.webp&ticket=admin-ticket', + { + responseType: 'blob', + } + ) + }) +}) diff --git a/frontend/src/shared/api/files.ts b/frontend/src/shared/api/files.ts index 4e085abe..2a2a93b2 100644 --- a/frontend/src/shared/api/files.ts +++ b/frontend/src/shared/api/files.ts @@ -12,6 +12,22 @@ export interface UploadedFile { size: number } +interface AccessTicketsResponse { + tickets: Record + expires_in_seconds: number +} + +interface PendingTicket { + promise: Promise + resolve: (ticket: string) => void + reject: (error: unknown) => void +} + +const pendingTickets: Record<'user' | 'admin', Map> = { + user: new Map(), + admin: new Map(), +} + export async function uploadFile(file: File, scene: string) { const uploadTarget = await optimizeImageForUpload(file, scene) const form = new FormData() @@ -38,15 +54,55 @@ export async function uploadAdminFile( return data.data } -export async function fetchAdminFileBlob(key: string) { +export async function fetchAdminFileBlob(key: string, ticket = '') { const { data } = await apiClient.get('/admin/files/object', { - params: { key }, + params: { key, ...(ticket ? { ticket } : {}) }, responseType: 'blob', }) return data } -export async function fetchFileBlobByURL(fileURL: string) { +export function fetchFileAccessTicket(key: string, admin = false): Promise { + const scope = admin ? 'admin' : 'user' + const pending = pendingTickets[scope] + const existing = pending.get(key) + if (existing) return existing.promise + + let resolveTicket!: (ticket: string) => void + let rejectTicket!: (error: unknown) => void + const promise = new Promise((resolve, reject) => { + resolveTicket = resolve + rejectTicket = reject + }) + pending.set(key, { promise, resolve: resolveTicket, reject: rejectTicket }) + queueMicrotask(flushTicketQueue) + return promise +} + +function flushTicketQueue() { + flushTicketScope('user') + flushTicketScope('admin') +} + +function flushTicketScope(scope: 'user' | 'admin') { + const pending = pendingTickets[scope] + if (!pending.size) return + const batch = Array.from(pending.entries()) + pending.clear() + const keys = [...new Set(batch.map(([key]) => key))] + const path = scope === 'admin' ? '/admin/files/access/batch' : '/files/access/batch' + apiClient + .post>(path, { keys }) + .then(({ data }) => { + const tickets = data.data?.tickets || {} + batch.forEach(([key, entry]) => entry.resolve(tickets[key] || '')) + }) + .catch(error => { + batch.forEach(([, entry]) => entry.reject(error)) + }) +} + +export async function fetchFileBlobByURL(fileURL: string, ticket = '') { let apiPath = fileURL try { const parsed = new URL(fileURL, window.location.origin) @@ -65,6 +121,11 @@ export async function fetchFileBlobByURL(fileURL: string) { apiPath = `/files/object${queryIndex >= 0 ? apiPath.slice(queryIndex) : ''}` } } + if (ticket) { + const parsed = new URL(apiPath, window.location.origin) + parsed.searchParams.set('ticket', ticket) + apiPath = parsed.pathname + parsed.search + } const { data } = await apiClient.get(apiPath, { responseType: 'blob', }) diff --git a/frontend/src/shared/components/business/AuthImage.vue b/frontend/src/shared/components/business/AuthImage.vue index 011ea89d..415d9d07 100644 --- a/frontend/src/shared/components/business/AuthImage.vue +++ b/frontend/src/shared/components/business/AuthImage.vue @@ -1,6 +1,6 @@