Skip to content

fix: sql injection, prompt injection#4997

Merged
harshgupta-mem0 merged 6 commits intomainfrom
fix/security_vulnerabilities
Apr 28, 2026
Merged

fix: sql injection, prompt injection#4997
harshgupta-mem0 merged 6 commits intomainfrom
fix/security_vulnerabilities

Conversation

@harshgupta-mem0
Copy link
Copy Markdown
Contributor

@harshgupta-mem0 harshgupta-mem0 commented Apr 28, 2026

Description

Fixes two categories of security vulnerabilities identified in the mem0 codebase:

1. SQL/CQL Injection in Vector Store Adapters

Table and keyspace names were interpolated directly into SQL/CQL queries using f-strings, which allows arbitrary query execution if an attacker controls the configuration values.

pgvector.py: Replaced all bare f-string table name interpolations with psycopg.sql.Identifier / psycopg2.sql.Identifier, the driver-native safe quoting mechanism. Also fixed col_info which embedded the name as a string literal inside pg_total_relation_size().
azure_mysql.py / cassandra.py: Added a validate_identifier() function that rejects any name not matching ^[a-zA-Z][a-zA-Z0-9_]{0,127}$ at init time. Also fixed Cassandra's list_cols() which embedded self.keyspace as a single-quoted string literal in a CQL query.

2. Prompt Injection in LLM Reranker

llm_reranker.py was embedding both the user search query and memory document content into a single formatted string sent as a user message, allowing either input to override the scoring instructions. Fixed by splitting instructions into a role=system message and user data into a separate role=user message. Also added 4000-character truncation on both inputs to prevent prompt flooding.

Type of Change

Bug fix (non-breaking change that fixes an issue)

Verified that:

Passing a malicious collection name (e.g. "x; DROP TABLE users") to AzureMySQL or CassandraDB raises ValueError at init time before any query executes.
PGVector uses sql.Identifier which double-quotes the table name, preventing injection while preserving correct quoting.
The LLM reranker sends scoring instructions as role=system and user data as role=user.
Setting MEM0_POSTHOG_API_KEY=test_key overrides the PostHog key in all Python telemetry modules.

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have added tests that prove my fix/feature works
  • New and existing tests pass locally
  • I have updated documentation if needed

@harshgupta-mem0 harshgupta-mem0 self-assigned this Apr 28, 2026
@harshgupta-mem0 harshgupta-mem0 added the security Security vulnerability label Apr 28, 2026
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 28, 2026

CLA assistant check
All committers have signed the CLA.

@whysosaket
Copy link
Copy Markdown
Member

@harshgupta-mem0 could you update the PR title and description to drop the PostHog env-variable bullet? The diff doesn't include those telemetry files, so the title/description and the actual changes are out of sync.

@harshgupta-mem0 harshgupta-mem0 changed the title fix: sql injection, prompt injection and defined posthog env variable fix: sql injection, prompt injection Apr 28, 2026
@harshgupta-mem0
Copy link
Copy Markdown
Contributor Author

@whysosaket Done! Thanks for mentioning

@harshgupta-mem0 harshgupta-mem0 merged commit 1b95c99 into main Apr 28, 2026
15 checks passed
@kartik-mem0 kartik-mem0 deleted the fix/security_vulnerabilities branch April 29, 2026 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

security Security vulnerability

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants