Skip to content

fix(security): prevent SQL identifier injection in introspect_schema#26

Open
ritoban23 wants to merge 1 commit into
agno-agi:mainfrom
ritoban23:fix/sql-identifier-injection
Open

fix(security): prevent SQL identifier injection in introspect_schema#26
ritoban23 wants to merge 1 commit into
agno-agi:mainfrom
ritoban23:fix/sql-identifier-injection

Conversation

@ritoban23

Copy link
Copy Markdown

What

introspect_schema interpolated schema and table names directly into SQL f-strings using only double-quote wrapping:

text(f'SELECT COUNT(*) FROM "{s}"."{obj}"')
text(f'SELECT * FROM "{found_schema}"."{table_name}" LIMIT :lim')

A value with an embedded double-quote breaks out of the wrapping and allows arbitrary SQL to execute - and since this tool is called by the LLM, a prompt injection attack makes it reachable.

Fix

Added a _safe_identifier() helper using SQLAlchemy's quoted_name(name, quote=True), which escapes embedded quotes by doubling them (SQL standard). Applied as defense-in-depth on top of the existing whitelist validation against get_table_names() / get_view_names().

Changes

  • dash/tools/introspect.py: add _safe_identifier(), use it in the COUNT listing query and the sample data SELECT.

Security Notes

  • Root cause: unsanitized identifier interpolation in f-string SQL
  • Attack surface: LLM-controlled tool input → prompt injection → SQL injection
  • Mitigation: identifier quoting via quoted_name(name, quote=True) (doubles embedded """ per SQL standard), layered on top of existing whitelist checks

… introspect_schema

The introspect_schema tool built SQL queries by interpolating schema and
table names directly into f-strings, relying only on double-quote wrapping
for safety:

    text(f'SELECT COUNT(*) FROM "{s}"."{obj}"')
    text(f'SELECT * FROM "{found_schema}"."{table_name}" LIMIT :lim')

This is unsafe because a value containing an embedded double-quote (e.g.
'foo"bar') breaks out of the quoting and allows arbitrary SQL to execute.
An attacker able to influence the table_name argument (e.g. via prompt
injection against the LLM) could exploit this to exfiltrate or destroy data.

Fix: replace all raw f-string identifier interpolation with
sqlalchemy.sql.quoted_name(..., quote=True), which escapes embedded
double-quotes by doubling them — the SQL-standard safe quoting method.
This is applied as defense-in-depth on top of the existing whitelist
validation that checks names against insp.get_table_names() /
insp.get_view_names().

Changes:
- Add _safe_identifier() helper that wraps quoted_name
- Use _safe_identifier() for schema and table/view in the COUNT listing query
- Use _safe_identifier() for schema and table/view in the sample data SELECT
- sample_limit was already fixed upstream via bind parameter :lim (no change)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant