Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
feat(datasource): enforce tenant isolation and knowledge base ownership
wizardchen442c340e7f96
diff --git a/internal/application/service/datasource_service.go b/internal/application/service/datasource_service.goindex cc776dcfb..b2f1b14d3 100644--- a/internal/application/service/datasource_service.go+++ b/internal/application/service/datasource_service.go@@ -66,6 +66,9 @@ func (s *DataSourceService) CreateDataSource(ctx context.Context, ds *types.Dataif err != nil || kb == nil {return nil, datasource.ErrKnowledgeBaseNotFound}+ if kb.TenantID != ds.TenantID {+ return nil, datasource.ErrKnowledgeBaseNotFound+ }// Validate connector type_, err = s.connectorRegistry.Get(ds.Type)@@ -135,6 +138,20 @@ func (s *DataSourceService) UpdateDataSource(ctx context.Context, ds *types.Datareturn nil, err}+ if ds.KnowledgeBaseID == "" {+ ds.KnowledgeBaseID = existing.KnowledgeBaseID+ }+ if ds.KnowledgeBaseID != existing.KnowledgeBaseID {+ return nil, fmt.Errorf("changing knowledge base is not allowed")+ }++ if ds.TenantID == 0 {+ ds.TenantID = existing.TenantID+ }+ if ds.TenantID != existing.TenantID {+ return nil, datasource.ErrDataSourceInvalid+ }+// Validate new configuration if changedif ds.Type != existing.Type || string(ds.Config) != string(existing.Config) {if err := s.validateDataSourceConfig(ctx, ds); err != nil {diff --git a/internal/handler/datasource.go b/internal/handler/datasource.goindex 418be481a..aea81fc31 100644--- a/internal/handler/datasource.go+++ b/internal/handler/datasource.go@@ -1,6 +1,7 @@package handlerimport (+ "context""net/http""strconv"@@ -12,13 +13,18 @@ import (// DataSourceHandler handles HTTP requests for data source managementtype DataSourceHandler struct {- service interfaces.DataSourceService+ service interfaces.DataSourceService+ kbService interfaces.KnowledgeBaseService}// NewDataSourceHandler creates a new data source handler-func NewDataSourceHandler(service interfaces.DataSourceService) *DataSourceHandler {+func NewDataSourceHandler(+ service interfaces.DataSourceService,+ kbService interfaces.KnowledgeBaseService,+) *DataSourceHandler {return &DataSourceHandler{- service: service,+ service: service,+ kbService: kbService,}}@@ -29,6 +35,45 @@ func (h *DataSourceHandler) getTenantID(c *gin.Context) uint64 {return tenantID}+// Data source settings contain connector credentials, so only the owning tenant can access them.+func (h *DataSourceHandler) getOwnedKnowledgeBase(+ ctx context.Context,+ tenantID uint64,+ kbID string,+) (*types.KnowledgeBase, int, string) {+ if kbID == "" {+ return nil, http.StatusBadRequest, "kb_id is required"+ }++ kb, err := h.kbService.GetKnowledgeBaseByID(ctx, kbID)+ if err != nil || kb == nil {+ return nil, http.StatusNotFound, "knowledge base not found"+ }++ if kb.TenantID != tenantID {+ return nil, http.StatusForbidden, "access denied"+ }++ return kb, http.StatusOK, ""+}++func (h *DataSourceHandler) getOwnedDataSource(+ ctx context.Context,+ tenantID uint64,+ id string,+) (*types.DataSource, int, string) {+ ds, err := h.service.GetDataSource(ctx, id)+ if err != nil {+ return nil, http.StatusNotFound, "data source not found"+ }++ if _, status, msg := h.getOwnedKnowledgeBase(ctx, tenantID, ds.KnowledgeBaseID); status != http.StatusOK {+ return nil, status, msg+ }++ return ds, http.StatusOK, ""+}+// CreateDataSource godoc// @Summary Create a new data source// @Description Create a new data source configuration for a knowledge base@@ -41,7 +86,7 @@ func (h *DataSourceHandler) getTenantID(c *gin.Context) uint64 {// @Router /api/v1/datasource [post]func (h *DataSourceHandler) CreateDataSource(c *gin.Context) {ctx := c.Request.Context()-+// Extract tenant ID from context (set by auth middleware)tenantID := c.GetUint64(types.TenantIDContextKey.String())if tenantID == 0 {@@ -55,6 +100,11 @@ func (h *DataSourceHandler) CreateDataSource(c *gin.Context) {return}+ if _, status, msg := h.getOwnedKnowledgeBase(ctx, tenantID, req.KnowledgeBaseID); status != http.StatusOK {+ c.JSON(status, gin.H{"error": msg})+ return+ }+// Enforce tenant isolationreq.TenantID = tenantID@@ -86,15 +136,9 @@ func (h *DataSourceHandler) GetDataSource(c *gin.Context) {id := c.Param("id")- ds, err := h.service.GetDataSource(ctx, id)- if err != nil {- c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})- return- }-- // Verify ownership (data source belongs to this tenant)- if ds.TenantID != tenantID {- c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})+ ds, status, msg := h.getOwnedDataSource(ctx, tenantID, id)+ if status != http.StatusOK {+ c.JSON(status, gin.H{"error": msg})return}@@ -112,7 +156,7 @@ func (h *DataSourceHandler) GetDataSource(c *gin.Context) {// @Router /api/v1/datasource [get]func (h *DataSourceHandler) ListDataSources(c *gin.Context) {ctx := c.Request.Context()-+// Extract tenant ID from contexttenantID := c.GetUint64(types.TenantIDContextKey.String())if tenantID == 0 {@@ -121,8 +165,8 @@ func (h *DataSourceHandler) ListDataSources(c *gin.Context) {}kbID := c.Query("kb_id")- if kbID == "" {- c.JSON(http.StatusBadRequest, gin.H{"error": "kb_id is required"})+ if _, status, msg := h.getOwnedKnowledgeBase(ctx, tenantID, kbID); status != http.StatusOK {+ c.JSON(status, gin.H{"error": msg})return}@@ -165,20 +209,15 @@ func (h *DataSourceHandler) UpdateDataSource(c *gin.Context) {return}- // Fetch existing data source to verify ownership- existing, err := h.service.GetDataSource(ctx, id)- if err != nil {- c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})- return- }-- if existing.TenantID != tenantID {- c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})+ existing, status, msg := h.getOwnedDataSource(ctx, tenantID, id)+ if status != http.StatusOK {+ c.JSON(status, gin.H{"error": msg})return}req.ID = id- req.TenantID = tenantID+ req.TenantID = existing.TenantID+ req.KnowledgeBaseID = existing.KnowledgeBaseIDds, err := h.service.UpdateDataSource(ctx, &req)if err != nil {c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})@@ -206,15 +245,8 @@ func (h *DataSourceHandler) DeleteDataSource(c *gin.Context) {id := c.Param("id")- // Verify ownership before deleting- ds, err := h.service.GetDataSource(ctx, id)- if err != nil {- c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})- return- }-- if ds.TenantID != tenantID {- c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})+ if _, status, msg := h.getOwnedDataSource(ctx, tenantID, id); status != http.StatusOK {+ c.JSON(status, gin.H{"error": msg})return}@@ -244,15 +276,8 @@ func (h *DataSourceHandler) ValidateConnection(c *gin.Context) {id := c.Param("id")- // Verify ownership- ds, err := h.service.GetDataSource(ctx, id)- if err != nil {- c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})- return- }-- if ds.TenantID != tenantID {- c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})+ if _, status, msg := h.getOwnedDataSource(ctx, tenantID, id); status != http.StatusOK {+ c.JSON(status, gin.H{"error": msg})return}@@ -280,6 +305,11 @@ func (h *DataSourceHandler) ValidateConnection(c *gin.Context) {// @Router /api/v1/datasource/validate-credentials [post]func (h *DataSourceHandler) ValidateCredentials(c *gin.Context) {ctx := c.Request.Context()+ tenantID := h.getTenantID(c)+ if tenantID == 0 {+ c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})+ return+ }var req struct {Type string `json:"type" binding:"required"`@@ -297,6 +327,7 @@ func (h *DataSourceHandler) ValidateCredentials(c *gin.Context) {c.JSON(http.StatusOK, gin.H{"status": "connected"})}+// @Summary List available resources in data source// @Description List resources available for sync in the external system// @Tags DataSource@@ -315,15 +346,8 @@ func (h *DataSourceHandler) ListAvailableResources(c *gin.Context) {id := c.Param("id")- // Verify ownership- ds, err := h.service.GetDataSource(ctx, id)- if err != nil {- c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})- return- }-- if ds.TenantID != tenantID {- c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})+ if _, status, msg := h.getOwnedDataSource(ctx, tenantID, id); status != http.StatusOK {+ c.JSON(status, gin.H{"error": msg})return}@@ -357,15 +381,8 @@ func (h *DataSourceHandler) ManualSync(c *gin.Context) {id := c.Param("id")- // Verify ownership- ds, err := h.service.GetDataSource(ctx, id)- if err != nil {- c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})- return- }-- if ds.TenantID != tenantID {- c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})+ if _, status, msg := h.getOwnedDataSource(ctx, tenantID, id); status != http.StatusOK {+ c.JSON(status, gin.H{"error": msg})return}@@ -396,15 +413,8 @@ func (h *DataSourceHandler) PauseDataSource(c *gin.Context) {id := c.Param("id")- // Verify ownership- ds, err := h.service.GetDataSource(ctx, id)- if err != nil {- c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})- return- }-- if ds.TenantID != tenantID {- c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})+ if _, status, msg := h.getOwnedDataSource(ctx, tenantID, id); status != http.StatusOK {+ c.JSON(status, gin.H{"error": msg})return}@@ -434,15 +444,8 @@ func (h *DataSourceHandler) ResumeDataSource(c *gin.Context) {id := c.Param("id")- // Verify ownership- ds, err := h.service.GetDataSource(ctx, id)- if err != nil {- c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})- return- }-- if ds.TenantID != tenantID {- c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})+ if _, status, msg := h.getOwnedDataSource(ctx, tenantID, id); status != http.StatusOK {+ c.JSON(status, gin.H{"error": msg})return}@@ -475,15 +478,8 @@ func (h *DataSourceHandler) GetSyncLogs(c *gin.Context) {id := c.Param("id")- // Verify ownership- ds, err := h.service.GetDataSource(ctx, id)- if err != nil {- c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})- return- }-- if ds.TenantID != tenantID {- c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})+ if _, status, msg := h.getOwnedDataSource(ctx, tenantID, id); status != http.StatusOK {+ c.JSON(status, gin.H{"error": msg})return}@@ -525,6 +521,12 @@ func (h *DataSourceHandler) GetSyncLogs(c *gin.Context) {// @Router /api/v1/datasource/logs/{log_id} [get]func (h *DataSourceHandler) GetSyncLog(c *gin.Context) {ctx := c.Request.Context()+ tenantID := h.getTenantID(c)+ if tenantID == 0 {+ c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})+ return+ }+logID := c.Param("log_id")log, err := h.service.GetSyncLog(ctx, logID)@@ -533,6 +535,11 @@ func (h *DataSourceHandler) GetSyncLog(c *gin.Context) {return}+ if _, status, msg := h.getOwnedDataSource(ctx, tenantID, log.DataSourceID); status != http.StatusOK {+ c.JSON(status, gin.H{"error": msg})+ return+ }+c.JSON(http.StatusOK, log)}