X Tutup
Skip to content

fix(provider): support 84-char Azure TTS subscription keys#5813

Open
lustresixx wants to merge 2 commits intoAstrBotDevs:masterfrom
lustresixx:fix/issue-5715-azure-tts-key-length
Open

fix(provider): support 84-char Azure TTS subscription keys#5813
lustresixx wants to merge 2 commits intoAstrBotDevs:masterfrom
lustresixx:fix/issue-5715-azure-tts-key-length

Conversation

@lustresixx
Copy link

@lustresixx lustresixx commented Mar 6, 2026

修复 #5715

当前 Azure TTS 提供商只接受 32 位订阅密钥,但新版 Azure 密钥可能为 84 位。
这会导致即使密钥有效,提供商初始化时仍报“密钥格式无效”。

Modifications / 改动点

  • 调整 Azure TTS 订阅密钥校验规则,支持 32 位和 84 位字母数字密钥。
  • 将两处校验逻辑统一为同一个正则常量,保证行为一致。
  • 同步更新用户可见的错误提示文案。
  • 新增回归测试,覆盖 32 位与 84 位密钥均可通过的场景。

核心改动文件:

  • astrbot/core/provider/sources/azure_tts_source.py

  • tests/test_azure_tts_provider.py

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

image ---

Checklist / 检查清单

  • 👀 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
  • 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.

Summary by Sourcery

更新 Azure TTS 提供程序,以同时接受旧版和新版订阅密钥格式,并增加测试覆盖以防止回归。

Bug Fixes:

  • 允许 Azure TTS 提供程序在初始化时,除 32 位字母数字订阅密钥外,也能使用 84 位字母数字订阅密钥。

Enhancements:

  • 将 Azure TTS 订阅密钥的校验逻辑集中到一个共享的模式常量中,并在密钥格式无效时提供更清晰的错误信息。

Tests:

  • 添加参数化测试,确保 Azure TTS 提供程序能够接受受支持长度的订阅密钥。
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:

  • Allow Azure TTS provider initialization with 84-character alphanumeric subscription keys in addition to 32-character keys.

Enhancements:

  • Centralize Azure TTS subscription key validation into a shared pattern constant and clarify error messaging for invalid key formats.

Tests:

  • Add parameterized tests ensuring Azure TTS provider accepts subscription keys of supported lengths.

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 6, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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

  • Azure TTS 订阅密钥校验规则: 调整了 Azure TTS 订阅密钥的校验规则,使其能够支持 32 位和 84 位的字母数字密钥,解决了新版 Azure 密钥可能不被接受的问题。
  • 校验逻辑统一: 将两处密钥校验逻辑统一为一个新的正则表达式常量 AZURE_TTS_SUBSCRIPTION_KEY_PATTERN,确保了行为的一致性。
  • 错误提示文案更新: 同步更新了用户可见的错误提示文案,明确指出订阅密钥应为 32 位或 84 位字母数字格式。
  • 新增回归测试: 添加了新的回归测试,覆盖了 32 位和 84 位密钥均能被正确接受的场景,提高了代码的健壮性。
Changelog
  • astrbot/core/provider/sources/azure_tts_source.py
    • 定义了新的正则表达式常量 AZURE_TTS_SUBSCRIPTION_KEY_PATTERN,用于匹配 32 到 84 位的字母数字密钥。
    • init 方法中,将硬编码的 32 位密钥校验正则表达式替换为新的常量。
    • 在 _parse_provider 方法中,将硬编码的 32 位密钥校验正则表达式替换为新的常量。
    • 更新了订阅密钥格式无效时的错误提示信息,明确指出支持 32 位或 84 位密钥。
  • tests/test_azure_tts_provider.py
    • 新增了测试文件 tests/test_azure_tts_provider.py。
    • 添加了参数化测试 test_azure_tts_accepts_subscription_keys_with_supported_lengths,验证 AzureTTSProvider 能够正确接受 32 位和 84 位的订阅密钥。
Activity
  • 目前没有与此拉取请求相关的评论或审查活动。
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Sourcery 对开源项目永久免费——如果你觉得这些评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据反馈改进后续的评审。
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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@dosubot dosubot bot added the area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. label Mar 6, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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.
  2. There is a redundant key validation in the AzureNativeProvider's __init__ method, which is already performed in the _parse_provider factory method.
  3. 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.

@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:provider The bug / feature is about AI Provider, Models, LLM Agent, LLM Agent Runner. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

X Tutup