fix(chat): 修复跨站会话文件权限
This commit is contained in:
@@ -530,6 +530,86 @@ func TestQrCodeReplenishmentDoesNotCrossTenant(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestListingQrCodePoolDoesNotCrossTenant(t *testing.T) {
|
||||
db := setupQrCodeTestDB(t)
|
||||
otherTenantQR := model.ChatQrCode{
|
||||
TenantID: 101,
|
||||
ImageURL: "/api/files/object?key=qrcode%2Ftenant-a-only.png",
|
||||
Status: QrCodeStatusUnused,
|
||||
}
|
||||
if err := db.Create(&otherTenantQR).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
listing := model.RentalListing{
|
||||
ID: 301,
|
||||
SourceTenantID: 202,
|
||||
ListingNo: "L202611170301",
|
||||
OwnerID: 10,
|
||||
}
|
||||
var conversation *model.ChatConversation
|
||||
if err := db.Transaction(func(tx *gorm.DB) error {
|
||||
created, err := EnsureListingConversation(tx, listing, 0)
|
||||
conversation = created
|
||||
return err
|
||||
}); err != nil {
|
||||
t.Fatalf("建发布群失败: %v", err)
|
||||
}
|
||||
|
||||
var savedQR model.ChatQrCode
|
||||
if err := db.First(&savedQR, otherTenantQR.ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if savedQR.Status != QrCodeStatusUnused {
|
||||
t.Fatalf("其他租户二维码不得被消耗, status = %q", savedQR.Status)
|
||||
}
|
||||
var imageCount int64
|
||||
if err := db.Model(&model.ChatMessage{}).
|
||||
Where("conversation_id = ? AND content_type = ?", conversation.ID, "image").
|
||||
Count(&imageCount).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if imageCount != 0 {
|
||||
t.Fatalf("其他租户码池不得发送到当前群, image messages = %d", imageCount)
|
||||
}
|
||||
var task model.ChatQrCodeDeliveryTask
|
||||
if err := db.Where("conversation_id = ?", conversation.ID).First(&task).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if task.TenantID != 202 || task.Status != "pending" {
|
||||
t.Fatalf("补发任务 = %+v, want tenant 202 pending", task)
|
||||
}
|
||||
|
||||
ownTenantQR := model.ChatQrCode{
|
||||
TenantID: 202,
|
||||
ImageURL: "/api/files/object?key=qrcode%2Ftenant-b-only.png",
|
||||
Status: QrCodeStatusUnused,
|
||||
}
|
||||
if err := db.Create(&ownTenantQR).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Transaction(func(tx *gorm.DB) error {
|
||||
_, err := deliverPendingQrCodes(tx, 202)
|
||||
return err
|
||||
}); err != nil {
|
||||
t.Fatalf("补发当前租户二维码失败: %v", err)
|
||||
}
|
||||
var usedOwnQR model.ChatQrCode
|
||||
if err := db.First(&usedOwnQR, ownTenantQR.ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if usedOwnQR.Status != QrCodeStatusUsed {
|
||||
t.Fatalf("当前租户二维码应被补发使用, status = %q", usedOwnQR.Status)
|
||||
}
|
||||
var unusedOtherQR model.ChatQrCode
|
||||
if err := db.First(&unusedOtherQR, otherTenantQR.ID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if unusedOtherQR.Status != QrCodeStatusUnused {
|
||||
t.Fatalf("其他租户二维码仍不得被使用, status = %q", unusedOtherQR.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureListingConversationIgnoresFixedQrAndUsesSitePool(t *testing.T) {
|
||||
db := setupQrCodeTestDB(t)
|
||||
site := model.DistributionSite{
|
||||
|
||||
@@ -15,6 +15,32 @@ type fileAccessQuery struct {
|
||||
args []any
|
||||
}
|
||||
|
||||
func conversationTenantAccessClause(alias string) string {
|
||||
return `(
|
||||
? = 0
|
||||
OR ` + alias + `.tenant_id = ?
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM rental_orders ro
|
||||
WHERE ro.id = ` + alias + `.order_id
|
||||
AND (ro.buyer_tenant_id = ? OR ro.source_tenant_id = ?)
|
||||
)
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM rental_listings rl
|
||||
WHERE rl.id = ` + alias + `.listing_id
|
||||
AND rl.source_tenant_id = ?
|
||||
)
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM rental_orders rlo
|
||||
WHERE rlo.listing_id = ` + alias + `.listing_id
|
||||
AND rlo.buyer_tenant_id = ?
|
||||
)
|
||||
)`
|
||||
}
|
||||
|
||||
func conversationTenantAccessArgs(tenantID uint64) []any {
|
||||
return []any{tenantID, tenantID, tenantID, tenantID, tenantID, tenantID}
|
||||
}
|
||||
|
||||
func fileReferenceValues(key, basePrefix string) []string {
|
||||
seen := make(map[string]struct{}, 16)
|
||||
values := make([]string, 0, 16)
|
||||
@@ -141,13 +167,9 @@ WHERE (o.owner_id = ? OR o.renter_id = ?)
|
||||
JOIN chat_participants p ON p.conversation_id = m.conversation_id
|
||||
LEFT JOIN chat_conversations c ON c.id = m.conversation_id
|
||||
WHERE p.participant_type = 'user' AND p.participant_id = ?
|
||||
AND (? = 0 OR c.tenant_id = ? OR EXISTS (
|
||||
SELECT 1 FROM rental_orders ro WHERE ro.id = c.order_id AND (ro.buyer_tenant_id = ? OR ro.source_tenant_id = ?)
|
||||
) OR EXISTS (
|
||||
SELECT 1 FROM rental_listings rl WHERE rl.id = c.listing_id AND rl.source_tenant_id = ?
|
||||
))
|
||||
AND ` + conversationTenantAccessClause("c") + `
|
||||
AND ` + chatMembers,
|
||||
args: append([]any{userID, tenantID, tenantID, tenantID, tenantID, tenantID}, jsonArgs...),
|
||||
args: append(append([]any{userID}, conversationTenantAccessArgs(tenantID)...), jsonArgs...),
|
||||
}}, true
|
||||
case "qrcode", "support-qrcode":
|
||||
return []fileAccessQuery{
|
||||
@@ -155,24 +177,18 @@ WHERE p.participant_type = 'user' AND p.participant_id = ?
|
||||
sql: `SELECT COUNT(1) FROM chat_qrcode_pool q
|
||||
JOIN chat_conversations c ON c.id = q.conversation_id
|
||||
JOIN chat_participants p ON p.conversation_id = c.id AND p.participant_type = 'user' AND p.participant_id = ?
|
||||
WHERE (? = 0 OR c.tenant_id = ? OR EXISTS (
|
||||
SELECT 1 FROM rental_orders ro WHERE ro.id = c.order_id AND (ro.buyer_tenant_id = ? OR ro.source_tenant_id = ?)
|
||||
))
|
||||
WHERE ` + conversationTenantAccessClause("c") + `
|
||||
AND ` + qrcodeClause,
|
||||
args: append([]any{userID, tenantID, tenantID, tenantID, tenantID}, qrcodeArgs...),
|
||||
args: append(append([]any{userID}, conversationTenantAccessArgs(tenantID)...), qrcodeArgs...),
|
||||
},
|
||||
{
|
||||
sql: `SELECT COUNT(1) FROM chat_messages m
|
||||
JOIN chat_participants p ON p.conversation_id = m.conversation_id
|
||||
LEFT JOIN chat_conversations c ON c.id = m.conversation_id
|
||||
WHERE p.participant_type = 'user' AND p.participant_id = ?
|
||||
AND (? = 0 OR c.tenant_id = ? OR EXISTS (
|
||||
SELECT 1 FROM rental_orders ro WHERE ro.id = c.order_id AND (ro.buyer_tenant_id = ? OR ro.source_tenant_id = ?)
|
||||
) OR EXISTS (
|
||||
SELECT 1 FROM rental_listings rl WHERE rl.id = c.listing_id AND rl.source_tenant_id = ?
|
||||
))
|
||||
AND ` + conversationTenantAccessClause("c") + `
|
||||
AND ` + chatMembers,
|
||||
args: append([]any{userID, tenantID, tenantID, tenantID, tenantID, tenantID}, jsonArgs...),
|
||||
args: append(append([]any{userID}, conversationTenantAccessArgs(tenantID)...), jsonArgs...),
|
||||
},
|
||||
}, true
|
||||
case "handoff":
|
||||
@@ -244,30 +260,48 @@ WHERE (? = 0 OR (l.source_tenant_id = ? AND (l.distribution_site_id IS NULL OR l
|
||||
args: append([]any{tenantID, tenantID}, jsonArgs...),
|
||||
}}, true
|
||||
case "chat":
|
||||
if isPlatformSuperAdmin(ctx, db, adminID) {
|
||||
return []fileAccessQuery{{
|
||||
sql: `SELECT COUNT(1) FROM chat_messages m WHERE ` + chatMembers,
|
||||
args: jsonArgs,
|
||||
}}, true
|
||||
}
|
||||
return []fileAccessQuery{{
|
||||
sql: `SELECT COUNT(1) FROM chat_messages m
|
||||
JOIN chat_conversations c ON c.id = m.conversation_id
|
||||
WHERE (? = 0 OR c.tenant_id = ?) AND ` + chatMembers,
|
||||
args: append([]any{tenantID, tenantID}, jsonArgs...),
|
||||
WHERE ` + conversationTenantAccessClause("c") + ` AND ` + chatMembers,
|
||||
args: append(conversationTenantAccessArgs(tenantID), jsonArgs...),
|
||||
}}, true
|
||||
case "qrcode", "support-qrcode":
|
||||
qrcodeScope := "(q.tenant_id = ? AND (q.distribution_site_id IS NULL OR q.distribution_site_id = 0))"
|
||||
if tenantID == 0 {
|
||||
return []fileAccessQuery{{
|
||||
sql: `SELECT COUNT(1) FROM chat_qrcode_pool q WHERE ` + qrcodeClause,
|
||||
args: qrcodeArgs,
|
||||
}}, true
|
||||
}
|
||||
poolScope := "(q.tenant_id = ? AND (q.distribution_site_id IS NULL OR q.distribution_site_id = 0))"
|
||||
if isPlatformSuperAdmin(ctx, db, adminID) {
|
||||
// 平台超级管理员需要查看经营租户下各历史分销站点的独立码池。
|
||||
qrcodeScope = "(q.tenant_id = ?)"
|
||||
poolScope = "(q.tenant_id = ?)"
|
||||
}
|
||||
return []fileAccessQuery{
|
||||
{
|
||||
sql: `SELECT COUNT(1) FROM chat_qrcode_pool q
|
||||
WHERE (? = 0 OR ` + qrcodeScope + `)
|
||||
AND ` + qrcodeClause,
|
||||
args: append([]any{tenantID, tenantID}, qrcodeArgs...),
|
||||
WHERE (
|
||||
` + poolScope + `
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM chat_conversations c
|
||||
WHERE c.id = q.conversation_id
|
||||
AND ` + conversationTenantAccessClause("c") + `
|
||||
)
|
||||
)
|
||||
AND ` + qrcodeClause,
|
||||
args: append(append([]any{tenantID}, conversationTenantAccessArgs(tenantID)...), qrcodeArgs...),
|
||||
},
|
||||
{
|
||||
sql: `SELECT COUNT(1) FROM chat_messages m
|
||||
JOIN chat_conversations c ON c.id = m.conversation_id
|
||||
WHERE (? = 0 OR c.tenant_id = ?) AND ` + chatMembers,
|
||||
args: append([]any{tenantID, tenantID}, jsonArgs...),
|
||||
WHERE ` + conversationTenantAccessClause("c") + ` AND ` + chatMembers,
|
||||
args: append(conversationTenantAccessArgs(tenantID), jsonArgs...),
|
||||
},
|
||||
}, true
|
||||
case "handoff":
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/database"
|
||||
"hfb_sys/backend/internal/model"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
|
||||
func TestPrivateFileAccessMatchesCrossTenantConversation(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
seller := model.User{TenantID: 202, Phone: "13900004001", Nickname: "解忧号主", Status: "active"}
|
||||
buyer := model.User{TenantID: 101, Phone: "13900004002", Nickname: "大锤租客", Status: "active"}
|
||||
outsider := model.User{TenantID: 101, Phone: "13900004003", Nickname: "无关用户", Status: "active"}
|
||||
for _, user := range []*model.User{&seller, &buyer, &outsider} {
|
||||
if err := db.Create(user).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
listing := model.RentalListing{
|
||||
SourceTenantID: 202,
|
||||
ListingNo: "L202611170001",
|
||||
AccountID: 1,
|
||||
OwnerID: seller.ID,
|
||||
Status: "rented",
|
||||
ReviewStatus: "approved",
|
||||
}
|
||||
if err := db.Create(&listing).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
order := model.RentalOrder{
|
||||
BuyerTenantID: 101,
|
||||
SourceTenantID: 202,
|
||||
OrderNo: "RO202611170001",
|
||||
ListingID: listing.ID,
|
||||
AccountID: listing.AccountID,
|
||||
OwnerID: seller.ID,
|
||||
RenterID: buyer.ID,
|
||||
Status: "pending_handoff",
|
||||
}
|
||||
if err := db.Create(&order).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conversation := model.ChatConversation{
|
||||
TenantID: 202,
|
||||
ListingID: &listing.ID,
|
||||
Type: "listing_group",
|
||||
Title: "账号群 " + listing.ListingNo,
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(&conversation).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.ChatParticipant{
|
||||
ConversationID: conversation.ID,
|
||||
ParticipantType: "user",
|
||||
ParticipantID: buyer.ID,
|
||||
Role: "renter",
|
||||
JoinedAt: time.Now(),
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
qrcodeURL := "/api/files/object?key=qrcode%2Fcross-tenant.webp"
|
||||
qrcode := model.ChatQrCode{
|
||||
TenantID: 202,
|
||||
ImageURL: qrcodeURL,
|
||||
Status: "used",
|
||||
ConversationID: &conversation.ID,
|
||||
}
|
||||
if err := db.Create(&qrcode).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
chatURL := "/api/files/object?key=chat%2Fcross-tenant.webp"
|
||||
if err := db.Create(&model.ChatMessage{
|
||||
ConversationID: conversation.ID,
|
||||
SenderType: "user",
|
||||
SenderID: seller.ID,
|
||||
ContentType: "image",
|
||||
AttachmentURLS: datatypes.JSON([]byte(`["` + chatURL + `"]`)),
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
key string
|
||||
}{
|
||||
{name: "二维码", key: "qrcode/cross-tenant.webp"},
|
||||
{name: "聊天附件", key: "chat/cross-tenant.webp"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
queries, known := knownUserFileQueries(
|
||||
buyer.ID,
|
||||
101,
|
||||
fileReferenceValues(tc.key, tc.key),
|
||||
)
|
||||
if !known {
|
||||
t.Fatal("文件类型应进入已知业务权限校验")
|
||||
}
|
||||
allowed, err := runFileAccessQueries(context.Background(), db, queries)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !allowed {
|
||||
t.Fatalf("跨站订单租客应可访问%s", tc.name)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
queries, _ := knownUserFileQueries(
|
||||
outsider.ID,
|
||||
101,
|
||||
fileReferenceValues("qrcode/cross-tenant.webp", "qrcode/cross-tenant.webp"),
|
||||
)
|
||||
allowed, err := runFileAccessQueries(context.Background(), db, queries)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if allowed {
|
||||
t.Fatal("非会话参与者不得访问跨站二维码")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminQrCodeFileAccessUsesConversationScope(t *testing.T) {
|
||||
db := database.NewTestDB()
|
||||
admin := model.AdminUser{Username: "dachui-ops", PasswordHash: "x", Status: "active", TokenVersion: 1}
|
||||
if err := db.Create(&admin).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seller := model.User{TenantID: 202, Phone: "13900005001", Status: "active"}
|
||||
buyer := model.User{TenantID: 101, Phone: "13900005002", Status: "active"}
|
||||
for _, user := range []*model.User{&seller, &buyer} {
|
||||
if err := db.Create(user).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
listing := model.RentalListing{
|
||||
SourceTenantID: 202,
|
||||
ListingNo: "L202611170002",
|
||||
AccountID: 2,
|
||||
OwnerID: seller.ID,
|
||||
Status: "rented",
|
||||
ReviewStatus: "approved",
|
||||
}
|
||||
if err := db.Create(&listing).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
order := model.RentalOrder{
|
||||
BuyerTenantID: 101,
|
||||
SourceTenantID: 202,
|
||||
OrderNo: "RO202611170002",
|
||||
ListingID: listing.ID,
|
||||
AccountID: listing.AccountID,
|
||||
OwnerID: seller.ID,
|
||||
RenterID: buyer.ID,
|
||||
Status: "pending_handoff",
|
||||
}
|
||||
if err := db.Create(&order).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conversation := model.ChatConversation{
|
||||
TenantID: 202,
|
||||
ListingID: &listing.ID,
|
||||
Type: "listing_group",
|
||||
Title: "账号群 " + listing.ListingNo,
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(&conversation).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.ChatQrCode{
|
||||
TenantID: 202,
|
||||
ImageURL: "/api/files/object?key=qrcode%2Fadmin-cross.webp",
|
||||
Status: "used",
|
||||
ConversationID: &conversation.ID,
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.ChatQrCode{
|
||||
TenantID: 202,
|
||||
ImageURL: "/api/files/object?key=qrcode%2Fadmin-unused.webp",
|
||||
Status: "unused",
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
queries, known := knownAdminFileQueries(
|
||||
context.Background(),
|
||||
db,
|
||||
admin.ID,
|
||||
101,
|
||||
fileReferenceValues("qrcode/admin-cross.webp", "qrcode/admin-cross.webp"),
|
||||
)
|
||||
if !known {
|
||||
t.Fatal("二维码文件类型应进入管理员权限校验")
|
||||
}
|
||||
allowed, err := runFileAccessQueries(context.Background(), db, queries)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !allowed {
|
||||
t.Fatal("买方租户管理员应可访问其可见跨站会话中的二维码")
|
||||
}
|
||||
|
||||
queries, _ = knownAdminFileQueries(
|
||||
context.Background(),
|
||||
db,
|
||||
admin.ID,
|
||||
101,
|
||||
fileReferenceValues("qrcode/admin-unused.webp", "qrcode/admin-unused.webp"),
|
||||
)
|
||||
allowed, err = runFileAccessQueries(context.Background(), db, queries)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if allowed {
|
||||
t.Fatal("管理员不得访问其他租户未绑定会话的码池图片")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user