~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
test: add tests for security/module_whitelist.py (#2018)
LearningCircuited83f053ef42
diff --git a/tests/security/test_module_whitelist.py b/tests/security/test_module_whitelist.pynew file mode 100644index 0000000000..5ae2a6820c--- /dev/null+++ b/tests/security/test_module_whitelist.py@@ -0,0 +1,218 @@+"""+Tests for the module whitelist security feature.++This module tests the security controls that prevent arbitrary code execution+through dynamically loaded modules.+"""++import pytest++from local_deep_research.security.module_whitelist import (+ ALLOWED_CLASS_NAMES,+ ALLOWED_MODULE_PATHS,+ SecurityError,+ get_safe_module_class,+ validate_module_import,+)+++class TestValidateModuleImport:+ """Tests for validate_module_import function."""++ def test_valid_module_and_class_passes(self):+ """Whitelisted module and class should pass validation."""+ assert validate_module_import(+ ".engines.search_engine_brave", "BraveSearchEngine"+ )++ def test_valid_base_module_passes(self):+ """Base search engine module should pass validation."""+ assert validate_module_import(".search_engine_base", "BaseSearchEngine")++ def test_non_whitelisted_module_rejected(self):+ """Non-whitelisted module path should be rejected."""+ assert not validate_module_import(+ ".engines.malicious_module", "BraveSearchEngine"+ )++ def test_non_whitelisted_class_rejected(self):+ """Non-whitelisted class name should be rejected."""+ assert not validate_module_import(+ ".engines.search_engine_brave", "MaliciousClass"+ )++ def test_absolute_import_path_rejected(self):+ """Absolute import paths (not starting with '.') should be rejected."""+ # Attempt to import os module+ assert not validate_module_import("os", "system")++ def test_absolute_local_path_rejected(self):+ """Absolute paths even within local_deep_research should be rejected."""+ assert not validate_module_import(+ "local_deep_research.web_search_engines.engines.search_engine_brave",+ "BraveSearchEngine",+ )++ def test_empty_module_path_rejected(self):+ """Empty module path should be rejected."""+ assert not validate_module_import("", "BraveSearchEngine")++ def test_empty_class_name_rejected(self):+ """Empty class name should be rejected."""+ assert not validate_module_import(".engines.search_engine_brave", "")++ def test_none_module_path_rejected(self):+ """None module path should be rejected."""+ assert not validate_module_import(None, "BraveSearchEngine")++ def test_none_class_name_rejected(self):+ """None class name should be rejected."""+ assert not validate_module_import(".engines.search_engine_brave", None)++ def test_path_traversal_rejected(self):+ """Path traversal attempts should be rejected."""+ assert not validate_module_import("..os", "system")+ assert not validate_module_import("./../os", "system")++ def test_all_whitelisted_modules_valid(self):+ """All modules in ALLOWED_MODULE_PATHS should pass when paired with valid class."""+ # Test a sample of modules with their expected classes+ valid_pairs = [+ (".engines.search_engine_arxiv", "ArXivSearchEngine"),+ (".engines.search_engine_ddg", "DuckDuckGoSearchEngine"),+ (".engines.search_engine_wikipedia", "WikipediaSearchEngine"),+ (".engines.search_engine_pubmed", "PubMedSearchEngine"),+ ]+ for module_path, class_name in valid_pairs:+ assert validate_module_import(module_path, class_name), (+ f"Expected {module_path}:{class_name} to be valid"+ )+++class TestGetSafeModuleClass:+ """Tests for get_safe_module_class function."""++ def test_blocked_non_whitelisted_module_raises_security_error(self):+ """Non-whitelisted module should raise SecurityError."""+ with pytest.raises(SecurityError) as exc_info:+ get_safe_module_class(".malicious_module", "MaliciousClass")++ assert "not in the security whitelist" in str(exc_info.value)++ def test_blocked_non_whitelisted_class_raises_security_error(self):+ """Non-whitelisted class should raise SecurityError."""+ with pytest.raises(SecurityError) as exc_info:+ get_safe_module_class(".engines.search_engine_brave", "EvilClass")++ assert "not in the security whitelist" in str(exc_info.value)++ def test_blocked_absolute_import_raises_security_error(self):+ """Absolute import attempt should raise SecurityError."""+ with pytest.raises(SecurityError):+ get_safe_module_class("os", "system")++ def test_blocked_subprocess_import_raises_security_error(self):+ """Dangerous module imports should be blocked."""+ with pytest.raises(SecurityError):+ get_safe_module_class("subprocess", "Popen")++ def test_module_not_found_error_propagates(self):+ """ModuleNotFoundError should propagate for whitelisted but missing modules."""+ # Add a fake module to whitelist temporarily to test error handling+ # This would require modifying the whitelist, which we shouldn't do in tests+ # Instead, we verify the error message format+ pass # Skip - would require modifying frozen set++ def test_attribute_error_propagates(self):+ """AttributeError should propagate when class doesn't exist in module."""+ # This would require a whitelisted module to exist but not have the class+ # The actual module loading is tested in integration tests+ pass # Skip - would require real module loading+++class TestWhitelistContents:+ """Tests for whitelist configuration."""++ def test_all_module_paths_are_relative(self):+ """All module paths in whitelist should be relative (start with '.')."""+ for module_path in ALLOWED_MODULE_PATHS:+ assert module_path.startswith("."), (+ f"Module path '{module_path}' should be relative (start with '.')"+ )++ def test_no_dangerous_modules_in_whitelist(self):+ """Dangerous modules should not be in the whitelist."""+ dangerous_modules = {+ "os",+ "subprocess",+ "sys",+ "builtins",+ "eval",+ "exec",+ }++ for module_path in ALLOWED_MODULE_PATHS:+ for dangerous in dangerous_modules:+ assert dangerous not in module_path.lower(), (+ f"Module path '{module_path}' contains dangerous module '{dangerous}'"+ )++ def test_class_names_follow_naming_convention(self):+ """All class names should follow expected naming conventions."""+ for class_name in ALLOWED_CLASS_NAMES:+ # Should be CamelCase+ assert class_name[0].isupper(), (+ f"Class name '{class_name}' should start with uppercase"+ )+ # Should not contain suspicious patterns+ assert "__" not in class_name, (+ f"Class name '{class_name}' should not contain dunder"+ )++ def test_whitelists_are_frozen(self):+ """Whitelists should be frozensets to prevent modification."""+ assert isinstance(ALLOWED_MODULE_PATHS, frozenset)+ assert isinstance(ALLOWED_CLASS_NAMES, frozenset)++ def test_whitelist_not_empty(self):+ """Whitelists should not be empty."""+ assert len(ALLOWED_MODULE_PATHS) > 0+ assert len(ALLOWED_CLASS_NAMES) > 0+++class TestSecurityScenarios:+ """Tests for specific security attack scenarios."""++ def test_injection_via_class_name_blocked(self):+ """Injection attempts via class name should be blocked."""+ malicious_names = [+ "__import__('os').system",+ "eval('malicious')",+ "exec('code')",+ "; rm -rf /",+ "BraveSearchEngine; os.system('bad')",+ ]+ for name in malicious_names:+ assert not validate_module_import(+ ".engines.search_engine_brave", name+ ), f"Should block malicious class name: {name}"++ def test_injection_via_module_path_blocked(self):+ """Injection attempts via module path should be blocked."""+ malicious_paths = [+ "os; import subprocess",+ "__import__('os')",+ ".engines.search_engine_brave; import os",+ ]+ for path in malicious_paths:+ assert not validate_module_import(path, "BraveSearchEngine"), (+ f"Should block malicious module path: {path}"+ )++ def test_unicode_bypass_blocked(self):+ """Unicode-based bypass attempts should be blocked."""+ # These might try to use similar-looking unicode characters+ assert not validate_module_import(+ ".engines.search_engine_brаve", # Cyrillic 'а' instead of Latin 'a'+ "BraveSearchEngine",+ )