fix(tenant): 封堵审计文件与历史用户跨站访问
This commit is contained in:
@@ -130,6 +130,38 @@ func TestPrivateFileAuthorizerRejectsCrossTenantOwner(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminPrivateFileAuthorizerScopesOperatingTenant(t *testing.T) {
|
||||
db := openAuthorizerTestDB(t)
|
||||
if err := database.AutoMigrateIfMissing(db, &model.FileUploadOwner{}); err != nil {
|
||||
t.Fatalf("AutoMigrate() error = %v", err)
|
||||
}
|
||||
rows := []model.FileUploadOwner{
|
||||
{TenantID: 101, UserID: 11, ObjectKey: "listing/admin-tenant-a.jpg"},
|
||||
{TenantID: 202, UserID: 12, ObjectKey: "listing/admin-tenant-b.jpg"},
|
||||
}
|
||||
if err := db.Create(&rows).Error; err != nil {
|
||||
t.Fatalf("create upload owners: %v", err)
|
||||
}
|
||||
authorize := adminPrivateFileAuthorizer(db)
|
||||
ctxA := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 101})
|
||||
ctxB := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 202})
|
||||
|
||||
if allowed, err := authorize(ctxA, 1, "listing/admin-tenant-a.jpg"); err != nil || !allowed {
|
||||
t.Fatalf("tenant A own file allowed=%v err=%v", allowed, err)
|
||||
}
|
||||
if allowed, err := authorize(ctxA, 1, "listing/admin-tenant-b.jpg"); err != nil {
|
||||
t.Fatalf("tenant A cross file error=%v", err)
|
||||
} else if allowed {
|
||||
t.Fatal("tenant A must not read tenant B file")
|
||||
}
|
||||
if allowed, err := authorize(ctxB, 2, "listing/admin-tenant-b.jpg"); err != nil || !allowed {
|
||||
t.Fatalf("tenant B own file allowed=%v err=%v", allowed, err)
|
||||
}
|
||||
if allowed, err := authorize(context.Background(), 3, "listing/admin-tenant-a.jpg"); err != nil || !allowed {
|
||||
t.Fatalf("legacy admin file allowed=%v err=%v", allowed, err)
|
||||
}
|
||||
}
|
||||
|
||||
func openAuthorizerTestDB(t *testing.T) *gorm.DB {
|
||||
t.Helper()
|
||||
db := database.NewTestDB()
|
||||
|
||||
Reference in New Issue
Block a user