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.7 KB
Raw Blame
"""Regression fences for three FastAPI-migration bugs, all fixed in
``web/fastapi_app.py``:

1. "Remember me" not shortening server-side session validity —
   ``SessionMiddleware`` is built with a single flat ``max_age`` (the
   ``security.session_remember_me_days`` window, ~30 days) applied to
   EVERY session's itsdangerous signature, regardless of whether the
   user ticked "remember me". ``RememberMeMiddleware`` only strips the
   *browser-facing* ``Max-Age``/``Expires`` cookie attributes for
   non-remember-me logins — the raw, itsdangerous-signed cookie VALUE
   stayed independently replayable for the full 30-day window. Fixed by
   ``_enforce_session_expiry`` / ``_stamp_session_expiry`` in
   ``fastapi_app.py``, which stamp and enforce the shorter
   ``security.session_timeout_hours`` (default 2h) deadline inside the
   session payload itself, checked in ``DatabaseMiddleware`` before any
   route handler runs. See time-control tests below — no real sleeps.

2. ``Expires: 0`` dropped from non-static responses — restored as part
   of ``SecurityHeadersMiddleware.cache_headers()``.

3. Unhandled-500 path (Starlette's ``ServerErrorMiddleware``, which sits
   OUTSIDE every ``add_middleware`` layer) missing Cache-Control/Pragma/
   Expires on its manually-stamped headers — fixed by reusing
   ``SecurityHeadersMiddleware.cache_headers()`` in the catch-all
   ``Exception`` handler.
"""

import uuid

import pytest
from fastapi.testclient import TestClient

from local_deep_research.web import fastapi_app as fastapi_app_module

TEST_PASSWORD = "TestPassword123!"  # noqa: S105


# ---------------------------------------------------------------------------
# Shared helpers (mirrors tests/web/test_session_cookie_behavior.py)
# ---------------------------------------------------------------------------


def _fresh_client(app) -> TestClient:
    """TestClient with a unique X-Forwarded-For so each client gets its
    own slowapi rate-limit bucket (registration is rate limited per client IP)."""
    client = TestClient(app, raise_server_exceptions=False)
    fwd_ip = f"10.{uuid.uuid4().int % 254 + 1}.{uuid.uuid4().int % 254 + 1}.2"
    client.headers.update({"X-Forwarded-For": fwd_ip})
    return client


def _csrf(client: TestClient) -> str:
    client.get("/auth/login")
    resp = client.get("/auth/csrf-token")
    return resp.json().get("csrf_token", "") if resp.status_code == 200 else ""


def _register(client: TestClient, username: str):
    return client.post(
        "/auth/register",
        data={
            "username": username,
            "password": TEST_PASSWORD,
            "confirm_password": TEST_PASSWORD,
            "acknowledge": "true",
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )


def _login(client: TestClient, username: str, remember: str):
    return client.post(
        "/auth/login",
        data={
            "username": username,
            "password": TEST_PASSWORD,
            "remember": remember,
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )


@pytest.fixture
def logged_in_client(app):
    """A fresh client with a freshly-registered, NOT-yet-logged-in user."""
    client = _fresh_client(app)
    username = f"regfix_{uuid.uuid4().hex[:8]}"
    resp = _register(client, username)
    assert resp.status_code == 302, (
        f"registration failed: {resp.status_code} {resp.text[:300]}"
    )
    return client, username


# ---------------------------------------------------------------------------
# Regression #1: non-remember-me server-side session validity
# ---------------------------------------------------------------------------


class TestNonRememberMeServerSideExpiry:
    """Uses monkeypatch on ``fastapi_app._now_ts`` to fast-forward the
    server's notion of "now" — no real sleeps, and independent of the
    real 30-day itsdangerous signature window (which real time never
    approaches in this test)."""

    def test_non_remember_me_session_rejected_after_shorter_lifetime(
        self, monkeypatch, logged_in_client
    ):
        client, username = logged_in_client
        resp = _login(client, username, remember="false")
        assert resp.status_code == 302, (
            f"login failed: {resp.status_code} {resp.text[:300]}"
        )

        # Sanity: authenticated immediately after login, using the exact
        # endpoint named in the PoC this regression describes.
        check = client.get("/auth/check")
        assert check.status_code == 200
        assert check.json()["authenticated"] is True

        # Fast-forward past security.session_timeout_hours without
        # sleeping. The raw cookie value is unchanged — only the server's
        # clock moves — so this proves server-side enforcement, not just
        # a client-side cookie attribute.
        future = (
            fastapi_app_module._now_ts()
            + fastapi_app_module._NON_REMEMBER_ME_SESSION_SECONDS
            + 1
        )
        monkeypatch.setattr(fastapi_app_module, "_now_ts", lambda: future)

        expired = client.get("/auth/check")
        assert expired.status_code == 401, (
            "a non-remember-me session must be rejected once its "
            "security.session_timeout_hours deadline has passed, even "
            "though the itsdangerous signature is still valid for up to "
            "security.session_remember_me_days"
        )
        assert expired.json()["authenticated"] is False

        # And a protected API route (also named in the PoC) must reject
        # it too, not just the /auth/check convenience endpoint.
        settings_resp = client.get("/settings/api")
        assert settings_resp.status_code in (401, 302), (
            f"expired non-remember-me session still granted /settings/api "
            f"access: {settings_resp.status_code}"
        )

    def test_remember_me_session_survives_the_same_elapsed_time(
        self, monkeypatch, logged_in_client
    ):
        """The remember-me cookie's itsdangerous-level 30-day cap is
        untouched by this fix — advancing past the non-remember-me
        deadline must NOT log out a remember-me session."""
        client, username = logged_in_client
        resp = _login(client, username, remember="true")
        assert resp.status_code == 302

        future = (
            fastapi_app_module._now_ts()
            + fastapi_app_module._NON_REMEMBER_ME_SESSION_SECONDS
            + 1
        )
        monkeypatch.setattr(fastapi_app_module, "_now_ts", lambda: future)

        still_ok = client.get("/auth/check")
        assert still_ok.status_code == 200
        assert still_ok.json()["authenticated"] is True

    def test_deadline_is_stamped_on_the_login_response_itself(
        self, logged_in_client
    ):
        """The very first Set-Cookie a non-remember-me login produces
        must already carry a stamped deadline — not just the second
        response onward — otherwise the raw login-response cookie value
        would stay unboundedly replayable in a fresh client that never
        sees a later response. Verified indirectly: after login, the
        in-request session scope (surfaced via /auth/check succeeding)
        combined with the monkeypatched-time test above proves
        enforcement; this test pins that a session cookie survives a
        round trip through a brand new client (i.e. is well-formed and
        carries the extra key without breaking anything else)."""
        client, username = logged_in_client
        resp = _login(client, username, remember="false")
        assert resp.status_code == 302

        cookie_value = client.cookies.get("session")
        assert cookie_value

        fresh_client = TestClient(client.app, raise_server_exceptions=False)
        fresh_client.cookies.set("session", cookie_value)
        replayed = fresh_client.get("/auth/check")
        assert replayed.status_code == 200
        assert replayed.json()["authenticated"] is True


# ---------------------------------------------------------------------------
# Regression #2: Expires: 0
# ---------------------------------------------------------------------------


class TestExpiresZeroHeader:
    def test_expires_zero_on_normal_response(self, app):
        client = TestClient(app, raise_server_exceptions=False)
        resp = client.get("/api/v1/health")
        assert resp.status_code == 200
        assert resp.headers.get("expires") == "0"
        # Sanity: still alongside the headers this already carried.
        assert resp.headers.get("cache-control") == (
            "no-store, no-cache, must-revalidate, max-age=0"
        )
        assert resp.headers.get("pragma") == "no-cache"

    def test_expires_zero_absent_on_static_asset(self, app):
        client = TestClient(app, raise_server_exceptions=False)
        resp = client.get("/static/favicon.png")
        assert resp.status_code == 200
        assert "expires" not in resp.headers


# ---------------------------------------------------------------------------
# Regression #3: cache headers on a genuine unhandled 500
# ---------------------------------------------------------------------------

# Registered once on the live singleton `app`, same technique as
# tests/web/test_middleware_order_and_headers.py's own probe routes.
# Distinctive prefix: no production route starts with it.


class _RegressionProbeUnregisteredError(RuntimeError):
    """Deliberately NOT one of the types `_register_exception_handlers`
    binds — matches only the bare ``Exception`` handler that Starlette
    wires into ``ServerErrorMiddleware``, OUTSIDE every
    ``app.add_middleware`` layer (including ``SecurityHeadersMiddleware``,
    which is where Cache-Control/Pragma/Expires normally come from)."""


@fastapi_app_module.app.get(
    "/__regression_probe__/boom", include_in_schema=False
)
async def _regression_probe_boom():
    raise _RegressionProbeUnregisteredError("probe: unregistered exception")


class TestCacheHeadersOnUnhandled500:
    def test_cache_headers_present_on_genuine_unhandled_500(self, app):
        client = TestClient(app, raise_server_exceptions=False)
        resp = client.get("/__regression_probe__/boom")
        assert resp.status_code == 500
        assert resp.json() == {"error": "Server error"}

        assert resp.headers.get("cache-control") == (
            "no-store, no-cache, must-revalidate, max-age=0"
        ), (
            "unhandled-500 path lost Cache-Control (bypasses SecurityHeadersMiddleware)"
        )
        assert resp.headers.get("pragma") == "no-cache"
        assert resp.headers.get("expires") == "0"

        # The pre-existing 8 unconditional security headers must still be
        # there too (not a regression from this change).
        assert "content-security-policy" in resp.headers
        assert "x-frame-options" in resp.headers