From 190b934ab5176f3c0ed95ac621638ee7741ee500 Mon Sep 17 00:00:00 2001 From: Jun Siang Cheah Date: Wed, 17 Apr 2024 09:33:22 +0100 Subject: [PATCH 1/2] feat: add ALLOW_ADMIN_EXPORT to disable exporting of chats and the db --- backend/apps/web/routers/chats.py | 10 +++- backend/apps/web/routers/utils.py | 6 ++- backend/config.py | 2 + backend/main.py | 2 + .../components/admin/Settings/Database.svelte | 47 ++++++++++--------- src/lib/components/chat/Settings/Chats.svelte | 2 +- 6 files changed, 44 insertions(+), 25 deletions(-) diff --git a/backend/apps/web/routers/chats.py b/backend/apps/web/routers/chats.py index 678c9aea7..07f483698 100644 --- a/backend/apps/web/routers/chats.py +++ b/backend/apps/web/routers/chats.py @@ -28,7 +28,10 @@ from apps.web.models.tags import ( from constants import ERROR_MESSAGES -from config import SRC_LOG_LEVELS +from config import ( + SRC_LOG_LEVELS, + ALLOW_ADMIN_EXPORT +) log = logging.getLogger(__name__) log.setLevel(SRC_LOG_LEVELS["MODELS"]) @@ -79,6 +82,11 @@ async def get_all_user_chats(user=Depends(get_current_user)): @router.get("/all/db", response_model=List[ChatResponse]) async def get_all_user_chats_in_db(user=Depends(get_admin_user)): + if not ALLOW_ADMIN_EXPORT: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail=ERROR_MESSAGES.ACCESS_PROHIBITED, + ) return [ ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)}) for chat in Chats.get_all_chats() diff --git a/backend/apps/web/routers/utils.py b/backend/apps/web/routers/utils.py index 0ee75cfe6..d40a56d54 100644 --- a/backend/apps/web/routers/utils.py +++ b/backend/apps/web/routers/utils.py @@ -91,7 +91,11 @@ async def download_chat_as_pdf( @router.get("/db/download") async def download_db(user=Depends(get_admin_user)): - + if not ALLOW_ADMIN_EXPORT: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail=ERROR_MESSAGES.ACCESS_PROHIBITED, + ) return FileResponse( f"{DATA_DIR}/webui.db", media_type="application/octet-stream", diff --git a/backend/config.py b/backend/config.py index fb9063eb7..c6e05811b 100644 --- a/backend/config.py +++ b/backend/config.py @@ -382,6 +382,8 @@ MODEL_FILTER_LIST = [model.strip() for model in MODEL_FILTER_LIST.split(";")] WEBHOOK_URL = os.environ.get("WEBHOOK_URL", "") +ALLOW_ADMIN_EXPORT = os.environ.get("ALLOW_ADMIN_EXPORT", "True").lower() == "true" + #################################### # WEBUI_VERSION #################################### diff --git a/backend/main.py b/backend/main.py index 579ff2ee0..0ee1ad18c 100644 --- a/backend/main.py +++ b/backend/main.py @@ -52,6 +52,7 @@ from config import ( GLOBAL_LOG_LEVEL, SRC_LOG_LEVELS, WEBHOOK_URL, + ALLOW_ADMIN_EXPORT, ) from constants import ERROR_MESSAGES @@ -207,6 +208,7 @@ async def get_app_config(): "default_models": webui_app.state.DEFAULT_MODELS, "default_prompt_suggestions": webui_app.state.DEFAULT_PROMPT_SUGGESTIONS, "trusted_header_auth": bool(webui_app.state.AUTH_TRUSTED_EMAIL_HEADER), + "allow_admin_export": ALLOW_ADMIN_EXPORT, } diff --git a/src/lib/components/admin/Settings/Database.svelte b/src/lib/components/admin/Settings/Database.svelte index 7d3a34444..483f83251 100644 --- a/src/lib/components/admin/Settings/Database.svelte +++ b/src/lib/components/admin/Settings/Database.svelte @@ -1,6 +1,7 @@