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.

10.5 KB
Raw Blame
"""Exact-VALUE assertions for security headers (restored from main).

Main's tests/security/test_security_headers.py pinned the exact values of
the security headers Flask's SecurityHeaders after_request stamped
(X-Frame-Options == "SAMEORIGIN", Referrer-Policy ==
"strict-origin-when-cross-origin", HSTS only over HTTPS, ...). The
FastAPI branch kept only *presence* checks (tests/web/routers/
test_fastapi_migration.py, test_authenticated_flows.py), so a silent
weakening — e.g. X-Frame-Options flipping to ALLOW-FROM, CSP dropping
``object-src 'none'``, HSTS leaking onto plain HTTP — would go unnoticed.

Every expected value below is a LITERAL, deliberately not read from
``SecurityHeadersMiddleware``'s class constants, so editing the constant
to a weaker value fails these tests.

Representative surfaces:
- HTML page:  GET /auth/login   (unauthenticated 200 HTML)
- API JSON:   GET /api/v1/health (public JSON endpoint)
- Static:     GET /static/favicon.png (cache-control carve-out)
"""

import re

import pytest
from fastapi.testclient import TestClient

# Imported as the system under test (wiring check below); value
# assertions intentionally use literals instead of these constants.
from local_deep_research.web.fastapi_app import SecurityHeadersMiddleware

HTML_PATH = "/auth/login"
API_PATH = "/api/v1/health"

# Exact policy apart from the independently validated per-response nonce.
EXPECTED_CSP = (
    "default-src 'self'; "
    "connect-src 'self'; "
    "script-src 'self'; "
    "script-src-attr 'none'; "
    "style-src 'self'; "
    "style-src-attr 'unsafe-inline'; "
    "font-src 'self' data:; "
    "img-src 'self' data:; "
    "media-src 'self'; "
    "worker-src blob:; "
    "child-src 'self' blob:; "
    "frame-src 'self'; "
    "frame-ancestors 'self'; "
    "manifest-src 'self'; "
    "object-src 'none'; "
    "base-uri 'self'; "
    "form-action 'self';"
)

EXPECTED_PERMISSIONS_POLICY = (
    "geolocation=(), midi=(), camera=(), usb=(), "
    "magnetometer=(), accelerometer=(), gyroscope=(), "
    "microphone=(), payment=(), sync-xhr=(), document-domain=()"
)

# Exact header -> value map every non-static response must carry.
EXPECTED_EXACT_HEADERS = {
    "content-security-policy": EXPECTED_CSP,
    "x-frame-options": "SAMEORIGIN",
    "x-content-type-options": "nosniff",
    "cross-origin-opener-policy": "same-origin",
    "cross-origin-embedder-policy": "credentialless",
    "cross-origin-resource-policy": "same-origin",
    "permissions-policy": EXPECTED_PERMISSIONS_POLICY,
    "referrer-policy": "strict-origin-when-cross-origin",
}

EXPECTED_HSTS = "max-age=31536000; includeSubDomains"
EXPECTED_NO_STORE = "no-store, no-cache, must-revalidate, max-age=0"


@pytest.fixture
def http_client(app):
    """Plain-HTTP TestClient (scheme == 'http')."""
    return TestClient(app, raise_server_exceptions=False)


@pytest.fixture
def https_client(app):
    """TestClient whose requests carry scheme == 'https' (drives HSTS)."""
    return TestClient(
        app, base_url="https://testserver", raise_server_exceptions=False
    )


def _normalise_header(name, value):
    if name != "content-security-policy":
        return value
    assert isinstance(value, str)
    nonces = re.findall(r" 'nonce-([A-Za-z0-9_-]{43})'", value)
    assert len(nonces) == 2 and nonces[0] == nonces[1]
    return re.sub(r" 'nonce-[A-Za-z0-9_-]{43}'", "", value)


def _assert_exact_headers(resp, label):
    for name, expected in EXPECTED_EXACT_HEADERS.items():
        actual = _normalise_header(name, resp.headers.get(name))
        assert actual == expected, (
            f"{label}: header {name!r} expected {expected!r}, got {actual!r}"
        )


class TestExactValuesOnHtmlPage:
    """Main asserted exact values on the HTML surface ('/')."""

    def test_html_response_headers_exact(self, http_client):
        resp = http_client.get(HTML_PATH)
        assert resp.status_code == 200
        assert "text/html" in resp.headers.get("content-type", "")
        _assert_exact_headers(resp, "HTML page")

    def test_x_frame_options_is_a_deny_style_value(self, http_client):
        # Main's "values_are_secure" check: never ALLOW-FROM / empty.
        xfo = http_client.get(HTML_PATH).headers.get("x-frame-options", "")
        assert xfo in ("DENY", "SAMEORIGIN")

    def test_referrer_policy_is_not_a_leaky_value(self, http_client):
        rp = http_client.get(HTML_PATH).headers.get("referrer-policy", "")
        assert rp not in ("", "unsafe-url", "no-referrer-when-downgrade")

    def test_csp_key_directives(self, http_client):
        # Belt-and-braces on the directives main asserted individually,
        # so a partial CSP rewrite still gets a readable failure.
        csp = http_client.get(HTML_PATH).headers["content-security-policy"]
        for directive in (
            "default-src 'self'",
            "frame-ancestors 'self'",
            "object-src 'none'",
            "base-uri 'self'",
            "form-action 'self'",
        ):
            assert directive in csp, f"CSP lost directive {directive!r}"


class TestExactValuesOnApiEndpoint:
    """Main asserted the same headers on API endpoints (/api/health)."""

    def test_api_response_headers_exact(self, http_client):
        resp = http_client.get(API_PATH)
        assert resp.status_code == 200
        _assert_exact_headers(resp, "API endpoint")

    def test_permissions_policy_disables_sensitive_features(self, http_client):
        permissions = http_client.get(API_PATH).headers["permissions-policy"]
        for feature in ("geolocation=()", "microphone=()", "camera=()"):
            assert feature in permissions


class TestHstsConditional:
    """HSTS must appear ONLY over HTTPS.

    Serving Strict-Transport-Security over plain HTTP would push
    browsers to force-HTTPS a host that may only speak HTTP (the
    common LAN deployment), locking users out for max-age seconds.
    """

    def test_no_hsts_over_http_html(self, http_client):
        resp = http_client.get(HTML_PATH)
        assert "strict-transport-security" not in resp.headers

    def test_no_hsts_over_http_api(self, http_client):
        resp = http_client.get(API_PATH)
        assert "strict-transport-security" not in resp.headers

    def test_hsts_exact_value_over_https_html(self, https_client):
        resp = https_client.get(HTML_PATH)
        assert resp.headers.get("strict-transport-security") == EXPECTED_HSTS

    def test_hsts_exact_value_over_https_api(self, https_client):
        resp = https_client.get(API_PATH)
        assert resp.headers.get("strict-transport-security") == EXPECTED_HSTS

    def test_https_still_carries_all_other_security_headers(self, https_client):
        _assert_exact_headers(https_client.get(HTML_PATH), "HTTPS HTML page")


class TestCacheControlCarveOut:
    """no-store cache headers on dynamic routes; NOT on /static/."""

    def test_dynamic_html_route_is_no_store(self, http_client):
        resp = http_client.get(HTML_PATH)
        assert resp.headers.get("cache-control") == EXPECTED_NO_STORE
        assert resp.headers.get("pragma") == "no-cache"

    def test_api_route_is_no_store(self, http_client):
        resp = http_client.get(API_PATH)
        assert resp.headers.get("cache-control") == EXPECTED_NO_STORE

    def test_static_asset_not_stamped_no_store(self, http_client):
        resp = http_client.get("/static/favicon.png")
        assert resp.status_code == 200
        cache_control = resp.headers.get("cache-control", "")
        assert "no-store" not in cache_control
        assert resp.headers.get("pragma") != "no-cache"

    def test_static_asset_still_gets_security_headers(self, http_client):
        # The carve-out is cache-control ONLY — CSP & friends must stay.
        _assert_exact_headers(
            http_client.get("/static/favicon.png"), "static asset"
        )


class TestErrorResponsesKeepExactValues:
    """Error paths go through the same middleware with the same values.

    test_authenticated_flows.py only checks header *presence* on error
    responses; a weakened value on the 404/401 path would slip past it.
    """

    def test_404_response_headers_exact(self, http_client):
        resp = http_client.get("/this-path-does-not-exist-424242")
        assert resp.status_code == 404
        _assert_exact_headers(resp, "404 page")
        assert resp.headers.get("cache-control") == EXPECTED_NO_STORE

    def test_404_over_https_gets_hsts(self, https_client):
        resp = https_client.get("/this-path-does-not-exist-424242")
        assert resp.status_code == 404
        assert resp.headers.get("strict-transport-security") == EXPECTED_HSTS


class TestServerHeaderStripped:
    """The middleware must REMOVE a Server header set by inner layers.

    tests/web/routers/test_fastapi_migration.py asserts ``server`` is
    absent on the real app, but Starlette never sets one under
    TestClient, so that check passes even if the strip logic is
    deleted. Here the inner app deliberately emits ``Server: leaky``
    so the assertion only holds if the middleware strips it.
    """

    def test_server_header_emitted_by_inner_app_is_removed(self):
        async def leaky_app(scope, receive, send):
            await send(
                {
                    "type": "http.response.start",
                    "status": 200,
                    "headers": [
                        (b"server", b"leaky/1.0"),
                        (b"content-type", b"text/plain"),
                    ],
                }
            )
            await send({"type": "http.response.body", "body": b"ok"})

        client = TestClient(SecurityHeadersMiddleware(leaky_app))
        resp = client.get("/anything")
        assert resp.status_code == 200
        assert "server" not in resp.headers
        # And the middleware still stamps its own headers on the way out.
        _assert_exact_headers(resp, "wrapped leaky app")


def test_middleware_is_the_installed_source_of_headers(app):
    """Wiring check: SecurityHeadersMiddleware is installed on the app.

    Guards against the middleware being dropped from the stack while an
    upstream proxy/test double keeps header tests green.
    """
    # FastAPI builds the ASGI chain lazily; before startup
    # ``app.middleware_stack`` is None, so build it the same way the
    # server would and walk the resulting outer->inner ``.app`` chain.
    layer = app.middleware_stack
    if layer is None:
        layer = app.build_middleware_stack()
    stack = []
    seen = set()
    while layer is not None and id(layer) not in seen:
        seen.add(id(layer))
        stack.append(type(layer))
        layer = getattr(layer, "app", None)
    assert SecurityHeadersMiddleware in stack