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: implement origin reflection for multi-origin CORS and address code quality issues

LearningCircuit0b0a45f34650
diff --git a/src/local_deep_research/security/security_headers.py b/src/local_deep_research/security/security_headers.py
index 6062adad16..92cb20180a 100644
--- a/src/local_deep_research/security/security_headers.py
+++ b/src/local_deep_research/security/security_headers.py
@@ -4,6 +4,8 @@ This module provides comprehensive HTTP security headers to protect against
common web vulnerabilities identified by OWASP ZAP and other security scanners.
"""
+from typing import Optional
+
from flask import Flask, Response, request
from loguru import logger
@@ -20,13 +22,12 @@ class SecurityHeaders:
- Information leakage: Server header removal
"""
- def __init__(self, app=None) -> None:
+ def __init__(self, app: Optional[Flask] = None) -> None:
"""Initialize security headers middleware.
Args:
app: Flask application instance (optional, can call init_app later)
"""
- self.app = app
if app is not None:
self.init_app(app)
@@ -40,14 +41,18 @@ class SecurityHeaders:
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")
+ SECURITY_CORS_ALLOWED_ORIGINS: Allowed CORS origins, comma-separated for multiple
+ (default: "*"). Multi-origin uses origin reflection.
+ SECURITY_COEP_POLICY: Cross-Origin-Embedder-Policy applied globally to ALL routes
+ (default: "credentialless"). Options: require-corp, credentialless, unsafe-none
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
+ - COEP policy is applied globally, not per-route. 'credentialless' allows cross-origin
+ requests without credentials. Use 'require-corp' for stricter isolation.
"""
# Set default security configuration
app.config.setdefault(
@@ -204,16 +209,16 @@ class SecurityHeaders:
"Set SECURITY_CORS_ALLOWED_ORIGINS to a specific origin or disable credentials."
)
- # Validate credentials with multi-origin strings
+ # Log info about multi-origin + credentials configuration
if allow_credentials and "," in allowed_origins:
- raise ValueError(
- f"CORS misconfiguration: Cannot use credentials with multiple origins. "
- f"Found: '{allowed_origins}'. Only a single origin is allowed when credentials are enabled. "
- f"Either disable credentials or specify a single origin."
+ logger.info(
+ f"CORS configured with multiple origins and credentials enabled. "
+ f"Origins: {allowed_origins}. "
+ f"Using origin reflection pattern for security."
)
def _add_cors_headers(self, response: Response) -> Response:
- """Add CORS headers for API routes.
+ """Add CORS headers for API routes using origin reflection for multi-origin support.
Args:
response: Flask response object to modify
@@ -222,29 +227,41 @@ class SecurityHeaders:
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.
+ - Uses "origin reflection" pattern for multi-origin support (comma-separated origins config)
+ - Reflects the requesting origin back if it matches the whitelist
+ - Wildcard origin (*) disables credentials per CORS spec
+ - Single origin or reflected origins allow credentials if configured
"""
- allowed_origins = self.app.config.get(
+ configured_origins = self.app.config.get(
"SECURITY_CORS_ALLOWED_ORIGINS", "*"
)
allow_credentials = self.app.config.get(
"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."
+ # Determine which origin to send back
+ origin_to_send = configured_origins
+
+ # If multiple origins configured (comma-separated), use origin reflection
+ if "," in configured_origins:
+ request_origin = request.headers.get("Origin")
+ # Parse configured origins into a set
+ allowed_origins_set = {
+ origin.strip() for origin in configured_origins.split(",")
+ }
+
+ # Reflect the request origin if it's in the whitelist
+ if request_origin and request_origin in allowed_origins_set:
+ origin_to_send = request_origin
+ else:
+ # Request origin not in whitelist - deny by not setting CORS headers
+ logger.warning(
+ f"CORS request from unauthorized origin: {request_origin}. "
+ f"Allowed origins: {configured_origins}"
)
- allow_credentials = False
+ return response
- response.headers["Access-Control-Allow-Origin"] = allowed_origins
+ response.headers["Access-Control-Allow-Origin"] = origin_to_send
response.headers["Access-Control-Allow-Methods"] = (
"GET, POST, PUT, DELETE, OPTIONS"
)
@@ -252,14 +269,10 @@ class SecurityHeaders:
"Content-Type, Authorization, X-Requested-With, X-HTTP-Method-Override"
)
- # Critical: Only set credentials if NOT using wildcard origin
- if allow_credentials and allowed_origins != "*":
+ # Set credentials if configured and not using wildcard
+ # Note: Startup validation ensures credentials is False with wildcard or multi-origin
+ if allow_credentials and origin_to_send != "*":
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"