mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-09-08 10:14:35 +08:00
Fix IDOR: Add permission checks to file ancestry endpoints (#14725)
Close #14292 ## Issue File ancestry endpoints return folder metadata without validating tenant permissions, allowing any authenticated user to query arbitrary `file_id` values across tenant boundaries. ## Affected Endpoints - `GET /v1/file/parent_folder?file_id={file_id}` - `GET /v1/file/all_parent_folder?file_id={file_id}` - `GET /api/v1/files/{id}/ancestors` ## Root Cause These endpoints **skip the permission check** that other file operations (Delete, Download, Move) perform. ## Expected Permission Check All file operations should follow this 3-step validation: - Check file.tenant_id - Check if user_id belongs to this tenant (via user_tenant join table) - Check KB permission type (team permission) **Code reference:** This is implemented in `checkFileTeamPermission()` and used by Delete/Download/Move, but **missing** from GetParentFolder/GetAllParentFolders. ## Reproduction ```bash # User B (tenant: BBB) accessing User A's file (tenant: AAA) curl -H "Authorization: Bearer USER_B_TOKEN" \ "http://localhost:9384/v1/file/parent_folder?file_id=AAA_FILE_123" # Result: Returns User A's folder metadata ❌ # Expected: "No authorization." ✅ Fix Pass userID from handler to service and call checkFileTeamPermission() — same as Download/Delete/Move handlers. --------- Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -213,13 +213,19 @@ func (s *FileService) fileInfoToResponse(info *FileInfo) map[string]interface{}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetParentFolder gets parent folder of a file
|
||||
func (s *FileService) GetParentFolder(fileID string) (map[string]interface{}, error) {
|
||||
// Check if file exists
|
||||
if _, err := s.fileDAO.GetByID(fileID); err != nil {
|
||||
// GetParentFolder gets parent folder of a file with permission check
|
||||
func (s *FileService) GetParentFolder(userID, fileID string) (map[string]interface{}, error) {
|
||||
// Get file
|
||||
file, err := s.fileDAO.GetByID(fileID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Permission check
|
||||
if !s.checkFileTeamPermission(file, userID) {
|
||||
return nil, fmt.Errorf("No authorization.")
|
||||
}
|
||||
|
||||
// Get parent folder
|
||||
parentFolder, err := s.fileDAO.GetParentFolder(fileID)
|
||||
if err != nil {
|
||||
@@ -229,13 +235,19 @@ func (s *FileService) GetParentFolder(fileID string) (map[string]interface{}, er
|
||||
return s.toFileResponse(parentFolder), nil
|
||||
}
|
||||
|
||||
// GetAllParentFolders gets all parent folders in path
|
||||
func (s *FileService) GetAllParentFolders(fileID string) ([]map[string]interface{}, error) {
|
||||
// Check if file exists
|
||||
if _, err := s.fileDAO.GetByID(fileID); err != nil {
|
||||
// GetAllParentFolders gets all parent folders in path with permission check
|
||||
func (s *FileService) GetAllParentFolders(userID, fileID string) ([]map[string]interface{}, error) {
|
||||
// Get file
|
||||
file, err := s.fileDAO.GetByID(fileID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Permission check
|
||||
if !s.checkFileTeamPermission(file, userID) {
|
||||
return nil, fmt.Errorf("No authorization.")
|
||||
}
|
||||
|
||||
// Get all parent folders
|
||||
parentFolders, err := s.fileDAO.GetAllParentFolders(fileID)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user