-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sql): allow paramerized query through sql sanitization (#1576)
- Loading branch information
1 parent
0c6738b
commit f667367
Showing
5 changed files
with
61 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
|
@@ -197,3 +197,31 @@ def test_mysql_safe_query(self, mysql_schema): | |
|
||
assert isinstance(result, DataFrame) | ||
mock_sql_query.assert_called_once_with("select * from users") | ||
|
||
def test_mysql_malicious_with_no_import(self, mysql_schema): | ||
"""Test loading data from a MySQL source creates a VirtualDataFrame and handles queries correctly.""" | ||
with patch( | ||
"pandasai.data_loader.sql_loader.is_sql_query_safe" | ||
) as mock_sql_query, patch( | ||
"pandasai.data_loader.sql_loader.SQLDatasetLoader._get_loader_function" | ||
) as mock_loader_function: | ||
mocked_exec_function = MagicMock() | ||
mock_df = DataFrame( | ||
pd.DataFrame( | ||
{ | ||
"email": ["[email protected]"], | ||
"first_name": ["John"], | ||
"timestamp": [pd.Timestamp.now()], | ||
} | ||
) | ||
) | ||
mocked_exec_function.return_value = mock_df | ||
|
||
mock_exec_function = MagicMock() | ||
mock_loader_function.return_value = mock_exec_function | ||
mock_exec_function.side_effect = ModuleNotFoundError("Error") | ||
loader = SQLDatasetLoader(mysql_schema, "test/users") | ||
mock_sql_query.return_value = True | ||
logging.debug("Loading schema from dataset path: %s", loader) | ||
with pytest.raises(ImportError): | ||
loader.execute_query("select * from users") |
This file contains 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