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: Resolve critical security vulnerabilities in PR #899

LearningCircuit75361428e5f3
diff --git a/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py b/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py
index 53f20dbb2c..31e3fd0a0b 100644
--- a/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py
+++ b/src/local_deep_research/advanced_search_system/strategies/topic_organization_strategy.py
@@ -166,7 +166,8 @@ class TopicOrganizationStrategy(BaseSearchStrategy):
# Process all sources - removed artificial limit
sources_to_process = sources
- # Shuffle sources to avoid ordering bias
+ # Shuffle sources to avoid ordering bias in topic organization
+ # Note: random.shuffle is used here for non-security purposes (preventing ML bias)
import random
sources_to_process = (
diff --git a/src/local_deep_research/research_library/routes/library_routes.py b/src/local_deep_research/research_library/routes/library_routes.py
index 2fa7c8d038..ad5080f4dc 100644
--- a/src/local_deep_research/research_library/routes/library_routes.py
+++ b/src/local_deep_research/research_library/routes/library_routes.py
@@ -36,6 +36,33 @@ from ...utilities.db_utils import get_settings_manager
library_bp = Blueprint("library", __name__, url_prefix="/library")
+def handle_api_error(operation: str, error: Exception, status_code: int = 500):
+ """
+ Handle API errors consistently - log internally, return generic message to user.
+
+ This prevents information exposure by logging full error details internally
+ while returning a generic message to the user.
+
+ Args:
+ operation: Description of the operation that failed (for logging)
+ error: The exception that occurred
+ status_code: HTTP status code to return (default: 500)
+
+ Returns:
+ Flask JSON response tuple (response, status_code)
+ """
+ # Log the full error internally with stack trace
+ logger.exception(f"Error during {operation}")
+
+ # Return generic message to user (no internal details exposed)
+ return jsonify(
+ {
+ "success": False,
+ "error": "An internal error occurred. Please try again or contact support.",
+ }
+ ), status_code
+
+
def is_downloadable_domain(url: str) -> bool:
"""Check if URL is from a downloadable academic domain using proper URL parsing."""
try:
@@ -508,11 +535,26 @@ def download_single_resource(resource_id):
@login_required
def download_text_single(resource_id):
"""Download a single resource as text file."""
- username = session.get("username")
- service = DownloadService(username)
+ try:
+ username = session.get("username")
+ service = DownloadService(username)
+
+ success, error = service.download_as_text(resource_id)
+
+ # Sanitize error message - don't expose internal details
+ if not success and error:
+ logger.warning(
+ f"Download as text failed for resource {resource_id}: {error}"
+ )
+ return jsonify(
+ {"success": False, "error": "Failed to download resource"}
+ )
- success, error = service.download_as_text(resource_id)
- return jsonify({"success": success, "error": error})
+ return jsonify({"success": success, "error": error})
+ except Exception as e:
+ return handle_api_error(
+ f"downloading resource {resource_id} as text", e
+ )
@library_bp.route("/api/download-all-text", methods=["POST"])
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 e7d9040479..5b6e79e676 100644
--- a/src/local_deep_research/research_library/routes/rag_routes.py
+++ b/src/local_deep_research/research_library/routes/rag_routes.py
@@ -20,6 +20,7 @@ from flask import (
from loguru import logger
import json
import uuid
+from pathlib import Path
from typing import Optional
from datetime import datetime, timezone
@@ -53,6 +54,94 @@ def get_rag_service() -> LibraryRAGService:
)
+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.
+
+ Args:
+ user_path: User-provided path string
+
+ Returns:
+ Validated and resolved Path object
+
+ Raises:
+ ValueError: If path is invalid or points to restricted location
+ """
+ import os
+
+ if not user_path or not isinstance(user_path, str):
+ raise ValueError("Invalid path input")
+
+ # Strip whitespace and expand user home directory
+ user_path = user_path.strip()
+
+ try:
+ # Expand ~ and resolve to absolute path
+ path = Path(user_path).expanduser().resolve()
+ except (RuntimeError, OSError) as e:
+ logger.warning(f"Path resolution failed for '{user_path}': {e}")
+ raise ValueError("Invalid path format")
+
+ # List of restricted system directories (Unix-like systems)
+ restricted_dirs = [
+ Path("/etc"),
+ Path("/sys"),
+ Path("/proc"),
+ Path("/dev"),
+ Path("/root"),
+ Path("/boot"),
+ Path("/var/log"),
+ ]
+
+ # Check if path is under any restricted directory
+ for restricted in restricted_dirs:
+ try:
+ if path.is_relative_to(restricted):
+ logger.warning(
+ f"Blocked attempt to index restricted directory: {path}"
+ )
+ raise ValueError("Cannot index system directories")
+ except (ValueError, AttributeError):
+ # is_relative_to may raise ValueError or not exist in older Python
+ # Fallback to string comparison
+ if str(path).startswith(str(restricted) + os.sep):
+ logger.warning(
+ f"Blocked attempt to index restricted directory: {path}"
+ )
+ raise ValueError("Cannot index system directories")
+
+ return path
+
+
+def handle_api_error(operation: str, error: Exception, status_code: int = 500):
+ """
+ Handle API errors consistently - log internally, return generic message to user.
+
+ This prevents information exposure by logging full error details internally
+ while returning a generic message to the user.
+
+ Args:
+ operation: Description of the operation that failed (for logging)
+ error: The exception that occurred
+ status_code: HTTP status code to return (default: 500)
+
+ Returns:
+ Flask JSON response tuple (response, status_code)
+ """
+ # Log the full error internally with stack trace
+ logger.exception(f"Error during {operation}")
+
+ # Return generic message to user (no internal details exposed)
+ return jsonify(
+ {
+ "success": False,
+ "error": "An internal error occurred. Please try again or contact support.",
+ }
+ ), status_code
+
+
# Page Routes
@@ -112,8 +201,7 @@ def get_current_settings():
}
)
except Exception as e:
- logger.exception("Error getting RAG settings")
- return jsonify({"success": False, "error": str(e)}), 500
+ return handle_api_error("getting RAG settings", e)
@rag_bp.route("/api/rag/models", methods=["GET"])
@@ -176,8 +264,7 @@ def get_available_models():
}
)
except Exception as e:
- logger.exception("Error getting available models")
- return jsonify({"success": False, "error": str(e)}), 500
+ return handle_api_error("getting available models", e)
@rag_bp.route("/api/rag/info", methods=["GET"])
@@ -195,8 +282,7 @@ def get_index_info():
return jsonify({"success": True, "info": info})
except Exception as e:
- logger.exception("Error getting index info")
- return jsonify({"success": False, "error": str(e)}), 500
+ return handle_api_error("getting index info", e)
@rag_bp.route("/api/rag/stats", methods=["GET"])
@@ -209,8 +295,7 @@ def get_rag_stats():
return jsonify({"success": True, "stats": stats})
except Exception as e:
- logger.exception("Error getting RAG stats")
- return jsonify({"success": False, "error": str(e)}), 500
+ return handle_api_error("getting RAG stats", e)
@rag_bp.route("/api/rag/index-document", methods=["POST"])
@@ -237,8 +322,7 @@ def index_document():
return jsonify({"success": True, "result": result})
except Exception as e:
- logger.exception(f"Error indexing document {text_doc_id}")
- return jsonify({"success": False, "error": str(e)}), 500
+ return handle_api_error(f"indexing document {text_doc_id}", e)
@rag_bp.route("/api/rag/remove-document", methods=["POST"])
@@ -264,8 +348,7 @@ def remove_document():
return jsonify({"success": True, "result": result})
except Exception as e:
- logger.exception(f"Error removing document {text_doc_id}")
- return jsonify({"success": False, "error": str(e)}), 500
+ return handle_api_error(f"removing document {text_doc_id}", e)
@rag_bp.route("/api/rag/index-research", methods=["POST"])
@@ -289,8 +372,7 @@ def index_research():
return jsonify({"success": True, "results": results})
except Exception as e:
- logger.exception(f"Error indexing research {research_id}")
- return jsonify({"success": False, "error": str(e)}), 500
+ return handle_api_error(f"indexing research {research_id}", e)
@rag_bp.route("/api/rag/index-all", methods=["GET"])
@@ -357,9 +439,9 @@ def index_all():
f"Bulk indexing complete: {results['successful']} successful, {results['skipped']} skipped, {results['failed']} failed"
)
- except Exception as e:
+ except Exception:
logger.exception("Error in bulk indexing")
- yield f"data: {json.dumps({'type': 'error', 'error': str(e)})}\n\n"
+ yield f"data: {json.dumps({'type': 'error', 'error': 'An internal error occurred during indexing'})}\n\n"
return Response(
stream_with_context(generate()), mimetype="text/event-stream"
@@ -420,8 +502,7 @@ def configure_rag():
)
except Exception as e:
- logger.exception("Error configuring RAG")
- return jsonify({"success": False, "error": str(e)}), 500
+ return handle_api_error("configuring RAG", e)
@rag_bp.route("/api/rag/documents", methods=["GET"])
@@ -457,15 +538,13 @@ def get_documents():
return jsonify({"success": True, "documents": documents})
except Exception as e:
- logger.exception("Error getting documents")
- return jsonify({"success": False, "error": str(e)}), 500
+ return handle_api_error("getting documents", e)
@rag_bp.route("/api/rag/index-local", methods=["GET"])
@login_required
def index_local_library():
"""Index documents from a local folder with Server-Sent Events progress."""
- from pathlib import Path
import glob
folder_path = request.args.get("path")
@@ -477,18 +556,19 @@ def index_local_library():
if not folder_path:
return jsonify({"success": False, "error": "Path is required"}), 400
- # Validate path exists and is a directory
- path = Path(folder_path).expanduser().resolve()
+ # Validate and sanitize the path to prevent traversal attacks
+ try:
+ path = validate_local_index_path(folder_path)
+ except ValueError as e:
+ logger.warning(f"Path validation failed for '{folder_path}': {e}")
+ return jsonify({"success": False, "error": "Invalid path"}), 400
+
+ # Check path exists and is a directory
if not path.exists():
- return jsonify(
- {"success": False, "error": f"Path does not exist: {folder_path}"}
- ), 400
+ return jsonify({"success": False, "error": "Path does not exist"}), 400
if not path.is_dir():
return jsonify(
- {
- "success": False,
- "error": f"Path is not a directory: {folder_path}",
- }
+ {"success": False, "error": "Path is not a directory"}
), 400
# Create RAG service in request context
@@ -545,10 +625,10 @@ def index_local_library():
"error": result.get("error", "Unknown error"),
}
)
- except Exception as e:
+ except Exception:
results["failed"] += 1
results["errors"].append(
- {"file": file_name, "error": str(e)}
+ {"file": file_name, "error": "Failed to index file"}
)
logger.exception(f"Error indexing file {file_path}")
@@ -562,9 +642,9 @@ def index_local_library():
f"{results['failed']} failed"
)
- except Exception as e:
+ except Exception:
logger.exception("Error in local library indexing")
- yield f"data: {json.dumps({'type': 'error', 'error': str(e)})}\n\n"
+ yield f"data: {json.dumps({'type': 'error', 'error': 'An internal error occurred during indexing'})}\n\n"
return Response(
stream_with_context(generate()), mimetype="text/event-stream"
@@ -621,8 +701,7 @@ def get_collections():
return jsonify({"success": True, "collections": result})
except Exception as e:
- logger.exception("Error getting collections")
- return jsonify({"success": False, "error": str(e)}), 500
+ return handle_api_error("getting collections", e)
@rag_bp.route("/api/collections", methods=["POST"])
@@ -681,8 +760,7 @@ def create_collection():
}
)
except Exception as e:
- logger.exception("Error creating collection")
- return jsonify({"success": False, "error": str(e)}), 500
+ return handle_api_error("creating collection", e)
@rag_bp.route("/api/collections/<string:collection_id>", methods=["PUT"])
@@ -752,8 +830,7 @@ def update_collection(collection_id):
}
)
except Exception as e:
- logger.exception("Error updating collection")
- return jsonify({"success": False, "error": str(e)}), 500
+ return handle_api_error("updating collection", e)
@rag_bp.route("/api/collections/<string:collection_id>", methods=["DELETE"])
@@ -824,8 +901,7 @@ def delete_collection(collection_id):
}
)
except Exception as e:
- logger.exception("Error deleting collection")
- return jsonify({"success": False, "error": str(e)}), 500
+ return handle_api_error("deleting collection", e)
def extract_text_from_file(
@@ -1090,8 +1166,13 @@ def upload_to_collection(collection_id):
}
)
- except Exception as e:
- errors.append({"filename": file.filename, "error": str(e)})
+ except Exception:
+ errors.append(
+ {
+ "filename": file.filename,
+ "error": "Failed to upload file",
+ }
+ )
logger.exception(f"Error uploading file {file.filename}")
db_session.commit()
@@ -1110,8 +1191,7 @@ def upload_to_collection(collection_id):
)
except Exception as e:
- logger.exception("Error uploading files")
- return jsonify({"success": False, "error": str(e)}), 500
+ return handle_api_error("uploading files", e)
@rag_bp.route(
@@ -1182,8 +1262,7 @@ def get_collection_documents(collection_id):
)
except Exception as e:
- logger.exception("Error getting collection documents")
- return jsonify({"success": False, "error": str(e)}), 500
+ return handle_api_error("getting collection documents", e)
@rag_bp.route("/api/collections/<string:collection_id>/index", methods=["GET"])
@@ -1303,10 +1382,13 @@ def index_collection(collection_id):
),
}
)
- except Exception as e:
+ except Exception:
results["failed"] += 1
results["errors"].append(
- {"filename": doc.filename, "error": str(e)}
+ {
+ "filename": doc.filename,
+ "error": "Failed to index document",
+ }
)
logger.exception(
f"Error indexing document {doc.filename}"
@@ -1316,9 +1398,9 @@ def index_collection(collection_id):
yield f"data: {json.dumps({'type': 'complete', 'results': results})}\n\n"
- except Exception as e:
+ except Exception:
logger.exception("Error in collection indexing")
- yield f"data: {json.dumps({'type': 'error', 'error': str(e)})}\n\n"
+ yield f"data: {json.dumps({'type': 'error', 'error': 'An internal error occurred during indexing'})}\n\n"
return Response(
stream_with_context(generate()), mimetype="text/event-stream"
diff --git a/src/local_deep_research/web/services/research_service.py b/src/local_deep_research/web/services/research_service.py
index 6c7245adb8..508923847b 100644
--- a/src/local_deep_research/web/services/research_service.py
+++ b/src/local_deep_research/web/services/research_service.py
@@ -308,6 +308,7 @@ def _generate_report_path(query: str) -> Path:
"""
# Generate a unique filename that does not contain
# non-alphanumeric characters.
+ # MD5 is used here for non-cryptographic purposes (filename generation) where speed matters
query_hash = hashlib.md5(query.encode("utf-8")).hexdigest()[:10]
return OUTPUT_DIR / (
f"research_report_{query_hash}_{int(datetime.now(UTC).timestamp())}.md"
diff --git a/src/local_deep_research/web/static/js/rag_manager.js b/src/local_deep_research/web/static/js/rag_manager.js
index 409569a9fd..676c0c6a8d 100644
--- a/src/local_deep_research/web/static/js/rag_manager.js
+++ b/src/local_deep_research/web/static/js/rag_manager.js
@@ -331,7 +331,7 @@ function checkSettingsMismatch() {
<div class="ldr-alert ldr-alert-warning" style="margin-top: 10px;">
<i class="fas fa-exclamation-triangle"></i>
<strong>Settings Changed!</strong><br>
- <small>${changes.join(', ')}</small><br>
+ <small>${changes.map(c => escapeHtml(c)).join(', ')}</small><br>
<small style="display: block; margin-top: 8px;">
⚠️ <strong>You'll need to re-index all documents</strong> for these changes to take effect.
The current index was created with different settings and won't work properly with the new configuration.
diff --git a/src/local_deep_research/web_search_engines/engines/search_engine_local.py b/src/local_deep_research/web_search_engines/engines/search_engine_local.py
index c67cb1b9f0..4efc8a843a 100644
--- a/src/local_deep_research/web_search_engines/engines/search_engine_local.py
+++ b/src/local_deep_research/web_search_engines/engines/search_engine_local.py
@@ -473,6 +473,7 @@ class LocalEmbeddingManager:
# Canonicalize the path so we don't have weird Windows vs. Linux
# problems or issues with trailing slashes.
canonical_folder_path = "/".join(folder_path.parts)
+ # MD5 is used here for non-cryptographic purposes (cache key generation) where speed matters
return hashlib.md5(canonical_folder_path.encode()).hexdigest()
def _get_index_path(self, folder_path: Path) -> Path:
@@ -1113,6 +1114,7 @@ class LocalSearchEngine(BaseSearchEngine):
previews = []
for i, result in enumerate(raw_results):
# Create a unique ID
+ # MD5 is used here for non-cryptographic purposes (unique ID generation) where speed matters
result_id = f"local-{i}-{hashlib.md5(result['content'][:50].encode()).hexdigest()}"
# Extract filename and path