Skip to content

fix: use parameterized queries to prevent SQL injection in vector stores#33421

Open
mango766 wants to merge 2 commits intolanggenius:mainfrom
mango766:fix/sql-injection-vector-stores
Open

fix: use parameterized queries to prevent SQL injection in vector stores#33421
mango766 wants to merge 2 commits intolanggenius:mainfrom
mango766:fix/sql-injection-vector-stores

Conversation

@mango766
Copy link

Summary

  • Replace f-string SQL interpolation with SQLAlchemy text() bind parameters in pgvecto_rs.py and relyt_vector.py to prevent SQL injection
  • Fix get_ids_by_metadata_field() and text_exists() in both files, plus delete_by_ids() in relyt
  • User-controlled key, value, and id parameters are now safely parameterized

Changes

  • pgvecto_rs.py: Parameterized key and value in get_ids_by_metadata_field(); parameterized id in text_exists()
  • relyt_vector.py: Parameterized key and value in get_ids_by_metadata_field(); replaced manual string-joined ids_str with ANY(:doc_ids) in delete_by_ids(); parameterized id in text_exists()

Closes #33420

… 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>
@mango766 mango766 requested a review from JohnJyong as a code owner March 13, 2026 14:33
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 text() bind parameters. This change directly addresses potential SQL injection vulnerabilities, ensuring that user-controlled input is safely processed when querying or modifying data in pgvecto_rs and relyt_vector vector stores.

Highlights

  • SQL Injection Prevention: Implemented parameterized queries using SQLAlchemy's text() bind parameters across pgvecto_rs.py and relyt_vector.py to prevent SQL injection vulnerabilities.
  • Method Corrections: Corrected the implementation of get_ids_by_metadata_field() and text_exists() in both pgvecto_rs.py and relyt_vector.py to utilize safe parameterization.
  • Deletion Method Enhancement: Fixed the delete_by_ids() method in relyt_vector.py to use parameterized queries, specifically ANY(:doc_ids), for secure handling of multiple IDs.
  • Parameter Safety: Ensured that user-controlled key, value, and id parameters are now safely handled through bind parameters.

🧠 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
  • api/core/rag/datasource/vdb/pgvecto_rs/pgvecto_rs.py
    • Parameterized key and value in the get_ids_by_metadata_field() method.
    • Parameterized id in the text_exists() method.
  • api/core/rag/datasource/vdb/relyt/relyt_vector.py
    • Parameterized key and value in the get_ids_by_metadata_field() method.
    • Replaced manual string concatenation for ids_str with ANY(:doc_ids) for safe parameterization in delete_by_ids().
    • Parameterized id in the text_exists() method.
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

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.

Suggested change
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"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

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.

Suggested change
f"""SELECT id FROM "{self._collection_name}" WHERE metadata->>:key = :value"""
f"""SELECT id FROM \"{self._collection_name}\"" WHERE metadata ->> :key = :value"""

@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 13, 2026
@github-actions
Copy link
Contributor

Pyrefly Diff

base → 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]

@github-actions
Copy link
Contributor

Pyrefly Diff

No changes detected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL injection in pgvecto_rs and relyt vector store queries

1 participant