Skip to content

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

fix(tts): reject truncated VoxCPM reference WAVs before requests

Rudy Celekli2b883617cf0f
diff --git a/app/services/voice.py b/app/services/voice.py
index 4d138237..d1766b37 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -2910,8 +2910,14 @@ def _encode_voxcpm_audio_data_uri(
raise ValueError(f"{field_name} audio exceeds ModelBest's 5 MiB limit")
try:
with wave.open(io.BytesIO(audio_bytes), "rb") as wav_file:
- if wav_file.getnframes() <= 0:
+ frame_count = wav_file.getnframes()
+ if frame_count <= 0:
raise ValueError(f"{field_name} audio is empty")
+ expected_bytes = frame_count * wav_file.getnchannels() * wav_file.getsampwidth()
+ # WAV headers can advertise frames that are not in the upload.
+ # Bound the read by the existing upload cap even for forged counts.
+ if expected_bytes > len(audio_bytes) or len(wav_file.readframes(frame_count)) != expected_bytes:
+ raise ValueError(f"{field_name} audio contains truncated PCM frames")
except wave.Error as exc:
raise ValueError(f"{field_name} audio must be a valid WAV") from exc
return "data:audio/wav;base64," + base64.b64encode(audio_bytes).decode("ascii")
diff --git a/test/services/test_voxcpm_truncated_reference.py b/test/services/test_voxcpm_truncated_reference.py
new file mode 100644
index 00000000..af1213f3
--- /dev/null
+++ b/test/services/test_voxcpm_truncated_reference.py
@@ -0,0 +1,33 @@
+import io
+import unittest
+import wave
+from unittest.mock import Mock, patch
+from app.services import voice
+
+
+def reference_wav():
+ output = io.BytesIO()
+ with wave.open(output, "wb") as wav:
+ wav.setnchannels(1)
+ wav.setsampwidth(2)
+ wav.setframerate(16000)
+ wav.writeframes(b"\x00\x00" * 160)
+ return output.getvalue()
+
+
+class TestVoxcpmTruncatedReference(unittest.TestCase):
+ def test_truncated_reference_and_prompt_rejected_before_provider_request(self):
+ valid = reference_wav()
+ for field in ("reference", "prompt"):
+ for cut in (44, len(valid) - 2):
+ with self.subTest(field=field, cut=cut), patch.dict(voice.config.voxcpm, {"api_key": "test-key", "model_id": "test-model"}), patch.object(voice.requests, "post", return_value=Mock(status_code=400, text="rejected invalid reference")) as post:
+ kwargs = {f"{field}_audio": valid[:cut]}
+ if field == "prompt":
+ kwargs["prompt_text"] = "Reference transcript"
+ result = voice.voxcpm_tts("Hello.", "default", "unused.mp3", **kwargs)
+ self.assertIsNone(result)
+ post.assert_not_called()
+
+ def test_complete_pcm_reference_is_encoded(self):
+ encoded = voice._encode_voxcpm_audio_data_uri(reference_wav(), "reference")
+ self.assertTrue(encoded.startswith("data:audio/wav;base64,"))