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: address AI code review recommendations

LearningCircuit15f067a65840
diff --git a/src/local_deep_research/security/security_headers.py b/src/local_deep_research/security/security_headers.py
index f70aaa645a..835b1fcc2f 100644
--- a/src/local_deep_research/security/security_headers.py
+++ b/src/local_deep_research/security/security_headers.py
@@ -36,6 +36,19 @@ class SecurityHeaders:
Args:
app: Flask application instance
+
+ Security Configuration:
+ SECURITY_CSP_CONNECT_SRC: Restricts WebSocket/fetch connections (default: localhost only)
+ SECURITY_CORS_ENABLED: Enable CORS for API routes (default: True)
+ SECURITY_CORS_ALLOW_CREDENTIALS: Allow credentials in CORS (default: False)
+ SECURITY_CORS_ALLOWED_ORIGINS: Allowed CORS origins (default: "*")
+ SECURITY_COEP_POLICY: Cross-Origin-Embedder-Policy (default: "credentialless")
+
+ Important Security Trade-offs:
+ - CSP includes 'unsafe-inline' and 'unsafe-eval' for Socket.IO compatibility
+ - This reduces XSS protection but is necessary for real-time WebSocket functionality
+ - If Socket.IO is removed, these directives should be tightened immediately
+ - Monitor CSP violation reports to detect potential XSS attempts
"""
# Set default security configuration
app.config.setdefault(
@@ -50,6 +63,10 @@ class SecurityHeaders:
self.app = app
app.after_request(self.add_security_headers)
logger.info("Security headers middleware initialized")
+ logger.warning(
+ "CSP configured with 'unsafe-inline' and 'unsafe-eval' for Socket.IO compatibility. "
+ "This is a security trade-off - monitor for XSS attempts."
+ )
def get_csp_policy(self) -> str:
"""Generate Content Security Policy header value.
@@ -107,6 +124,12 @@ class SecurityHeaders:
Returns:
Response object with security headers added
+
+ Note:
+ Cross-Origin-Embedder-Policy (COEP) is applied globally to all routes.
+ Default is 'credentialless' which allows cross-origin requests without
+ credentials. If stricter isolation is needed, configure SECURITY_COEP_POLICY
+ to 'require-corp', but this may break cross-origin API access.
"""
# Content Security Policy - prevents XSS and injection attacks
csp = self.get_csp_policy()
@@ -118,7 +141,7 @@ class SecurityHeaders:
# Prevent MIME-type sniffing
response.headers["X-Content-Type-Options"] = "nosniff"
- # Spectre vulnerability mitigation
+ # Spectre vulnerability mitigation - applied globally
response.headers["Cross-Origin-Opener-Policy"] = "same-origin"
coep_policy = self.app.config.get(
"SECURITY_COEP_POLICY", "credentialless"
@@ -176,6 +199,17 @@ class SecurityHeaders:
"SECURITY_CORS_ALLOW_CREDENTIALS", False
)
+ # Validate multi-origin configuration with credentials
+ if allow_credentials and allowed_origins != "*":
+ # Check for comma-separated origins (invalid with credentials)
+ if "," in allowed_origins:
+ logger.error(
+ "CORS misconfiguration: Cannot use multiple origins with credentials. "
+ f"Found: '{allowed_origins}'. Only single origin allowed when credentials enabled. "
+ "Credentials disabled for security."
+ )
+ allow_credentials = False
+
response.headers["Access-Control-Allow-Origin"] = allowed_origins
response.headers["Access-Control-Allow-Methods"] = (
"GET, POST, PUT, DELETE, OPTIONS"
diff --git a/src/local_deep_research/web/app_factory.py b/src/local_deep_research/web/app_factory.py
index 7d162f06b1..d967407e2d 100644
--- a/src/local_deep_research/web/app_factory.py
+++ b/src/local_deep_research/web/app_factory.py
@@ -334,24 +334,8 @@ def apply_middleware(app):
# Return empty response to prevent further processing
return "", 200
- # Handle CORS preflight requests
- @app.before_request
- def handle_preflight():
- if request.method == "OPTIONS":
- if request.path.startswith("/api/") or request.path.startswith(
- "/research/api/"
- ):
- response = app.make_default_options_response()
- response.headers["Access-Control-Allow-Origin"] = "*"
- response.headers["Access-Control-Allow-Methods"] = (
- "GET, POST, PUT, DELETE, OPTIONS"
- )
- response.headers["Access-Control-Allow-Headers"] = (
- "Content-Type, Authorization, X-Requested-With, X-HTTP-Method-Override"
- )
- # Note: Credentials removed - cannot use with wildcard origin (security risk)
- response.headers["Access-Control-Max-Age"] = "3600"
- return response
+ # Note: CORS headers for API routes are now handled by SecurityHeaders middleware
+ # (see src/local_deep_research/security/security_headers.py)
def register_blueprints(app):