RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(api): validate memory search numeric filters (#19883)
Harsh Kashyap46737b195bfb
diff --git a/api/apps/restful_apis/memory_api.py b/api/apps/restful_apis/memory_api.pyindex 56e761b304..6ed86b34d4 100644--- a/api/apps/restful_apis/memory_api.py+++ b/api/apps/restful_apis/memory_api.py@@ -294,9 +294,13 @@ async def search_message():if len(memory_ids) == 1 and "," in memory_ids[0]:memory_ids = memory_ids[0].split(",")query = args.get("query")- similarity_threshold = float(args.get("similarity_threshold", 0.2))- keywords_similarity_weight = float(args.get("keywords_similarity_weight", 0.7))try:+ similarity_threshold = float(args.get("similarity_threshold", 0.2))+ keywords_similarity_weight = float(args.get("keywords_similarity_weight", 0.7))+ if not 0 <= similarity_threshold <= 1:+ raise ValueError("similarity_threshold must be between 0 and 1")+ if not 0 <= keywords_similarity_weight <= 1:+ raise ValueError("keywords_similarity_weight must be between 0 and 1")validate_rest_api_ids(memory_ids, "memory_id")top_n = validate_rest_api_page_size(int(args.get("top_n", 5)))except ValueError as exc:diff --git a/test/unit_test/api/apps/restful_apis/test_memory_message_routes_unit.py b/test/unit_test/api/apps/restful_apis/test_memory_message_routes_unit.pynew file mode 100644index 0000000000..a22bb9adb7--- /dev/null+++ b/test/unit_test/api/apps/restful_apis/test_memory_message_routes_unit.py@@ -0,0 +1,231 @@+#+# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+#+# Licensed under the Apache License, Version 2.0 (the "License");+# you may not use this file except in compliance with the License.+# You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.+#++import asyncio+import importlib.util+import inspect+import sys+from copy import deepcopy+from pathlib import Path+from types import ModuleType, SimpleNamespace++import pytest+++class _DummyManager:+ def route(self, *_args, **_kwargs):+ def decorator(func):+ return func++ return decorator+++class _AwaitableValue:+ def __init__(self, value):+ self._value = value++ def __await__(self):+ async def _co():+ return self._value++ return _co().__await__()+++class _DummyArgs(dict):+ def getlist(self, key):+ value = self.get(key)+ if value is None:+ return []+ if isinstance(value, list):+ return value+ return [value]+++class _DummyMemoryApiService:+ async def add_message(self, *_args, **_kwargs):+ return True, "ok"++ async def get_messages(self, *_args, **_kwargs):+ return []++ async def search_message(self, *_args, **_kwargs):+ return []+++def _run(coro):+ return asyncio.run(coro)+++def _load_memory_routes_module(monkeypatch):+ repo_root = Path(__file__).resolve().parents[5]++ common_pkg = ModuleType("common")+ common_pkg.__path__ = [str(repo_root / "common")]+ monkeypatch.setitem(sys.modules, "common", common_pkg)+ constants_mod = ModuleType("common.constants")+ constants_mod.RetCode = SimpleNamespace(SUCCESS=0, ARGUMENT_ERROR=101, SERVER_ERROR=100, NOT_FOUND=104)+ monkeypatch.setitem(sys.modules, "common.constants", constants_mod)+ exceptions_mod = ModuleType("common.exceptions")+ exceptions_mod.ArgumentException = type("ArgumentException", (Exception,), {})+ exceptions_mod.NotFoundException = type("NotFoundException", (Exception,), {})+ monkeypatch.setitem(sys.modules, "common.exceptions", exceptions_mod)++ api_utils_mod = ModuleType("api.utils.api_utils")+ api_utils_mod.validate_request = lambda *_a, **_k: lambda f: f+ api_utils_mod.get_request_json = lambda: _AwaitableValue({})+ api_utils_mod.get_error_argument_result = lambda message: {"code": 101, "message": message, "data": None}+ api_utils_mod.get_json_result = lambda **kw: {"code": kw.get("code", 0), "message": kw.get("message", ""), "data": kw.get("data")}+ monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)+ pagination_mod = ModuleType("api.utils.pagination_utils")+ pagination_mod.DEFAULT_PAGE = 1+ pagination_mod.DEFAULT_PAGE_SIZE = 30+ pagination_mod.validate_rest_api_ids = lambda ids, _name: ids+ pagination_mod.validate_rest_api_page = lambda page: int(page)+ pagination_mod.validate_rest_api_page_size = lambda size: int(size) if 1 <= int(size) <= 100 else (_ for _ in ()).throw(ValueError("page_size must be less than or equal to 100"))+ monkeypatch.setitem(sys.modules, "api.utils.pagination_utils", pagination_mod)+ tenant_mod = ModuleType("api.db.joint_services.tenant_model_service")+ tenant_mod.ensure_tenant_model_ids_for_params = lambda *_a, **_k: None+ monkeypatch.setitem(sys.modules, "api.db.joint_services.tenant_model_service", tenant_mod)++ apps_mod = ModuleType("api.apps")+ apps_mod.__path__ = [str(repo_root / "api" / "apps")]+ apps_mod.AUTH_API = "API"+ apps_mod.current_user = SimpleNamespace(id="user-1")+ apps_mod.login_required = lambda func: func+ monkeypatch.setitem(sys.modules, "api.apps", apps_mod)++ services_mod = ModuleType("api.apps.services")+ services_mod.memory_api_service = _DummyMemoryApiService()+ monkeypatch.setitem(sys.modules, "api.apps.services", services_mod)++ module_name = "test_message_routes_unit_module"+ module_path = repo_root / "api" / "apps" / "restful_apis" / "memory_api.py"+ spec = importlib.util.spec_from_file_location(module_name, module_path)+ module = importlib.util.module_from_spec(spec)+ module.manager = _DummyManager()+ monkeypatch.setitem(sys.modules, module_name, module)+ spec.loader.exec_module(module)+ return module+++def _set_request_json(monkeypatch, module, payload):+ monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue(deepcopy(payload)))+++def test_add_message_partial_failure_branch(monkeypatch):+ module = _load_memory_routes_module(monkeypatch)++ _set_request_json(+ monkeypatch,+ module,+ {+ "memory_id": ["memory-1"],+ "agent_id": "agent-1",+ "session_id": "session-1",+ "user_input": "hello",+ "agent_response": "world",+ },+ )++ async def _add_message(_memory_ids, _message_dict):+ return False, "cannot enqueue"++ monkeypatch.setattr(module.memory_api_service, "add_message", _add_message)++ res = _run(inspect.unwrap(module.add_message)())+ assert res["code"] == module.RetCode.SERVER_ERROR, res+ assert "Some messages failed to add" in res["message"], res+++def test_get_messages_csv_and_missing_memory_ids(monkeypatch):+ module = _load_memory_routes_module(monkeypatch)++ monkeypatch.setattr(module, "request", SimpleNamespace(args=_DummyArgs({})))+ res = _run(inspect.unwrap(module.get_messages)())+ assert res["code"] == module.RetCode.ARGUMENT_ERROR, res+ assert "memory_ids is required." in res["message"], res++ monkeypatch.setattr(+ module,+ "request",+ SimpleNamespace(args=_DummyArgs({"memory_id": "m1,m2", "agent_id": "a1", "session_id": "s1", "limit": "5"})),+ )++ async def _get_messages(memory_ids, agent_id, session_id, limit):+ assert memory_ids == ["m1", "m2"]+ assert agent_id == "a1"+ assert session_id == "s1"+ assert limit == 5+ return [{"message_id": 1}]++ monkeypatch.setattr(module.memory_api_service, "get_messages", _get_messages)+ res = _run(inspect.unwrap(module.get_messages)())+ assert res["code"] == module.RetCode.SUCCESS, res+ assert isinstance(res["data"], list), res+++def test_get_messages_rejects_limit_above_rest_api_max(monkeypatch):+ module = _load_memory_routes_module(monkeypatch)+ monkeypatch.setattr(+ module,+ "request",+ SimpleNamespace(args=_DummyArgs({"memory_id": "m1", "limit": "1000"})),+ )+ res = _run(inspect.unwrap(module.get_messages)())+ assert res["code"] == module.RetCode.ARGUMENT_ERROR, res+ assert "page_size must be less than or equal to 100" in res["message"], res+++def test_search_message_rejects_top_n_above_rest_api_max(monkeypatch):+ module = _load_memory_routes_module(monkeypatch)+ monkeypatch.setattr(+ module,+ "request",+ SimpleNamespace(args=_DummyArgs({"memory_id": "m1", "query": "hello", "top_n": "500"})),+ )++ async def _search_message(_filter_dict, _params):+ return []++ monkeypatch.setattr(module.memory_api_service, "search_message", _search_message)+ res = _run(inspect.unwrap(module.search_message)())+ assert res["code"] == module.RetCode.ARGUMENT_ERROR, res+ assert "page_size must be less than or equal to 100" in res["message"], res++[email protected]("field", ["similarity_threshold", "keywords_similarity_weight"])+def test_search_message_rejects_non_numeric_weights(monkeypatch, field):+ module = _load_memory_routes_module(monkeypatch)+ monkeypatch.setattr(module, "request", SimpleNamespace(args=_DummyArgs({"memory_id": "m1", "query": "hello", field: "bad"})))+ res = _run(inspect.unwrap(module.search_message)())+ assert res["code"] == module.RetCode.ARGUMENT_ERROR, res+ assert "could not convert string to float" in res["message"], res++[email protected]("field", ["similarity_threshold", "keywords_similarity_weight"])[email protected]("value", ["-0.1", "1.1", "nan", "inf"])+def test_search_message_rejects_out_of_range_and_non_finite_weights(monkeypatch, field, value):+ module = _load_memory_routes_module(monkeypatch)+ monkeypatch.setattr(module, "request", SimpleNamespace(args=_DummyArgs({"memory_id": "m1", "query": "hello", field: value})))+ res = _run(inspect.unwrap(module.search_message)())+ assert res["code"] == module.RetCode.ARGUMENT_ERROR, res+ assert "must be between 0 and 1" in res["message"], res