利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.
Python ai-video-generatorcontent-creationffmpeginstagram-reelsllmpythonshort-videosubtitlestext-to-speechtiktokvideo-automationvideo-workflowworkflow-automationyoutube-shorts
Fix custom BGM path handling
Harry042deb84f01e
diff --git a/app/services/video.py b/app/services/video.pyindex 214b295e..4c0515f1 100644--- a/app/services/video.py+++ b/app/services/video.py@@ -234,6 +234,28 @@ def delete_files(files: List[str] | str):except Exception as e:logger.debug(f"failed to delete file {file}: {str(e)}")++def _resolve_bgm_file_path(song_dir: str, bgm_file: str) -> str:+ # 背景音乐只允许读取 resource/songs 目录内的文件,避免用户输入任意路径后+ # 被 MoviePy 打开。这里兼容两种常见输入:+ # 1. output000.mp3:来自 BGM 列表或用户只填写文件名+ # 2. ./resource/songs/output000.mp3:用户按项目目录结构填写的相对路径+ # 两种写法最终都会再次通过 resource/songs 白名单校验,不能绕过目录限制。+ try:+ return file_security.resolve_path_within_directory(song_dir, bgm_file)+ except ValueError as song_dir_exc:+ if os.path.isabs(bgm_file):+ raise song_dir_exc++ project_relative_file = os.path.join(utils.root_dir(), bgm_file)+ try:+ return file_security.resolve_path_within_directory(+ song_dir, project_relative_file+ )+ except ValueError as root_dir_exc:+ raise ValueError(str(root_dir_exc)) from song_dir_exc++def get_bgm_file(bgm_type: str = "random", bgm_file: str = ""):if not bgm_type:return ""@@ -241,9 +263,7 @@ def get_bgm_file(bgm_type: str = "random", bgm_file: str = ""):if bgm_file:song_dir = utils.song_dir()try:- resolved_bgm_file = file_security.resolve_path_within_directory(- song_dir, bgm_file- )+ resolved_bgm_file = _resolve_bgm_file_path(song_dir, bgm_file)except ValueError as exc:# API 请求里的 bgm_file 来自用户输入,不能直接把任意绝对路径交给# MoviePy 打开。这里强制限制到 resource/songs 目录,阻止读取diff --git a/test/services/test_video.py b/test/services/test_video.pyindex f98a756d..3da5c2a1 100644--- a/test/services/test_video.py+++ b/test/services/test_video.py@@ -149,6 +149,25 @@ class TestVideoService(unittest.TestCase):if os.path.exists(bgm_path):os.remove(bgm_path)+ def test_get_bgm_file_accepts_project_relative_song_path(self):+ """+ 用户在 WebUI 中可能直接填写 ./resource/songs/xxx.mp3。该路径虽然是+ 项目根目录相对路径,但实际文件仍在 resource/songs 白名单目录内,+ 应该被接受,避免自定义背景音乐被误判为不存在。+ """+ song_dir = utils.song_dir()+ bgm_path = os.path.join(song_dir, "test-relative-bgm.mp3")+ Path(bgm_path).write_bytes(b"fake-mp3")++ try:+ self.assertEqual(+ vd.get_bgm_file(bgm_file="./resource/songs/test-relative-bgm.mp3"),+ bgm_path,+ )+ finally:+ if os.path.exists(bgm_path):+ os.remove(bgm_path)+def test_get_bgm_file_rejects_path_outside_song_directory(self):"""用户传入的 bgm_file 不能直接作为本地路径打开,否则可能读取系统文件。diff --git a/webui/Main.py b/webui/Main.pyindex 14a241b6..8e9cdc0a 100644--- a/webui/Main.py+++ b/webui/Main.py@@ -914,8 +914,11 @@ with middle_panel:custom_bgm_file = st.text_input(tr("Custom Background Music File"), key="custom_bgm_file_input")- if custom_bgm_file and os.path.exists(custom_bgm_file):- params.bgm_file = custom_bgm_file+ if custom_bgm_file:+ # 这里不直接用 os.path.exists 判断,因为用户常见输入是+ # output000.mp3,这个文件名需要由服务层映射到 resource/songs+ # 目录后再校验。服务层会统一限制目录和文件类型,避免任意路径读取。+ params.bgm_file = custom_bgm_file.strip()# st.write(f":red[已选择自定义背景音乐]:**{custom_bgm_file}**")params.bgm_volume = st.selectbox(tr("Background Music Volume"),