fix: use parameterized queries to prevent SQL injection in vector stores#33421
fix: use parameterized queries to prevent SQL injection in vector stores#33421mango766 wants to merge 2 commits intolanggenius:mainfrom
Conversation
… and relyt vector stores Replace f-string interpolation of user-controlled values (key, value, id) in SQL queries with SQLAlchemy text() bind parameters to prevent SQL injection attacks. Affected methods: - pgvecto_rs.py: get_ids_by_metadata_field(), text_exists() - relyt_vector.py: get_ids_by_metadata_field(), delete_by_ids(), text_exists() Co-Authored-By: Claude (claude-opus-4-6) <noreply@anthropic.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the security of database interactions within the vector store modules by migrating from f-string based SQL interpolation to SQLAlchemy's Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request is a great initiative to enhance security by replacing f-string based SQL query construction with parameterized queries, effectively mitigating SQL injection risks. The changes in delete_by_ids and text_exists are well-implemented. However, I've found a recurring critical syntax error in the get_ids_by_metadata_field method in both modified files, which will cause the queries to fail. Please see the detailed comments for the suggested fixes.
| select_statement = sql_text(f"SELECT id FROM {self._collection_name} WHERE meta->>'{key}' = '{value}'; ") | ||
| result = session.execute(select_statement).fetchall() | ||
| select_statement = sql_text( | ||
| f"SELECT id FROM {self._collection_name} WHERE meta->>:key = :value" |
There was a problem hiding this comment.
The SQL syntax meta->>:key is incorrect. The JSON operator ->> requires a space before its operand. This will cause a syntax error when the query is executed. It should be meta ->> :key.
| f"SELECT id FROM {self._collection_name} WHERE meta->>:key = :value" | |
| f"SELECT id FROM {self._collection_name} WHERE meta ->> :key = :value" |
| with Session(self.client) as session: | ||
| select_statement = sql_text( | ||
| f"""SELECT id FROM "{self._collection_name}" WHERE metadata->>'{key}' = '{value}'; """ | ||
| f"""SELECT id FROM "{self._collection_name}" WHERE metadata->>:key = :value""" |
There was a problem hiding this comment.
The SQL syntax metadata->>:key is incorrect. The JSON operator ->> requires a space before its operand. This will cause a syntax error when the query is executed. It should be metadata ->> :key.
| f"""SELECT id FROM "{self._collection_name}" WHERE metadata->>:key = :value""" | |
| f"""SELECT id FROM \"{self._collection_name}\"" WHERE metadata ->> :key = :value""" |
Pyrefly Diffbase → PR--- /tmp/pyrefly_base.txt 2026-03-13 15:44:23.624398760 +0000
+++ /tmp/pyrefly_pr.txt 2026-03-13 15:44:14.881314807 +0000
@@ -5359,25 +5359,25 @@
ERROR `in` is not supported between `Literal['is not archived']` and `None` [not-iterable]
--> tests/unit_tests/services/retention/workflow_run/test_delete_archived_workflow_run.py:91:20
ERROR Argument `() -> Mock` is not assignable to parameter `session_maker` with type `sessionmaker[Unknown]` in function `services.retention.workflow_run.restore_archived_workflow_run.WorkflowRunRestore._restore_from_run` [bad-argument-type]
- --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:612:67
+ --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:522:67
ERROR `in` is not supported between `Literal['Storage not configured']` and `None` [not-iterable]
- --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:615:16
+ --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:525:16
ERROR Argument `() -> Mock` is not assignable to parameter `session_maker` with type `sessionmaker[Unknown]` in function `services.retention.workflow_run.restore_archived_workflow_run.WorkflowRunRestore._restore_from_run` [bad-argument-type]
- --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:629:67
+ --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:539:67
ERROR `in` is not supported between `Literal['Archive bundle not found']` and `None` [not-iterable]
- --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:632:16
+ --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:542:16
ERROR Argument `() -> Mock` is not assignable to parameter `session_maker` with type `sessionmaker[Unknown]` in function `services.retention.workflow_run.restore_archived_workflow_run.WorkflowRunRestore._restore_from_run` [bad-argument-type]
- --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:652:63
+ --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:562:63
ERROR Argument `() -> Mock` is not assignable to parameter `session_maker` with type `sessionmaker[Unknown]` in function `services.retention.workflow_run.restore_archived_workflow_run.WorkflowRunRestore._restore_from_run` [bad-argument-type]
- --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:708:71
+ --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:618:71
ERROR Argument `() -> Mock` is not assignable to parameter `session_maker` with type `sessionmaker[Unknown]` in function `services.retention.workflow_run.restore_archived_workflow_run.WorkflowRunRestore._restore_from_run` [bad-argument-type]
- --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:734:67
+ --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:644:67
ERROR `in` is not supported between `Literal['File is not a zip file']` and `None` [not-iterable]
- --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:738:16
+ --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:648:16
ERROR Argument `() -> Mock` is not assignable to parameter `session_maker` with type `sessionmaker[Unknown]` in function `services.retention.workflow_run.restore_archived_workflow_run.WorkflowRunRestore._restore_from_run` [bad-argument-type]
- --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:758:71
+ --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:668:71
ERROR `in` is not supported between `Literal['not found']` and `None` [not-iterable]
- --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:905:16
+ --> tests/unit_tests/services/retention/workflow_run/test_restore_archived_workflow_run.py:815:16
ERROR Argument `None` is not assignable to parameter `name` with type `str` in function `_make_extension` [bad-argument-type]
--> tests/unit_tests/services/test_api_based_extension_service.py:230:46
ERROR Argument `None` is not assignable to parameter `api_endpoint` with type `str` in function `_make_extension` [bad-argument-type]
|
Pyrefly DiffNo changes detected. |
Summary
text()bind parameters inpgvecto_rs.pyandrelyt_vector.pyto prevent SQL injectionget_ids_by_metadata_field()andtext_exists()in both files, plusdelete_by_ids()in relytkey,value, andidparameters are now safely parameterizedChanges
pgvecto_rs.py: Parameterizedkeyandvalueinget_ids_by_metadata_field(); parameterizedidintext_exists()relyt_vector.py: Parameterizedkeyandvalueinget_ids_by_metadata_field(); replaced manual string-joinedids_strwithANY(:doc_ids)indelete_by_ids(); parameterizedidintext_exists()Closes #33420