fix(polardb): escape user inputs in get_children_with_embeddings to prevent Cypher/SQL injection (CWE-89)#1638
Open
sebastiondev wants to merge 1 commit intoMemTensor:mainfrom
Open
Conversation
…pher injection (CWE-89)
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.
Summary
Fixes a Cypher/SQL injection vulnerability (CWE-89) in
PolarDBGraphDB.get_children_with_embeddingsinsrc/memos/graph_dbs/polardb.py. Theidanduser_nameparameters were interpolated directly into a Cypher query string via f-strings without escaping, allowing an attacker who controls either value to break out of the quoted literal and inject arbitrary Cypher.Vulnerable code (before)
A value such as
' OR '1'='1supplied asidoruser_namecloses the literal and changes the query semantics. In a multi-tenant deployment this would allow a user to read children of memories belonging to other users (cross-tenant data exposure), or to alter the query in other ways depending on what subsequent SQL the wrapper executes.Fix
Apply the existing module-level
escape_sql_string()helper (single-quote doubling) to both interpolated values before they are placed into the query. This matches the pattern already used in 30+ other call sites in the same file.Diff is 4 lines added, 2 removed — minimal and consistent with existing project conventions.
Why this is exploitable
get_children_with_embeddingsis part of theBaseGraphDBinterface and is invoked by higher-level memory APIs whereid(a memory identifier) anduser_name(a tenant identifier) may be derived from user-influenced input — particularly in multi-tenant setups.escape_sql_stringfor the same kinds of values, which both confirms the project considers this the right mitigation and means this function was an inconsistency / oversight.Adversarial review
Before submitting, I tried to disprove this finding. I checked whether: (a) the input values are constrained by an upstream schema or sanitizer (they aren't — they flow through as plain strings), (b) the AGE/Cypher layer would itself reject the injected payload (it doesn't — quote-breakout payloads parse as valid Cypher), and (c) other PolarDB methods already protect themselves so the fix would be redundant (they do, but this specific method does not, which is the entire problem). I also considered that
escape_sql_stringonly handles'and not backslash escapes, so it isn't full defense in depth — parameterized queries would be stronger — but it does close the trivially exploitable single-quote breakout, which is the realistic attack, and keeps the change consistent with the rest of the file. A follow-up to move the whole module to parameterized AGE queries would be welcome but is out of scope here.Testing
escape_sql_stringis defined at module scope (line 96) and is the same helper used elsewhere inpolardb.py.id="x' OR '1'='1") and confirmed the escaped form produces'x'' OR ''1''=''1', which AGE parses as a literal string and not as injected Cypher.References
src/memos/graph_dbs/polardb.py, functionget_children_with_embeddings(around line 1171)cc @lewiswigmore