fix(resources): bind collection filter values as SQL parameters (#1304) - #1310
Open
Anai-Guo wants to merge 1 commit into
Open
fix(resources): bind collection filter values as SQL parameters (#1304)#1310Anai-Guo wants to merge 1 commit into
Anai-Guo wants to merge 1 commit into
Conversation
…are-ai#1304) The SQLite and Postgres collection-database backends built WHERE clauses by interpolating filter values straight into the SQL string, so a value carrying SQL metacharacters escaped its quotes and rewrote the predicate. The key names are validated against the library's allowed-key list and the table name goes through safe_name(), but the values were never escaped. Bind the values as query parameters instead, in the four value sinks reported in llmware-ai#1304: * PGRetrieval.text_search_with_key_value_dict_filter * PGRetrieval.filter_by_key_dict * SQLiteRetrieval.text_search_with_key_value_dict_filter * SQLiteRetrieval.filter_by_key_dict Postgres uses %s placeholders and SQLite uses ?, matching the parameter style already used elsewhere in this module. The mongo-style {"$in": [...]} ranges are bound too, which also fixes string members of those lists being emitted unquoted (previously "IN (alice,bob)" -> "no such column: alice").
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.
Fixes #1304.
Problem
llmware/resources.pybuilds theWHEREclause of the SQLite and Postgres collection backends by interpolating the filter value directly into the SQL string:The
keyis validated against the library's allowed-key list (retrieval.py) and the table name goes throughsafe_name(), but the value is never escaped or parameterized. A value containing a quote closes the literal and rewrites the predicate.These sinks are reachable from the public API —
Library.block_lookup()/Query.document_lookup()tofilter_by_key_dict, andQuery.text_query_with_custom_filter()/text_query_by_author_or_speaker()totext_search_with_key_value_dict_filter— where the values come from end-user query input or document metadata.Reproduced against the SQLite
filter_by_key_dictlogic, with two rows belonging to different "tenants" and the filter{"author_or_speaker": "alice' OR '1'='1"}:Fix
Bind the values as query parameters in the four value sinks named in the issue:
PGRetrieval.text_search_with_key_value_dict_filterPGRetrieval.filter_by_key_dictSQLiteRetrieval.text_search_with_key_value_dict_filterSQLiteRetrieval.filter_by_key_dictPostgres uses
%splaceholders and SQLite uses?, matching the parameter style already used elsewhere in this module (e.g.PGRetrieval.embedding_job_cursor, andSQLiteRetrieval.text_search_with_key_value_dict_filterwhich already carried an unusedinsert_array = ()). On Postgres the parameter tuple is only passed when it is non-empty, so a filter-less query still goes through the same single-argumentexecute()path as before and no%in the tsquery string gets reinterpreted.The mongo-style
{"$in": [...]}ranges are bound too. That also fixes a latent bug in those branches: list members were emitted throughstr(entry)with no quoting, so a list of strings producedIN (alice,bob)and raisedsqlite3.OperationalError: no such column: alice. With placeholders it returns the expected rows.After the change, same repro:
Scope is limited to the value sinks reported in #1304 — key/table-name handling and the
_prep_queryfull-text search string are untouched.🤖 Generated with Claude Code