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 safe_join to sanitize paths for CodeQL compliance

LearningCircuit4da20a1e071f
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 aa0895bf0f..9e912bc2ce 100644
--- a/src/local_deep_research/research_library/routes/rag_routes.py
+++ b/src/local_deep_research/research_library/routes/rag_routes.py
@@ -979,17 +979,15 @@ def index_local_library():
# Validate and sanitize the path to prevent traversal attacks
try:
- path = PathValidator.validate_local_filesystem_path(folder_path)
+ validated_path = PathValidator.validate_local_filesystem_path(
+ folder_path
+ )
+ # Re-sanitize for static analyzer recognition (CodeQL)
+ path = PathValidator.sanitize_for_filesystem_ops(validated_path)
except ValueError as e:
logger.warning(f"Path validation failed for '{folder_path}': {e}")
return jsonify({"success": False, "error": "Invalid path"}), 400
- # Explicit safety check for static analyzers - path was validated above
- path_str = str(path)
- if ".." in path_str or not path.is_absolute():
- logger.warning(f"Path safety check failed for: {path_str}")
- return jsonify({"success": False, "error": "Invalid path"}), 400
-
# Check path exists and is a directory
if not path.exists():
return jsonify({"success": False, "error": "Path does not exist"}), 400
diff --git a/src/local_deep_research/security/path_validator.py b/src/local_deep_research/security/path_validator.py
index f97fb253ff..fa0b6f86fc 100644
--- a/src/local_deep_research/security/path_validator.py
+++ b/src/local_deep_research/security/path_validator.py
@@ -5,11 +5,13 @@ This module provides secure path validation to prevent path traversal attacks
and other filesystem-based security vulnerabilities.
"""
+import os
import re
from pathlib import Path
from typing import Optional, Union
-from werkzeug.security import safe_join
+
from loguru import logger
+from werkzeug.security import safe_join
class PathValidator:
@@ -137,9 +139,30 @@ class PathValidator:
if ".." in user_path:
raise ValueError("Path traversal patterns not allowed")
- # Resolve the path - this normalizes and makes absolute
- # Security is enforced via restricted_dirs check below
- validated_path = Path(user_path).resolve()
+ # Use safe_join to sanitize the path - this is recognized by static analyzers
+ # For absolute paths, we validate against the root directory
+ if user_path.startswith("/"):
+ # Unix absolute path - use safe_join with root
+ safe_path = safe_join("/", user_path.lstrip("/"))
+ if safe_path is None:
+ raise ValueError("Invalid path - failed security validation")
+ validated_path = Path(safe_path).resolve()
+ elif len(user_path) > 2 and user_path[1] == ":":
+ # Windows absolute path (e.g., C:\Users\...)
+ drive = user_path[:2]
+ rest = user_path[2:].lstrip("\\").lstrip("/")
+ safe_path = safe_join(drive + "\\", rest)
+ if safe_path is None:
+ raise ValueError("Invalid path - failed security validation")
+ validated_path = Path(safe_path).resolve()
+ else:
+ # Relative path - resolve relative to current directory
+ # Use safe_join to validate
+ cwd = os.getcwd()
+ safe_path = safe_join(cwd, user_path)
+ if safe_path is None:
+ raise ValueError("Invalid path - failed security validation")
+ validated_path = Path(safe_path).resolve()
# Default restricted directories
if restricted_dirs is None:
@@ -174,6 +197,41 @@ class PathValidator:
return validated_path
+ @staticmethod
+ def sanitize_for_filesystem_ops(validated_path: Path) -> Path:
+ """
+ Re-sanitize a validated path for static analyzer recognition.
+
+ This method takes an already-validated Path and passes it through
+ werkzeug's safe_join to create a path that static analyzers like
+ CodeQL recognize as sanitized.
+
+ Note: This exists because CodeQL doesn't trace through custom validation
+ functions. The path is already secure from validate_local_filesystem_path(),
+ but safe_join makes that explicit to static analyzers.
+
+ Args:
+ validated_path: A Path that has already been validated by
+ validate_local_filesystem_path()
+
+ Returns:
+ A Path object safe for filesystem operations
+
+ Raises:
+ ValueError: If the path fails sanitization
+ """
+ if not validated_path.is_absolute():
+ raise ValueError("Path must be absolute")
+
+ # Use safe_join to create a sanitized path that static analyzers recognize
+ # safe_join handles path traversal detection properly (not substring matching)
+ path_str = str(validated_path)
+ safe_path_str = safe_join("/", path_str.lstrip("/"))
+ if safe_path_str is None:
+ raise ValueError("Path failed security sanitization")
+
+ return Path(safe_path_str)
+
@staticmethod
def validate_model_path(
model_path: str, model_root: Optional[str] = None