fix: prevent SQL injection in SnowflakeSearchTool and NL2SQLTool#4997
Open
jmanico wants to merge 1 commit intocrewAIInc:mainfrom
Open
fix: prevent SQL injection in SnowflakeSearchTool and NL2SQLTool#4997jmanico wants to merge 1 commit intocrewAIInc:mainfrom
jmanico wants to merge 1 commit intocrewAIInc:mainfrom
Conversation
Add identifier validation to SnowflakeSearchTool database and schema parameters to prevent SQL injection via f-string interpolation in USE DATABASE/SCHEMA statements. Defense-in-depth: Pydantic pattern validation on input fields, runtime regex check, and double-quoted identifiers. For NL2SQLTool, replace f-string interpolation with parameterized query using SQLAlchemy text() bind parameters in _fetch_all_available_columns. Closes crewAIInc#4993
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 SQL injection vulnerabilities in
SnowflakeSearchToolandNL2SQLToolreported in #4993.SnowflakeSearchTool — the
databaseandsnowflake_schemaparameters in_run()were interpolated directly intoUSE DATABASE/USE SCHEMASQL statements via f-strings with no validation. This PR adds three layers of defense:SnowflakeSearchToolInputandSnowflakeConfigfields — rejects anything that isn't a valid Snowflake identifier before it reaches any code path_validate_identifier()check in_run()— defense-in-depth for callers that bypass Pydantic (e.g. direct method calls)NL2SQLTool —
_fetch_all_available_columns()was using f-string interpolation to build a WHERE clause with the table name (flagged with# noqa: S608). Replaced with SQLAlchemytext()bind parameters, which is the standard parameterized query approach. Added aparamsargument toexecute_sql()to support this.Changes
snowflake_search_tool.py: AddedSNOWFLAKE_IDENTIFIER_PATTERNconstant,_validate_identifier()helper, pattern validation on input/config fields, quoted identifiers in_run()nl2sql_tool.py: Switched_fetch_all_available_columns()to parameterized query, addedparamsarg toexecute_sql()snowflake_search_tool_test.py: Added comprehensive tests covering identifier validation, Pydantic-level rejection of injection payloads, defense-in-depth runtime checks, and quoted identifier verificationTest plan
TestIdentifierValidation— validates_validate_identifier()accepts valid identifiers and rejects injection payloads (semicolons, quotes, spaces, parens, empty strings, leading numbers)TestInputSchemaValidation— confirms Pydantic rejects injection in database/schema fieldsTestConfigIdentifierValidation— confirms SnowflakeConfig rejects injection in database/schematest_run_rejects_injection_in_database/test_run_rejects_injection_in_schema— defense-in-depth tests at the_run()leveltest_run_uses_quoted_identifiers— verifies double-quoting in generated SQLCloses #4993