refac
This commit is contained in:
1 parent
4a1064cefd
commit
2dbf7b6764
4 files changed
+74
-19
No files matched your search
@@ -16,8 +16,6 @@ from open_webui.models.folders import (
|
||||
Folders,
|
||||
)
|
||||
from open_webui.models.chats import Chats
|
||||
from open_webui.models.files import Files
|
||||
from open_webui.models.knowledge import Knowledges
|
||||
|
||||
|
||||
from open_webui.config import UPLOAD_DIR
|
||||
@@ -32,6 +30,7 @@ from fastapi.responses import FileResponse, StreamingResponse
|
||||
|
||||
from open_webui.utils.auth import get_admin_user, get_verified_user
|
||||
from open_webui.utils.access_control import has_permission
|
||||
from open_webui.utils.access_control.files import get_accessible_folder_files
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
@@ -75,20 +74,10 @@ async def get_folders(
|
||||
if folder.parent_id and not await Folders.get_folder_by_id_and_user_id(folder.parent_id, user.id, db=db):
|
||||
folder = await Folders.update_folder_parent_id_by_id_and_user_id(folder.id, user.id, None, db=db)
|
||||
|
||||
if folder.data:
|
||||
if 'files' in folder.data:
|
||||
valid_files = []
|
||||
for file in folder.data['files']:
|
||||
if file.get('type') == 'file':
|
||||
if await Files.check_access_by_user_id(file.get('id'), user.id, 'read', db=db):
|
||||
valid_files.append(file)
|
||||
elif file.get('type') == 'collection':
|
||||
if await Knowledges.check_access_by_user_id(file.get('id'), user.id, 'read', db=db):
|
||||
valid_files.append(file)
|
||||
else:
|
||||
valid_files.append(file)
|
||||
|
||||
folder.data['files'] = valid_files
|
||||
if folder.data and 'files' in folder.data:
|
||||
accessible_files = await get_accessible_folder_files(folder.data['files'], user, db=db)
|
||||
if len(accessible_files) != len(folder.data.get('files', [])):
|
||||
folder.data['files'] = accessible_files
|
||||
await Folders.update_folder_by_id_and_user_id(
|
||||
folder.id, user.id, FolderUpdateForm(data=folder.data), db=db
|
||||
)
|
||||
@@ -173,6 +162,16 @@ async def update_folder_name_by_id(
|
||||
detail=ERROR_MESSAGES.DEFAULT('Folder already exists'),
|
||||
)
|
||||
|
||||
# Validate read access to every file/collection being attached.
|
||||
# Folder files are consumed by chat middleware as RAG context.
|
||||
if form_data.data and isinstance(form_data.data.get('files'), list):
|
||||
accessible_files = await get_accessible_folder_files(form_data.data['files'], user, db=db)
|
||||
if len(accessible_files) != len(form_data.data['files']):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
||||
)
|
||||
|
||||
try:
|
||||
folder = await Folders.update_folder_by_id_and_user_id(id, user.id, form_data, db=db)
|
||||
return folder
|
||||
|
||||
@@ -31,6 +31,7 @@ from open_webui.storage.provider import Storage
|
||||
from open_webui.constants import ERROR_MESSAGES
|
||||
from open_webui.utils.auth import get_verified_user, get_admin_user
|
||||
from open_webui.utils.access_control import has_permission, filter_allowed_access_grants
|
||||
from open_webui.utils.access_control.files import has_access_to_file
|
||||
from open_webui.models.access_grants import AccessGrants
|
||||
|
||||
|
||||
@@ -656,6 +657,14 @@ async def add_file_to_knowledge_by_id(
|
||||
detail=ERROR_MESSAGES.FILE_NOT_PROCESSED,
|
||||
)
|
||||
|
||||
# KB write-access alone is not enough — caller must also be able to read the file.
|
||||
if file.user_id != user.id and user.role != 'admin':
|
||||
if not await has_access_to_file(file.id, 'read', user, db=db):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
||||
)
|
||||
|
||||
# Add content to the vector database
|
||||
try:
|
||||
await process_file(
|
||||
@@ -1017,6 +1026,15 @@ async def add_files_to_knowledge_batch(
|
||||
detail=f'File {missing_ids[0]} not found',
|
||||
)
|
||||
|
||||
# Per-file read-access check — same gate as the single-file endpoint.
|
||||
if user.role != 'admin':
|
||||
for file in files:
|
||||
if file.user_id != user.id and not await has_access_to_file(file.id, 'read', user, db=db):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
|
||||
)
|
||||
|
||||
# Process files
|
||||
try:
|
||||
result = await process_files_batch(
|
||||
|
||||
Reference in new issue
Block a user