Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(material): bound remote video downloads

Rudy Celekli8ef236beba52
diff --git a/app/services/material.py b/app/services/material.py
index a10efbd2..eb98f49b 100644
--- a/app/services/material.py
+++ b/app/services/material.py
@@ -33,6 +33,10 @@ from app.utils import utils
_api_key_counter = 0
_api_key_lock = threading.Lock()
+# A provider URL can point to an unexpectedly large object or never-ending stream.
+# Short stock and generated clips should stay well below this conservative cap.
+MAX_VIDEO_DOWNLOAD_BYTES = 512 * 1024 * 1024
+
class _OpenAIImageDecodeError(ValueError):
"""表示兼容接口返回的字节无法解码为图片,不包含本地文件写入故障。"""
@@ -1132,8 +1136,20 @@ def save_video(video_url: str, save_dir: str = "") -> str:
stream=True,
) as response:
response.raise_for_status()
+ headers = getattr(response, "headers", {}) or {}
+ try:
+ declared_size = int(headers.get("Content-Length", ""))
+ except (TypeError, ValueError):
+ declared_size = 0
+ if declared_size > MAX_VIDEO_DOWNLOAD_BYTES:
+ raise ValueError("video download exceeds 512 MB limit")
+
+ downloaded_bytes = 0
for chunk in response.iter_content(chunk_size=1024 * 1024):
if chunk:
+ downloaded_bytes += len(chunk)
+ if downloaded_bytes > MAX_VIDEO_DOWNLOAD_BYTES:
+ raise ValueError("video download exceeds 512 MB limit")
temp_file.write(chunk)
if os.path.getsize(temp_path) == 0:
diff --git a/test/services/test_material.py b/test/services/test_material.py
index d4ee40c9..4fe0aca0 100644
--- a/test/services/test_material.py
+++ b/test/services/test_material.py
@@ -842,6 +842,45 @@ class TestMaterialTlsVerification(unittest.TestCase):
self.assertTrue(response.closed)
self.assertEqual(list(Path(temp_dir).iterdir()), [])
+ def test_save_video_rejects_declared_oversized_download_before_streaming(self):
+ class OversizedResponse(_FakeVideoDownloadResponse):
+ headers = {"Content-Length": "9"}
+
+ def iter_content(self, chunk_size):
+ raise AssertionError("oversized body should not be read")
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ with (
+ patch("app.services.material.MAX_VIDEO_DOWNLOAD_BYTES", 8, create=True),
+ patch(
+ "app.services.material.requests.get",
+ return_value=OversizedResponse(b""),
+ ),
+ ):
+ with self.assertRaisesRegex(ValueError, "download exceeds"):
+ material.save_video("https://example.com/large.mp4", temp_dir)
+
+ self.assertEqual(list(Path(temp_dir).iterdir()), [])
+
+ def test_save_video_stops_undeclared_oversized_stream_and_cleans_temp(self):
+ class StreamingResponse(_FakeVideoDownloadResponse):
+ def iter_content(self, chunk_size):
+ yield b"first"
+ yield b"second"
+
+ with tempfile.TemporaryDirectory() as temp_dir:
+ with (
+ patch("app.services.material.MAX_VIDEO_DOWNLOAD_BYTES", 8, create=True),
+ patch(
+ "app.services.material.requests.get",
+ return_value=StreamingResponse(b""),
+ ),
+ ):
+ with self.assertRaisesRegex(ValueError, "download exceeds"):
+ material.save_video("https://example.com/stream.mp4", temp_dir)
+
+ self.assertEqual(list(Path(temp_dir).iterdir()), [])
+
def test_invalid_download_is_not_reused_as_cached_video(self):
url = "https://example.com/broken-then-valid.mp4"
cached_name = f"vid-{material.utils.md5(url)}.mp4"