fix: prevent SQL injection in SnowflakeSearchTool and NL2SQLTool#4994
Open
devin-ai-integration[bot] wants to merge 2 commits intomainfrom
Open
fix: prevent SQL injection in SnowflakeSearchTool and NL2SQLTool#4994devin-ai-integration[bot] wants to merge 2 commits intomainfrom
devin-ai-integration[bot] wants to merge 2 commits intomainfrom
Conversation
- Add identifier validation regex to database and snowflake_schema fields in SnowflakeSearchToolInput to reject malicious values at schema level - Add _validate_identifier() runtime check in SnowflakeSearchTool._run() and double-quote identifiers in USE DATABASE/SCHEMA SQL statements - Add _validate_identifier() to NL2SQLTool to sanitize table_name in _fetch_all_available_columns() preventing second-order SQL injection - Add comprehensive tests for both tools covering injection vectors Closes #4993 Co-Authored-By: João <joao@crewai.com>
Contributor
Author
|
Prompt hidden (unlisted session) |
Contributor
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #4993 — SQL injection vulnerability in
SnowflakeSearchToolviadatabaseandsnowflake_schemaparameters, and second-order SQL injection inNL2SQLToolviatable_namein_fetch_all_available_columns.SnowflakeSearchTool (
snowflake_search_tool.py):pattern=r"^[A-Za-z_][A-Za-z0-9_$]*$"todatabaseandsnowflake_schemafields onSnowflakeSearchToolInput(Pydantic schema-level validation)_validate_identifier()runtime check in_run()as defense-in-depthUSE DATABASE/USE SCHEMASQL statementsNL2SQLTool (
nl2sql_tool.py):_validate_identifier()to sanitizetable_namebefore f-string interpolation in_fetch_all_available_columns()Both use the same regex matching Snowflake's unquoted identifier rules: must start with a letter or underscore, followed by alphanumerics, underscores, or
$.Review & Testing Checklist for Human
^[A-Za-z_][A-Za-z0-9_$]*$rejects dot-separated identifiers (e.g.db.schema) and hyphenated names. Verify this is acceptable for standalone database/schema/table name fields — should not be an issue since these are single identifiers, but confirm against real Snowflake usage._validate_identifier: The identical static method exists in bothSnowflakeSearchToolandNL2SQLTool. Decide if this should be extracted to a shared utility.queryparameter remains unsanitized: By design (the tool's purpose is to execute SQL), but confirm this trade-off is acceptable.Notes
# noqa: S608onnl2sql_tool.py:57is preserved — the f-string interpolation remains but is now gated behind validation.test_cleanup_on_deletiontest in the Snowflake test file was already failing before this PR (async context manager onthreading.Lock) — not related to these changes.Link to Devin session: https://app.devin.ai/sessions/c92ac82a6bb44a8889173bbdd664acee