fix(milvus,baidu): sanitize filter values to prevent expression injection (#5746)

This commit is contained in:
Hrushikesh Yadav
2026-06-23 16:51:05 +05:30
committed by GitHub
parent c0ac9f81fa
commit 565db27121
4 changed files with 85 additions and 4 deletions
+13 -2
View File
@@ -1,4 +1,5 @@
import logging
import re
import time
from typing import Dict, Optional
@@ -47,6 +48,8 @@ class OutputData(BaseModel):
class BaiduDB(VectorStoreBase):
_SAFE_FILTER_KEY = re.compile(r"^[a-zA-Z_][a-zA-Z0-9_]*$")
def __init__(
self,
endpoint: str,
@@ -404,8 +407,16 @@ class BaiduDB(VectorStoreBase):
"""
conditions = []
for key, value in filters.items():
if not self._SAFE_FILTER_KEY.match(key):
raise ValueError(f"Invalid filter key: {key!r}")
if isinstance(value, str):
conditions.append(f'metadata["{key}"] = "{value}"')
else:
escaped = value.replace("\\", "\\\\").replace('"', '\\"')
conditions.append(f'metadata["{key}"] = "{escaped}"')
elif isinstance(value, (int, float, bool)):
conditions.append(f'metadata["{key}"] = {value}')
else:
raise ValueError(
f"Filter value for {key!r} must be str, int, float, or bool, "
f"got {type(value).__name__}"
)
return " AND ".join(conditions)