adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

Security: Validate SQL identifiers in Spanner search tool to prevent injection

Open Ashutosh0x opened this issue 5 months ago • 5 comments

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:

  1. UNION-based exfiltration via additional_filter: sql WHERE 1=1 UNION ALL SELECT password, 0.0 FROM admin_credentials

  2. INFORMATION_SCHEMA enumeration via columns: sql SELECT (SELECT STRING_AGG(table_name, ',') FROM INFORMATION_SCHEMA.TABLES) AS dump

  3. 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, ;, --, /* */) in additional_filter
  • Model name validation: Validate GSQL model names as identifiers and PG endpoints as URI format
  • Type enforcement: Cast top_k and num_leaves_to_search to int

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

Ashutosh0x avatar Jun 03 '26 05:06 Ashutosh0x

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 pytest results (e.g., the terminal output showing the new tests under tests/unittests/tools/spanner/test_spanner_sql_validation.py are passing).

Thank you for contributing to the ADK project!

adk-bot avatar Jun 03 '26 05:06 adk-bot

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!

Ashutosh0x avatar Jun 03 '26 06:06 Ashutosh0x

/adk-pr-analyze

rohityan avatar Jun 05 '26 18:06 rohityan

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 avatar Jun 12 '26 00:06 rohityan

@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:

  1. Updated the regex to allow hyphens in identifiers (GCP resource names commonly use them)
  2. 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 bare test_endpoint string

All 3 files reformatted, tests should be green now. Please re-review when you get a chance!

Ashutosh0x avatar Jun 12 '26 03:06 Ashutosh0x

Hi @Ashutosh0x , can you also fix the formatting errors.

rohityan avatar Jun 17 '26 18:06 rohityan