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.

chore: add pre-commit hook to catch absolute module paths (#2163)

LearningCircuit0ee6c17c0d2e
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index df0fbb1d04..435d5662d1 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -165,3 +165,10 @@ repos:
language: script
files: \.py$
description: "Ensure DownloadService, LibraryRAGService, LocalEmbeddingManager use context managers"
+ - id: check-absolute-module-paths
+ name: Check for absolute module paths in engine configs
+ entry: .pre-commit-hooks/check-absolute-module-paths.py
+ language: script
+ files: \.(py|json)$
+ description: "Ensure search engine module_path values use relative imports, not
+ absolute paths"
diff --git a/.pre-commit-hooks/check-absolute-module-paths.py b/.pre-commit-hooks/check-absolute-module-paths.py
new file mode 100755
index 0000000000..95ef4e82b6
--- /dev/null
+++ b/.pre-commit-hooks/check-absolute-module-paths.py
@@ -0,0 +1,197 @@
+#!/usr/bin/env python3
+"""
+Pre-commit hook to prevent absolute module paths for search engines.
+
+Search engine module_path values MUST use relative imports (starting with ".")
+instead of absolute paths (starting with "local_deep_research.web_search_engines.").
+The module whitelist security boundary requires relative paths.
+
+See: src/local_deep_research/security/module_whitelist.py
+"""
+
+import ast
+import json
+import sys
+from pathlib import Path
+
+
+# The absolute base that must NOT appear as a module path prefix
+ABSOLUTE_BASE = "local_deep_research.web_search_engines"
+
+# Directory patterns where absolute paths are allowed
+ALLOWED_DIR_PATTERNS = {
+ "tests/", # Tests may verify rejection of absolute paths
+ ".pre-commit-hooks/", # Hook files themselves
+}
+
+# Specific file paths where absolute paths are allowed
+ALLOWED_FILES = {
+ "security/module_whitelist.py", # Defines the security boundary itself
+}
+
+# Basename prefixes/suffixes for test files
+ALLOWED_BASENAME_PATTERNS = {
+ "test_", # test_*.py files
+ "_test.", # *_test.py files
+}
+
+
+class AbsoluteModulePathChecker(ast.NodeVisitor):
+ """AST visitor to detect absolute module paths in string literals."""
+
+ def __init__(self, filename: str):
+ self.filename = filename
+ self.errors = []
+
+ def visit_Constant(self, node):
+ """Check string constants for absolute module paths."""
+ if isinstance(node.value, str) and node.value.startswith(
+ ABSOLUTE_BASE + "."
+ ):
+ relative = node.value[len(ABSOLUTE_BASE) :]
+ self.errors.append(
+ (
+ node.lineno,
+ f'Absolute module path "{node.value}" found. '
+ f'Use relative path "{relative}" instead.',
+ )
+ )
+ self.generic_visit(node)
+
+
+def _is_file_allowed(filename: str) -> bool:
+ """Check if this file is allowed to contain absolute module paths."""
+ for pattern in ALLOWED_DIR_PATTERNS:
+ if pattern in filename:
+ return True
+ for pattern in ALLOWED_FILES:
+ if pattern in filename:
+ return True
+ basename = Path(filename).name
+ for pattern in ALLOWED_BASENAME_PATTERNS:
+ if basename.startswith(pattern) or pattern in basename:
+ return True
+ return False
+
+
+def check_python_file(filename: str) -> bool:
+ """Check a Python file for absolute module paths in string literals."""
+ if _is_file_allowed(filename):
+ return True
+
+ try:
+ with open(filename, "r", encoding="utf-8") as f:
+ content = f.read()
+ except Exception as e:
+ print(f"Error reading {filename}: {e}")
+ return False
+
+ try:
+ tree = ast.parse(content, filename=filename)
+ checker = AbsoluteModulePathChecker(filename)
+ checker.visit(tree)
+
+ if checker.errors:
+ print(f"\n{filename}:")
+ for line_num, error in checker.errors:
+ print(f" Line {line_num}: {error}")
+ return False
+
+ except SyntaxError:
+ # Skip files with syntax errors (let other tools handle that)
+ pass
+ except Exception as e:
+ print(f"Error parsing {filename}: {e}")
+ return False
+
+ return True
+
+
+def check_json_file(filename: str) -> bool:
+ """Check a JSON config file for absolute module paths."""
+ if _is_file_allowed(filename):
+ return True
+
+ try:
+ with open(filename, "r", encoding="utf-8") as f:
+ data = json.load(f)
+ except Exception as e:
+ print(f"Error reading {filename}: {e}")
+ return False
+
+ errors = []
+
+ def _check_value(obj, path=""):
+ if isinstance(obj, dict):
+ for key, value in obj.items():
+ current_path = f"{path}.{key}" if path else key
+ _check_value(value, current_path)
+ elif isinstance(obj, list):
+ for i, item in enumerate(obj):
+ _check_value(item, f"{path}[{i}]")
+ elif isinstance(obj, str) and obj.startswith(ABSOLUTE_BASE + "."):
+ relative = obj[len(ABSOLUTE_BASE) :]
+ errors.append((path, obj, relative))
+
+ _check_value(data)
+
+ if errors:
+ print(f"\n{filename}:")
+ for key_path, value, relative in errors:
+ print(
+ f' Key "{key_path}": Absolute module path "{value}" found. '
+ f'Use relative path "{relative}" instead.'
+ )
+ return False
+
+ return True
+
+
+def main():
+ """Check all provided files for absolute module paths."""
+ if len(sys.argv) < 2:
+ print("Usage: check-absolute-module-paths.py <file1> <file2> ...")
+ sys.exit(1)
+
+ files_to_check = sys.argv[1:]
+ has_errors = False
+
+ for filename in files_to_check:
+ if filename.endswith(".py"):
+ if not check_python_file(filename):
+ has_errors = True
+ elif filename.endswith(".json"):
+ if not check_json_file(filename):
+ has_errors = True
+
+ if has_errors:
+ print("\n" + "=" * 70)
+ print("Module Path Security: Absolute module paths detected!")
+ print("=" * 70)
+ print(
+ "\nSearch engine module paths must use relative imports"
+ " (starting with '.')"
+ )
+ print(
+ "instead of absolute paths starting with"
+ " 'local_deep_research.web_search_engines.'."
+ )
+ print("\nExamples:")
+ print(
+ ' BAD: "local_deep_research.web_search_engines'
+ '.engines.search_engine_local"'
+ )
+ print(' GOOD: ".engines.search_engine_local"')
+ print(
+ "\nRelative paths are resolved against"
+ " local_deep_research.web_search_engines"
+ )
+ print("and are required by the module whitelist security boundary.")
+ print("\nSee: src/local_deep_research/security/module_whitelist.py")
+ sys.exit(1)
+ else:
+ sys.exit(0)
+
+
+if __name__ == "__main__":
+ main()