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.

refactor: centralize local filesystem path validation in PathValidator

LearningCircuit856891075339
diff --git a/src/local_deep_research/research_library/routes/rag_routes.py b/src/local_deep_research/research_library/routes/rag_routes.py
index a3802f18f7..8564068ecd 100644
--- a/src/local_deep_research/research_library/routes/rag_routes.py
+++ b/src/local_deep_research/research_library/routes/rag_routes.py
@@ -20,7 +20,6 @@ from flask import (
from loguru import logger
import glob
import json
-import sys
import uuid
from pathlib import Path
from typing import Optional
@@ -175,6 +174,7 @@ def validate_local_index_path(user_path: str) -> Path:
Validate and sanitize a user-provided path for local folder indexing.
Prevents path traversal and blocks access to sensitive system directories.
+ Delegates to PathValidator.validate_local_filesystem_path() for centralized security.
Args:
user_path: User-provided path string
@@ -185,88 +185,7 @@ def validate_local_index_path(user_path: str) -> Path:
Raises:
ValueError: If path is invalid or points to restricted location
"""
- if not user_path or not isinstance(user_path, str):
- raise ValueError("Invalid path input")
-
- # Strip whitespace
- user_path = user_path.strip()
-
- # Expand ~ to home directory if present
- if user_path.startswith("~"):
- home_dir = Path.home()
- relative_part = user_path[2:].lstrip("/")
- if relative_part:
- user_path = str(home_dir / relative_part)
- else:
- user_path = str(home_dir)
-
- # Get user's home directory as safe base
- # This allows indexing anywhere in user's home but blocks system dirs
- home = Path.home()
-
- # Try to use PathValidator with home as base
- # If path is absolute and not in home, validate it separately
- # CodeQL: This is intentional - local file indexing on user's own machine.
- # Users are allowed to index their own filesystem. System directories are blocked below.
- if Path(user_path).is_absolute():
- # codeql[py/path-injection] - Intentional: Local filesystem indexing feature with system directory protection
- validated_path = Path(
- user_path
- ).resolve() # lgtm[py/path-injection] nosec B108
- else:
- # Use PathValidator for relative paths
- validated_path = PathValidator.validate_safe_path(
- user_path, home, allow_absolute=False
- )
-
- # Block restricted system directories (Unix and Windows)
- restricted_dirs = [
- # Unix system directories
- Path("/etc"),
- Path("/sys"),
- Path("/proc"),
- Path("/dev"),
- Path("/root"),
- Path("/boot"),
- Path("/var/log"),
- ]
-
- # Add Windows system directories if on Windows
- if sys.platform == "win32":
- # Common Windows system directories (all possible drive letters)
- for drive in ["C:", "D:", "E:"]:
- restricted_dirs.extend(
- [
- Path(f"{drive}\\Windows"),
- Path(f"{drive}\\System32"),
- Path(f"{drive}\\Program Files"),
- Path(f"{drive}\\Program Files (x86)"),
- ]
- )
-
- for restricted in restricted_dirs:
- # Check if path is under restricted directory
- is_restricted = False
-
- try:
- # Try modern Python's is_relative_to() method
- is_restricted = validated_path.is_relative_to(restricted)
- except (ValueError, AttributeError):
- # Fallback for older Python versions or path comparison issues
- # Check both Unix-style and Windows-style path separators
- path_str = str(validated_path)
- restricted_str = str(restricted)
- is_restricted = path_str.startswith(
- restricted_str + "/"
- ) or path_str.startswith(restricted_str + "\\")
-
- if is_restricted:
- logger.error(
- f"Security: Blocked attempt to index restricted directory: {validated_path}"
- )
- raise ValueError("Cannot index system directories")
-
- return validated_path
+ return PathValidator.validate_local_filesystem_path(user_path)
# Page Routes
diff --git a/src/local_deep_research/security/path_validator.py b/src/local_deep_research/security/path_validator.py
index 229d191cb5..f1d9aa831d 100644
--- a/src/local_deep_research/security/path_validator.py
+++ b/src/local_deep_research/security/path_validator.py
@@ -80,6 +80,92 @@ class PathValidator:
)
raise ValueError(f"Invalid path: {e}") from e
+ @staticmethod
+ def validate_local_filesystem_path(
+ user_path: str,
+ restricted_dirs: Optional[list[Path]] = None,
+ ) -> Path:
+ """
+ Validate a user-provided absolute filesystem path for local indexing.
+
+ This is for features like local folder indexing where users need to
+ access files anywhere on their own machine, but system directories
+ should be blocked.
+
+ Args:
+ user_path: User-provided path string (absolute or with ~)
+ restricted_dirs: List of restricted directories to block
+
+ Returns:
+ Validated and resolved Path object
+
+ Raises:
+ ValueError: If path is invalid or points to restricted location
+ """
+ import sys
+
+ if not user_path or not isinstance(user_path, str):
+ raise ValueError("Invalid path input")
+
+ user_path = user_path.strip()
+
+ # Expand ~ to home directory
+ if user_path.startswith("~"):
+ home_dir = Path.home()
+ relative_part = user_path[2:].lstrip("/")
+ if relative_part:
+ user_path = str(home_dir / relative_part)
+ else:
+ user_path = str(home_dir)
+
+ # Resolve the path (this is intentional for local filesystem access)
+ # Security is enforced via restricted_dirs check below
+ validated_path = Path(user_path).resolve()
+
+ # Default restricted directories
+ if restricted_dirs is None:
+ restricted_dirs = [
+ Path("/etc"),
+ Path("/sys"),
+ Path("/proc"),
+ Path("/dev"),
+ Path("/root"),
+ Path("/boot"),
+ Path("/var/log"),
+ ]
+ # Add Windows system directories if on Windows
+ if sys.platform == "win32":
+ for drive in ["C:", "D:", "E:"]:
+ restricted_dirs.extend(
+ [
+ Path(f"{drive}\\Windows"),
+ Path(f"{drive}\\System32"),
+ Path(f"{drive}\\Program Files"),
+ Path(f"{drive}\\Program Files (x86)"),
+ ]
+ )
+
+ # Check against restricted directories
+ for restricted in restricted_dirs:
+ is_restricted = False
+ try:
+ is_restricted = validated_path.is_relative_to(restricted)
+ except (ValueError, AttributeError):
+ # Fallback for older Python or path comparison issues
+ path_str = str(validated_path)
+ restricted_str = str(restricted)
+ is_restricted = path_str.startswith(
+ restricted_str + "/"
+ ) or path_str.startswith(restricted_str + "\\")
+
+ if is_restricted:
+ logger.error(
+ f"Security: Blocked access to restricted directory: {validated_path}"
+ )
+ raise ValueError("Cannot access system directories")
+
+ return validated_path
+
@staticmethod
def validate_model_path(
model_path: str, model_root: Optional[str] = None