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.

8.3 KB
Raw Blame
"""A CSRF retry is distinguishable from an application error and session-bound."""

import pytest

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi.testclient import TestClient
from starlette.middleware.sessions import SessionMiddleware

from local_deep_research.web.dependencies.csrf import CSRFMiddleware
from local_deep_research.web.routers.auth import get_csrf_token


# Use the real application to cover its template injection and unconditional
# session-revocation check before the token endpoint (not just the light stack).
from tests.web.test_auth_session_lifecycle import (
    _client as _lifecycle_client,
    _login,
    _register,
    _session_payload,
    live_app as _lifecycle_app,
)

live_app = _lifecycle_app


def _client():
    app = FastAPI()
    calls = []

    @app.get("/seed/{owner}")
    def seed(request: Request, owner: str, mint_csrf: bool = True):
        request.session.clear()
        request.session.update(username="alice", session_id=owner)
        return get_csrf_token(request) if mint_csrf else {"ok": True}

    app.get("/auth/csrf-token")(get_csrf_token)

    @app.post("/mutate")
    def mutate():
        calls.append("mutate")
        return {"ok": True}

    @app.post("/conflict")
    def conflict():
        calls.append("conflict")
        return JSONResponse(
            {"success": False, "error": "Already running"}, status_code=409
        )

    @app.post("/forbidden")
    def forbidden():
        calls.append("forbidden")
        return JSONResponse({"error": "Permission denied"}, status_code=403)

    app.add_middleware(CSRFMiddleware)
    app.add_middleware(SessionMiddleware, secret_key="csrf-recovery-test")
    return TestClient(app), calls


def test_csrf_rejection_marker_means_handler_did_not_run():
    client, calls = _client()
    missing = client.post("/mutate")
    assert missing.status_code == 403
    assert missing.headers.get("X-LDR-CSRF-Rejected") == "1"
    token = client.get("/seed/owner-a").json()["csrf_token"]
    invalid = client.post("/mutate", headers={"X-CSRFToken": "stale"})
    assert invalid.status_code == 403
    assert invalid.headers.get("X-LDR-CSRF-Rejected") == "1"
    assert calls == []
    assert (
        client.post("/mutate", headers={"X-CSRFToken": token}).status_code
        == 200
    )
    assert calls == ["mutate"]
    forbidden = client.post("/forbidden", headers={"X-CSRFToken": token})
    assert forbidden.status_code == 403
    assert "X-LDR-CSRF-Rejected" not in forbidden.headers
    assert calls == ["mutate", "forbidden"]


def test_refresh_context_identifies_the_login_without_exposing_its_id():
    client, _ = _client()
    anonymous = client.get("/auth/csrf-token")
    assert anonymous.headers.get("Cache-Control") == "no-store"
    assert anonymous.json()["auth_context"] is None
    first = client.get("/seed/private-session-a").json()
    assert (
        first["auth_context"]
        and "private-session-a" not in first["auth_context"]
    )
    assert first["auth_context"] != first["csrf_token"]
    assert client.get("/auth/csrf-token").json() == first
    second = client.get("/seed/private-session-b").json()
    assert second["auth_context"] != first["auth_context"]


def test_changed_login_cannot_receive_a_replayed_write_even_with_its_csrf_token():
    client, calls = _client()
    old = client.get("/seed/owner-a").json()
    current = client.get("/seed/owner-b").json()
    response = client.post(
        "/mutate",
        headers={
            "X-CSRFToken": current["csrf_token"],
            "X-LDR-Auth-Context": old["auth_context"],
        },
    )
    assert response.status_code == 409
    assert response.headers.get("X-LDR-Auth-Changed") == "1"
    assert "X-LDR-CSRF-Rejected" not in response.headers
    assert calls == []
    accepted = client.post(
        "/mutate",
        headers={
            "X-CSRFToken": current["csrf_token"],
            "X-LDR-Auth-Context": current["auth_context"],
        },
    )
    assert accepted.status_code == 200
    assert calls == ["mutate"]


def test_replaced_login_is_rejected_even_before_it_has_a_csrf_token():
    client, calls = _client()
    old = client.get("/seed/owner-a").json()
    # Login clears the old token; a new page normally mints the next one.
    client.get("/seed/owner-b", params={"mint_csrf": "false"})
    response = client.post(
        "/mutate",
        headers={
            "X-CSRFToken": old["csrf_token"],
            "X-LDR-Auth-Context": old["auth_context"],
        },
    )
    assert response.status_code == 409
    assert "sign-in changed" in response.json()["error"]
    assert response.headers.get("X-LDR-Auth-Changed") == "1"
    assert "X-LDR-CSRF-Rejected" not in response.headers
    assert calls == []


def test_only_the_replaced_login_409_carries_the_auth_changed_marker():
    """Clients key on the header to tell a login change from a handler 409."""
    client, calls = _client()
    current = client.get("/seed/owner-a").json()
    own = client.post(
        "/conflict",
        headers={
            "X-CSRFToken": current["csrf_token"],
            "X-LDR-Auth-Context": current["auth_context"],
        },
    )
    assert own.status_code == 409
    assert "X-LDR-Auth-Changed" not in own.headers
    assert calls == ["conflict"]
    client.get("/seed/owner-b")
    replaced = client.post(
        "/conflict",
        headers={
            "X-CSRFToken": current["csrf_token"],
            "X-LDR-Auth-Context": current["auth_context"],
        },
    )
    assert replaced.status_code == 409
    assert replaced.headers.get("X-LDR-Auth-Changed") == "1"
    assert calls == ["conflict"]


@pytest.mark.parametrize("anonymous_token", [False, True])
def test_bound_write_requires_login_after_the_cookie_is_cleared(
    anonymous_token,
):
    client, calls = _client()
    old = client.get("/seed/owner-a").json()
    client.cookies.clear()
    if anonymous_token:
        client.get("/auth/csrf-token")
    response = client.post(
        "/mutate", headers={"X-LDR-Auth-Context": old["auth_context"]}
    )
    assert response.status_code == 401
    assert "X-LDR-CSRF-Rejected" not in response.headers
    assert "X-LDR-Auth-Changed" not in response.headers
    assert calls == []


def test_recovery_fetch_rejects_anonymous_or_replaced_login_without_minting_token():
    client, _ = _client()
    anonymous = client.get(
        "/auth/csrf-token", headers={"X-LDR-Auth-Context": "old"}
    )
    assert anonymous.status_code == 401
    assert anonymous.headers["Cache-Control"] == "no-store"
    assert "X-LDR-Auth-Changed" not in anonymous.headers
    assert "csrf_token" not in anonymous.json()
    old = client.get("/seed/owner-a").json()
    current = client.get("/seed/owner-b").json()
    changed = client.get(
        "/auth/csrf-token", headers={"X-LDR-Auth-Context": old["auth_context"]}
    )
    assert changed.status_code == 409
    assert changed.headers.get("X-LDR-Auth-Changed") == "1"
    assert changed.headers["Cache-Control"] == "no-store"
    assert "csrf_token" not in changed.json()
    accepted = client.get(
        "/auth/csrf-token",
        headers={"X-LDR-Auth-Context": current["auth_context"]},
    )
    assert accepted.status_code == 200
    assert accepted.json() == current


@pytest.mark.real_session_check
def test_real_page_and_refresh_share_a_context_that_expires_with_the_login(
    live_app,
):
    import re
    import uuid
    from local_deep_research.web.auth.session_manager import session_manager

    client = _lifecycle_client(live_app)
    username = "csrf_context_" + uuid.uuid4().hex[:8]
    assert _register(client, username).status_code == 302
    identity = client.get("/auth/csrf-token").json()
    page = client.get("/settings/")
    assert page.status_code == 200
    rendered = re.search(
        r'<meta name="auth-context" content="([a-f0-9]+)">', page.text
    )
    assert rendered and rendered.group(1) == identity["auth_context"]
    session_id = _session_payload(client)["session_id"]
    session_manager.destroy_session(session_id)
    expired = client.get(
        "/auth/csrf-token",
        headers={"X-LDR-Auth-Context": identity["auth_context"]},
    )
    assert expired.status_code == 401
    assert "csrf_token" not in expired.json()
    assert _login(client, username).status_code == 302
    replacement = client.get("/auth/csrf-token").json()
    assert replacement["auth_context"] != identity["auth_context"]