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(security): resolve CodeQL URL redirection vulnerability #6685

LearningCircuit53a1a585800e
diff --git a/src/local_deep_research/security/url_validator.py b/src/local_deep_research/security/url_validator.py
index 7c91601f0a..0c88b1d403 100644
--- a/src/local_deep_research/security/url_validator.py
+++ b/src/local_deep_research/security/url_validator.py
@@ -7,7 +7,7 @@ data exfiltration, and other URL-based security vulnerabilities.
import re
from typing import Optional, List
-from urllib.parse import urlparse
+from urllib.parse import urlparse, urljoin
from loguru import logger
@@ -342,6 +342,34 @@ class URLValidator:
raise
raise URLValidationError(f"Failed to validate URL: {e}")
+ @staticmethod
+ def is_safe_redirect_url(target: str, host_url: str) -> bool:
+ """
+ Validate that a redirect target is safe (same host).
+
+ Prevents open redirect attacks by ensuring the target URL
+ is either relative or points to the same host as the application.
+
+ Uses the standard Flask pattern from:
+ https://github.com/fengsp/flask-snippets/blob/master/security/redirect_back.py
+
+ Args:
+ target: The redirect URL to validate (can be relative or absolute)
+ host_url: The application's host URL (e.g., request.host_url)
+
+ Returns:
+ True if the URL is safe to redirect to, False otherwise
+ """
+ if not target:
+ return False
+
+ ref_url = urlparse(host_url)
+ test_url = urlparse(urljoin(host_url, target))
+ return (
+ test_url.scheme in ("http", "https")
+ and ref_url.netloc == test_url.netloc
+ )
+
def get_javascript_url_validator() -> str:
"""
diff --git a/src/local_deep_research/web/auth/routes.py b/src/local_deep_research/web/auth/routes.py
index 34858b134a..74d9efba68 100644
--- a/src/local_deep_research/web/auth/routes.py
+++ b/src/local_deep_research/web/auth/routes.py
@@ -4,7 +4,6 @@ Uses SQLCipher encrypted databases with browser password manager support.
"""
from datetime import datetime, timezone, UTC
-from pathlib import PurePosixPath
from flask import (
Blueprint,
@@ -24,6 +23,7 @@ from ...database.models.auth import User
from .session_manager import SessionManager
from ..server_config import load_server_config
from ..utils.rate_limiter import login_limit, registration_limit
+from ...security.url_validator import URLValidator
auth_bp = Blueprint("auth", __name__, url_prefix="/auth")
session_manager = SessionManager()
@@ -194,62 +194,14 @@ def login():
logger.exception("Failed to clear model cache on login")
# Redirect to original requested page or dashboard
- # Validate redirect URL to prevent open redirect vulnerability
- from urllib.parse import urlparse
-
- next_page = request.args.get("next", url_for("index"))
-
- # Parse the URL and validate its structure
- parsed_url = urlparse(next_page)
-
- # Whitelist of allowed paths - add more as needed
- allowed_paths = {
- "/",
- "/dashboard",
- "/profile",
- "/settings",
- "/research",
- "/history",
- "/metrics",
- "/benchmark",
- }
-
- # Also allow any path generated by url_for (they start with /)
- safe_redirect = url_for("index") # Default safe URL
-
- # Validate the URL: must be relative (no netloc) and safe
- if next_page and not parsed_url.netloc and not parsed_url.scheme:
- # Normalize the path to prevent tricks like //example.com
- # Using PurePosixPath for URL path normalization (not filesystem paths)
- normalized_path = (
- str(PurePosixPath(parsed_url.path)) if parsed_url.path else ""
- )
+ next_page = request.args.get("next", "")
- # Check if it's in the whitelist or is a safe relative path
- if parsed_url.path in allowed_paths:
- safe_redirect = parsed_url.path # Use parsed path, not raw input
- elif (
- parsed_url.path
- and parsed_url.path.startswith("/")
- and not parsed_url.path.startswith("//")
- ):
- # Additional checks for safety
- if ".." not in parsed_url.path and normalized_path.startswith("/"):
- safe_redirect = (
- parsed_url.path
- ) # Use parsed path, not raw input
- else:
- logger.warning(
- f"Blocked potentially unsafe redirect with path traversal: {next_page}"
- )
- else:
- logger.warning(
- f"Blocked potentially unsafe redirect to: {next_page}"
- )
- elif next_page:
- logger.warning(f"Blocked redirect with protocol/domain: {next_page}")
+ if next_page and URLValidator.is_safe_redirect_url(
+ next_page, request.host_url
+ ):
+ return redirect(next_page)
- return redirect(safe_redirect)
+ return redirect(url_for("index"))
@auth_bp.route("/register", methods=["GET"])