Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(vdb-oceanbase): reject trailing newlines in metadata-key and document-id validators (#39884) (#39901)

Harsh Kashyap8dbac966218e
diff --git a/api/providers/vdb/vdb-oceanbase/src/dify_vdb_oceanbase/oceanbase_vector.py b/api/providers/vdb/vdb-oceanbase/src/dify_vdb_oceanbase/oceanbase_vector.py
index 93be92a62f2..0cd445f857e 100644
--- a/api/providers/vdb/vdb-oceanbase/src/dify_vdb_oceanbase/oceanbase_vector.py
+++ b/api/providers/vdb/vdb-oceanbase/src/dify_vdb_oceanbase/oceanbase_vector.py
@@ -321,8 +321,13 @@ class OceanBaseVector(BaseVector):
from sqlalchemy import text
- # Validate key to prevent injection in JSON path
- if not re.match(r"^[a-zA-Z0-9_.]+$", key):
+ # Validate key to prevent injection in JSON path.
+ # Use re.fullmatch instead of re.match to reject trailing newlines.
+ # Python's '$' matches at end-of-string OR just before a trailing
+ # newline, so re.match accepts "user_id\n". re.fullmatch requires
+ # the whole string to match.
+ # Regression for #39884 (sibling of #39234 / #39548 / #39666 / #39730 / #39880).
+ if not re.fullmatch(r"[a-zA-Z0-9_.]+", key):
raise ValueError(f"Invalid characters in metadata key: {key}")
# Use parameterized query to prevent SQL injection
@@ -454,11 +459,14 @@ class OceanBaseVector(BaseVector):
_where_clause = None
if document_ids_filter:
# Validate document IDs to prevent SQL injection
- # Document IDs should be alphanumeric with hyphens and underscores
+ # Document IDs should be alphanumeric with hyphens and underscores.
+ # Use re.fullmatch instead of re.match to reject trailing newlines.
+ # See the metadata-key validator above for the rationale.
+ # Regression for #39884 (sibling of #39234 / #39548 / #39666 / #39730 / #39880).
import re
for doc_id in document_ids_filter:
- if not isinstance(doc_id, str) or not re.match(r"^[a-zA-Z0-9_-]+$", doc_id):
+ if not isinstance(doc_id, str) or not re.fullmatch(r"[a-zA-Z0-9_-]+", doc_id):
raise ValueError(f"Invalid document ID format: {doc_id}")
# Safe to use in query after validation
diff --git a/api/providers/vdb/vdb-oceanbase/tests/unit_tests/test_oceanbase_vector.py b/api/providers/vdb/vdb-oceanbase/tests/unit_tests/test_oceanbase_vector.py
index 36393cc486d..c5ca9d0a873 100644
--- a/api/providers/vdb/vdb-oceanbase/tests/unit_tests/test_oceanbase_vector.py
+++ b/api/providers/vdb/vdb-oceanbase/tests/unit_tests/test_oceanbase_vector.py
@@ -551,3 +551,54 @@ def test_oceanbase_factory_uses_existing_or_generated_collection(oceanbase_modul
assert vector_cls.call_args_list[0].args[0] == "existing_collection"
assert vector_cls.call_args_list[1].args[0] == "auto_collection"
assert dataset_without_index.index_struct is not None
+
+
+ "bad_doc_id",
+ [
+ "doc-123\n", # trailing LF -- the bug the #39884 fix closes
+ "doc-123\r", # trailing CR
+ "doc-123\r\n", # trailing CRLF
+ ],
+)
+def test_search_by_vector_rejects_document_id_with_trailing_newline(oceanbase_module, bad_doc_id):
+ """Regression for #39884 (sibling of #39234 / #39548 / #39666 / #39730 / #39880).
+
+ The old `re.match(r"^[a-zA-Z0-9_-]+$", doc_id)` accepted a doc_id ending in
+ `\n` because Python's `$` matches just before a trailing newline. The fix
+ uses `re.fullmatch`, which requires the whole string to match. The
+ doc_ids are joined into a SQL `IN` clause so a trailing newline would
+ land in the SQL fragment.
+ """
+ vector = oceanbase_module.OceanBaseVector.__new__(oceanbase_module.OceanBaseVector)
+ vector._collection_name = "collection_1"
+ vector._hnsw_ef_search = -1
+ vector._config = SimpleNamespace(metric_type="cosine")
+ vector._client = MagicMock()
+
+ with pytest.raises(ValueError, match="Invalid document ID format"):
+ vector.search_by_vector([0.1, 0.2], document_ids_filter=[bad_doc_id])
+
+
+ "bad_key",
+ [
+ "user_id\n", # trailing LF -- the bug the #39884 fix closes
+ "user_id\r", # trailing CR
+ "user_id\r\n", # trailing CRLF
+ ],
+)
+def test_get_ids_by_metadata_field_rejects_key_with_trailing_newline(oceanbase_module, bad_key):
+ """Regression for #39884 (sibling of #39234 / #39548 / #39666 / #39730 / #39880).
+
+ The old `re.match(r"^[a-zA-Z0-9_.]+$", key)` accepted a key ending in `\n`.
+ The key is interpolated into a SQL JSON-path expression
+ (`WHERE metadata->>'$.{key}' = :value`), so a trailing newline would
+ land in the SQL fragment.
+ """
+ vector = oceanbase_module.OceanBaseVector.__new__(oceanbase_module.OceanBaseVector)
+ vector._collection_name = "collection_1"
+ vector._client = MagicMock()
+
+ with pytest.raises(Exception, match="Failed to query documents by metadata field"):
+ vector.get_ids_by_metadata_field(bad_key, "doc-1")