Security: Validate SQL identifiers in Spanner search tool to prevent injection
Summary
Fix SQL injection vulnerabilities in google.adk.tools.spanner.search_tool where LLM-controllable parameters ( able_name, columns, embedding_column_to_search, additional_filter, op_k) are interpolated into SQL via f-strings without validation.
Problem
The _generate_sql_for_knn() and _generate_sql_for_ann() functions build SQL queries using f-string interpolation. Since similarity_search is registered as a GoogleTool in SpannerToolset.get_tools(), the LLM populates these parameters at runtime via tool calls. A successful prompt injection can cause the LLM to call the tool with malicious values, enabling:
-
UNION-based exfiltration via additional_filter:
sql WHERE 1=1 UNION ALL SELECT password, 0.0 FROM admin_credentials -
INFORMATION_SCHEMA enumeration via columns:
sql SELECT (SELECT STRING_AGG(table_name, ',') FROM INFORMATION_SCHEMA.TABLES) AS dump -
Cross-table reads via able_name:
sql FROM documents JOIN admin_credentials ac ON TRUE
Fix
- Identifier validation: Regex-validate able_name, columns, and embedding_column_to_search against a safe pattern (
^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)*$or quoted with backticks/double-quotes) - Filter validation: Reject common injection patterns (
UNION,;,--,/* */) inadditional_filter - Model name validation: Validate GSQL model names as identifiers and PG endpoints as URI format
- Type enforcement: Cast
top_kandnum_leaves_to_searchtoint
Testing
Added tests/unittests/tools/spanner/test_spanner_sql_validation.py with tests covering:
- Valid identifiers (simple, schema-qualified, quoted)
- Rejected injection patterns (JOIN in table_name, subquery in columns, UNION in filter)
- String-to-int coercion for top_k
Fixes #5913
Response from ADK Triaging Agent
Hello @Ashutosh0x, thank you for creating this PR! It is fantastic to see SQL injection validation being added to the Spanner search tool.
To help our reviewers process your contribution more efficiently, could you please update the PR to include:
- A summary of passed
pytestresults (e.g., the terminal output showing the new tests undertests/unittests/tools/spanner/test_spanner_sql_validation.pyare passing).
Thank you for contributing to the ADK project!
Hi @wukath — this PR fixes a SQL injection vulnerability in the Spanner search tool (issue #5913).
The similarity_search function is registered as a GoogleTool, meaning the LLM populates parameters like able_name, columns, and additional_filter at runtime. These values are interpolated directly into SQL via f-strings in _generate_sql_for_knn() and _generate_sql_for_ann() without any validation — a prompt injection can cause the LLM to supply malicious values enabling UNION-based data exfiltration, schema enumeration, or cross-table JOINs.
The fix adds regex-based identifier validation and keyword filtering at a single chokepoint (_validate_identifier, _validate_additional_filter), plus type enforcement on op_k. I also included a test suite covering the main injection primitives. Happy to adjust the approach if needed!
/adk-pr-analyze
Hi @Ashutosh0x , Thank you for your contribution! We appreciate you taking the time to submit this pull request. Please fix formatting errors, failing unit tests before we can proceed with the review.
@rohityan Thanks for the review! I've fixed all the issues:
Formatting: Applied pyink formatter and isort to all changed files — pre-commit checks should now pass.
Failing unit tests: The root cause was that my identifier validation regex was too strict — it rejected hyphens, but existing test fixtures use test-table (with a hyphen) as the table name. I've:
- Updated the regex to allow hyphens in identifiers (GCP resource names commonly use them)
- Updated PostgreSQL test fixtures to use proper Vertex AI endpoint URIs (
projects/test-project/locations/us-central1/publishers/google/models/text-embedding-005) instead of the baretest_endpointstring
All 3 files reformatted, tests should be green now. Please re-review when you get a chance!
Hi @Ashutosh0x , can you also fix the formatting errors.