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.

fix: validate model path before filesystem operations (CWE-22) (#1910)

LearningCircuit62a1caf141be
diff --git a/src/local_deep_research/config/llm_config.py b/src/local_deep_research/config/llm_config.py
index 9017f0e3cf..200653c4ca 100644
--- a/src/local_deep_research/config/llm_config.py
+++ b/src/local_deep_research/config/llm_config.py
@@ -876,45 +876,58 @@ def get_llm(
"with the server's /v1 endpoint (e.g., 'http://localhost:8000/v1')"
)
- # Pre-validate path with helpful messages before security validation
- from pathlib import Path as PathLib
-
- raw_path = PathLib(model_path)
-
- # Check if path is a directory instead of a file
- if raw_path.is_dir():
- # List .gguf files in directory if any
- gguf_files = list(raw_path.glob("*.gguf"))
- if gguf_files:
- files_list = ", ".join(f.name for f in gguf_files[:5])
- if len(gguf_files) > 5:
- files_list += f" (and {len(gguf_files) - 5} more)"
- suggestion = f"Found .gguf files: {files_list}"
- else:
- suggestion = "No .gguf files found in this directory"
- raise ValueError(
- f"Model path is a directory, not a file: {model_path}\n"
- f"Please specify the full path to a .gguf model file.\n"
- f"{suggestion}"
- )
-
- # Check for .gguf extension (warning only, don't block)
- if raw_path.suffix.lower() not in (".gguf", ".bin"):
- logger.warning(
- f"Model file does not have .gguf extension: {raw_path.suffix}. "
- "LlamaCpp typically expects .gguf files."
- )
-
- # Validate model path for security using centralized validator
+ # Validate model path for security FIRST using centralized validator
+ # This MUST happen before any filesystem operations on user input
from ..security.path_validator import PathValidator
+ from ..utilities.path_config import get_models_directory
try:
validated_path = PathValidator.validate_model_path(model_path)
- model_path = str(validated_path)
- except ValueError:
+ except ValueError as e:
+ error_msg = str(e)
+ # If the path is not a file, try to provide helpful directory listing
+ # Only do this after path has passed security validation (safe_join check)
+ if "not a file" in error_msg:
+ try:
+ model_root = str(get_models_directory())
+ safe_path = PathValidator.validate_safe_path(
+ model_path, model_root, allow_absolute=False
+ )
+ if safe_path and safe_path.is_dir():
+ gguf_files = list(safe_path.glob("*.gguf"))
+ if gguf_files:
+ files_list = ", ".join(
+ f.name for f in gguf_files[:5]
+ )
+ if len(gguf_files) > 5:
+ files_list += (
+ f" (and {len(gguf_files) - 5} more)"
+ )
+ suggestion = f"Found .gguf files: {files_list}"
+ else:
+ suggestion = (
+ "No .gguf files found in this directory"
+ )
+ raise ValueError(
+ f"Model path is a directory, not a file: {model_path}\n"
+ f"Please specify the full path to a .gguf model file.\n"
+ f"{suggestion}"
+ ) from e
+ except ValueError:
+ pass # Re-raise original error if secondary validation fails
logger.exception("Model path validation failed")
raise
+ model_path = str(validated_path)
+
+ # Check for .gguf extension (warning only, don't block)
+ # Safe to use validated_path here since it passed security validation
+ if validated_path.suffix.lower() not in (".gguf", ".bin"):
+ logger.warning(
+ f"Model file does not have .gguf extension: {validated_path.suffix}. "
+ "LlamaCpp typically expects .gguf files."
+ )
+
# Get additional LlamaCpp parameters
n_gpu_layers = get_setting_from_snapshot(
"llm.llamacpp_n_gpu_layers",