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.

test(security): add whitelist/config consistency tests and fix stale serpapi refs (#2848)

LearningCircuit80130662db93
diff --git a/tests/security/test_whitelist_config_consistency.py b/tests/security/test_whitelist_config_consistency.py
new file mode 100644
index 0000000000..1ffffeeb8a
--- /dev/null
+++ b/tests/security/test_whitelist_config_consistency.py
@@ -0,0 +1,195 @@
+"""
+Consistency tests between default config files and the security whitelist.
+
+These tests ensure that:
+- Every module_path/class_name in default configs is whitelisted
+- Every whitelisted module actually exists and can be imported
+- Every (module_path, class_name) pair from configs resolves to a real class
+
+This prevents silent runtime failures when configs and whitelist drift out of sync.
+"""
+
+import importlib
+import json
+from pathlib import Path
+
+import pytest
+
+from local_deep_research.security.module_whitelist import (
+ ALLOWED_CLASS_NAMES,
+ ALLOWED_MODULE_PATHS,
+)
+
+# Root of the defaults directory
+_DEFAULTS_DIR = (
+ Path(__file__).resolve().parents[2]
+ / "src"
+ / "local_deep_research"
+ / "defaults"
+)
+
+
+def _collect_configured_engines():
+ """Collect all (source_file, module_path, class_name) triples from default configs.
+
+ Scans two JSON structures:
+
+ 1. Flat keys like ``search.engine.web.brave.module_path`` where the sibling
+ ``search.engine.web.brave.class_name`` key holds the class name.
+ 2. Nested value dicts like ``search.engine.web.openalex`` whose ``value``
+ is a dict containing ``module_path`` and ``class_name``.
+
+ Also picks up ``full_search_module`` / ``full_search_class`` pairs used by
+ engines that support full-document fetching.
+ """
+ results = []
+
+ # All JSON files to scan
+ json_files = [_DEFAULTS_DIR / "default_settings.json"]
+ json_files.extend(_DEFAULTS_DIR.glob("settings_*.json"))
+ json_files.extend(
+ (_DEFAULTS_DIR / "settings" / "search_engines").glob("*.json")
+ )
+
+ for json_path in json_files:
+ if not json_path.exists():
+ continue
+
+ data = json.loads(json_path.read_text(encoding="utf-8"))
+ source = json_path.name
+
+ # --- Pattern 1: flat keys ending in .module_path ---
+ module_path_keys = [k for k in data if k.endswith(".module_path")]
+ for mp_key in module_path_keys:
+ module_path = data[mp_key].get("value")
+ if not module_path or not isinstance(module_path, str):
+ continue
+
+ # Derive sibling class_name key
+ prefix = mp_key.rsplit(".module_path", 1)[0]
+ cn_key = f"{prefix}.class_name"
+ if cn_key in data:
+ class_name = data[cn_key].get("value")
+ if class_name and isinstance(class_name, str):
+ results.append((source, module_path, class_name))
+
+ # --- Pattern 1b: full_search_module / full_search_class pairs ---
+ fs_module_keys = [k for k in data if k.endswith(".full_search_module")]
+ for fsm_key in fs_module_keys:
+ module_path = data[fsm_key].get("value")
+ if not module_path or not isinstance(module_path, str):
+ continue
+
+ prefix = fsm_key.rsplit(".full_search_module", 1)[0]
+ fsc_key = f"{prefix}.full_search_class"
+ if fsc_key in data:
+ class_name = data[fsc_key].get("value")
+ if class_name and isinstance(class_name, str):
+ results.append((source, module_path, class_name))
+
+ # --- Pattern 2: nested value dicts with module_path + class_name ---
+ for key, entry in data.items():
+ if not isinstance(entry, dict):
+ continue
+ value = entry.get("value")
+ if not isinstance(value, dict):
+ continue
+ mp = value.get("module_path")
+ cn = value.get("class_name")
+ if mp and cn and isinstance(mp, str) and isinstance(cn, str):
+ results.append((source, mp, cn))
+
+ return results
+
+
+class TestDefaultSettingsModulePathsInWhitelist:
+ """Every module_path in default configs must be in ALLOWED_MODULE_PATHS."""
+
+ def test_default_settings_module_paths_in_whitelist(self):
+ """All configured module_path values are present in the whitelist."""
+ engines = _collect_configured_engines()
+ assert engines, "No engines found — collector may be broken"
+
+ missing = []
+ for source, module_path, _class_name in engines:
+ if module_path not in ALLOWED_MODULE_PATHS:
+ missing.append(f"{source}: {module_path}")
+
+ assert not missing, (
+ "Module paths in default configs but NOT in ALLOWED_MODULE_PATHS:\n"
+ + "\n".join(f" - {m}" for m in missing)
+ )
+
+
+class TestDefaultSettingsClassNamesInWhitelist:
+ """Every class_name in default configs must be in ALLOWED_CLASS_NAMES."""
+
+ def test_default_settings_class_names_in_whitelist(self):
+ """All configured class_name values are present in the whitelist."""
+ engines = _collect_configured_engines()
+ assert engines, "No engines found — collector may be broken"
+
+ missing = []
+ for source, _module_path, class_name in engines:
+ if class_name not in ALLOWED_CLASS_NAMES:
+ missing.append(f"{source}: {class_name}")
+
+ assert not missing, (
+ "Class names in default configs but NOT in ALLOWED_CLASS_NAMES:\n"
+ + "\n".join(f" - {m}" for m in missing)
+ )
+
+
+class TestWhitelistedModulesResolve:
+ """Every entry in ALLOWED_MODULE_PATHS must be importable."""
+
+ @pytest.mark.parametrize("module_path", sorted(ALLOWED_MODULE_PATHS))
+ def test_whitelisted_module_resolves(self, module_path):
+ """Whitelisted module '{module_path}' can be imported."""
+ try:
+ importlib.import_module(
+ module_path,
+ package="local_deep_research.web_search_engines",
+ )
+ except ModuleNotFoundError:
+ pytest.fail(
+ f"Whitelisted module {module_path!r} cannot be imported. "
+ f"Was the module deleted or renamed?"
+ )
+
+
+class TestWhitelistedClassesExistInModules:
+ """Every (module_path, class_name) pair from configs must resolve to a real class."""
+
+ def test_whitelisted_classes_exist_in_modules(self):
+ """Each configured class exists in its declared module."""
+ engines = _collect_configured_engines()
+ assert engines, "No engines found — collector may be broken"
+
+ # Deduplicate by (module_path, class_name)
+ seen = set()
+ failures = []
+ for source, module_path, class_name in engines:
+ pair = (module_path, class_name)
+ if pair in seen:
+ continue
+ seen.add(pair)
+
+ try:
+ mod = importlib.import_module(
+ module_path,
+ package="local_deep_research.web_search_engines",
+ )
+ except ModuleNotFoundError:
+ failures.append(f"{source}: module {module_path!r} not found")
+ continue
+
+ if not hasattr(mod, class_name):
+ failures.append(
+ f"{source}: class {class_name!r} not found in {module_path!r}"
+ )
+
+ assert not failures, (
+ "Config references that don't resolve:\n"
+ + "\n".join(f" - {f}" for f in failures)
+ )