fix(provider): support 84-char Azure TTS subscription keys#5813
fix(provider): support 84-char Azure TTS subscription keys#5813lustresixx wants to merge 2 commits intoAstrBotDevs:masterfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! 此拉取请求旨在解决 Azure TTS 提供商在处理新版 Azure 订阅密钥时遇到的兼容性问题。此前,系统仅接受 32 位密钥,导致 84 位密钥即使有效也会被拒绝。通过更新密钥校验逻辑和错误提示,并引入新的测试用例,确保了 Azure TTS 服务能够正确识别和使用不同长度的有效订阅密钥,从而提升了服务的稳定性和用户体验。 Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Hey - 我发现了两个问题,并给出了一些总体反馈:
- 正则表达式
^[a-zA-Z0-9]{32,84}$实际上允许长度在 32 到 84 之间的任意值,而不是描述中所说的“恰好为 32 或 84 位”;如果你确实只想允许 32 或 84 两种长度,可以考虑使用类似^(?:[a-zA-Z0-9]{32}|[a-zA-Z0-9]{84})$这样的两种备选模式。
供 AI Agents 使用的提示词
Please address the comments from this code review:
## Overall Comments
- The regex `^[a-zA-Z0-9]{32,84}$` allows any length between 32 and 84 characters, not exactly 32 or 84 as described; if you really only want 32 or 84, consider two alternatives like `^(?:[a-zA-Z0-9]{32}|[a-zA-Z0-9]{84})$`.
## Individual Comments
### Comment 1
<location path="astrbot/core/provider/sources/azure_tts_source.py" line_range="23-26" />
<code_context>
TEMP_DIR = Path(get_astrbot_temp_path()) / "azure_tts"
TEMP_DIR.mkdir(parents=True, exist_ok=True)
+AZURE_TTS_SUBSCRIPTION_KEY_PATTERN = r"^[a-zA-Z0-9]{32,84}$"
</code_context>
<issue_to_address>
**issue (bug_risk):** 正则 `{32,84}` 匹配的是长度在 32 到 84 之间的任意值,而不是错误消息所暗示的“仅为 32 或 84 位”。
此模式允许任意长度在 32–84 之间的字母数字字符串,但后面的提示信息“32位或84位字母数字”表明只有长度为 32 或 84 的才是有效的。如果这是你想要的行为,应使用 `r"^(?:[a-zA-Z0-9]{32}|[a-zA-Z0-9]{84})$"` 之类的写法,而不是 `{32,84}`。
</issue_to_address>
### Comment 2
<location path="tests/test_azure_tts_provider.py" line_range="6" />
<code_context>
+from astrbot.core.provider.sources.azure_tts_source import AzureTTSProvider
+
+
+@pytest.mark.parametrize("key_length", [32, 84])
+def test_azure_tts_accepts_subscription_keys_with_supported_lengths(key_length: int):
+ config = {
</code_context>
<issue_to_address>
**suggestion (testing):** 为不支持的密钥长度和非字母数字密钥添加负向测试
当前测试只验证了被接受的长度,并不能防止无效密钥被误允许。请添加一个参数化测试,用来检查当密钥长度无效(例如 0、31、33、83、85)以及包含非字母数字字符(例如 `'-'`、`'_'`、`'~'`)时,`AzureTTSProvider` 会抛出 `ValueError`,从而同时锁定长度和字符集的校验行为。
</issue_to_address>帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据反馈改进后续的评审。
Original comment in English
Hey - I've found 2 issues, and left some high level feedback:
- The regex
^[a-zA-Z0-9]{32,84}$allows any length between 32 and 84 characters, not exactly 32 or 84 as described; if you really only want 32 or 84, consider two alternatives like^(?:[a-zA-Z0-9]{32}|[a-zA-Z0-9]{84})$.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The regex `^[a-zA-Z0-9]{32,84}$` allows any length between 32 and 84 characters, not exactly 32 or 84 as described; if you really only want 32 or 84, consider two alternatives like `^(?:[a-zA-Z0-9]{32}|[a-zA-Z0-9]{84})$`.
## Individual Comments
### Comment 1
<location path="astrbot/core/provider/sources/azure_tts_source.py" line_range="23-26" />
<code_context>
TEMP_DIR = Path(get_astrbot_temp_path()) / "azure_tts"
TEMP_DIR.mkdir(parents=True, exist_ok=True)
+AZURE_TTS_SUBSCRIPTION_KEY_PATTERN = r"^[a-zA-Z0-9]{32,84}$"
</code_context>
<issue_to_address>
**issue (bug_risk):** The regex `{32,84}` matches any length between 32 and 84, not "32 or 84" specifically as suggested by the error message.
This pattern allows any alphanumeric string 32–84 chars long, but the later message "32位或84位字母数字" implies only 32 or 84 chars are valid. If that’s the intent, use something like `r"^(?:[a-zA-Z0-9]{32}|[a-zA-Z0-9]{84})$"` instead of `{32,84}`.
</issue_to_address>
### Comment 2
<location path="tests/test_azure_tts_provider.py" line_range="6" />
<code_context>
+from astrbot.core.provider.sources.azure_tts_source import AzureTTSProvider
+
+
+@pytest.mark.parametrize("key_length", [32, 84])
+def test_azure_tts_accepts_subscription_keys_with_supported_lengths(key_length: int):
+ config = {
</code_context>
<issue_to_address>
**suggestion (testing):** Add negative tests for unsupported key lengths and non-alphanumeric keys
This only verifies the accepted lengths; it doesn’t protect against invalid keys accidentally becoming allowed. Please add a parametrized test that checks `AzureTTSProvider` raises `ValueError` for invalid lengths (e.g. 0, 31, 33, 83, 85) and for keys with non‑alphanumeric characters (e.g. `'-'`, `'_'`, `'~'`) to lock in both length and character-set validation.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This pull request is a great improvement, adding support for 84-character Azure TTS subscription keys. The changes are clear, with the validation logic extracted into a constant and a new test case added.
I have a few suggestions to further improve the code quality:
- The current regular expression
r"^[a-zA-Z0-9]{32,84}$"accepts all key lengths between 32 and 84, not just 32 and 84. This could lead to accepting invalid keys. - There is a redundant key validation in the
AzureNativeProvider's__init__method, which is already performed in the_parse_providerfactory method. - The test case could be more comprehensive by adding tests for invalid key lengths to ensure the robustness of the validation logic.
Overall, this is a good fix, and with a few minor changes, it will be perfect.
修复 #5715
当前 Azure TTS 提供商只接受 32 位订阅密钥,但新版 Azure 密钥可能为 84 位。
这会导致即使密钥有效,提供商初始化时仍报“密钥格式无效”。
Modifications / 改动点
核心改动文件:
astrbot/core/provider/sources/azure_tts_source.pytests/test_azure_tts_provider.pyThis is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
Checklist / 检查清单
requirements.txt和pyproject.toml文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations inrequirements.txtandpyproject.toml.Summary by Sourcery
更新 Azure TTS 提供程序,以同时接受旧版和新版订阅密钥格式,并增加测试覆盖以防止回归。
Bug Fixes:
Enhancements:
Tests:
Original summary in English
Summary by Sourcery
Update Azure TTS provider to accept both legacy and new subscription key formats and add coverage to prevent regressions.
Bug Fixes:
Enhancements:
Tests: