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.

test: verify security headers fire across all response paths (#4150)

LearningCircuitd91b19004d20
diff --git a/tests/security/test_security_headers.py b/tests/security/test_security_headers.py
index 7e8d738a3a..b245221604 100644
--- a/tests/security/test_security_headers.py
+++ b/tests/security/test_security_headers.py
@@ -576,3 +576,100 @@ class TestContentTypeHeaders:
response = client.get("/api/health")
content_type = response.headers.get("Content-Type", "")
assert "application/json" in content_type
+
+
+class TestSecurityHeadersResponseCoverage:
+ """Pin that security headers fire across every response path Flask
+ produces, not just successful HTML routes.
+
+ SecurityHeaders registers app.after_request, which Flask invokes on
+ every Response — including error handlers, streaming responses, and
+ static file routes. These tests catch silent regressions if anyone
+ refactors error handling, adds WSGI middleware that strips headers,
+ or returns a raw response that somehow bypasses the after_request
+ chain.
+ """
+
+ REQUIRED_HEADERS = (
+ "Content-Security-Policy",
+ "X-Frame-Options",
+ "X-Content-Type-Options",
+ "Referrer-Policy",
+ "Permissions-Policy",
+ )
+
+ def _assert_required_headers(self, response):
+ missing = [
+ h for h in self.REQUIRED_HEADERS if h not in response.headers
+ ]
+ assert not missing, (
+ f"Response (status={response.status_code}) missing security "
+ f"headers: {missing}. Present headers: {list(response.headers.keys())}"
+ )
+
+ def test_headers_on_unknown_path_404(self, client):
+ """404 from the global errorhandler must still carry headers."""
+ response = client.get("/this-path-does-not-exist-zzz-12345")
+ assert response.status_code == 404
+ self._assert_required_headers(response)
+
+ def test_headers_on_unknown_api_path_404(self, client):
+ """404 on an /api/ path (JSON branch of the errorhandler)."""
+ response = client.get("/api/does-not-exist-zzz-12345")
+ assert response.status_code == 404
+ self._assert_required_headers(response)
+
+ def test_headers_on_favicon(self, client):
+ """The custom /favicon.ico route uses send_from_directory."""
+ response = client.get("/favicon.ico")
+ # Status may be 200 (file exists) or 404 (file missing in test
+ # fixture). Either path must have headers applied.
+ self._assert_required_headers(response)
+
+ def test_headers_on_unhandled_exception_500(self, app, client):
+ """The 500 errorhandler returns make_response — headers must apply."""
+ # TESTING=True normally propagates exceptions; turn that off so
+ # the errorhandler runs.
+ app.config["PROPAGATE_EXCEPTIONS"] = False
+
+ @app.route("/_test_force_500")
+ def _force_500():
+ raise RuntimeError("forced exception for header coverage test")
+
+ try:
+ response = client.get("/_test_force_500")
+ assert response.status_code == 500
+ self._assert_required_headers(response)
+ finally:
+ app.config["PROPAGATE_EXCEPTIONS"] = True
+
+ def test_headers_on_streaming_sse_response(self, app, client):
+ """Streaming Response(stream_with_context(...)) — used by SSE
+ endpoints in library_routes.py and rag_routes.py."""
+ from flask import Response, stream_with_context
+
+ @app.route("/_test_stream_sse")
+ def _stream():
+ def generate():
+ yield "data: one\n\n"
+ yield "data: two\n\n"
+
+ return Response(
+ stream_with_context(generate()), mimetype="text/event-stream"
+ )
+
+ response = client.get("/_test_stream_sse")
+ assert response.status_code == 200
+ self._assert_required_headers(response)
+
+ def test_headers_on_abort_413_request_too_large(self, app, client):
+ """Custom 413 errorhandler returns make_response."""
+ from flask import abort
+
+ @app.route("/_test_force_413")
+ def _force_413():
+ abort(413)
+
+ response = client.get("/_test_force_413")
+ assert response.status_code == 413
+ self._assert_required_headers(response)