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) {
+60
View File
@@ -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()
}
}
}()
}
+54
View File
@@ -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
}
+21
View File
@@ -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)
}
})
}
}
@@ -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
}
}
@@ -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)
}
}
}
+138
View File
@@ -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)
}
@@ -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",
@@ -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",
@@ -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)
}
@@ -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")
}
}
+217 -25
View File
@@ -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 {
+284 -10
View File
@@ -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
}
@@ -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{
+8 -2
View File
@@ -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)