-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix: sanitize qqofficial command tags #8756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tangtaizong666
wants to merge
3
commits into
AstrBotDevs:master
Choose a base branch
from
tangtaizong666:fix/8755-qqofficial-command-tags-pr
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from astrbot.api.message_components import Plain | ||
| from astrbot.api.platform import MessageType | ||
| from astrbot.core.platform.sources.qqofficial import qqofficial_platform_adapter | ||
| from astrbot.core.platform.sources.qqofficial.qqofficial_platform_adapter import ( | ||
| QQOfficialPlatformAdapter, | ||
| ) | ||
|
|
||
|
|
||
| class FakeAuthor: | ||
| member_openid = "member-openid" | ||
| user_openid = "user-openid" | ||
| id = "author-id" | ||
| username = "author-name" | ||
|
|
||
|
|
||
| class FakeMention: | ||
| id = "bot-id" | ||
|
|
||
|
|
||
| class FakeGroupMessage: | ||
| def __init__(self, content: str | None) -> None: | ||
| self.id = "message-id" | ||
| self.content = content | ||
| self.author = FakeAuthor() | ||
| self.group_openid = "group-openid" | ||
| self.attachments = [] | ||
|
|
||
|
|
||
| class FakeChannelMessage: | ||
| def __init__(self, content: str | None) -> None: | ||
| self.id = "guild-message-id" | ||
| self.content = content | ||
| self.author = FakeAuthor() | ||
| self.mentions = [FakeMention()] | ||
| self.attachments = [] | ||
| self.channel_id = "channel-id" | ||
|
|
||
|
|
||
| def _plain_texts(message_components: list[object]) -> list[str]: | ||
| return [ | ||
| component.text | ||
| for component in message_components | ||
| if isinstance(component, Plain) | ||
| ] | ||
|
|
||
|
|
||
| def test_sanitize_command_interaction_tags_keeps_plain_text() -> None: | ||
| assert ( | ||
| QQOfficialPlatformAdapter._sanitize_command_interaction_tags("hello world") | ||
| == "hello world" | ||
| ) | ||
|
|
||
|
|
||
| def test_sanitize_command_interaction_tags_extracts_double_quoted_text() -> None: | ||
| content = ( | ||
| 'hello <qqbot-cmd-input text="/quick-map" show="quick map" ' | ||
| 'reference="false" /> now' | ||
| ) | ||
|
|
||
| assert ( | ||
| QQOfficialPlatformAdapter._sanitize_command_interaction_tags(content) | ||
| == "hello /quick-map now" | ||
| ) | ||
|
|
||
|
|
||
| def test_sanitize_command_interaction_tags_extracts_single_quoted_text() -> None: | ||
| content = "run <QQBOT-CMD-ENTER show='go' TEXT='/start' />" | ||
|
|
||
| assert ( | ||
| QQOfficialPlatformAdapter._sanitize_command_interaction_tags(content) | ||
| == "run /start" | ||
| ) | ||
|
|
||
|
|
||
| def test_sanitize_command_interaction_tags_removes_tag_without_text() -> None: | ||
| content = 'hello <qqbot-cmd-enter show="enter" /> world' | ||
|
|
||
| assert ( | ||
| QQOfficialPlatformAdapter._sanitize_command_interaction_tags(content) | ||
| == "hello world" | ||
| ) | ||
|
|
||
|
|
||
| def test_sanitize_command_interaction_tags_leaves_non_target_tag_unchanged() -> None: | ||
| content = '<custom-tag text="/keep" />' | ||
|
|
||
| assert ( | ||
| QQOfficialPlatformAdapter._sanitize_command_interaction_tags(content) == content | ||
| ) | ||
|
|
||
|
|
||
| def test_normalize_message_content_handles_none() -> None: | ||
| assert QQOfficialPlatformAdapter._normalize_message_content(None) == "" | ||
|
|
||
|
|
||
| def test_normalize_message_content_removes_bot_mention_and_sanitizes_tags() -> None: | ||
| content = '<@!bot-id> <qqbot-cmd-input text="/quick-map" show="quick map" />' | ||
|
|
||
| assert ( | ||
| QQOfficialPlatformAdapter._normalize_message_content( | ||
| content, | ||
| mention_id="bot-id", | ||
| ) | ||
| == "/quick-map" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_parse_from_qqofficial_sanitizes_message_str_and_plain_component( | ||
| monkeypatch, | ||
| ) -> None: | ||
| monkeypatch.setattr( | ||
| qqofficial_platform_adapter.botpy.message, | ||
| "GroupMessage", | ||
| FakeGroupMessage, | ||
| ) | ||
| raw_message = FakeGroupMessage( | ||
| 'hello <qqbot-cmd-input text="/quick-map" show="quick map" />' | ||
| ) | ||
|
|
||
| message = await QQOfficialPlatformAdapter._parse_from_qqofficial( | ||
| raw_message, | ||
| MessageType.GROUP_MESSAGE, | ||
| ) | ||
|
|
||
| assert message.message_str == "hello /quick-map" | ||
| assert _plain_texts(message.message) == ["hello /quick-map"] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_parse_from_qqofficial_handles_none_group_content( | ||
| monkeypatch, | ||
| ) -> None: | ||
| monkeypatch.setattr( | ||
| qqofficial_platform_adapter.botpy.message, | ||
| "GroupMessage", | ||
| FakeGroupMessage, | ||
| ) | ||
| raw_message = FakeGroupMessage(None) | ||
|
|
||
| message = await QQOfficialPlatformAdapter._parse_from_qqofficial( | ||
| raw_message, | ||
| MessageType.GROUP_MESSAGE, | ||
| ) | ||
|
|
||
| assert message.message_str == "" | ||
| assert _plain_texts(message.message) == [""] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_parse_from_qqofficial_handles_none_channel_content( | ||
| monkeypatch, | ||
| ) -> None: | ||
| monkeypatch.setattr( | ||
| qqofficial_platform_adapter.botpy.message, | ||
| "Message", | ||
| FakeChannelMessage, | ||
| ) | ||
| raw_message = FakeChannelMessage(None) | ||
|
|
||
| message = await QQOfficialPlatformAdapter._parse_from_qqofficial( | ||
| raw_message, | ||
| MessageType.GROUP_MESSAGE, | ||
| ) | ||
|
|
||
| assert message.message_str == "" | ||
| assert _plain_texts(message.message) == [""] | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be beneficial to add a test case to verify that the parser handles
Nonecontent gracefully without crashing, especially since we added defensive checks for it.References