RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix: enforce dataset access for document detail and metadata summary (#20649)
adwsc76ee6fcbdd0
diff --git a/internal/handler/document.go b/internal/handler/document.goindex dd54e90f3a..3397f0f7fa 100644--- a/internal/handler/document.go+++ b/internal/handler/document.go@@ -120,7 +120,7 @@ func NewDocumentHandler(documentService documentServiceIface, datasetService *da// @Success 200 {object} map[string]interface{}// @Router /api/v1/documents/{id} [get]func (h *DocumentHandler) GetDocumentByID(c *gin.Context) {- _, errorCode, errorMessage := GetUser(c)+ user, errorCode, errorMessage := GetUser(c)if errorCode != common.CodeSuccess {common.ErrorWithCode(c, errorCode, errorMessage)return@@ -143,6 +143,11 @@ func (h *DocumentHandler) GetDocumentByID(c *gin.Context) {return}+ if !h.datasetService.Accessible(ctx, doc.KbID, user.ID) {+ common.ResponseWithHttpCodeData(c, http.StatusForbidden, common.CodeForbidden, nil, "no permission to access this dataset")+ return+ }+c.JSON(http.StatusOK, gin.H{"data": doc,})@@ -1304,7 +1309,7 @@ func stringValue(value *string) string {// MetadataSummary handles the metadata summary requestfunc (h *DocumentHandler) MetadataSummary(c *gin.Context) {- _, errorCode, errorMessage := GetUser(c)+ user, errorCode, errorMessage := GetUser(c)if errorCode != common.CodeSuccess {common.ErrorWithCode(c, errorCode, errorMessage)return@@ -1327,6 +1332,11 @@ func (h *DocumentHandler) MetadataSummary(c *gin.Context) {}ctx := c.Request.Context()+ if !h.datasetService.Accessible(ctx, kbID, user.ID) {+ common.ResponseWithHttpCodeData(c, http.StatusForbidden, common.CodeForbidden, nil, "no permission to access this dataset")+ return+ }+summary, err := h.documentService.GetMetadataSummary(ctx, kbID, requestBody.DocIDs)if err != nil {common.ResponseWithHttpCodeData(c, http.StatusInternalServerError, 1, nil, "Failed to get metadata summary: "+err.Error())diff --git a/internal/handler/document_test.go b/internal/handler/document_test.goindex da9379ee09..c873148d16 100644--- a/internal/handler/document_test.go+++ b/internal/handler/document_test.go@@ -428,6 +428,52 @@ func setupDocumentPermissionDB(t *testing.T, accessible bool) {t.Cleanup(func() { dao.DB = orig })}+func TestDocumentReadHandlersForbidden(t *testing.T) {+ setupDocumentPermissionDB(t, false)+ fake := &fakeDocumentService{+ doc: &document.DocumentResponse{ID: "doc-1", KbID: "kb-owner"},+ }+ h := &DocumentHandler{+ documentService: fake,+ datasetService: dataset.NewDatasetService(),+ }+ tests := []struct {+ name string+ method string+ path string+ body string+ params gin.Params+ handler gin.HandlerFunc+ }{+ {"document detail", http.MethodGet, "/api/v1/documents/doc-1", "", gin.Params{{Key: "id", Value: "doc-1"}}, h.GetDocumentByID},+ {"metadata summary", http.MethodPost, "/api/v1/document/metadata/summary", `{"kb_id":"kb-owner","doc_ids":["doc-1"]}`, nil, h.MetadataSummary},+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ c, w := setupGinContextWithUser(tt.method, tt.path, tt.body)+ c.Params = tt.params+ tt.handler(c)+ if w.Code != http.StatusForbidden {+ t.Fatalf("status = %d, want 403: %s", w.Code, w.Body.String())+ }+ var resp struct {+ Code common.ErrorCode `json:"code"`+ Message string `json:"message"`+ Data interface{} `json:"data"`+ }+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {+ t.Fatal(err)+ }+ if resp.Code != common.CodeForbidden || resp.Message != "no permission to access this dataset" || resp.Data != nil {+ t.Fatalf("unexpected response: %s", w.Body.String())+ }+ if fake.metadataKBID != "" {+ t.Fatal("GetMetadataSummary should not be called without dataset access")+ }+ })+ }+}+func TestSetMetaHandler_NotAccessible(t *testing.T) {setupDocumentPermissionDB(t, false)