Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Fix: clear GetByID (#20371)

Lynn9d66daa154dd
diff --git a/internal/dao/chat_channel.go b/internal/dao/chat_channel.go
index f845413bce..2ee9aadd47 100644
--- a/internal/dao/chat_channel.go
+++ b/internal/dao/chat_channel.go
@@ -17,32 +17,42 @@ func (dao *ChatChannelDAO) Create(ctx context.Context, db *gorm.DB, channel *ent
return db.WithContext(ctx).Create(channel).Error
}
-func (dao *ChatChannelDAO) GetByIDOnly(ctx context.Context, db *gorm.DB, id string) (*entity.ChatChannel, error) {
+// GetByID returns the chat channel with the given id, ignoring tenant isolation.
+// Callers must enforce tenant authorization themselves (see service.accessible).
+func (dao *ChatChannelDAO) GetByID(ctx context.Context, db *gorm.DB, id string) (*entity.ChatChannel, error) {
var channel entity.ChatChannel
- err := db.WithContext(ctx).Where("id = ?", id).First(&channel).Error
- if err != nil {
+ if err := db.WithContext(ctx).Where("id = ?", id).First(&channel).Error; err != nil {
return nil, err
}
- return &channel, err
+ return &channel, nil
}
-func (dao *ChatChannelDAO) GetByID(ctx context.Context, db *gorm.DB, id string, tenantID string) (*entity.ChatChannel, error) {
+// UpdateByID updates the channel with the given id, but only when its tenant_id
+// matches the expected value. A tenant mismatch is treated as not-found so the
+// operation is a no-op rather than a cross-tenant write.
+func (dao *ChatChannelDAO) UpdateByID(ctx context.Context, db *gorm.DB, id string, tenantID string, updates map[string]any) error {
var channel entity.ChatChannel
- err := db.WithContext(ctx).Where("id = ? AND tenant_id = ?", id, tenantID).First(&channel).Error
- if err != nil {
- return nil, err
+ if err := db.WithContext(ctx).Where("id = ?", id).First(&channel).Error; err != nil {
+ return err
}
- return &channel, err
-}
-
-// UpdateByID Update a single record by ID
-func (dao *ChatChannelDAO) UpdateByID(ctx context.Context, db *gorm.DB, id string, tenantID string, updates map[string]any) error {
- return db.WithContext(ctx).Model(&entity.ChatChannel{}).Where("id = ? AND tenant_id = ?", id, tenantID).Updates(updates).Error
+ if channel.TenantID != tenantID {
+ return gorm.ErrRecordNotFound
+ }
+ return db.WithContext(ctx).Model(&entity.ChatChannel{}).Where("id = ?", id).Updates(updates).Error
}
-// DeleteByID Delete a single record by ID
+// DeleteByID deletes the channel with the given id, but only when its tenant_id
+// matches the expected value. A tenant mismatch is treated as not-found so the
+// operation is a no-op rather than a cross-tenant delete.
func (dao *ChatChannelDAO) DeleteByID(ctx context.Context, db *gorm.DB, id string, tenantID string) error {
- return db.WithContext(ctx).Where("id = ? AND tenant_id = ?", id, tenantID).Delete(&entity.ChatChannel{}).Error
+ var channel entity.ChatChannel
+ if err := db.WithContext(ctx).Where("id = ?", id).First(&channel).Error; err != nil {
+ return err
+ }
+ if channel.TenantID != tenantID {
+ return gorm.ErrRecordNotFound
+ }
+ return db.WithContext(ctx).Where("id = ?", id).Delete(&entity.ChatChannel{}).Error
}
// ListByTenantID List a single record by TenantID
diff --git a/internal/dao/chat_channel_test.go b/internal/dao/chat_channel_test.go
index 1869cb6b7d..fe1b8ed2ef 100644
--- a/internal/dao/chat_channel_test.go
+++ b/internal/dao/chat_channel_test.go
@@ -17,6 +17,7 @@
package dao
import (
+ "errors"
"testing"
"time"
@@ -75,8 +76,8 @@ func TestChatChannelDAO_CRUD(t *testing.T) {
t.Fatalf("failed to create chat channel: %v", err)
}
- // 2. Test GetByID
- res, err := dao.GetByID(ctx, db, "chan-1", "tenant-1")
+ // 2. Test GetByID (id-only lookup; tenant isolation is enforced by callers)
+ res, err := dao.GetByID(ctx, db, "chan-1")
if err != nil {
t.Fatalf("failed to get chat channel: %v", err)
}
@@ -91,23 +92,23 @@ func TestChatChannelDAO_CRUD(t *testing.T) {
t.Fatalf("expected corp_id %q, got %v", "ww123456", cred["corp_id"])
}
- // 2b. Test tenant isolation for GetByID
- _, err = dao.GetByID(ctx, db, "chan-1", "tenant-2")
- if err == nil {
- t.Fatalf("expected error (not found) when getting with wrong tenant, got nil")
+ // 2b. GetByID exposes the channel regardless of tenant; callers check tenant
+ _, err = dao.GetByID(ctx, db, "chan-1")
+ if err != nil {
+ t.Fatalf("expected to load channel by id, got error: %v", err)
}
// 3. Test UpdateByID
updates := map[string]interface{}{
"name": "Updated WeCom Bot",
}
- // Try updating with wrong tenant
+ // Try updating with wrong tenant: must report not-found (no cross-tenant write)
err = dao.UpdateByID(ctx, db, "chan-1", "tenant-2", updates)
- if err != nil {
- t.Fatalf("failed to run UpdateByID with wrong tenant: %v", err)
+ if !errors.Is(err, gorm.ErrRecordNotFound) {
+ t.Fatalf("expected not-found for UpdateByID with wrong tenant, got: %v", err)
}
- // Verify it was NOT updated (should still be "Test WeCom Bot" since wrong tenant was used)
- res, err = dao.GetByID(ctx, db, "chan-1", "tenant-1")
+ // Verify it was NOT updated
+ res, err = dao.GetByID(ctx, db, "chan-1")
if err != nil {
t.Fatalf("failed to get chat channel: %v", err)
}
@@ -121,7 +122,7 @@ func TestChatChannelDAO_CRUD(t *testing.T) {
t.Fatalf("failed to update chat channel: %v", err)
}
- res, err = dao.GetByID(ctx, db, "chan-1", "tenant-1")
+ res, err = dao.GetByID(ctx, db, "chan-1")
if err != nil {
t.Fatalf("failed to get updated chat channel: %v", err)
}
@@ -129,13 +130,13 @@ func TestChatChannelDAO_CRUD(t *testing.T) {
t.Fatalf("expected updated Name %q, got %q", "Updated WeCom Bot", res.Name)
}
- // 3b. Test DeleteByID with wrong tenant (should not delete)
+ // 3b. Test DeleteByID with wrong tenant (must report not-found, no deletion)
err = dao.DeleteByID(ctx, db, "chan-1", "tenant-2")
- if err != nil {
- t.Fatalf("failed to delete with wrong tenant: %v", err)
+ if !errors.Is(err, gorm.ErrRecordNotFound) {
+ t.Fatalf("expected not-found for DeleteByID with wrong tenant, got: %v", err)
}
// Verify it still exists for tenant-1
- _, err = dao.GetByID(ctx, db, "chan-1", "tenant-1")
+ _, err = dao.GetByID(ctx, db, "chan-1")
if err != nil {
t.Fatalf("expected chat channel to still exist for tenant-1, got error: %v", err)
}
@@ -146,7 +147,7 @@ func TestChatChannelDAO_CRUD(t *testing.T) {
t.Fatalf("failed to delete chat channel: %v", err)
}
- _, err = dao.GetByID(ctx, db, "chan-1", "tenant-1")
+ _, err = dao.GetByID(ctx, db, "chan-1")
if err == nil {
t.Fatalf("expected record not found error, got nil")
}
diff --git a/internal/service/chat_channel.go b/internal/service/chat_channel.go
index ea6ebde53f..1fcfc1ef7c 100644
--- a/internal/service/chat_channel.go
+++ b/internal/service/chat_channel.go
@@ -95,7 +95,7 @@ func (s *ChatChannelService) GetByID(ctx context.Context, id string) (*entity.Ch
if id == "" {
return nil, errors.New("id is empty")
}
- return s.chatChannelDAO.GetByIDOnly(ctx, dao.DB, id)
+ return s.chatChannelDAO.GetByID(ctx, dao.DB, id)
}
func (s *ChatChannelService) List(ctx context.Context, tenantID string) ([]*entity.ChatChannelListResponse, error) {
@@ -137,7 +137,7 @@ func (s *ChatChannelService) CreateChatChannel(ctx context.Context, tenantID, na
}
func (s *ChatChannelService) accessible(ctx context.Context, userID, channelID string) (*entity.ChatChannel, bool, error) {
- channel, err := s.chatChannelDAO.GetByIDOnly(ctx, dao.DB, channelID)
+ channel, err := s.chatChannelDAO.GetByID(ctx, dao.DB, channelID)
if err != nil {
if dao.IsNotFoundErr(err) {
return nil, false, nil
@@ -171,7 +171,7 @@ func (s *ChatChannelService) GetChatChannel(ctx context.Context, userID, channel
return nil, common.CodeAuthenticationError, errors.New("no authorization")
}
- channel, err := s.chatChannelDAO.GetByIDOnly(ctx, dao.DB, channelID)
+ channel, err := s.chatChannelDAO.GetByID(ctx, dao.DB, channelID)
if err != nil {
if dao.IsNotFoundErr(err) {
return nil, common.CodeDataError, errors.New("can't find this chat channel")
@@ -245,7 +245,7 @@ func (s *ChatChannelService) UpdateChatChannel(ctx context.Context, userID, chan
}
}
- updated, err := s.chatChannelDAO.GetByIDOnly(ctx, dao.DB, channelID)
+ updated, err := s.chatChannelDAO.GetByID(ctx, dao.DB, channelID)
if err != nil {
if dao.IsNotFoundErr(err) {
return nil, common.CodeDataError, errors.New("can't find this chat channel")
@@ -286,7 +286,7 @@ func (s *ChatChannelService) HandleIncomingMessage(ctx context.Context, msg Chat
return "", nil
}
- channel, err := s.chatChannelDAO.GetByIDOnly(ctx, dao.DB, msg.AccountID)
+ channel, err := s.chatChannelDAO.GetByID(ctx, dao.DB, msg.AccountID)
if err != nil {
if dao.IsNotFoundErr(err) {
return "", nil