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(api): authorize dataset and workspace commit routes (#19357)
Harsh Kashyapfb2f88187166
diff --git a/api/apps/restful_apis/file_commit_api.py b/api/apps/restful_apis/file_commit_api.pyindex 37c35ec884..06df38c960 100644--- a/api/apps/restful_apis/file_commit_api.py+++ b/api/apps/restful_apis/file_commit_api.py@@ -20,7 +20,9 @@ from quart import requestfrom api.apps import login_required, current_userfrom api.utils.api_utils import get_json_result, get_data_error_result, get_request_json, server_error_response, validate_requestfrom api.utils.pagination_utils import DEFAULT_PAGE, DEFAULT_PAGE_SIZE, validate_rest_api_page, validate_rest_api_page_size+from api.common.check_team_permission import check_file_team_permissionfrom api.db.services.file_commit_service import FileCommitService+from api.db.services.file_service import FileServicefrom api.db.services.knowledgebase_service import KnowledgebaseServicelogger = logging.getLogger(__name__)@@ -75,8 +77,10 @@ def _resolve_dataset_folder(dataset_id):missing KB drives ``_resolve`` to reject the request before it hitsa query."""- success, _kb = KnowledgebaseService.get_by_id(dataset_id)- if not success:+ # Authorize, not just existence: the commit surface exposes artifact+ # contents and history, so it needs the same access check the artifact+ # routes in dataset_api.py apply.+ if not KnowledgebaseService.accessible(dataset_id, current_user.id):return Nonereturn dataset_id@@ -98,7 +102,14 @@ def _register_commit_routes(prefix, param_name, resolver_type=None):def _resolve(entity_id):if resolver_type is None:- return entity_id # already a folder_id+ # entity_id IS the folder_id. Every folder is a File row owned by+ # a tenant, so authorize it the same way file_api does - a logged-in+ # user must not read or write another tenant's commit history just+ # by guessing a folder id.+ e, folder = FileService.get_by_id(entity_id)+ if not e or not check_file_team_permission(folder, current_user.id):+ raise ValueError(f"Could not resolve folder '{entity_id}'")+ return entity_idfolder_id = _resolve_folder_id(resolver_type, entity_id)if folder_id is None:raise ValueError(f"Could not resolve {resolver_type} '{entity_id}' to a folder")@@ -371,6 +382,9 @@ _register_commit_routes("/workspaces/<entity_id>", "entity_id")@login_requiredasync def get_file_version_history(file_id):try:+ e, file = FileService.get_by_id(file_id)+ if not e or not check_file_team_permission(file, current_user.id):+ return get_data_error_result("File not found")versions = FileCommitService.get_file_version_history(file_id)return get_json_result(data=versions)except Exception as e:diff --git a/internal/handler/file_commit.go b/internal/handler/file_commit.goindex 2a20bd4a03..447de804fd 100644--- a/internal/handler/file_commit.go+++ b/internal/handler/file_commit.go@@ -22,6 +22,7 @@ import ("ragflow/internal/common""ragflow/internal/dao""ragflow/internal/entity"+ "ragflow/internal/service""strconv""github.com/gin-gonic/gin"@@ -83,6 +84,20 @@ func CommitFolderResolver(h *FileCommitHandler, entityType, urlParam string) ginreturn}ctx := c.Request.Context()+ user, errorCode, errorMessage := GetUser(c)+ if errorCode != common.CodeSuccess {+ common.ErrorWithCode(c, errorCode, errorMessage)+ c.Abort()+ return+ }+ // Authorize, not just existence: the commit surface exposes artifact+ // contents and history, so it needs the same access check the dataset+ // routes apply.+ if entityType == "datasets" && !h.kbDAO.Accessible(ctx, dao.DB, id, user.ID) {+ common.ResponseWithCodeData(c, common.CodeNotFound, nil, fmt.Sprintf("%s not found", entityType))+ c.Abort()+ return+ }folderID, err := h.ResolveFolderID(ctx, entityType, id)if err != nil {common.ResponseWithCodeData(c, common.CodeNotFound, nil, fmt.Sprintf("%s folder not found", entityType))@@ -111,6 +126,20 @@ func (h *FileCommitHandler) resolveDatasetFolderID(ctx context.Context, datasetIreturn "", common.ErrNotFound}+// folderAccessible reports whether userID may access the given workspace+// folder. Every folder is a File row owned by a tenant: the commit surface+// exposes artifact contents, history, and writes, so apply the same access+// check the other file routes use instead of trusting a caller-supplied+// folder id.+func (h *FileCommitHandler) folderAccessible(c *gin.Context, userID, folderID string) bool {+ ctx := c.Request.Context()+ folder, err := h.fileDAO.GetByID(ctx, dao.DB, folderID)+ if err != nil || folder == nil {+ return false+ }+ return service.CheckFileTeamPermission(ctx, h.fileDAO, folder, userID)+}+// CreateCommitRequest represents the request body for creating a committype CreateCommitRequest struct {Message string `json:"message" binding:"required"`@@ -138,6 +167,11 @@ func (h *FileCommitHandler) CreateCommit(c *gin.Context) {return}+ if !h.folderAccessible(c, user.ID, folderID) {+ common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Workspace not found")+ return+ }+var req CreateCommitRequestif err := c.ShouldBindJSON(&req); err != nil {common.ErrorWithCode(c, common.CodeBadRequest, err.Error())@@ -182,7 +216,7 @@ func (h *FileCommitHandler) CreateCommit(c *gin.Context) {// @Success 200 {object} map[string]interface{}// @Router /api/v1/workspaces/{folder_id}/commits [get]func (h *FileCommitHandler) ListCommits(c *gin.Context) {- _, errorCode, errorMessage := GetUser(c)+ user, errorCode, errorMessage := GetUser(c)if errorCode != common.CodeSuccess {common.ErrorWithCode(c, errorCode, errorMessage)return@@ -194,6 +228,10 @@ func (h *FileCommitHandler) ListCommits(c *gin.Context) {return}+ if !h.folderAccessible(c, user.ID, folderID) {+ common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Workspace not found")+ return+ }page := 1if pageStr := c.Query("page"); pageStr != "" {if p, err := strconv.Atoi(pageStr); err == nil && p >= 1 {@@ -303,7 +341,7 @@ func (h *FileCommitHandler) ListCommits(c *gin.Context) {// @Success 200 {object} map[string]interface{}// @Router /api/v1/workspaces/{folder_id}/commits/{commit_id} [get]func (h *FileCommitHandler) GetCommit(c *gin.Context) {- _, errorCode, errorMessage := GetUser(c)+ user, errorCode, errorMessage := GetUser(c)if errorCode != common.CodeSuccess {common.ErrorWithCode(c, errorCode, errorMessage)return@@ -316,6 +354,10 @@ func (h *FileCommitHandler) GetCommit(c *gin.Context) {return}+ if !h.folderAccessible(c, user.ID, folderID) {+ common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Workspace not found")+ return+ }ctx := c.Request.Context()commit, err := h.commitService.GetCommit(ctx, commitID)if err != nil {@@ -361,7 +403,7 @@ func (h *FileCommitHandler) GetCommit(c *gin.Context) {// @Success 200 {object} map[string]interface{}// @Router /api/v1/workspaces/{folder_id}/commits/{commit_id}/files [get]func (h *FileCommitHandler) ListCommitFiles(c *gin.Context) {- _, errorCode, errorMessage := GetUser(c)+ user, errorCode, errorMessage := GetUser(c)if errorCode != common.CodeSuccess {common.ErrorWithCode(c, errorCode, errorMessage)return@@ -374,6 +416,10 @@ func (h *FileCommitHandler) ListCommitFiles(c *gin.Context) {return}+ if !h.folderAccessible(c, user.ID, folderID) {+ common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Workspace not found")+ return+ }ctx := c.Request.Context()commit, err := h.commitService.GetCommit(ctx, commitID)if err != nil {@@ -406,7 +452,7 @@ func (h *FileCommitHandler) ListCommitFiles(c *gin.Context) {// @Success 200 {object} map[string]interface{}// @Router /api/v1/workspaces/{folder_id}/commits/diff [get]func (h *FileCommitHandler) DiffCommits(c *gin.Context) {- _, errorCode, errorMessage := GetUser(c)+ user, errorCode, errorMessage := GetUser(c)if errorCode != common.CodeSuccess {common.ErrorWithCode(c, errorCode, errorMessage)return@@ -420,6 +466,10 @@ func (h *FileCommitHandler) DiffCommits(c *gin.Context) {return}+ if !h.folderAccessible(c, user.ID, folderID) {+ common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Workspace not found")+ return+ }ctx := c.Request.Context()fromCommit, err := h.commitService.GetCommit(ctx, fromID)if err != nil {@@ -455,7 +505,7 @@ func (h *FileCommitHandler) DiffCommits(c *gin.Context) {// @Success 200 {object} map[string]interface{}// @Router /api/v1/workspaces/{folder_id}/changes [get]func (h *FileCommitHandler) GetUncommittedChanges(c *gin.Context) {- _, errorCode, errorMessage := GetUser(c)+ user, errorCode, errorMessage := GetUser(c)if errorCode != common.CodeSuccess {common.ErrorWithCode(c, errorCode, errorMessage)return@@ -467,6 +517,10 @@ func (h *FileCommitHandler) GetUncommittedChanges(c *gin.Context) {return}+ if !h.folderAccessible(c, user.ID, folderID) {+ common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Workspace not found")+ return+ }ctx := c.Request.Context()changes, err := h.commitService.GetUncommittedChanges(ctx, folderID)if err != nil {@@ -488,7 +542,7 @@ func (h *FileCommitHandler) GetUncommittedChanges(c *gin.Context) {// @Success 200 {object} map[string]interface{}// @Router /api/v1/workspaces/{folder_id}/commits/{commit_id}/tree [get]func (h *FileCommitHandler) GetCommitTree(c *gin.Context) {- _, errorCode, errorMessage := GetUser(c)+ user, errorCode, errorMessage := GetUser(c)if errorCode != common.CodeSuccess {common.ErrorWithCode(c, errorCode, errorMessage)return@@ -501,6 +555,10 @@ func (h *FileCommitHandler) GetCommitTree(c *gin.Context) {return}+ if !h.folderAccessible(c, user.ID, folderID) {+ common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Workspace not found")+ return+ }ctx := c.Request.Context()commit, err := h.commitService.GetCommit(ctx, commitID)if err != nil {@@ -533,7 +591,7 @@ func (h *FileCommitHandler) GetCommitTree(c *gin.Context) {// @Success 200 {object} map[string]interface{}// @Router /api/v1/workspaces/{folder_id}/commits/{commit_id}/files/{file_id}/content [get]func (h *FileCommitHandler) GetCommitFileContent(c *gin.Context) {- _, errorCode, errorMessage := GetUser(c)+ user, errorCode, errorMessage := GetUser(c)if errorCode != common.CodeSuccess {common.ErrorWithCode(c, errorCode, errorMessage)return@@ -548,6 +606,10 @@ func (h *FileCommitHandler) GetCommitFileContent(c *gin.Context) {return}+ if !h.folderAccessible(c, user.ID, folderID) {+ common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Workspace not found")+ return+ }ctx := c.Request.Context()commit, err := h.commitService.GetCommit(ctx, commitID)if err != nil {@@ -578,7 +640,7 @@ func (h *FileCommitHandler) GetCommitFileContent(c *gin.Context) {// @Success 200 {object} map[string]interface{}// @Router /api/v1/files/{file_id}/versions [get]func (h *FileCommitHandler) GetFileVersionHistory(c *gin.Context) {- _, errorCode, errorMessage := GetUser(c)+ user, errorCode, errorMessage := GetUser(c)if errorCode != common.CodeSuccess {common.ErrorWithCode(c, errorCode, errorMessage)return@@ -591,6 +653,12 @@ func (h *FileCommitHandler) GetFileVersionHistory(c *gin.Context) {}ctx := c.Request.Context()+ file, err := h.fileDAO.GetByID(ctx, dao.DB, fileID)+ if err != nil || file == nil || !service.CheckFileTeamPermission(ctx, h.fileDAO, file, user.ID) {+ common.ResponseWithCodeData(c, common.CodeNotFound, nil, "File not found")+ return+ }+versions, err := h.commitService.GetFileVersionHistory(ctx, fileID)if err != nil {jsonInternalError(c, err)diff --git a/internal/handler/file_commit_test.go b/internal/handler/file_commit_test.goindex a8ecb07e3b..4230c156fe 100644--- a/internal/handler/file_commit_test.go+++ b/internal/handler/file_commit_test.go@@ -25,9 +25,12 @@ import ("testing""ragflow/internal/common"+ "ragflow/internal/dao""ragflow/internal/entity""github.com/gin-gonic/gin"+ "github.com/glebarez/sqlite"+ "gorm.io/gorm")// mockFileCommitSvc implements FileCommitServiceInterface for testing@@ -137,9 +140,34 @@ func (m *mockFileCommitSvc) ListPageCommits(ctx context.Context, datasetID, page}func setupFileCommitTest(userID string) (*gin.Engine, *mockFileCommitSvc) {+ r, mock, _ := setupFileCommitTestWithHandler(userID)+ return r, mock+}++func setupFileCommitTestWithHandler(userID string) (*gin.Engine, *mockFileCommitSvc, *FileCommitHandler) {mock := &mockFileCommitSvc{}- h := &FileCommitHandler{commitService: mock}+ h := NewFileCommitHandler(mock)++ db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{TranslateError: true})+ if err != nil {+ panic(err)+ }+ if err := db.AutoMigrate(&entity.File{}); err != nil {+ panic(err)+ }+ dao.DB = db+ // Seed the workspace folder and file the tests reference, owned by the+ // calling user so the tenant authorization passes for them and fails for+ // anyone else.+ db.Create(&entity.File{ID: "folder-1", TenantID: userID, Name: "workspace", Type: "folder"})+ db.Create(&entity.File{ID: "f1", TenantID: userID, Name: "test.txt", Type: "file"})+gin.SetMode(gin.TestMode)+ r := fileCommitRouter(h, userID)+ return r, mock, h+}++func fileCommitRouter(h *FileCommitHandler, userID string) *gin.Engine {r := gin.New()r.Use(func(c *gin.Context) {c.Set("user", &entity.User{ID: userID})@@ -153,7 +181,7 @@ func setupFileCommitTest(userID string) (*gin.Engine, *mockFileCommitSvc) {r.GET("/api/v1/folders/:folder_id/commits/:commit_id/tree", h.GetCommitTree)r.GET("/api/v1/folders/:folder_id/commits/:commit_id/files/:file_id/content", h.GetCommitFileContent)r.GET("/api/v1/files/:id/versions", h.GetFileVersionHistory)- return r, mock+ return r}func setupFileCommitTestNoAuth() *gin.Engine {@@ -420,3 +448,81 @@ func TestFileCommit_GetFileVersionHistory_Success(t *testing.T) {t.Errorf("expected success, got code %v", resp["code"])}}++// ── Tenant authorization tests ───────────────────────────────────────────++func decodeCode(t *testing.T, w *httptest.ResponseRecorder) float64 {+ t.Helper()+ var resp map[string]interface{}+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {+ t.Fatalf("failed to parse response: %v", err)+ }+ code, _ := resp["code"].(float64)+ return code+}++func TestFileCommit_ForeignFolderRejected(t *testing.T) {+ // folder-1 is seeded for user-1; user-2 must not read or write it.+ _, _, h := setupFileCommitTestWithHandler("user-1")+ foreign := fileCommitRouter(h, "user-2")++ endpoints := []struct {+ method string+ path string+ body string+ }{+ {"GET", "/api/v1/folders/folder-1/commits", ""},+ {"GET", "/api/v1/folders/folder-1/changes", ""},+ {"GET", "/api/v1/folders/folder-1/commits/commit-1", ""},+ {"GET", "/api/v1/folders/folder-1/commits/commit-1/files", ""},+ {"GET", "/api/v1/folders/folder-1/commits/commit-1/tree", ""},+ {"GET", "/api/v1/folders/folder-1/commits/commit-1/files/f1/content", ""},+ {"GET", "/api/v1/folders/folder-1/commits/diff?from=c1&to=c2", ""},+ {"POST", "/api/v1/folders/folder-1/commits", `{"message": "x", "files": [{"file_id": "f1", "file_name": "t", "operation": "add", "content": "c"}]}`},+ {"GET", "/api/v1/files/f1/versions", ""},+ }++ for _, e := range endpoints {+ w := httptest.NewRecorder()+ var req *http.Request+ if e.body != "" {+ req, _ = http.NewRequest(e.method, e.path, strings.NewReader(e.body))+ req.Header.Set("Content-Type", "application/json")+ } else {+ req, _ = http.NewRequest(e.method, e.path, nil)+ }+ foreign.ServeHTTP(w, req)+ if code := decodeCode(t, w); code != float64(common.CodeNotFound) {+ t.Errorf("%s %s: expected code %d for foreign user, got %v: %s", e.method, e.path, common.CodeNotFound, code, w.Body.String())+ }+ }+}++func TestFileCommit_MissingFolderRejected(t *testing.T) {+ r, _ := setupFileCommitTest("user-1")++ w := httptest.NewRecorder()+ req, _ := http.NewRequest("GET", "/api/v1/folders/no-such-folder/commits", nil)+ r.ServeHTTP(w, req)+ if code := decodeCode(t, w); code != float64(common.CodeNotFound) {+ t.Errorf("expected code %d for unknown folder, got %v", common.CodeNotFound, code)+ }+}++func TestFileCommit_OwnFolderStillWorks(t *testing.T) {+ r, _ := setupFileCommitTest("user-1")++ w := httptest.NewRecorder()+ req, _ := http.NewRequest("GET", "/api/v1/folders/folder-1/commits", nil)+ r.ServeHTTP(w, req)+ if code := decodeCode(t, w); code != float64(common.CodeSuccess) {+ t.Errorf("expected code %d for owner, got %v: %s", common.CodeSuccess, code, w.Body.String())+ }++ w = httptest.NewRecorder()+ req, _ = http.NewRequest("GET", "/api/v1/files/f1/versions", nil)+ r.ServeHTTP(w, req)+ if code := decodeCode(t, w); code != float64(common.CodeSuccess) {+ t.Errorf("expected code %d for owner version history, got %v: %s", common.CodeSuccess, code, w.Body.String())+ }+}diff --git a/test/testcases/restful_api/test_file_commit_routes_unit.py b/test/testcases/restful_api/test_file_commit_routes_unit.pyindex 9573929eb1..d3b9f21dce 100644--- a/test/testcases/restful_api/test_file_commit_routes_unit.py+++ b/test/testcases/restful_api/test_file_commit_routes_unit.py@@ -334,6 +334,18 @@ def _load_module(monkeypatch):services_pkg.__path__ = [str(repo_root / "api" / "db" / "services")]monkeypatch.setitem(sys.modules, "api.db.services", services_pkg)+ # Stub: api.common.check_team_permission (same-tenant check against the+ # stubbed services; the team-KB path needs FileService.get_kb_id_by_file_id,+ # which the harness does not model)+ ctp_mod = ModuleType("api.common.check_team_permission")++ def check_file_team_permission(file, other):+ tenant_id = file["tenant_id"] if isinstance(file, dict) else getattr(file, "tenant_id", None)+ return tenant_id == other++ ctp_mod.check_file_team_permission = check_file_team_permission+ monkeypatch.setitem(sys.modules, "api.common.check_team_permission", ctp_mod)+# Pre-stub service modules that file_commit_api.py imports.# Each stub prevents the real .py file from loading (and cascading deps).file_svc_mod = ModuleType("api.db.services.file_service")@@ -351,10 +363,15 @@ def _load_module(monkeypatch):class _StubKnowledgebaseService:@staticmethoddef get_by_id(dataset_id):- if dataset_id == "ds-1":+ if dataset_id in ("ds-1", "ds-other"):return True, SimpleNamespace(name="test-ds", tenant_id="t1")return False, None+ @staticmethod+ def accessible(kb_id, user_id):+ # ds-other exists but belongs to another tenant+ return kb_id == "ds-1"+kb_svc_mod.KnowledgebaseService = _StubKnowledgebaseServicemonkeypatch.setitem(sys.modules, "api.db.services.knowledgebase_service", kb_svc_mod)@@ -404,6 +421,10 @@ def set_tenant_info():def reset_db():"""Clear all rows before each test to prevent order-dependent failures."""_clear_db()+ # Workspace folders used across the tests, owned by the logged-in user.+ # The commit routes resolve these through FileService and check ownership.+ for folder_id in ("root-folder", "ws-folder", "folder-a", "folder-b"):+ FileTestModel.create(id=folder_id, parent_id="root", tenant_id="test-user", created_by="test-user", name=folder_id, type="folder")# ── Tests ─────────────────────────────────────────────────────────────────@@ -715,7 +736,7 @@ def test_get_commit_file_content(monkeypatch):@pytest.mark.p2def test_get_file_version_history(monkeypatch):module = _load_module(monkeypatch)- FileTestModel.create(id="f1", parent_id="root-folder", tenant_id="t1", created_by="test-user", name="a.txt", type="txt")+ FileTestModel.create(id="f1", parent_id="root-folder", tenant_id="test-user", created_by="test-user", name="a.txt", type="txt")# Two commits modifying f1_setup_request(@@ -783,3 +804,53 @@ def test_get_commit_wrong_folder_returns_not_found(monkeypatch):res = _run(module.get_commit("folder-b", commit_id))assert res["code"] == 102assert "not found in workspace" in res["message"].lower()+++# ── Authorization regression tests ────────────────────────────────────────+# The commit routes must not serve another tenant's folders or datasets.+++def test_list_commits_denies_cross_tenant_folder(monkeypatch):+ module = _load_module(monkeypatch)+ FileCommitTestModel.create(id="c-x", folder_id="other-folder", parent_id=None, message="secret work", author_id="other-user", file_count=1)++ module.request.args = {"page": "1", "page_size": "10"}+ try:+ res = _run(module.list_commits("other-folder"))+ assert res["code"] != 0, f"cross-tenant folder listing must be denied, got {res}"+ assert res["data"]["total"] == 0+ except ValueError:+ pass # unresolved = denied+++def test_get_commit_file_content_denies_cross_tenant_folder(monkeypatch):+ module = _load_module(monkeypatch)+ FileCommitTestModel.create(id="c-x", folder_id="other-folder", parent_id=None, message="secret", author_id="other-user", file_count=1)+ FileCommitItemTestModel.create(id="i-x", commit_id="c-x", file_id="f-x", operation="add", new_hash="h1")++ try:+ res = _run(module.get_commit_file_content("other-folder", "c-x", "f-x"))+ assert res["code"] != 0, f"cross-tenant commit content must be denied, got {res}"+ except ValueError:+ pass+++def test_version_history_denies_cross_tenant_file(monkeypatch):+ module = _load_module(monkeypatch)+ FileTestModel.create(id="f-other", parent_id="other-folder", tenant_id="other-tenant", created_by="other-user", name="secret.txt", type="txt")+ FileCommitTestModel.create(id="c-x", folder_id="other-folder", parent_id=None, message="m", author_id="other-user", file_count=1)+ FileCommitItemTestModel.create(id="i-x", commit_id="c-x", file_id="f-other", operation="add", new_hash="h1")++ res = _run(module.get_file_version_history("f-other"))+ assert res["code"] != 0, f"cross-tenant version history must be denied, got {res}"+++def test_dataset_resolver_denies_inaccessible_dataset(monkeypatch):+ module = _load_module(monkeypatch)+ assert module._resolve_folder_id("datasets", "ds-1") == "ds-1"+ # ds-other exists but belongs to another tenant+ assert module._resolve_folder_id("datasets", "ds-other") is None