From 586af47fbcae0cb86efeb0e1acf26c739b9eb391 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 14 Jul 2026 05:15:48 +0000 Subject: [PATCH] Fix SQL injection in pgvector table names Co-authored-by: Himan-D <262577684+Himan-D@users.noreply.github.com> --- .jules/sentinel.md | 4 ++++ internal/memory/pgvector/client.go | 6 ++++++ 2 files changed, 10 insertions(+) create mode 100644 .jules/sentinel.md diff --git a/.jules/sentinel.md b/.jules/sentinel.md new file mode 100644 index 00000000..1dfc1592 --- /dev/null +++ b/.jules/sentinel.md @@ -0,0 +1,4 @@ +## 2025-03-09 - SQL Injection in Dynamic Table Names +**Vulnerability:** The pgvector client was constructing SQL queries by directly substituting the configured table name via `fmt.Sprintf` without prior validation. Because table names cannot be parameterized in SQL, this allows SQL injection if the table name originates from an untrusted configuration source. +**Learning:** Even internal configuration values used as SQL identifiers (like table names) must be strictly validated against an allowed character set, especially when dynamic queries are built using `fmt.Sprintf`. +**Prevention:** Always validate SQL identifiers using strict regular expressions (e.g., allowing only alphanumeric characters, underscores, and periods for schema-qualified names) before embedding them into SQL query strings. \ No newline at end of file diff --git a/internal/memory/pgvector/client.go b/internal/memory/pgvector/client.go index 48f9bdd2..649af12e 100644 --- a/internal/memory/pgvector/client.go +++ b/internal/memory/pgvector/client.go @@ -5,6 +5,7 @@ import ( "database/sql" "encoding/json" "fmt" + "regexp" "strings" "time" @@ -26,6 +27,11 @@ func NewClient(cfg config.PgvectorConfig) (*Client, error) { return nil, fmt.Errorf("pgvector: PGVECTOR_URL is required") } + validTable := regexp.MustCompile(`^[a-zA-Z0-9_.]+$`) + if !validTable.MatchString(cfg.Table) { + return nil, fmt.Errorf("pgvector: invalid table name") + } + db, err := sql.Open("postgres", cfg.URL) if err != nil { return nil, fmt.Errorf("pgvector: open db: %w", err)