fix(security): prevent SQL identifier injection in introspect_schema#26
Open
ritoban23 wants to merge 1 commit into
Open
fix(security): prevent SQL identifier injection in introspect_schema#26ritoban23 wants to merge 1 commit into
introspect_schema#26ritoban23 wants to merge 1 commit into
Conversation
… 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)
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.
What
introspect_schemainterpolated schema and table names directly into SQL f-strings using only double-quote wrapping: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'squoted_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 againstget_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
quoted_name(name, quote=True)(doubles embedded"→""per SQL standard), layered on top of existing whitelist checks