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: Use pathlib.Path for safer path validation

LearningCircuit609d4c74058a
diff --git a/src/local_deep_research/config/llm_config.py b/src/local_deep_research/config/llm_config.py
index dc59368ffb..10cfe6cfdb 100644
--- a/src/local_deep_research/config/llm_config.py
+++ b/src/local_deep_research/config/llm_config.py
@@ -149,47 +149,40 @@ def is_llamacpp_available(settings_snapshot=None):
"""Check if LlamaCpp is available and configured"""
try:
from langchain_community.llms import LlamaCpp # noqa: F401
+ from pathlib import Path
from .paths import get_data_directory
- import stat
- model_path = get_setting_from_snapshot(
+ model_path_str = get_setting_from_snapshot(
"llm.llamacpp_model_path", settings_snapshot=settings_snapshot
)
- if not model_path:
+ if not model_path_str:
return False
# Sanitize and validate the path to prevent path traversal
try:
- # Resolve to absolute, normalized path
- model_path = os.path.normpath(
- os.path.abspath(os.path.expanduser(model_path))
- )
+ # Convert to Path object and resolve (follows symlinks, normalizes)
+ model_path = Path(model_path_str).expanduser().resolve(strict=False)
# Get allowed base directories for models
- data_dir = str(get_data_directory())
- home_dir = os.path.expanduser("~")
-
- # Check if the path is within allowed directories
- # Models can be in data directory, home directory, or /usr/local
- allowed_prefixes = [
- os.path.normpath(os.path.abspath(data_dir)),
- os.path.normpath(os.path.abspath(home_dir)),
- os.path.normpath("/usr/local"),
- os.path.normpath("/opt"),
+ allowed_dirs = [
+ Path(get_data_directory()).resolve(),
+ Path.home().resolve(),
+ Path("/usr/local").resolve(),
+ Path("/opt").resolve(),
]
- # Verify the path is within an allowed directory using os.path.commonpath
+ # Check if the resolved path is within allowed directories
is_allowed = False
- for prefix in allowed_prefixes:
+ for allowed_dir in allowed_dirs:
try:
- # commonpath returns the longest common sub-path
- common = os.path.commonpath([model_path, prefix])
- if common == prefix:
- is_allowed = True
- break
+ # Check if model_path is relative to allowed_dir
+ # This will raise ValueError if not under allowed_dir
+ model_path.relative_to(allowed_dir)
+ is_allowed = True
+ break
except ValueError:
- # Raised if paths are on different drives (Windows)
+ # Not a subdirectory of this allowed_dir
continue
if not is_allowed:
@@ -198,17 +191,11 @@ def is_llamacpp_available(settings_snapshot=None):
)
return False
- # After validation, safely check if the file exists
- # Use try-except to handle any filesystem access issues
- try:
- # Use os.stat instead of exists/isfile for better security
- stat_info = os.stat(model_path)
- # Verify it's a regular file, not a directory or special file
- return stat.S_ISREG(stat_info.st_mode)
- except (OSError, IOError):
- # File doesn't exist or isn't accessible
- return False
- except (OSError, ValueError) as e:
+ # After validation, check if it's a regular file
+ # Path.is_file() internally handles the stat call safely
+ return model_path.is_file()
+
+ except (OSError, ValueError, RuntimeError) as e:
logger.debug(f"Invalid model path: {e}")
return False
except Exception: