feat: implement tenant isolation for knowledge base retrieval
- Added GetKnowledgeBaseByIDAndTenant method to enforce tenant isolation when retrieving knowledge bases by ID. - Updated CopyKnowledgeBase method to use tenant-scoped retrieval, preventing cross-tenant cloning. - Enhanced error handling in the KnowledgeBaseHandler to validate tenant ownership for source and target knowledge bases during copy operations.
This commit is contained in:
@@ -26,7 +26,7 @@ func (r *knowledgeBaseRepository) CreateKnowledgeBase(ctx context.Context, kb *t
|
||||
return r.db.WithContext(ctx).Create(kb).Error
|
||||
}
|
||||
|
||||
// GetKnowledgeBaseByID gets a knowledge base by id
|
||||
// GetKnowledgeBaseByID gets a knowledge base by id (no tenant scope; caller must enforce isolation where needed)
|
||||
func (r *knowledgeBaseRepository) GetKnowledgeBaseByID(ctx context.Context, id string) (*types.KnowledgeBase, error) {
|
||||
var kb types.KnowledgeBase
|
||||
if err := r.db.WithContext(ctx).Where("id = ?", id).First(&kb).Error; err != nil {
|
||||
@@ -38,6 +38,18 @@ func (r *knowledgeBaseRepository) GetKnowledgeBaseByID(ctx context.Context, id s
|
||||
return &kb, nil
|
||||
}
|
||||
|
||||
// GetKnowledgeBaseByIDAndTenant gets a knowledge base by id only if it belongs to the given tenant (enforces tenant isolation)
|
||||
func (r *knowledgeBaseRepository) GetKnowledgeBaseByIDAndTenant(ctx context.Context, id string, tenantID uint64) (*types.KnowledgeBase, error) {
|
||||
var kb types.KnowledgeBase
|
||||
if err := r.db.WithContext(ctx).Where("id = ? AND tenant_id = ?", id, tenantID).First(&kb).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, ErrKnowledgeBaseNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &kb, nil
|
||||
}
|
||||
|
||||
// GetKnowledgeBaseByIDs gets knowledge bases by multiple ids
|
||||
func (r *knowledgeBaseRepository) GetKnowledgeBaseByIDs(ctx context.Context, ids []string) ([]*types.KnowledgeBase, error) {
|
||||
if len(ids) == 0 {
|
||||
|
||||
@@ -437,21 +437,23 @@ func (s *knowledgeBaseService) SetEmbeddingModel(ctx context.Context, id string,
|
||||
return nil
|
||||
}
|
||||
|
||||
// CopyKnowledgeBase copies a knowledge base to a new knowledge base
|
||||
// 浅拷贝
|
||||
// CopyKnowledgeBase copies a knowledge base to a new knowledge base (shallow copy).
|
||||
// Source and target must belong to the tenant in context; cross-tenant access is rejected.
|
||||
func (s *knowledgeBaseService) CopyKnowledgeBase(ctx context.Context,
|
||||
srcKB string, dstKB string,
|
||||
) (*types.KnowledgeBase, *types.KnowledgeBase, error) {
|
||||
sourceKB, err := s.repo.GetKnowledgeBaseByID(ctx, srcKB)
|
||||
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
|
||||
// Load source KB with tenant scope to prevent cross-tenant cloning
|
||||
sourceKB, err := s.repo.GetKnowledgeBaseByIDAndTenant(ctx, srcKB, tenantID)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Get source knowledge base failed: %v", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
sourceKB.EnsureDefaults()
|
||||
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
|
||||
var targetKB *types.KnowledgeBase
|
||||
if dstKB != "" {
|
||||
targetKB, err = s.repo.GetKnowledgeBaseByID(ctx, dstKB)
|
||||
// Load target KB with tenant scope so we only clone into the caller's tenant
|
||||
targetKB, err = s.repo.GetKnowledgeBaseByIDAndTenant(ctx, dstKB, tenantID)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user