Skip to content

Commit 3f1c52e

Browse files
authored
fix: gate chat-file links by caller access + repair insert_chat_files db arg (open-webui#25054)
insert_chat_files() stored any caller-supplied file_id with no ownership check, so a user could attach another user's file to their own chat and then read it through the shared-chat access path in has_access_to_file(). Filter file_ids to those the caller owns, is admin for, or can read. Also repairs an UnboundLocalError introduced in 260ead6: the existing duplicate-check referenced `session` before it was assigned (db=session), so the function threw on every call and no chat_file rows were persisted.
1 parent f5f4b58 commit 3f1c52e

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

backend/open_webui/models/chats.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1624,13 +1624,34 @@ async def insert_chat_files(
16241624
return None
16251625

16261626
chat_message_file_ids = {
1627-
item.id for item in await self.get_chat_files_by_chat_id_and_message_id(chat_id, message_id, db=session)
1627+
item.id for item in await self.get_chat_files_by_chat_id_and_message_id(chat_id, message_id, db=db)
16281628
}
16291629
# Remove duplicates and existing file_ids
16301630
file_ids = list({file_id for file_id in file_ids if file_id and file_id not in chat_message_file_ids})
16311631
if not file_ids:
16321632
return None
16331633

1634+
# Only link files the caller can read; blocks forging a chat_file row to another user's file.
1635+
from open_webui.models.files import Files
1636+
from open_webui.models.users import Users
1637+
from open_webui.utils.access_control.files import has_access_to_file
1638+
1639+
user = await Users.get_user_by_id(user_id, db=db)
1640+
accessible_file_ids = []
1641+
for file_id in file_ids:
1642+
file = await Files.get_file_by_id(file_id, db=db)
1643+
if not file:
1644+
continue
1645+
if (
1646+
file.user_id == user_id
1647+
or (user and user.role == 'admin')
1648+
or (user and await has_access_to_file(file_id, 'read', user, db=db))
1649+
):
1650+
accessible_file_ids.append(file_id)
1651+
file_ids = accessible_file_ids
1652+
if not file_ids:
1653+
return None
1654+
16341655
try:
16351656
async with get_async_db_context(db) as session:
16361657
now = int(time.time())

0 commit comments

Comments
 (0)