From 956a4af3c96735e14b3c46a734d46de233e529c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B6=B5=E6=9B=A6?= Date: Tue, 10 Jun 2025 18:46:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xiaomusic/httpserver.py | 9 +++++---- xiaomusic/utils.py | 11 ++++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/xiaomusic/httpserver.py b/xiaomusic/httpserver.py index 7442717..6575d3b 100644 --- a/xiaomusic/httpserver.py +++ b/xiaomusic/httpserver.py @@ -54,6 +54,7 @@ from xiaomusic.utils import ( remove_common_prefix, remove_id3_tags, restart_xiaomusic, + safe_join_path, try_add_access_control_param, update_version, ) @@ -560,10 +561,10 @@ async def downloadplaylist(data: DownloadPlayList, Verifcation=Depends(verificat for title, download_proc_sigle in download_proc_list.items(): exit_code = await download_proc_sigle.wait() log.info(f"Download completed {title} with exit code {exit_code}") - dir_path = os.path.join(config.download_path, data.dirname) + dir_path = safe_join_path(config.download_path, data.dirname) log.debug(f"Download dir_path: {dir_path}") # 可能只是部分失败,都需要整理下载目录 - remove_common_prefix(config.download_path, dir_path) + remove_common_prefix(dir_path) chmoddir(dir_path) return {"ret": "OK"} else: @@ -574,10 +575,10 @@ async def downloadplaylist(data: DownloadPlayList, Verifcation=Depends(verificat exit_code = await download_proc.wait() log.info(f"Download completed with exit code {exit_code}") - dir_path = os.path.join(config.download_path, data.dirname) + dir_path = safe_join_path(config.download_path, data.dirname) log.debug(f"Download dir_path: {dir_path}") # 可能只是部分失败,都需要整理下载目录 - remove_common_prefix(config.download_path, dir_path) + remove_common_prefix(dir_path) chmoddir(dir_path) asyncio.create_task(check_download_proc()) diff --git a/xiaomusic/utils.py b/xiaomusic/utils.py index b771c5e..65693de 100644 --- a/xiaomusic/utils.py +++ b/xiaomusic/utils.py @@ -1107,14 +1107,19 @@ def _longest_common_prefix(file_names): return prefix -# 移除目录下文件名前缀相同的 -def remove_common_prefix(safe_root, directory): +def safe_join_path(safe_root, directory): + directory = os.path.join(safe_root, directory) # Normalize the directory path normalized_directory = os.path.normpath(directory) # Ensure the directory is within the safe root if not normalized_directory.startswith(os.path.normpath(safe_root)): raise ValueError(f"Access to directory '{directory}' is not allowed.") - files = os.listdir(normalized_directory) + return normalized_directory + + +# 移除目录下文件名前缀相同的 +def remove_common_prefix(directory): + files = os.listdir(directory) # 获取所有文件的前缀 common_prefix = _longest_common_prefix(files)