GitHub advisory · PyPI
Flexible GraphRAG: Cypher/Graph Injection via broken string-escaping in FalkorDB adapter (_escape()) - full graph wipe from ingested-document content
Description
Description def _escape(v: str) -> str: return str(v).replace("'", "\'").replace("\", "\\") The escaping order is backwards, exactly like the classic HTML double-escape bug: step 1 inserts a literal backslash in front of every ', then step 2 re-matches that just-inserted backslash and doubles it. For an input ending in a single quote, the two steps combined produce \' (two backslashes followed by the quote) in the output — which, under the standard backslash-escape convention used by Cypher/openCypher single-quoted string literals, is parsed as one escaped backslash followed by an unescaped, string-terminating quote. The quote is not neutralized; the string breaks open.
This escaped value is interpolated directly into Cypher query strings built by string formatting (not parameters) at 5 call sites in the same method:
self.lc_graph.query(
f"MERGE (n:{node.type} {{id: '{_escape(node.id)}'}}) "
f"{props}"
f"RETURN distinct 'done' AS result"
)
...
self.lc_graph.query(
f"MATCH (a:{rel.source.type} {{id: '{_escape(rel.source.id)}'}}), "
f"(b:{rel.target.type} {{id: '{_escape(rel.target.id)}'}}) "
f"MERGE (a)-[r:{rel_type}]->(b) "
f"{props}"
f"RETURN distinct 'done' AS result"
)
node.type, rel.source.type, rel.target.type and rel_type are also interpolated into backtick-quoted identifiers with no escaping at all.
Where the attacker-controlled data comes from: node/rel objects are LLM-extracted entities and relationships derived from ingested document content (the whole point of this GraphRAG pipeline). Document content is therefore untrusted input relative to this database layer — a classic indirect-prompt-injection-to-query-injection chain: craft a document whose text causes the extraction LLM to produce an entity id containing the payload below, ingest it, and the broken escape function turns it into live Cypher.
Impact Full Cypher clause injection into lc_graph.query() calls that run with no query…
Manual fix · PyPI
pip install --upgrade "flexible-graphrag>=0.8.2"Sources