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.

3.8 KB
Raw Blame
"""Security-header assertions the FastAPI port left unpinned.

Ported from ``tests/security/test_security_headers.py`` and
``tests/security/test_security_headers_gaps.py`` on ``origin/main``, both
deleted by the migration. The large majority of those two files IS
superseded on this branch — ``tests/web/test_security_headers.py`` pins
every header value exactly (stronger than main's ``in`` checks),
``tests/web/test_cors_path_scoping.py`` pins the API-prefix scoping, and
``tests/web/test_cors_config.py`` pins origin reflection. What follows is
the residue: three properties main asserted that nothing on this branch
asserts.
"""

from starlette.middleware.cors import CORSMiddleware
from starlette.responses import PlainTextResponse
from starlette.testclient import TestClient

from local_deep_research.web.fastapi_app import (
    _CORS_API_PREFIXES,
    _PathScopedCORSMiddleware,
)

ORIGIN = "https://allowed.example"


def _cors_app():
    """Same harness as tests/web/test_cors_path_scoping.py."""

    async def inner(scope, receive, send):
        await PlainTextResponse("ok")(scope, receive, send)

    return _PathScopedCORSMiddleware(
        inner,
        prefixes=_CORS_API_PREFIXES,
        cors_factory=lambda app: CORSMiddleware(
            app,
            allow_origins=[ORIGIN],
            allow_credentials=False,
            allow_methods=["GET", "POST", "OPTIONS"],
            allow_headers=["Content-Type"],
        ),
    )


def test_static_paths_do_not_receive_cors_headers():
    """Main's ``_is_api_route`` explicitly rejected ``/static/js/app.js``
    (``test_non_api_not_detected`` / ``test_static_not_api``). The branch's
    prefix list still excludes it, but every CORS-scoping test on this
    branch checks only ``/``, ``/settings``, ``/history`` and
    ``/research/index`` — a prefix list that grew a bare ``/s`` or
    ``/static`` entry would go unnoticed. Static files are served from the
    same origin as the session cookie, so cross-origin readability of the
    JS bundle is a real widening.
    """
    client = TestClient(_cors_app())
    for path in ("/static/js/app.js", "/static/css/styles.css"):
        resp = client.get(path, headers={"Origin": ORIGIN})
        assert "access-control-allow-origin" not in resp.headers, (
            f"{path} carried CORS headers; static assets are not an API "
            f"surface and main's _is_api_route excluded them"
        )


def test_expires_header_is_zero_on_dynamic_routes():
    """``test_expires_header_present`` pinned ``Expires: 0``.

    The branch's matrix test asserts the header EXISTS (and that it is not
    duplicated), and that static assets do not get one — but nothing
    asserts its value. ``Expires`` is the HTTP/1.0 half of the no-store
    triple; a value that is a date rather than ``0`` re-enables caching
    for old intermediaries, and every existing assertion would stay green.
    """
    from local_deep_research.web.fastapi_app import SecurityHeadersMiddleware

    cache_headers = dict(SecurityHeadersMiddleware.cache_headers())
    assert cache_headers[b"expires"] == b"0"


def test_root_page_declares_html_content_type(authenticated_client):
    """``test_root_page_has_html_content_type``.

    Every content-type assertion on this branch uses ``/auth/login`` as the
    representative HTML route. ``/`` is the authenticated dashboard and is
    served by a different handler; main pinned it separately. Asserted
    against an authenticated client so the check is on the page itself,
    not on the login bounce (main's version was conditional on
    ``status == 200`` and therefore vacuous whenever the bounce happened).
    """
    resp = authenticated_client.get("/", follow_redirects=False)
    assert resp.status_code == 200, (
        f"authenticated GET / returned {resp.status_code}, not the dashboard"
    )
    assert "text/html" in resp.headers.get("content-type", "")