Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

security: fix MCP engine validation fail-open and add collection_name validation (#3084)

LearningCircuitf20d166f4691
diff --git a/src/local_deep_research/mcp/server.py b/src/local_deep_research/mcp/server.py
index 6e43c1c681..7cd6f23c19 100644
--- a/src/local_deep_research/mcp/server.py
+++ b/src/local_deep_research/mcp/server.py
@@ -29,6 +29,7 @@ Usage:
ldr-mcp
"""
+import re
import sys
from typing import Any, Dict, Optional
@@ -84,6 +85,9 @@ class ValidationError(Exception):
pass
+_COLLECTION_NAME_RE = re.compile(r"^[A-Za-z0-9 _-]{1,100}$")
+
+
def _validate_query(query: str) -> str:
"""Validate and sanitize query parameter."""
if not query or not query.strip():
@@ -164,8 +168,10 @@ def _validate_search_engine(engine: Optional[str]) -> Optional[str]:
except ValidationError:
raise
except Exception:
- # If we can't load engine config, log but don't block
- logger.warning(f"Could not validate search engine '{engine}'")
+ logger.exception("Could not load engine config to validate engine")
+ raise ValidationError(
+ f"Cannot validate search engine '{engine}': engine configuration unavailable"
+ )
return engine
@@ -514,6 +520,10 @@ def analyze_documents(
if not collection_name or not collection_name.strip():
raise ValidationError("Collection name cannot be empty")
collection_name = collection_name.strip()
+ if not _COLLECTION_NAME_RE.match(collection_name):
+ raise ValidationError(
+ "Collection name may only contain letters, digits, spaces, hyphens, and underscores (max 100 chars)"
+ )
max_results = _validate_max_results(max_results)
logger.info(
diff --git a/tests/mcp/test_server_validators.py b/tests/mcp/test_server_validators.py
index 148d5c6612..81f73fe210 100644
--- a/tests/mcp/test_server_validators.py
+++ b/tests/mcp/test_server_validators.py
@@ -21,23 +21,27 @@ def _import_server():
"""Lazy import to avoid collection-time failures when mcp is missing."""
from local_deep_research.mcp.server import (
ValidationError,
+ _COLLECTION_NAME_RE,
_build_settings_overrides,
_classify_error,
_validate_iterations,
_validate_max_results,
_validate_query,
_validate_questions_per_iteration,
+ _validate_search_engine,
_validate_temperature,
)
return {
"ValidationError": ValidationError,
+ "_COLLECTION_NAME_RE": _COLLECTION_NAME_RE,
"_build_settings_overrides": _build_settings_overrides,
"_classify_error": _classify_error,
"_validate_iterations": _validate_iterations,
"_validate_max_results": _validate_max_results,
"_validate_query": _validate_query,
"_validate_questions_per_iteration": _validate_questions_per_iteration,
+ "_validate_search_engine": _validate_search_engine,
"_validate_temperature": _validate_temperature,
}
@@ -366,3 +370,85 @@ class TestBuildSettingsOverrides:
srv = _import_server()
result = srv["_build_settings_overrides"](search_engine="")
assert "search.tool" not in result
+
+
+# ---------------------------------------------------------------------------
+# _COLLECTION_NAME_RE
+# ---------------------------------------------------------------------------
+
+
+class TestCollectionNameRegex:
+ """Tests for _COLLECTION_NAME_RE collection name validation."""
+
+ def test_path_traversal_rejected(self):
+ srv = _import_server()
+ assert srv["_COLLECTION_NAME_RE"].match("../etc/passwd") is None
+
+ def test_too_long_rejected(self):
+ srv = _import_server()
+ assert srv["_COLLECTION_NAME_RE"].match("a" * 101) is None
+
+ def test_valid_name_with_hyphens_and_underscores(self):
+ srv = _import_server()
+ assert (
+ srv["_COLLECTION_NAME_RE"].match("my-collection_name") is not None
+ )
+
+ def test_valid_alphanumeric_with_spaces(self):
+ srv = _import_server()
+ assert srv["_COLLECTION_NAME_RE"].match("My Collection 42") is not None
+
+ def test_exactly_100_chars_accepted(self):
+ srv = _import_server()
+ assert srv["_COLLECTION_NAME_RE"].match("a" * 100) is not None
+
+ def test_empty_string_rejected(self):
+ srv = _import_server()
+ assert srv["_COLLECTION_NAME_RE"].match("") is None
+
+ def test_special_chars_rejected(self):
+ srv = _import_server()
+ assert srv["_COLLECTION_NAME_RE"].match("col;drop table") is None
+
+ def test_tab_rejected(self):
+ srv = _import_server()
+ assert srv["_COLLECTION_NAME_RE"].match("col\tname") is None
+
+ def test_newline_rejected(self):
+ srv = _import_server()
+ assert srv["_COLLECTION_NAME_RE"].match("col\nname") is None
+
+
+# ---------------------------------------------------------------------------
+# _validate_search_engine
+# ---------------------------------------------------------------------------
+
+
+class TestValidateSearchEngine:
+ """Tests for _validate_search_engine."""
+
+ def test_none_returns_none(self):
+ srv = _import_server()
+ assert srv["_validate_search_engine"](None) is None
+
+ def test_empty_string_returns_none(self):
+ srv = _import_server()
+ assert srv["_validate_search_engine"]("") is None
+
+ def test_whitespace_only_returns_none(self):
+ srv = _import_server()
+ assert srv["_validate_search_engine"](" ") is None
+
+ @patch(
+ "local_deep_research.web_search_engines.search_engines_config.search_config",
+ side_effect=RuntimeError("config unavailable"),
+ )
+ @patch("local_deep_research.mcp.server.create_settings_snapshot")
+ def test_config_load_failure_raises_validation_error(
+ self, mock_settings, mock_config
+ ):
+ srv = _import_server()
+ with pytest.raises(
+ srv["ValidationError"], match="engine configuration unavailable"
+ ):
+ srv["_validate_search_engine"]("some_engine")