Injection via non-String metadata filter values in the pgvector, MariaDB, Milvus, MongoDB Atlas and Chroma embedding stores
Description
Several embedding stores built the query for a metadata Filter by writing the filter values into the query text. A value that was not a String was written with toString(), without escaping. The filter API accepts any object as a value: new IsEqualTo(String, Object), new IsIn(String, Collection<?>) and MetadataFilterBuilder.isIn(Collection<?>). So an application that passes loosely typed data into a filter lets that data change the query. Examples of such data are a List or Map parsed from request JSON, or from LLM tool output.
Details
- langchain4j-pgvector: in
isIn/isNotIn, a non-String value was written as'+value.toString()+', without escaping. A nested list such asisIn(List.of(List.of("x') or 1=1 --")))closes the literal. Scalar comparisons were not affected (the query failed). Before 1.11.0,isIn/isNotIndid not escapeStringvalues either, so a plainStringsuch asx') or 1=1 --closed the literal too. This affects all versions before 1.11.0, including the 1.5.x line up to 1.5.3-beta11. - langchain4j-mariadb: a value that was not a
String,UUIDorCollectionwas written withtoString(), without quotes. Examples are aMapwhose text starts with a JDBC escape ({fn ...}), or aStringBuilder. This applies to comparison filters and toisIn/isNotIn. - langchain4j-milvus and langchain4j-milvus-v2: non-String values were written into the filter expression with
toString(), without quotes. - langchain4j-mongodb-atlas and langchain4j-chroma:
IsEqualTowas written in the short form{key: value}, so aMapvalue such as{"$ne": "x"}was read as a query operator.
The affected operations are search (including pgvector hybrid search) and removeAll(Filter).
These are not affected:
- Applications that build filters only from
String, number,UUIDorBooleanvalues, for example with the typedMetadataFilterBuildermethods. Exception:langchain4j-pgvectorbefore 1.11.0, see above. - Filters built by
LanguageModelSqlFilterBuilder: it only producesString,LongandDoublevalues.
Impact
For an application that passes untrusted, loosely typed values into a metadata filter:
- Reading other tenants' or users' rows through
search. An injectedoroverrides the conditions the application added withand(...). - Deleting all rows through
removeAll(Filter).
Patches
Fixed in these versions (langchain4j-milvus-v2 exists only since 1.19.0-beta29):
1.23.0-beta331.19.4-beta291.11.12-beta191.5.4-beta11
The fix is the same in all of them:
- pgvector and MariaDB bind filter values as JDBC parameters.
- Milvus and Milvus v2 accept only supported value types.
- MongoDB Atlas and Chroma write equality with an explicit
$eq.
In addition, langchain4j-core 1.23.0 checks filter values when a filter is created. This check is not in the backports, because it is a breaking change. It accepts only:
String,UUIDandBoolean- numbers:
Integer,Long,Float,Double,Byte,Short,BigIntegerandBigDecimal - enums
- date/time values
This also protects other embedding stores, including stores outside this repository.
Workarounds
- Pass only
String, number,UUIDorBooleanvalues into metadata filters. Onlangchain4j-pgvectorbefore 1.11.0 this is not enough, becauseStringvalues inisIn/isNotInare not escaped there either: upgrade. - Do not pass collections, maps,
JsonNodes or other objects from request data or LLM output into a filter without converting each value first.
References
- Fix: github.com/
langchain4j/ langchain4j/ pull/ 6895 and github.com/ langchain4j/ langchain4j/ pull/ 6896 - GHSA-2mfg-cc43-9pcj: an earlier, related issue with filter keys
Affected versions
- dev.langchain4j:langchain4j-pgvectorfixed in 1.5.4-beta11
- dev.langchain4j:langchain4j-pgvectorFrom 1.6.0-beta12 · fixed in 1.11.12-beta19
- dev.langchain4j:langchain4j-pgvectorFrom 1.12.1-beta21 · fixed in 1.19.4-beta29
- dev.langchain4j:langchain4j-pgvectorFrom 1.20.0-beta30 · fixed in 1.23.0-beta33
- dev.langchain4j:langchain4j-mariadbfixed in 1.5.4-beta11
- dev.langchain4j:langchain4j-mariadbFrom 1.6.0-beta12 · fixed in 1.11.12-beta19
- dev.langchain4j:langchain4j-mariadbFrom 1.12.1-beta21 · fixed in 1.19.4-beta29
- dev.langchain4j:langchain4j-mariadbFrom 1.20.0-beta30 · fixed in 1.23.0-beta33
- dev.langchain4j:langchain4j-milvusfixed in 1.5.4-beta11
- dev.langchain4j:langchain4j-milvusFrom 1.6.0-beta12 · fixed in 1.11.12-beta19
- dev.langchain4j:langchain4j-milvusFrom 1.12.1-beta21 · fixed in 1.19.4-beta29
- dev.langchain4j:langchain4j-milvusFrom 1.20.0-beta30 · fixed in 1.23.0-beta33
- dev.langchain4j:langchain4j-mongodb-atlasfixed in 1.5.4-beta11
- dev.langchain4j:langchain4j-mongodb-atlasFrom 1.6.0-beta12 · fixed in 1.11.12-beta19
- dev.langchain4j:langchain4j-mongodb-atlasFrom 1.12.1-beta21 · fixed in 1.19.4-beta29
- dev.langchain4j:langchain4j-mongodb-atlasFrom 1.20.0-beta30 · fixed in 1.23.0-beta33
- dev.langchain4j:langchain4j-chromafixed in 1.5.4-beta11
- dev.langchain4j:langchain4j-chromaFrom 1.6.0-beta12 · fixed in 1.11.12-beta19
- dev.langchain4j:langchain4j-chromaFrom 1.12.1-beta21 · fixed in 1.19.4-beta29
- dev.langchain4j:langchain4j-chromaFrom 1.20.0-beta30 · fixed in 1.23.0-beta33
- dev.langchain4j:langchain4j-milvus-v2fixed in 1.19.4-beta29
- dev.langchain4j:langchain4j-milvus-v2From 1.20.0-beta30 · fixed in 1.23.0-beta33