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.

16.0 KB
Raw Blame
"""Security-header coverage restored from main's deleted Flask suites.

Ports the still-applicable half of ``tests/security/test_security_headers.py``
and ``tests/security/test_security_headers_gaps.py`` (both deleted by the
FastAPI migration). Everything those files asserted about *values* of the
headers on the ordinary HTML/API surfaces is already re-pinned by
``tests/web/test_security_headers.py``; everything about Flask internals
(``SecurityHeaders(app)``, ``init_app``, ``after_request``, ``_add_cors_headers``
on a ``flask.Response``, the ``SECURITY_CSP_CONNECT_SRC`` /
``SECURITY_COEP_POLICY`` config knobs) died with Flask. What was left
unguarded on this branch, and is restored here, is:

1. ``TestSecurityHeadersResponseCoverage`` — main's fence that the headers
   fire on *every response path*, not just the happy ones. The branch
   covers 200/404-HTML/405/422/CSRF-403/unhandled-500
   (``tests/web/test_middleware_order_and_headers.py``), but NOT the four
   remaining classes main pinned: the JSON branch of the 404 handler, the
   ``/favicon.ico`` ``FileResponse`` route, a ``StreamingResponse`` (SSE),
   and the ``413`` that ``BodySizeLimitMiddleware`` emits *itself*, before
   routing, on both its content-negotiated branches. Each is a response
   built by a different layer; each could lose the headers independently.

2. CORS response hygiene from the gaps file that
   ``tests/web/test_cors_config.py`` does not assert: ``Vary: Origin`` on a
   reflected (per-origin) ``Access-Control-Allow-Origin`` — without it a
   shared cache can hand one origin's ACAO to another — its deliberate
   absence for the fixed wildcard, ``Access-Control-Max-Age``, the
   never-credentials invariant for explicit origins, and main's
   "CORS and security headers coexist" check.

Style/values follow ``tests/web/test_security_headers.py``: exact header
values, imported from that module so the two cannot drift (they are test-owned
literals there, deliberately not read from ``SecurityHeadersMiddleware``'s
constants, so weakening a constant still fails).
"""

from unittest.mock import patch

import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from starlette.responses import JSONResponse, StreamingResponse

from local_deep_research.web.fastapi_app import (
    SecurityHeadersMiddleware,
    _configure_cors,
)
from local_deep_research.web.fastapi_app import app as _live_app

# Test-owned literals (NOT the middleware's constants) — see that module's
# docstring. Reused rather than re-copied so a value only has to be updated
# in one place when it legitimately changes.
from tests.web.test_security_headers import (
    EXPECTED_NO_STORE,
    _assert_exact_headers as _assert_policy_headers,
)

ENV_SETTING = "local_deep_research.settings.env_registry.get_env_setting"

# Any Content-Length above BodySizeLimitMiddleware's smallest cap (16 MB for
# non-multipart bodies). Declared rather than actually sent: the middleware's
# fast path rejects on the declared Content-Length before the body is read,
# which is exactly the branch main's ``abort(413)`` test stood in for.
OVER_CAP_CONTENT_LENGTH = "999999999"


# ---------------------------------------------------------------------------
# Test-only probe route on the live `app` singleton, registered at import
# time — same technique (and same `/__` prefix, which the route sweeps in
# tests/web/routers/test_all_endpoints.py and test_full_surface_smoke.py
# explicitly skip) as tests/web/test_middleware_order_and_headers.py. The
# app has no anonymous streaming endpoint, and the point of the test is the
# response *class*, not any particular route.
# ---------------------------------------------------------------------------


@_live_app.get("/__sec_hdr_probe__/sse", include_in_schema=False)
async def _sec_hdr_probe_sse():
    async def _generate():
        yield "data: one\n\n"
        yield "data: two\n\n"

    return StreamingResponse(_generate(), media_type="text/event-stream")


@pytest.fixture
def client(app):
    """Plain-HTTP TestClient on the real, fully-wired app."""
    return TestClient(app, raise_server_exceptions=False)


def _assert_exact_headers(resp, label):
    _assert_policy_headers(resp, f"{label} (status={resp.status_code})")


# ---------------------------------------------------------------------------
# 1. Every response path still carries the headers
# ---------------------------------------------------------------------------


class TestHeadersOnRemainingResponsePaths:
    """The response classes main pinned that this branch did not.

    Each assertion is on an exact value, and each response is proven to be
    the intended one (status + body/content-type) first, so none of these
    can pass against a response that never happened.
    """

    def test_headers_on_api_404_json_branch(self, client):
        """The 404 handler branches on ``_is_api_request``; the JSON side
        builds its own ``JSONResponse`` and is a separate code path from
        the HTML 404 already covered in tests/web/test_security_headers.py.
        """
        resp = client.get("/api/does-not-exist-zzz-12345")
        assert resp.status_code == 404
        assert resp.json() == {"error": "Not found"}
        _assert_exact_headers(resp, "API 404 (JSON branch)")
        assert resp.headers.get("cache-control") == EXPECTED_NO_STORE
        assert "server" not in resp.headers

    def test_headers_on_favicon_route(self, client):
        """``/favicon.ico`` is its own route returning a ``FileResponse``
        (or a JSON 404 when the file is absent, as in a test data dir).
        It is NOT under ``/static/``, so it also keeps the no-store cache
        headers. Either status must carry the security headers — main
        asserted the same way, for the same reason."""
        resp = client.get("/favicon.ico")
        assert resp.status_code in (200, 404)
        _assert_exact_headers(resp, "/favicon.ico")
        assert resp.headers.get("cache-control") == EXPECTED_NO_STORE

    def test_headers_on_streaming_sse_response(self, client):
        """A ``StreamingResponse`` sends ``http.response.start`` once and
        then many body messages; the middleware's send wrapper must stamp
        the first one. Main covered this because the app's SSE progress
        endpoints (library/rag) are real streaming responses."""
        resp = client.get("/__sec_hdr_probe__/sse")
        assert resp.status_code == 200
        assert resp.headers["content-type"].startswith("text/event-stream")
        # Proves the stream really ran rather than an early error response.
        assert resp.text == "data: one\n\ndata: two\n\n"
        _assert_exact_headers(resp, "SSE streaming response")

    def test_headers_on_413_non_api_request_too_large(self, client):
        """``BodySizeLimitMiddleware`` answers on the raw ``send`` it was
        handed, before routing and before CSRF. It sits INSIDE
        SecurityHeadersMiddleware, so its 413 must still come back
        stamped — main asserted this via ``abort(413)``."""
        resp = client.post(
            "/auth/login",
            content=b"x",
            headers={
                "Content-Length": OVER_CAP_CONTENT_LENGTH,
                "Content-Type": "application/json",
            },
        )
        assert resp.status_code == 413
        # This is the declared-length path (the Content-Length above is
        # declared, not sent -- see OVER_CAP_CONTENT_LENGTH), so it is
        # rejected before `self.app` -- and therefore SessionMiddleware --
        # ever runs. `_send_413` (fastapi_app.py) fabricates its own
        # empty-session scope regardless, so `request.session` doesn't
        # raise, and the standalone `pages/error.html` page (PR #5424)
        # renders successfully here rather than falling back to the
        # plain-text body used when rendering isn't possible.
        assert "data-error-page" in resp.text
        assert resp.headers["content-type"].startswith("text/html")
        _assert_exact_headers(resp, "413 (non-API branch)")
        assert resp.headers.get("cache-control") == EXPECTED_NO_STORE

    def test_headers_on_413_api_request_too_large(self, client):
        """The other side of the 413's content negotiation (its own copy
        of the ``/api/`` path test), which returns JSON."""
        resp = client.post(
            "/api/v1/quick_summary",
            content=b"x",
            headers={
                "Content-Length": OVER_CAP_CONTENT_LENGTH,
                "Content-Type": "application/json",
            },
        )
        assert resp.status_code == 413
        assert resp.json() == {"error": "Request too large"}
        _assert_exact_headers(resp, "413 (API branch)")
        assert resp.headers.get("cache-control") == EXPECTED_NO_STORE

    def test_api_endpoint_declares_json_content_type(self, client):
        """Main's ``test_api_endpoint_has_json_content_type``. Matters
        next to ``X-Content-Type-Options: nosniff``: an API response that
        declared ``text/html`` would be *rendered* by the browser, turning
        reflected content into stored XSS, and nosniff would not save it."""
        resp = client.get("/api/v1/health")
        assert resp.status_code == 200
        assert resp.headers.get("content-type", "").startswith(
            "application/json"
        )
        assert resp.headers.get("x-content-type-options") == "nosniff"


# ---------------------------------------------------------------------------
# 2. CORS response hygiene
# ---------------------------------------------------------------------------


def _cors_app(configured, response_headers=None):
    """Throwaway app wired exactly like the real one: SecurityHeaders first,
    then ``_configure_cors`` (so CORS ends up OUTERMOST, as in fastapi_app).

    The real app reads ``security.cors.allowed_origins`` once at import, so —
    like tests/web/test_cors_config.py — the setting is patched and a fresh
    app is built per case.
    """
    app = FastAPI()

    @app.get("/api/v1/health")
    def health():
        return JSONResponse({"status": "ok"}, headers=response_headers or {})

    app.add_middleware(SecurityHeadersMiddleware)
    with patch(ENV_SETTING, return_value=configured):
        _configure_cors(app)
    return app


def _vary_tokens(resp):
    return [
        token.strip().lower()
        for token in resp.headers.get("vary", "").split(",")
        if token.strip()
    ]


class TestCorsVaryOrigin:
    """A reflected ACAO without ``Vary: Origin`` lets a shared cache serve
    the ACAO minted for one origin to a different one — the cross-origin
    read the whitelist exists to prevent."""

    def test_vary_origin_present_when_acao_is_reflected(self):
        client = TestClient(
            _cors_app("https://a.example.com,https://b.example.com")
        )
        resp = client.get(
            "/api/v1/health", headers={"Origin": "https://b.example.com"}
        )
        # The reflection itself must have happened, or the Vary assertion
        # below would be about a response with no ACAO at all.
        assert (
            resp.headers.get("access-control-allow-origin")
            == "https://b.example.com"
        )
        assert "origin" in _vary_tokens(resp)

    def test_vary_origin_absent_for_fixed_wildcard(self):
        """``*`` is identical for every origin, so Vary would only fragment
        caches. Paired with the positive ACAO assertion so the ``not in``
        cannot pass against an empty/absent header set."""
        client = TestClient(_cors_app("*"))
        resp = client.get(
            "/api/v1/health", headers={"Origin": "https://anything.example"}
        )
        assert resp.headers.get("access-control-allow-origin") == "*"
        assert "origin" not in _vary_tokens(resp)

    def test_vary_origin_appended_once_to_an_existing_vary(self):
        """A route that already sets ``Vary`` must keep it and gain Origin
        exactly once (a duplicate is a caching-correctness smell and hid a
        real bug in main's hand-rolled implementation)."""
        client = TestClient(
            _cors_app(
                "https://a.example.com,https://b.example.com",
                response_headers={"Vary": "Accept-Encoding"},
            )
        )
        resp = client.get(
            "/api/v1/health", headers={"Origin": "https://b.example.com"}
        )
        assert (
            resp.headers.get("access-control-allow-origin")
            == "https://b.example.com"
        )
        tokens = _vary_tokens(resp)
        assert tokens.count("origin") == 1, tokens
        assert "accept-encoding" in tokens


class TestCorsPreflightAndCredentials:
    def test_preflight_advertises_max_age(self):
        """Main pinned ``Access-Control-Max-Age: 3600``; the branch passes
        ``max_age=3600`` to CORSMiddleware but nothing asserted it, so a
        silent change to the preflight cache lifetime went unnoticed."""
        client = TestClient(_cors_app("https://a.example.com"))
        resp = client.options(
            "/api/v1/health",
            headers={
                "Origin": "https://a.example.com",
                "Access-Control-Request-Method": "POST",
            },
        )
        assert resp.status_code == 200
        assert (
            resp.headers.get("access-control-allow-origin")
            == "https://a.example.com"
        )
        assert resp.headers.get("access-control-max-age") == "3600"

    def test_credentials_never_allowed_for_explicit_origins(self):
        """``_configure_cors`` hardcodes ``allow_credentials=False`` on BOTH
        branches. tests/web/test_cors_config.py only asserts this for the
        wildcard (where the spec forbids it anyway); the explicit-origin
        branch — where a mistake would actually be honoured by browsers and
        would expose the session cookie to a whitelisted third-party origin
        — was unguarded. Main enforced it by refusing to start."""
        client = TestClient(_cors_app("https://a.example.com"))
        resp = client.get(
            "/api/v1/health", headers={"Origin": "https://a.example.com"}
        )
        assert (
            resp.headers.get("access-control-allow-origin")
            == "https://a.example.com"
        )
        assert "access-control-allow-credentials" not in resp.headers

    def test_credentials_never_allowed_on_preflight_for_explicit_origins(self):
        client = TestClient(_cors_app("https://a.example.com"))
        resp = client.options(
            "/api/v1/health",
            headers={
                "Origin": "https://a.example.com",
                "Access-Control-Request-Method": "POST",
            },
        )
        assert resp.status_code == 200
        assert (
            resp.headers.get("access-control-allow-origin")
            == "https://a.example.com"
        )
        assert "access-control-allow-credentials" not in resp.headers


class TestCorsAndSecurityHeadersCoexist:
    """Main's ``test_cors_and_security_headers_coexist``, made non-vacuous.

    Main's version only asserted the security headers (CORS was never
    configured in its fixture), so it proved nothing about coexistence.
    Here CORS really is enabled and really answers, and both header
    families are asserted on the same response.

    Note (documented, not asserted): ``_configure_cors`` runs last, so
    CORSMiddleware is OUTSIDE SecurityHeadersMiddleware — a *preflight*
    OPTIONS is answered by CORSMiddleware and therefore carries no CSP /
    frame-options. That response has no body and no credentials, so it is
    not pinned here either way; every response with content still goes
    through the full stack, which is what the assertions below cover.
    """

    def test_cors_response_still_carries_security_headers(self):
        client = TestClient(_cors_app("https://a.example.com"))
        resp = client.get(
            "/api/v1/health", headers={"Origin": "https://a.example.com"}
        )
        assert resp.status_code == 200
        assert (
            resp.headers.get("access-control-allow-origin")
            == "https://a.example.com"
        )
        _assert_exact_headers(resp, "CORS-enabled API response")
        assert resp.headers.get("cache-control") == EXPECTED_NO_STORE