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 comprehensive HTTP security headers to address OWASP ZAP findings

LearningCircuitfebd6ce8ce67
diff --git a/src/local_deep_research/security/__init__.py b/src/local_deep_research/security/__init__.py
index f03e9ee2cd..a2d71c3c28 100644
--- a/src/local_deep_research/security/__init__.py
+++ b/src/local_deep_research/security/__init__.py
@@ -1,5 +1,6 @@
"""Security utilities for Local Deep Research."""
from .path_validator import PathValidator
+from .security_headers import SecurityHeaders
-__all__ = ["PathValidator"]
+__all__ = ["PathValidator", "SecurityHeaders"]
diff --git a/src/local_deep_research/security/security_headers.py b/src/local_deep_research/security/security_headers.py
new file mode 100644
index 0000000000..b43c4d59fe
--- /dev/null
+++ b/src/local_deep_research/security/security_headers.py
@@ -0,0 +1,142 @@
+"""Security headers middleware for Flask applications.
+
+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 loguru import logger
+
+
+class SecurityHeaders:
+ """Configure and apply security headers to Flask responses.
+
+ Addresses security vulnerabilities:
+ - CSP: Content Security Policy to prevent XSS
+ - Clickjacking: X-Frame-Options protection
+ - MIME sniffing: X-Content-Type-Options
+ - Spectre: Cross-Origin policies
+ - Feature abuse: Permissions-Policy
+ - Information leakage: Server header removal
+ """
+
+ def __init__(self, app=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)
+
+ def init_app(self, app):
+ """Initialize the Flask application with security headers.
+
+ Args:
+ app: Flask application instance
+ """
+ app.after_request(self.add_security_headers)
+ logger.info("Security headers middleware initialized")
+
+ @staticmethod
+ def get_csp_policy() -> str:
+ """Generate Content Security Policy header value.
+
+ Returns:
+ CSP policy string with directives for Socket.IO compatibility
+ """
+ return (
+ "default-src 'self'; "
+ "connect-src 'self' ws: wss: http: https:; "
+ "script-src 'self' 'unsafe-inline' 'unsafe-eval'; "
+ "style-src 'self' 'unsafe-inline'; "
+ "font-src 'self' data:; "
+ "img-src 'self' data:; "
+ "worker-src blob:; "
+ "frame-src 'self'; "
+ "object-src 'none'; "
+ "base-uri 'self'; "
+ "form-action 'self';"
+ )
+
+ @staticmethod
+ def get_permissions_policy() -> str:
+ """Generate Permissions-Policy header value.
+
+ Disables potentially dangerous browser features by default.
+
+ Returns:
+ Permissions-Policy string
+ """
+ return (
+ "geolocation=(), "
+ "midi=(), "
+ "camera=(), "
+ "usb=(), "
+ "magnetometer=(), "
+ "accelerometer=(), "
+ "gyroscope=(), "
+ "microphone=(), "
+ "payment=()"
+ )
+
+ def add_security_headers(self, response: Response) -> Response:
+ """Add comprehensive security headers to Flask response.
+
+ Args:
+ response: Flask response object
+
+ Returns:
+ Response object with security headers added
+ """
+ # Content Security Policy - prevents XSS and injection attacks
+ csp = self.get_csp_policy()
+ response.headers["Content-Security-Policy"] = csp
+
+ # Anti-clickjacking protection
+ response.headers["X-Frame-Options"] = "SAMEORIGIN"
+
+ # Prevent MIME-type sniffing
+ response.headers["X-Content-Type-Options"] = "nosniff"
+
+ # Spectre vulnerability mitigation
+ response.headers["Cross-Origin-Opener-Policy"] = "same-origin"
+ response.headers["Cross-Origin-Embedder-Policy"] = "require-corp"
+ response.headers["Cross-Origin-Resource-Policy"] = "same-origin"
+
+ # Permissions Policy - disable dangerous features
+ response.headers["Permissions-Policy"] = self.get_permissions_policy()
+
+ # Remove server version information to prevent information disclosure
+ response.headers.pop("Server", None)
+
+ # 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/"
+ ):
+ self._add_cors_headers(response)
+
+ return response
+
+ @staticmethod
+ def _add_cors_headers(response: Response) -> None:
+ """Add CORS headers for API routes.
+
+ Args:
+ response: Flask response object to modify
+ """
+ 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"
+ )
+ response.headers["Access-Control-Allow-Credentials"] = "true"
+ 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"
diff --git a/src/local_deep_research/web/app_factory.py b/src/local_deep_research/web/app_factory.py
index e08d656835..fb4cbf148f 100644
--- a/src/local_deep_research/web/app_factory.py
+++ b/src/local_deep_research/web/app_factory.py
@@ -14,6 +14,7 @@ from loguru import logger
from local_deep_research.settings.logger import log_settings
from ..utilities.log_utils import InterceptHandler
+from ..security import SecurityHeaders
# Removed DB_PATH import - using per-user databases now
from .services.socket_service import SocketIOService
@@ -98,6 +99,9 @@ def create_app():
# Exempt Socket.IO from CSRF protection
# Note: Flask-SocketIO handles CSRF internally, so we don't need to exempt specific views
+ # Initialize security headers middleware
+ SecurityHeaders(app)
+
# Disable CSRF for API routes
@app.before_request
def disable_csrf_for_api():
@@ -314,39 +318,9 @@ def apply_middleware(app):
finally:
g.db_session = None
- # Add Content Security Policy headers to allow Socket.IO to function
- @app.after_request
- def add_security_headers(response):
- # Define a permissive CSP for development that allows Socket.IO to function
- csp = (
- "default-src 'self'; "
- "connect-src 'self' ws: wss: http: https:; "
- "script-src 'self' 'unsafe-inline' 'unsafe-eval'; "
- "style-src 'self' 'unsafe-inline'; "
- "font-src 'self' data:; "
- "img-src 'self' data:; "
- "worker-src blob:; "
- "frame-src 'self';"
- )
-
- response.headers["Content-Security-Policy"] = csp
- response.headers["X-Content-Security-Policy"] = csp
-
- # Add CORS headers for API requests
- if request.path.startswith("/api/") or request.path.startswith(
- "/research/api/"
- ):
- 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"
- )
- response.headers["Access-Control-Allow-Credentials"] = "true"
- response.headers["Access-Control-Max-Age"] = "3600"
-
- return response
+ # Security headers are now handled by SecurityHeaders middleware
+ # See: local_deep_research.security.security_headers
+ # Initialized above with SecurityHeaders(app)
# Add a middleware layer to handle abrupt disconnections
@app.before_request