Skip to content

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

fix(tts): escape derived Azure SSML locale attributes

Rudy Celeklif9f4de19d739
diff --git a/app/services/voice.py b/app/services/voice.py
index 49ae5dd6..a8e25d2a 100644
--- a/app/services/voice.py
+++ b/app/services/voice.py
@@ -1675,10 +1675,11 @@ def _build_azure_v2_ssml(text: str, voice_name: str, voice_rate: float) -> str:
else "en-US"
)
escaped_text = escape(text)
+ escaped_voice_locale = escape(voice_locale, {'"': """})
escaped_voice_name = escape(voice_name, {'"': """})
return (
'<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" '
- f'xml:lang="{voice_locale}">'
+ f'xml:lang="{escaped_voice_locale}">'
f'<voice name="{escaped_voice_name}">'
f'<prosody rate="{normalized_rate:g}">{escaped_text}</prosody>'
"</voice></speak>"
diff --git a/test/services/test_azure_locale_escaping.py b/test/services/test_azure_locale_escaping.py
new file mode 100644
index 00000000..6e93f05b
--- /dev/null
+++ b/test/services/test_azure_locale_escaping.py
@@ -0,0 +1,24 @@
+import unittest
+from unittest.mock import patch
+import xml.etree.ElementTree as ET
+from app.services import voice
+
+
+class TestAzureLocaleEscaping(unittest.TestCase):
+ def test_supplied_v2_voice_cannot_inject_ssml_attributes_or_break_xml(self):
+ for locale in ('en-US" data_injected="yes', 'en-US&unexpected'):
+ with self.subTest(locale=locale):
+ supplied_voice = f"{locale}-AriaNeural-V2-Female"
+ captured = {}
+ def synthesize(text, voice_name, voice_file, voice_rate):
+ normalized = voice.is_azure_v2_voice(voice_name)
+ captured["ssml"] = voice._build_azure_v2_ssml(text, normalized, voice_rate)
+ return "synthesis sentinel"
+ with patch.object(voice, "azure_tts_v2", side_effect=synthesize) as sdk_path:
+ result = voice._single_tts("Hello & goodbye.", supplied_voice, 1, "unused.mp3", 1)
+ self.assertEqual(result, "synthesis sentinel")
+ sdk_path.assert_called_once()
+ document = ET.fromstring(captured["ssml"])
+ self.assertEqual(set(document.attrib), {"version", "{http://www.w3.org/XML/1998/namespace}lang"})
+ self.assertEqual(document.attrib["{http://www.w3.org/XML/1998/namespace}lang"], locale)
+ self.assertEqual(document.find(".//{http://www.w3.org/2001/10/synthesis}prosody").text, "Hello & goodbye.")