From 9195d7aeb0a64e5710c9435bbedac84054940d04 Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Wed, 31 Dec 2025 14:39:33 +0100 Subject: [PATCH] fix: prevent delete_chat_by_id crash when chat is None (#20270) Add null check after Chats.get_chat_by_id() in both admin and user code paths. When chat doesn't exist, now raises HTTP 404 instead of crashing with AttributeError when accessing chat.meta. --- backend/open_webui/routers/chats.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/backend/open_webui/routers/chats.py b/backend/open_webui/routers/chats.py index ab558c5c8..7f0172d0e 100644 --- a/backend/open_webui/routers/chats.py +++ b/backend/open_webui/routers/chats.py @@ -1028,6 +1028,11 @@ async def delete_chat_by_id( ): if user.role == "admin": chat = Chats.get_chat_by_id(id, db=db) + if not chat: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail=ERROR_MESSAGES.NOT_FOUND, + ) for tag in chat.meta.get("tags", []): if Chats.count_chats_by_tag_name_and_user_id(tag, user.id, db=db) == 1: Tags.delete_tag_by_name_and_user_id(tag, user.id, db=db) @@ -1045,6 +1050,11 @@ async def delete_chat_by_id( ) chat = Chats.get_chat_by_id(id, db=db) + if not chat: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail=ERROR_MESSAGES.NOT_FOUND, + ) for tag in chat.meta.get("tags", []): if Chats.count_chats_by_tag_name_and_user_id(tag, user.id, db=db) == 1: Tags.delete_tag_by_name_and_user_id(tag, user.id, db=db)