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: add security module test coverage (#2005)

LearningCircuit5210aef4a0f9
diff --git a/tests/security/test_module_whitelist.py b/tests/security/test_module_whitelist.py
index 5ae2a6820c..7375989018 100644
--- a/tests/security/test_module_whitelist.py
+++ b/tests/security/test_module_whitelist.py
@@ -1,218 +1,430 @@
"""
-Tests for the module whitelist security feature.
+Module Whitelist Tests
-This module tests the security controls that prevent arbitrary code execution
-through dynamically loaded modules.
+Tests for the dynamic import security module that prevents arbitrary code execution
+through user-controlled configuration values.
+
+Security model:
+- Only allows relative imports (starting with ".")
+- Validates module paths against a strict whitelist of trusted search engine modules
+- Validates class names against a strict whitelist of legitimate search engine classes
+- Blocks dangerous modules like os, subprocess, sys, etc.
+- Prevents path traversal attacks in module paths
"""
import pytest
+from unittest.mock import MagicMock, patch
from local_deep_research.security.module_whitelist import (
ALLOWED_CLASS_NAMES,
ALLOWED_MODULE_PATHS,
+ ModuleNotAllowedError,
SecurityError,
get_safe_module_class,
validate_module_import,
)
+class TestAllowedModulePaths:
+ """Test the ALLOWED_MODULE_PATHS constant."""
+
+ def test_constants_are_relative_paths(self):
+ """All allowed module paths should start with '.' (relative imports)."""
+ for module_path in ALLOWED_MODULE_PATHS:
+ assert module_path.startswith("."), (
+ f"Module path '{module_path}' should be a relative import "
+ f"(starting with '.'). Absolute imports are not allowed for security."
+ )
+
+ def test_whitelist_is_frozen_set(self):
+ """ALLOWED_MODULE_PATHS should be a frozenset (immutable)."""
+ assert isinstance(ALLOWED_MODULE_PATHS, frozenset)
+
+ def test_whitelist_contains_expected_engines(self):
+ """Whitelist should contain expected search engine modules."""
+ expected_engines = [
+ ".engines.search_engine_brave",
+ ".engines.search_engine_ddg",
+ ".engines.search_engine_tavily",
+ ".engines.search_engine_wikipedia",
+ ]
+ for engine in expected_engines:
+ assert engine in ALLOWED_MODULE_PATHS, (
+ f"Expected engine '{engine}' not found in whitelist"
+ )
+
+
+class TestAllowedClassNames:
+ """Test the ALLOWED_CLASS_NAMES constant."""
+
+ def test_class_names_whitelist_is_frozen_set(self):
+ """ALLOWED_CLASS_NAMES should be a frozenset (immutable)."""
+ assert isinstance(ALLOWED_CLASS_NAMES, frozenset)
+
+ def test_whitelist_contains_expected_classes(self):
+ """Whitelist should contain expected search engine classes."""
+ expected_classes = [
+ "BraveSearchEngine",
+ "DuckDuckGoSearchEngine",
+ "TavilySearchEngine",
+ "WikipediaSearchEngine",
+ "BaseSearchEngine",
+ ]
+ for cls_name in expected_classes:
+ assert cls_name in ALLOWED_CLASS_NAMES, (
+ f"Expected class '{cls_name}' not found in whitelist"
+ )
+
+
+class TestLegacyAliases:
+ """Test backward compatibility aliases."""
+
+ def test_module_not_allowed_error_is_security_error(self):
+ """ModuleNotAllowedError should be an alias for SecurityError."""
+ assert ModuleNotAllowedError is SecurityError
+
+
class TestValidateModuleImport:
- """Tests for validate_module_import function."""
+ """Test the validate_module_import function."""
- def test_valid_module_and_class_passes(self):
- """Whitelisted module and class should pass validation."""
- assert validate_module_import(
+ def test_valid_module_and_class(self):
+ """Should return True for valid whitelisted module and class."""
+ result = validate_module_import(
".engines.search_engine_brave", "BraveSearchEngine"
)
+ assert result is True
- def test_valid_base_module_passes(self):
- """Base search engine module should pass validation."""
- assert validate_module_import(".search_engine_base", "BaseSearchEngine")
+ def test_valid_base_search_engine(self):
+ """Should return True for the base search engine module."""
+ result = validate_module_import(
+ ".search_engine_base", "BaseSearchEngine"
+ )
+ assert result is True
- 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_rejects_absolute_path(self):
+ """Should reject absolute module paths."""
+ # Absolute path attempt
+ result = validate_module_import(
+ "local_deep_research.web_search_engines.engines.search_engine_brave",
+ "BraveSearchEngine",
+ )
+ assert result is False
+
+ def test_rejects_subprocess_module(self):
+ """Should reject dangerous subprocess module."""
+ result = validate_module_import("subprocess", "Popen")
+ assert result is False
+
+ def test_rejects_os_module(self):
+ """Should reject dangerous os module."""
+ result = validate_module_import("os", "system")
+ assert result is False
+
+ def test_rejects_sys_module(self):
+ """Should reject dangerous sys module."""
+ result = validate_module_import("sys", "exit")
+ assert result is False
+
+ def test_case_sensitivity(self):
+ """Module and class names should be case-sensitive."""
+ # Lowercase - should fail
+ result = validate_module_import(
+ ".engines.search_engine_brave", "bravesearchengine"
)
+ assert result is False
- def test_non_whitelisted_class_rejected(self):
- """Non-whitelisted class name should be rejected."""
- assert not validate_module_import(
+ # Uppercase - should fail
+ result = validate_module_import(
+ ".ENGINES.SEARCH_ENGINE_BRAVE", "BraveSearchEngine"
+ )
+ assert result is False
+
+ def test_empty_module_path(self):
+ """Should reject empty module path."""
+ result = validate_module_import("", "BraveSearchEngine")
+ assert result is False
+
+ def test_empty_class_name(self):
+ """Should reject empty class name."""
+ result = validate_module_import(".engines.search_engine_brave", "")
+ assert result is False
+
+ def test_none_module_path(self):
+ """Should reject None module path."""
+ result = validate_module_import(None, "BraveSearchEngine")
+ assert result is False
+
+ def test_none_class_name(self):
+ """Should reject None class name."""
+ result = validate_module_import(".engines.search_engine_brave", None)
+ assert result is False
+
+ def test_valid_module_invalid_class(self):
+ """Should reject valid module with invalid class name."""
+ result = validate_module_import(
".engines.search_engine_brave", "MaliciousClass"
)
+ assert result is False
- 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_invalid_module_valid_class(self):
+ """Should reject invalid module with valid class name."""
+ result = validate_module_import(
+ ".engines.nonexistent_engine", "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"
- )
+ assert result is False
class TestGetSafeModuleClass:
- """Tests for get_safe_module_class function."""
+ """Test the get_safe_module_class function."""
+
+ def test_loads_valid_search_engine(self):
+ """Should successfully load a valid whitelisted search engine class."""
+ # Use a module that exists and should be loadable
+ cls = get_safe_module_class(".search_engine_base", "BaseSearchEngine")
+ assert cls is not None
+ assert cls.__name__ == "BaseSearchEngine"
- def test_blocked_non_whitelisted_module_raises_security_error(self):
- """Non-whitelisted module should raise SecurityError."""
+ def test_raises_security_error_for_invalid_module(self):
+ """Should raise SecurityError for non-whitelisted module."""
with pytest.raises(SecurityError) as exc_info:
- get_safe_module_class(".malicious_module", "MaliciousClass")
+ get_safe_module_class("os", "system")
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")
+ def test_raises_security_error_for_absolute_import(self):
+ """Should raise SecurityError for absolute import paths."""
+ with pytest.raises(SecurityError):
+ get_safe_module_class(
+ "local_deep_research.security.module_whitelist",
+ "SecurityError",
+ )
- assert "not in the security whitelist" in str(exc_info.value)
+ def test_handles_module_not_found(self):
+ """Should raise ModuleNotFoundError if whitelisted module doesn't exist."""
+ # First, temporarily add a fake module to the whitelist for testing
+ # We can't actually test this without modifying the whitelist, so we mock
+ with patch(
+ "local_deep_research.security.module_whitelist.validate_module_import",
+ return_value=True,
+ ):
+ with patch(
+ "local_deep_research.security.module_whitelist.importlib.import_module",
+ side_effect=ModuleNotFoundError("No module named 'fake'"),
+ ):
+ with pytest.raises(ModuleNotFoundError):
+ get_safe_module_class(".engines.fake_module", "FakeEngine")
+
+ def test_handles_attribute_not_found(self):
+ """Should raise AttributeError if class doesn't exist in module."""
+ with patch(
+ "local_deep_research.security.module_whitelist.validate_module_import",
+ return_value=True,
+ ):
+ mock_module = MagicMock(spec=[]) # Module without the class
+ with patch(
+ "local_deep_research.security.module_whitelist.importlib.import_module",
+ return_value=mock_module,
+ ):
+ with pytest.raises(AttributeError):
+ get_safe_module_class(
+ ".engines.search_engine_brave",
+ "NonExistentClass",
+ )
+
+ def test_uses_default_package_for_relative_imports(self):
+ """Should use default package when none provided for relative imports."""
+ with patch(
+ "local_deep_research.security.module_whitelist.validate_module_import",
+ return_value=True,
+ ):
+ with patch(
+ "local_deep_research.security.module_whitelist.importlib.import_module"
+ ) as mock_import:
+ mock_module = MagicMock()
+ mock_module.FakeEngine = type("FakeEngine", (), {})
+ mock_import.return_value = mock_module
+
+ get_safe_module_class(".engines.fake", "FakeEngine")
+
+ # Check that default package was used
+ mock_import.assert_called_once_with(
+ ".engines.fake",
+ package="local_deep_research.web_search_engines",
+ )
- def test_blocked_absolute_import_raises_security_error(self):
- """Absolute import attempt should raise SecurityError."""
+ def test_uses_custom_package_when_provided(self):
+ """Should use custom package when explicitly provided."""
+ with patch(
+ "local_deep_research.security.module_whitelist.validate_module_import",
+ return_value=True,
+ ):
+ with patch(
+ "local_deep_research.security.module_whitelist.importlib.import_module"
+ ) as mock_import:
+ mock_module = MagicMock()
+ mock_module.FakeEngine = type("FakeEngine", (), {})
+ mock_import.return_value = mock_module
+
+ get_safe_module_class(
+ ".engines.fake",
+ "FakeEngine",
+ package="custom.package",
+ )
+
+ mock_import.assert_called_once_with(
+ ".engines.fake",
+ package="custom.package",
+ )
+
+
+class TestSecurityScenarios:
+ """Test specific security attack scenarios."""
+
+ def test_blocks_os_system_import(self):
+ """Should block attempts to import os.system for command execution."""
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."""
+ def test_blocks_subprocess_import(self):
+ """Should block attempts to import subprocess for command execution."""
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
+ with pytest.raises(SecurityError):
+ get_safe_module_class("subprocess", "call")
+ with pytest.raises(SecurityError):
+ get_safe_module_class("subprocess", "run")
-class TestWhitelistContents:
- """Tests for whitelist configuration."""
+ def test_path_traversal_in_module_path(self):
+ """Should block path traversal attempts in module paths."""
+ # Attempt to break out of expected package
+ malicious_paths = [
+ "..os",
+ "....subprocess",
+ ".engines/../../../os",
+ ".engines/..\\..\\subprocess", # Windows-style
+ ".engines%2F..%2F..%2Fos", # URL-encoded
+ ]
- 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 '.')"
+ for path in malicious_paths:
+ result = validate_module_import(path, "system")
+ assert result is False, (
+ f"Path traversal attempt '{path}' should be blocked"
)
- def test_no_dangerous_modules_in_whitelist(self):
- """Dangerous modules should not be in the whitelist."""
- dangerous_modules = {
- "os",
- "subprocess",
- "sys",
- "builtins",
- "eval",
- "exec",
- }
+ def test_blocks_builtins_module(self):
+ """Should block attempts to import builtins."""
+ with pytest.raises(SecurityError):
+ get_safe_module_class("builtins", "eval")
- 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}'"
- )
+ with pytest.raises(SecurityError):
+ get_safe_module_class("builtins", "__import__")
- 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_blocks_importlib_module(self):
+ """Should block attempts to import importlib directly."""
+ with pytest.raises(SecurityError):
+ get_safe_module_class("importlib", "import_module")
- 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_blocks_code_module(self):
+ """Should block attempts to import code execution modules."""
+ with pytest.raises(SecurityError):
+ get_safe_module_class("code", "interact")
- def test_whitelist_not_empty(self):
- """Whitelists should not be empty."""
- assert len(ALLOWED_MODULE_PATHS) > 0
- assert len(ALLOWED_CLASS_NAMES) > 0
+ def test_blocks_pickle_module(self):
+ """Should block attempts to import pickle (deserialization attacks)."""
+ with pytest.raises(SecurityError):
+ get_safe_module_class("pickle", "loads")
+ def test_blocks_ctypes_module(self):
+ """Should block attempts to import ctypes (memory manipulation)."""
+ with pytest.raises(SecurityError):
+ get_safe_module_class("ctypes", "CDLL")
-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')",
+ def test_blocks_socket_module(self):
+ """Should block attempts to import socket (network access)."""
+ with pytest.raises(SecurityError):
+ get_safe_module_class("socket", "socket")
+
+ def test_sql_injection_style_attack(self):
+ """Should block SQL-injection-style attacks in module names."""
+ malicious_inputs = [
+ (".engines; import os;", "system"),
+ ('.engines"; import os; "', "system"),
+ (".engines' OR '1'='1", "BraveSearchEngine"),
]
- 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}"
+ for module_path, class_name in malicious_inputs:
+ result = validate_module_import(module_path, class_name)
+ assert result is False, (
+ f"Injection attempt '{module_path}' should be blocked"
)
- 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",
- )
+ def test_unicode_bypass_attempt(self):
+ """Should handle unicode normalization bypass attempts."""
+ # These are unlikely to work but good to test
+ unicode_attempts = [
+ "\u006fs", # 'o' followed by 's' -> 'os'
+ "o\u0073", # 'o' followed by 's' -> 'os'
+ ]
+
+ for module_path in unicode_attempts:
+ # Even if they normalize to 'os', should still be blocked
+ result = validate_module_import(module_path, "system")
+ assert result is False
+
+
+class TestSecurityErrorException:
+ """Test the SecurityError exception class."""
+
+ def test_security_error_is_exception(self):
+ """SecurityError should be an Exception."""
+ assert issubclass(SecurityError, Exception)
+
+ def test_security_error_message(self):
+ """SecurityError should contain helpful error message."""
+ error = SecurityError("Test error message")
+ assert "Test error message" in str(error)
+
+ def test_security_error_can_be_raised_and_caught(self):
+ """SecurityError should be raisable and catchable."""
+ with pytest.raises(SecurityError) as exc_info:
+ raise SecurityError("blocked import")
+
+ assert "blocked import" in str(exc_info.value)
+
+
+class TestDocumentation:
+ """Documentation tests explaining the security model."""
+
+ def test_security_model_documentation(self):
+ """
+ Document the module whitelist security model.
+
+ WHY THIS EXISTS:
+ - Dynamic imports from user configuration can lead to arbitrary code execution
+ - Attackers could inject malicious module paths to run arbitrary code
+ - Example: config.module = "os"; config.class = "system" -> command execution
+
+ THE WHITELIST APPROACH:
+ - Only explicitly approved modules can be loaded
+ - Only explicitly approved class names can be instantiated
+ - All imports must be relative (starting with '.') for additional safety
+ - Relative imports are anchored to 'local_deep_research.web_search_engines'
+
+ DEFENSE IN DEPTH:
+ - Whitelist validation before any import attempt
+ - Relative import requirement prevents direct access to dangerous modules
+ - Class name whitelist prevents instantiation of dangerous classes
+ - Logging of blocked attempts for security auditing
+
+ SECURITY MODEL:
+ | Input Type | Action | Reason |
+ |-------------------|----------|-------------------------------------|
+ | Relative + listed | ALLOWED | Trusted search engine module |
+ | Absolute path | BLOCKED | Could access dangerous modules |
+ | Unlisted module | BLOCKED | Not in trusted whitelist |
+ | Unlisted class | BLOCKED | Could be dangerous class |
+ | Empty/None | BLOCKED | Invalid input |
+ """
+ assert True # Documentation test