perf(files): 优化私有文件鉴权与高成本查询隔离
This commit is contained in:
@@ -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{
|
||||
|
||||
Reference in New Issue
Block a user