Skip to content

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

fix(file): scope commit changes to the target folder tree (#21795)

CCLUCKY33806977d1e5
diff --git a/internal/handler/file_commit.go b/internal/handler/file_commit.go
index 21a4e55da1..bb3da38e8e 100644
--- a/internal/handler/file_commit.go
+++ b/internal/handler/file_commit.go
@@ -201,6 +201,11 @@ func (h *FileCommitHandler) CreateCommit(c *gin.Context) {
common.RespondError(c, common.CodeBadRequest, err.Error())
return
}
+ // A file_id outside the folder tree is caller input, not a server
+ // fault: answer it like the other file writes instead of a 500.
+ if respondPermissionErrorIf(c, err, true) {
+ return
+ }
jsonInternalError(c, err)
return
}
diff --git a/internal/handler/file_commit_test.go b/internal/handler/file_commit_test.go
index f377a3ab95..ae6e3033ba 100644
--- a/internal/handler/file_commit_test.go
+++ b/internal/handler/file_commit_test.go
@@ -303,6 +303,32 @@ func TestFileCommit_CreateCommit_DeleteLiveFileIsBadRequest(t *testing.T) {
}
}
+func TestFileCommit_CreateCommit_RejectsFileOutsideFolder(t *testing.T) {
+ r, mock := setupFileCommitTest("user-1")
+ mock.createCommitFn = func(ctx context.Context, folderID, authorID, message string, changes []entity.FileChange) (*entity.FileCommit, error) {
+ return nil, fmt.Errorf("%w: file f2 is outside folder %s", permission.ErrPermissionDenied, folderID)
+ }
+
+ body := `{"message": "attempt", "files": [{"file_id": "f2", "file_name": "other.txt", "operation": "modify", "content": "x"}]}`
+ w := httptest.NewRecorder()
+ req, _ := http.NewRequest("POST", "/api/v1/folders/folder-1/commits", strings.NewReader(body))
+ req.Header.Set("Content-Type", "application/json")
+ r.ServeHTTP(w, req)
+
+ if w.Code != http.StatusOK {
+ t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
+ }
+ var resp map[string]interface{}
+ if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
+ t.Fatal(err)
+ }
+ // Hidden resource: a file the caller cannot commit is reported exactly like
+ // one that does not exist, not as a server error.
+ if code, _ := resp["code"].(float64); code != float64(common.CodeNotFound) {
+ t.Errorf("expected not found, got code %v", code)
+ }
+}
+
func TestFileCommit_CreateCommit_NoAuth(t *testing.T) {
r := setupFileCommitTestNoAuth()
body := `{"message": "test", "files": [{"file_id": "f1", "file_name": "t.txt", "operation": "add"}]}`
diff --git a/internal/service/file/file_commit.go b/internal/service/file/file_commit.go
index dc5b725f4e..03d5e1fe53 100644
--- a/internal/service/file/file_commit.go
+++ b/internal/service/file/file_commit.go
@@ -28,6 +28,7 @@ import (
"ragflow/internal/engine"
enginetypes "ragflow/internal/engine/types"
"ragflow/internal/entity"
+ "ragflow/internal/permission"
"ragflow/internal/storage"
"sort"
"strings"
@@ -37,6 +38,7 @@ import (
"go.uber.org/zap"
"gorm.io/gorm"
+ "gorm.io/gorm/clause"
)
// FileCommitService file commit service
@@ -57,6 +59,11 @@ func NewFileCommitService() *FileCommitService {
}
}
+// maxCommitFolderDepth bounds the ancestor walk that places a commit's file IDs
+// inside the commit folder. Folder chains are short; the bound only stops a
+// corrupted parent_id cycle.
+const maxCommitFolderDepth = 64
+
// ErrDeleteLiveFile rejects a delete change for a file that still exists in the
// commit folder. Recording the tombstone would hide the live file from
// GetUncommittedChanges, which skips every committed tombstone and marks the
@@ -122,6 +129,13 @@ func (s *FileCommitService) CreateCommit(ctx context.Context, folderID, authorID
// All DB operations run inside a single transaction.
var treeStr string
if err := dao.DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
+ // Authorize the named files against the rows this transaction is about
+ // to write, holding them so a concurrent move cannot relocate a file
+ // between the ancestry check and the update below.
+ if err := s.authorizeCommitFiles(ctx, tx, folderID, changes, treeState); err != nil {
+ return err
+ }
+
// Save commit
if err := tx.Create(commit).Error; err != nil {
return fmt.Errorf("failed to create commit: %w", err)
@@ -282,6 +296,165 @@ func (s *FileCommitService) CreateCommit(ctx context.Context, folderID, authorID
return commit, nil
}
+// authorizeCommitFiles rejects commit changes whose file IDs fall outside the
+// commit folder. The route only proves the caller may use folderID, while every
+// files[].file_id is independent client input and the writes below address file
+// rows by ID alone, so an unchecked ID retargets another workspace or tenant
+// (IDOR, CWE-639). A change may record the deletion of a file that was already
+// removed or moved away, so such a delete-only ID is validated against the
+// previous tree state instead of the file table: every ID that ever entered the
+// folder history has a tree entry, and one that does not would mint a tombstone
+// for an arbitrary ID that the global per-file version history then exposes.
+//
+// It runs inside the commit transaction and locks the rows it reads: a
+// concurrent move would otherwise be able to relocate a file (or one of its
+// ancestors) after the parent chain was checked and before the write.
+func (s *FileCommitService) authorizeCommitFiles(ctx context.Context, tx *gorm.DB, folderID string, changes []entity.FileChange, treeState map[string]interface{}) error {
+ var folder entity.File
+ if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("id = ?", folderID).First(&folder).Error; err != nil {
+ return fmt.Errorf("resolve commit folder %s: %w", folderID, err)
+ }
+ if folder.Type != FileTypeFolder {
+ return fmt.Errorf("%w: %s is not a folder", permission.ErrResourceNotFound, folderID)
+ }
+
+ operations := make(map[string][]string, len(changes))
+ ids := make([]string, 0, len(changes))
+ for _, change := range changes {
+ if change.FileID == "" {
+ return fmt.Errorf("%w: file_id is required", permission.ErrResourceNotFound)
+ }
+ if _, seen := operations[change.FileID]; !seen {
+ ids = append(ids, change.FileID)
+ }
+ operations[change.FileID] = append(operations[change.FileID], change.Operation)
+ }
+
+ files, err := lockedCommitFiles(tx, ids)
+ if err != nil {
+ return fmt.Errorf("resolve commit files: %w", err)
+ }
+ existing := make(map[string]*entity.File, len(files))
+ for _, file := range files {
+ existing[file.ID] = file
+ }
+ scoped := make(map[string]*entity.File, len(existing))
+ for _, id := range ids {
+ hasNonDelete := false
+ for _, operation := range operations[id] {
+ if operation != "delete" {
+ hasNonDelete = true
+ break
+ }
+ }
+ if hasNonDelete {
+ file, ok := existing[id]
+ if !ok {
+ return fmt.Errorf("%w: file %s not found in folder %s", permission.ErrPermissionDenied, id, folderID)
+ }
+ scoped[id] = file
+ } else {
+ // A delete-only change records a tombstone in tree_state and never
+ // writes the file row, so the ID must be one this folder actually
+ // committed before (a file since removed or moved away). An ID
+ // absent from the previous tree never belonged to this history,
+ // whether it still has a live row elsewhere or not.
+ if _, ok := treeState[id]; !ok {
+ return fmt.Errorf("%w: file %s not found in folder %s", permission.ErrPermissionDenied, id, folderID)
+ }
+ }
+ }
+ return ensureFilesInFolder(tx, folderID, scoped)
+}
+
+// lockedCommitFiles loads file rows for the commit transaction with write locks
+// so their parent_id cannot change underneath the ancestry check.
+func lockedCommitFiles(tx *gorm.DB, ids []string) ([]*entity.File, error) {
+ if len(ids) == 0 {
+ return nil, nil
+ }
+ var files []*entity.File
+ err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("id IN ?", ids).Find(&files).Error
+ return files, err
+}
+
+// ensureFilesInFolder rejects files whose parent chain does not reach folderID.
+// Ancestors are resolved one folder level at a time, so a commit verifies all of
+// its files with one query per tree depth instead of one query per file.
+func ensureFilesInFolder(tx *gorm.DB, folderID string, files map[string]*entity.File) error {
+ if len(files) == 0 {
+ return nil
+ }
+ parentOf := make(map[string]string, len(files))
+ frontier := make([]string, 0, len(files))
+ for id, file := range files {
+ if id == folderID {
+ continue
+ }
+ parentOf[id] = file.ParentID
+ frontier = append(frontier, file.ParentID)
+ }
+
+ for depth := 0; depth < maxCommitFolderDepth && len(frontier) > 0; depth++ {
+ load := make([]string, 0, len(frontier))
+ queued := make(map[string]struct{}, len(frontier))
+ for _, id := range frontier {
+ if id == "" || id == folderID {
+ continue
+ }
+ if _, known := parentOf[id]; known {
+ continue
+ }
+ if _, dup := queued[id]; dup {
+ continue
+ }
+ queued[id] = struct{}{}
+ load = append(load, id)
+ }
+ if len(load) == 0 {
+ break
+ }
+ ancestors, err := lockedCommitFiles(tx, load)
+ if err != nil {
+ return fmt.Errorf("resolve commit file ancestors: %w", err)
+ }
+ for _, ancestor := range ancestors {
+ parentOf[ancestor.ID] = ancestor.ParentID
+ }
+ frontier = frontier[:0]
+ for _, id := range load {
+ parent, ok := parentOf[id]
+ if !ok {
+ // A missing ancestor terminates the chain for its descendants.
+ parentOf[id] = ""
+ }
+ frontier = append(frontier, parent)
+ }
+ }
+
+ for id := range files {
+ if !commitFileInFolder(id, folderID, parentOf) {
+ return fmt.Errorf("%w: file %s is outside folder %s", permission.ErrPermissionDenied, id, folderID)
+ }
+ }
+ return nil
+}
+
+func commitFileInFolder(fileID, folderID string, parentOf map[string]string) bool {
+ visited := make(map[string]struct{})
+ for current := fileID; current != ""; {
+ if current == folderID {
+ return true
+ }
+ if _, cycle := visited[current]; cycle {
+ return false
+ }
+ visited[current] = struct{}{}
+ current = parentOf[current]
+ }
+ return false
+}
+
// PageEditCommitInput carries the data needed to record a single wiki/skill
// page edit as an audit commit.
type PageEditCommitInput struct {
diff --git a/internal/service/file/file_commit_authorization_test.go b/internal/service/file/file_commit_authorization_test.go
new file mode 100644
index 0000000000..cd63af6320
--- /dev/null
+++ b/internal/service/file/file_commit_authorization_test.go
@@ -0,0 +1,184 @@
+package file
+
+import (
+ "context"
+ "testing"
+
+ "ragflow/internal/entity"
+
+ "gorm.io/gorm"
+)
+
+// newCommitAuthTestDB reuses the page-commit harness and adds the file table.
+func newCommitAuthTestDB(t *testing.T) *gorm.DB {
+ t.Helper()
+ db := newPageCommitTestDB(t)
+ if err := db.AutoMigrate(&entity.File{}); err != nil {
+ t.Fatalf("auto migrate file: %v", err)
+ }
+ return db
+}
+
+type commitAuthFixture struct {
+ db *gorm.DB
+ fileSub *entity.File
+}
+
+func newCommitAuthFixture(t *testing.T) *commitAuthFixture {
+ t.Helper()
+ f := &commitAuthFixture{db: newCommitAuthTestDB(t)}
+ mk := func(id, parentID, tenantID, name, fileType, location string, size int64) *entity.File {
+ row := &entity.File{
+ ID: id,
+ ParentID: parentID,
+ TenantID: tenantID,
+ CreatedBy: tenantID,
+ Name: name,
+ Type: fileType,
+ Size: size,
+ }
+ if location != "" {
+ row.Location = &location
+ }
+ if err := f.db.Create(row).Error; err != nil {
+ t.Fatalf("create %s: %v", id, err)
+ }
+ return row
+ }
+ // tenant-a owns two sibling workspaces; tenant-b owns a third one.
+ mk("folder-a", "folder-a", "tenant-a", "workspace-a", "folder", "", 0)
+ mk("file-a", "folder-a", "tenant-a", "a.txt", "txt", "live/a.txt", 3)
+ mk("sub-a", "folder-a", "tenant-a", "sub", "folder", "", 0)
+ f.fileSub = mk("file-sub", "sub-a", "tenant-a", "nested.txt", "txt", "live/nested.txt", 3)
+ mk("folder-b", "folder-b", "tenant-a", "workspace-b", "folder", "", 0)
+ mk("file-b", "folder-b", "tenant-a", "b.txt", "txt", "live/b.txt", 3)
+ mk("folder-c", "folder-c", "tenant-b", "workspace-c", "folder", "", 0)
+ mk("file-c", "folder-c", "tenant-b", "c.txt", "txt", "live/c.txt", 3)
+ return f
+}
+
+func (f *commitAuthFixture) assertUntouched(t *testing.T, id, wantName, wantLocation string) {
+ t.Helper()
+ var row entity.File
+ if err := f.db.Where("id = ?", id).First(&row).Error; err != nil {
+ t.Fatalf("reload %s: %v", id, err)
+ }
+ if row.Name != wantName {
+ t.Fatalf("file %s name = %q, want %q", id, row.Name, wantName)
+ }
+ if row.Location == nil || *row.Location != wantLocation {
+ t.Fatalf("file %s location = %v, want %q", id, row.Location, wantLocation)
+ }
+ if row.Size != 3 {
+ t.Fatalf("file %s size = %d, want 3", id, row.Size)
+ }
+ var commits int64
+ if err := f.db.Model(&entity.FileCommit{}).Count(&commits).Error; err != nil {
+ t.Fatalf("count commits: %v", err)
+ }
+ if commits != 0 {
+ t.Fatalf("rejected commit was persisted: %d rows", commits)
+ }
+}
+
+// A commit only proves the caller may use the target folder; every
+// files[].file_id is independent input, so the service must reject IDs that
+// live outside that folder tree instead of writing those rows (IDOR/CWE-639).
+func TestCreateCommitRejectsFilesOutsideFolder(t *testing.T) {
+ f := newCommitAuthFixture(t)
+ svc := NewFileCommitService()
+ ctx := context.Background()
+
+ cases := []struct {
+ name string
+ change entity.FileChange
+ victim string
+ wantName string
+ wantLocation string
+ }{
+ {
+ name: "modify file of another tenant",
+ change: entity.FileChange{FileID: "file-c", Operation: "modify", FileName: "c.txt", Content: "pwned"},
+ victim: "file-c",
+ wantName: "c.txt",
+ wantLocation: "live/c.txt",
+ },
+ {
+ name: "modify file of a sibling workspace",
+ change: entity.FileChange{FileID: "file-b", Operation: "modify", FileName: "b.txt", Content: "pwned"},
+ victim: "file-b",
+ wantName: "b.txt",
+ wantLocation: "live/b.txt",
+ },
+ {
+ name: "rename file of a sibling workspace",
+ change: entity.FileChange{FileID: "file-b", Operation: "rename", OldName: "b.txt", NewName: "pwned.txt"},
+ victim: "file-b",
+ wantName: "b.txt",
+ wantLocation: "live/b.txt",
+ },
+ {
+ name: "add an unknown file id",
+ change: entity.FileChange{FileID: "no-such-file", Operation: "add", FileName: "ghost.txt", Content: "pwned"},
+ victim: "file-a",
+ wantName: "a.txt",
+ wantLocation: "live/a.txt",
+ },
+ }
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ if _, err := svc.CreateCommit(ctx, "folder-a", "user-a", "attempt", []entity.FileChange{tc.change}); err == nil {
+ t.Fatalf("expected CreateCommit to reject %s", tc.change.FileID)
+ }
+ f.assertUntouched(t, tc.victim, tc.wantName, tc.wantLocation)
+ })
+ }
+}
+
+func TestCreateCommitAcceptsFilesInsideFolderTree(t *testing.T) {
+ f := newCommitAuthFixture(t)
+ svc := NewFileCommitService()
+ ctx := context.Background()
+
+ commit, err := svc.CreateCommit(ctx, "folder-a", "user-a", "edit", []entity.FileChange{
+ {FileID: "file-a", Operation: "modify", FileName: "a.txt", Content: "hello"},
+ {FileID: "file-sub", Operation: "modify", FileName: "nested.txt", Content: "nested hello"},
+ })
+ if err != nil {
+ t.Fatalf("CreateCommit: %v", err)
+ }
+ if commit.FileCount != 2 {
+ t.Fatalf("expected 2 changed files, got %d", commit.FileCount)
+ }
+ for id, content := range map[string]string{"file-a": "hello", "file-sub": "nested hello"} {
+ var row entity.File
+ if err := f.db.Where("id = ?", id).First(&row).Error; err != nil {
+ t.Fatalf("reload %s: %v", id, err)
+ }
+ if row.Size != int64(len(content)) {
+ t.Fatalf("file %s size = %d, want %d", id, row.Size, len(content))
+ }
+ if row.Location == nil || *row.Location == "" {
+ t.Fatalf("file %s location was not updated", id)
+ }
+ }
+}
+
+func TestCreateCommitRejectsMissingFolder(t *testing.T) {
+ f := newCommitAuthFixture(t)
+ svc := NewFileCommitService()
+
+ if _, err := svc.CreateCommit(context.Background(), "no-such-folder", "user-a", "edit", []entity.FileChange{
+ {FileID: "file-a", Operation: "modify", FileName: "a.txt", Content: "hello"},
+ }); err == nil {
+ t.Fatal("expected CreateCommit to reject an unknown folder")
+ }
+ var commits int64
+ if err := f.db.Model(&entity.FileCommit{}).Count(&commits).Error; err != nil {
+ t.Fatalf("count commits: %v", err)
+ }
+ if commits != 0 {
+ t.Fatalf("commit was persisted for an unknown folder: %d rows", commits)
+ }
+}
diff --git a/internal/service/file/file_commit_delete_test.go b/internal/service/file/file_commit_delete_test.go
index 436d9ca3a7..cc18c9a156 100644
--- a/internal/service/file/file_commit_delete_test.go
+++ b/internal/service/file/file_commit_delete_test.go
@@ -7,6 +7,7 @@ import (
"ragflow/internal/dao"
"ragflow/internal/entity"
+ "ragflow/internal/permission"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
@@ -40,6 +41,7 @@ func TestCreateCommit_DeleteRecordsTombstoneWhenLiveFileIsGone(t *testing.T) {
if err := db.Create(parent).Error; err != nil {
t.Fatalf("seed parent commit: %v", err)
}
+ seedFolder(t, db, "folder-1", "")
commit, err := NewFileCommitService().CreateCommit(t.Context(), "folder-1", "u1", "remove a.txt", []entity.FileChange{
{FileID: "f1", FileName: "a.txt", Operation: "delete"},
@@ -105,6 +107,7 @@ func TestCreateCommit_RejectsDeleteOfLiveFile(t *testing.T) {
if err := db.Create(parent).Error; err != nil {
t.Fatalf("seed parent commit: %v", err)
}
+ seedFolder(t, db, "folder-1", "")
seedLiveFile(t, db, "f2", "folder-1")
seedFolder(t, db, "sub-1", "folder-1")
seedLiveFile(t, db, "f4", "sub-1")
@@ -136,6 +139,7 @@ func TestCreateCommit_AllowsDeleteOfFileOutsideFolder(t *testing.T) {
if err := db.Create(parent).Error; err != nil {
t.Fatalf("seed parent commit: %v", err)
}
+ seedFolder(t, db, "folder-1", "")
seedLiveFile(t, db, "f3", "other-folder")
if _, err := NewFileCommitService().CreateCommit(t.Context(), "folder-1", "u1", "remove f3", []entity.FileChange{
@@ -144,3 +148,59 @@ func TestCreateCommit_AllowsDeleteOfFileOutsideFolder(t *testing.T) {
t.Fatalf("delete of a file outside the folder: %v", err)
}
}
+
+// A delete-only change mints a tombstone without touching the file table, so
+// an ID that never entered this folder's history must be rejected: the global
+// per-file version history would otherwise expose a fabricated tombstone.
+func TestCreateCommit_RejectsDeleteOfUnknownFile(t *testing.T) {
+ db := newCommitDeleteTestDB(t)
+ tree := `{"f1":{"hash":"h1","location":".objects/h1","name":"a.txt","size":3,"status":"1","parent_id":"folder-1"}}`
+ parent := &entity.FileCommit{ID: "c1", FolderID: "folder-1", Message: "init", AuthorID: "u1", TreeState: &tree}
+ if err := db.Create(parent).Error; err != nil {
+ t.Fatalf("seed parent commit: %v", err)
+ }
+ seedFolder(t, db, "folder-1", "")
+
+ if _, err := NewFileCommitService().CreateCommit(t.Context(), "folder-1", "u1", "remove ghost", []entity.FileChange{
+ {FileID: "no-such-file", FileName: "ghost.txt", Operation: "delete"},
+ }); !errors.Is(err, permission.ErrPermissionDenied) {
+ t.Fatalf("delete of unknown file: err = %v, want permission.ErrPermissionDenied", err)
+ }
+
+ var commits int64
+ if err := db.Model(&entity.FileCommitItem{}).Where("file_id = ?", "no-such-file").Count(&commits).Error; err != nil {
+ t.Fatalf("count commit items: %v", err)
+ }
+ if commits != 0 {
+ t.Fatalf("expected no tombstone for the unknown file, found %d commit items", commits)
+ }
+}
+
+// An existing file outside the target folder that never entered this folder's
+// history (absent from treeState) must be rejected on delete, even though its
+// live row exists in the file table.
+func TestCreateCommit_RejectsDeleteOfOutOfFolderFileAbsentFromTree(t *testing.T) {
+ db := newCommitDeleteTestDB(t)
+ tree := `{"f1":{"hash":"h1","location":".objects/h1","name":"a.txt","size":3,"status":"1","parent_id":"folder-1"}}`
+ parent := &entity.FileCommit{ID: "c1", FolderID: "folder-1", Message: "init", AuthorID: "u1", TreeState: &tree}
+ if err := db.Create(parent).Error; err != nil {
+ t.Fatalf("seed parent commit: %v", err)
+ }
+ seedFolder(t, db, "folder-1", "")
+ seedFolder(t, db, "other-folder", "")
+ seedLiveFile(t, db, "alien-file", "other-folder")
+
+ if _, err := NewFileCommitService().CreateCommit(t.Context(), "folder-1", "u1", "delete alien", []entity.FileChange{
+ {FileID: "alien-file", FileName: "alien.txt", Operation: "delete"},
+ }); !errors.Is(err, permission.ErrPermissionDenied) {
+ t.Fatalf("delete of out-of-folder file absent from tree: err = %v, want permission.ErrPermissionDenied", err)
+ }
+
+ var commits int64
+ if err := db.Model(&entity.FileCommitItem{}).Where("file_id = ?", "alien-file").Count(&commits).Error; err != nil {
+ t.Fatalf("count commit items: %v", err)
+ }
+ if commits != 0 {
+ t.Fatalf("expected no tombstone for alien file, found %d commit items", commits)
+ }
+}