-
Notifications
You must be signed in to change notification settings - Fork 8.1k
[fix] mysql search table name validation #6341
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
Merged
theCyberTech
merged 7 commits into
main
from
codex/fix-mysql-search-table-name-injection
Aug 13, 2026
+133
−1
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e3f7d1a
fix mysql search table name validation
theCyberTech 0c5e87e
Merge branch 'main' into codex/fix-mysql-search-table-name-injection
theCyberTech b4c65f5
Merge branch 'main' into codex/fix-mysql-search-table-name-injection
theCyberTech 5b15858
Merge branch 'main' into codex/fix-mysql-search-table-name-injection
theCyberTech fa87de2
Merge branch 'main' into codex/fix-mysql-search-table-name-injection
theCyberTech 7566889
Merge branch 'main' into codex/fix-mysql-search-table-name-injection
theCyberTech 0daaa22
Merge branch 'main' into codex/fix-mysql-search-table-name-injection
theCyberTech 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,110 @@ | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from crewai_tools.rag.data_types import DataType | ||
| from crewai_tools.tools.mysql_search_tool.mysql_search_tool import MySQLSearchTool | ||
| from crewai_tools.tools.rag.rag_tool import RagTool | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_rag_client() -> MagicMock: | ||
| mock_client = MagicMock() | ||
| mock_client.get_or_create_collection = MagicMock(return_value=None) | ||
| mock_client.add_documents = MagicMock(return_value=None) | ||
| mock_client.search = MagicMock(return_value=[]) | ||
| return mock_client | ||
|
|
||
|
|
||
| def create_mysql_search_tool( | ||
| mock_rag_client: MagicMock, table_name: str | ||
| ) -> MySQLSearchTool: | ||
| with ( | ||
| patch( | ||
| "crewai_tools.adapters.crewai_rag_adapter.get_rag_client", | ||
| return_value=mock_rag_client, | ||
| ), | ||
| patch( | ||
| "crewai_tools.adapters.crewai_rag_adapter.create_client", | ||
| return_value=mock_rag_client, | ||
| ), | ||
| ): | ||
| return MySQLSearchTool( | ||
| db_uri="mysql://user:password@localhost:3306/test_database", | ||
| table_name=table_name, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("table_name", "expected_query"), | ||
| [ | ||
| ("users", "SELECT * FROM `users`;"), | ||
| ("user_profiles_2026", "SELECT * FROM `user_profiles_2026`;"), | ||
| ("schema_name.users", "SELECT * FROM `schema_name`.`users`;"), | ||
| ("information_schema.tables", "SELECT * FROM `information_schema`.`tables`;"), | ||
| ], | ||
| ) | ||
| def test_mysql_search_tool_quotes_valid_table_identifiers( | ||
| mock_rag_client: MagicMock, table_name: str, expected_query: str | ||
| ) -> None: | ||
| with patch.object(RagTool, "add", return_value=None) as mock_add: | ||
| create_mysql_search_tool(mock_rag_client, table_name) | ||
|
|
||
| mock_add.assert_called_once_with( | ||
| expected_query, | ||
| data_type=DataType.MYSQL, | ||
| metadata={"db_uri": "mysql://user:password@localhost:3306/test_database"}, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "table_name", | ||
| [ | ||
| "users where 1=1", | ||
| "users; drop table users;--", | ||
| "users -- comment", | ||
| "users/*comment*/", | ||
| "`users`", | ||
| "schema.users.extra", | ||
| "schema.", | ||
| ".users", | ||
| "123users", | ||
| ], | ||
| ) | ||
| def test_mysql_search_tool_rejects_invalid_table_identifiers( | ||
| mock_rag_client: MagicMock, table_name: str | ||
| ) -> None: | ||
| with ( | ||
| patch.object(RagTool, "add", return_value=None) as mock_add, | ||
| pytest.raises(ValueError, match="MySQL table_name must be a valid"), | ||
| ): | ||
| create_mysql_search_tool(mock_rag_client, table_name) | ||
|
|
||
| mock_add.assert_not_called() | ||
|
|
||
|
|
||
| def test_mysql_search_tool_still_runs_search_queries( | ||
| mock_rag_client: MagicMock, | ||
| ) -> None: | ||
| with patch.object(RagTool, "add", return_value=None): | ||
| tool = create_mysql_search_tool(mock_rag_client, "users") | ||
|
|
||
| with patch.object(RagTool, "_run", return_value="Alice") as mock_run: | ||
| result = tool._run("alice") | ||
|
|
||
| assert "Alice" in result | ||
| mock_run.assert_called_once_with( | ||
| query="alice", similarity_threshold=None, limit=None | ||
| ) | ||
|
|
||
|
|
||
| def test_mysql_search_tool_uses_mysql_data_type_metadata( | ||
| mock_rag_client: MagicMock, | ||
| ) -> None: | ||
| with patch.object(RagTool, "add", return_value=None) as mock_add: | ||
| create_mysql_search_tool(mock_rag_client, "users") | ||
|
|
||
| assert mock_add.call_args.kwargs == { | ||
| "data_type": DataType.MYSQL, | ||
| "metadata": {"db_uri": "mysql://user:password@localhost:3306/test_database"}, | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.