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 findings for security headers

LearningCircuit9c7a668aed45
diff --git a/src/local_deep_research/security/security_headers.py b/src/local_deep_research/security/security_headers.py
index b43c4d59fe..f70aaa645a 100644
--- a/src/local_deep_research/security/security_headers.py
+++ b/src/local_deep_research/security/security_headers.py
@@ -4,7 +4,7 @@ This module provides comprehensive HTTP security headers to protect against
common web vulnerabilities identified by OWASP ZAP and other security scanners.
"""
-from flask import Response, request
+from flask import Flask, Response, request
from loguru import logger
@@ -27,28 +27,44 @@ class SecurityHeaders:
app: Flask application instance (optional, can call init_app later)
"""
self.app = app
+ self.config = {}
if app is not None:
self.init_app(app)
- def init_app(self, app):
+ def init_app(self, app: Flask):
"""Initialize the Flask application with security headers.
Args:
app: Flask application instance
"""
+ # Set default security configuration
+ app.config.setdefault(
+ "SECURITY_CSP_CONNECT_SRC",
+ "'self' ws://localhost:* wss://localhost:* ws://127.0.0.1:* wss://127.0.0.1:*",
+ )
+ app.config.setdefault("SECURITY_CORS_ENABLED", True)
+ app.config.setdefault("SECURITY_CORS_ALLOW_CREDENTIALS", False)
+ app.config.setdefault("SECURITY_CORS_ALLOWED_ORIGINS", "*")
+ app.config.setdefault("SECURITY_COEP_POLICY", "credentialless")
+
+ self.app = app
app.after_request(self.add_security_headers)
logger.info("Security headers middleware initialized")
- @staticmethod
- def get_csp_policy() -> str:
+ def get_csp_policy(self) -> str:
"""Generate Content Security Policy header value.
Returns:
CSP policy string with directives for Socket.IO compatibility
+
+ Note:
+ - 'unsafe-inline' and 'unsafe-eval' are required for Socket.IO compatibility
+ - connect-src is restricted to localhost WebSocket connections by default
"""
+ connect_src = self.app.config.get("SECURITY_CSP_CONNECT_SRC", "'self'")
return (
"default-src 'self'; "
- "connect-src 'self' ws: wss: http: https:; "
+ f"connect-src {connect_src}; "
"script-src 'self' 'unsafe-inline' 'unsafe-eval'; "
"style-src 'self' 'unsafe-inline'; "
"font-src 'self' data:; "
@@ -78,7 +94,9 @@ class SecurityHeaders:
"accelerometer=(), "
"gyroscope=(), "
"microphone=(), "
- "payment=()"
+ "payment=(), "
+ "sync-xhr=(), "
+ "document-domain=()"
)
def add_security_headers(self, response: Response) -> Response:
@@ -102,7 +120,10 @@ class SecurityHeaders:
# Spectre vulnerability mitigation
response.headers["Cross-Origin-Opener-Policy"] = "same-origin"
- response.headers["Cross-Origin-Embedder-Policy"] = "require-corp"
+ coep_policy = self.app.config.get(
+ "SECURITY_COEP_POLICY", "credentialless"
+ )
+ response.headers["Cross-Origin-Embedder-Policy"] = coep_policy
response.headers["Cross-Origin-Resource-Policy"] = "same-origin"
# Permissions Policy - disable dangerous features
@@ -114,29 +135,64 @@ class SecurityHeaders:
# Referrer Policy - control referrer information
response.headers["Referrer-Policy"] = "strict-origin-when-cross-origin"
- # Add CORS headers for API requests
- if request.path.startswith("/api/") or request.path.startswith(
- "/research/api/"
+ # Add CORS headers for API requests if enabled
+ if self._is_api_route(request.path) and self.app.config.get(
+ "SECURITY_CORS_ENABLED", True
):
- self._add_cors_headers(response)
+ response = self._add_cors_headers(response)
return response
@staticmethod
- def _add_cors_headers(response: Response) -> None:
+ def _is_api_route(path: str) -> bool:
+ """Check if the request path is an API route.
+
+ Args:
+ path: Request path to check
+
+ Returns:
+ True if path is an API route
+ """
+ return path.startswith("/api/") or path.startswith("/research/api/")
+
+ def _add_cors_headers(self, response: Response) -> Response:
"""Add CORS headers for API routes.
Args:
response: Flask response object to modify
+
+ Returns:
+ Response object with CORS headers added
+
+ Security Note:
+ When using wildcard origin (*), credentials MUST be disabled to prevent
+ security vulnerabilities. If credentials are needed, specific origins must
+ be whitelisted instead of using wildcard.
"""
- response.headers["Access-Control-Allow-Origin"] = "*"
+ allowed_origins = self.app.config.get(
+ "SECURITY_CORS_ALLOWED_ORIGINS", "*"
+ )
+ allow_credentials = self.app.config.get(
+ "SECURITY_CORS_ALLOW_CREDENTIALS", False
+ )
+
+ response.headers["Access-Control-Allow-Origin"] = allowed_origins
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"
)
- response.headers["Access-Control-Allow-Credentials"] = "true"
+
+ # Critical: Only set credentials if NOT using wildcard origin
+ if allow_credentials and allowed_origins != "*":
+ response.headers["Access-Control-Allow-Credentials"] = "true"
+ elif allow_credentials and allowed_origins == "*":
+ logger.warning(
+ "CORS misconfiguration: Cannot use credentials with wildcard origin. "
+ "Credentials disabled for security."
+ )
+
response.headers["Access-Control-Max-Age"] = "3600"
- # Relax COEP for API routes to allow cross-origin requests
- response.headers["Cross-Origin-Embedder-Policy"] = "unsafe-none"
+
+ return response
diff --git a/src/local_deep_research/web/app_factory.py b/src/local_deep_research/web/app_factory.py
index 2ebc978620..7d162f06b1 100644
--- a/src/local_deep_research/web/app_factory.py
+++ b/src/local_deep_research/web/app_factory.py
@@ -349,7 +349,7 @@ def apply_middleware(app):
response.headers["Access-Control-Allow-Headers"] = (
"Content-Type, Authorization, X-Requested-With, X-HTTP-Method-Override"
)
- response.headers["Access-Control-Allow-Credentials"] = "true"
+ # Note: Credentials removed - cannot use with wildcard origin (security risk)
response.headers["Access-Control-Max-Age"] = "3600"
return response