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.

9.8 KB
Raw Blame
"""Session-cookie behavior fences for the FastAPI migration.

Pins the interaction of fastapi_app's SessionMiddleware config with
RememberMeMiddleware and SecureCookieMiddleware, full-stack via
TestClient:

- login WITHOUT "remember me" issues a browser-session cookie
  (RememberMeMiddleware strips Max-Age/Expires when the login handler
  stored _remember_me=False in the session);
- login WITH "remember me" keeps the ~30-day Max-Age SessionMiddleware
  is configured with;
- every session Set-Cookie carries SameSite=lax and HttpOnly
  (fastapi_app configures same_site="lax"), and the attribute
  stripping for non-remember-me logins must not drop them;
- SecureCookieMiddleware appends the Secure flag iff the request scheme
  is https (exercised through the real middleware stack by flipping the
  built instance's `testing` attribute — pure-ASGI unit coverage lives
  in tests/web/test_secure_cookie_middleware.py).

Registration auto-login (also non-persistent) is already fenced by
tests/web/test_registration_session_cookie.py — not duplicated here.
"""

import uuid

import pytest
from fastapi.testclient import TestClient

TEST_PASSWORD = "TestPassword123!"  # noqa: S105

# The value SessionMiddleware is configured with in fastapi_app.py:
# security.session_remember_me_days (default 30) in seconds.
THIRTY_DAYS_SECONDS = 30 * 24 * 3600


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


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


def _csrf(client):
    """Stamp the session with a CSRF token and return it."""
    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, username):
    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, username, remember):
    return client.post(
        "/auth/login",
        data={
            "username": username,
            "password": TEST_PASSWORD,
            "remember": remember,
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )


def _session_set_cookies(resp):
    """All Set-Cookie header values for the `session` cookie."""
    return [
        v
        for k, v in resp.headers.multi_items()
        if k.lower() == "set-cookie" and v.lower().startswith("session=")
    ]


def _cookie_attr(cookie, name):
    """Return the value of a cookie attribute (case-insensitive), or the
    empty string for a value-less attribute, or None when absent."""
    for part in cookie.split(";")[1:]:
        attr, sep, value = part.strip().partition("=")
        if attr.lower() == name.lower():
            return value if sep else ""
    return None


@pytest.fixture
def registered_user(app):
    """Register a fresh user (unique per test) and hand back a client
    that is NOT yet logged in via /auth/login plus the username."""
    client = _fresh_client(app)
    username = f"cookiebhv_{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


# ---------------------------------------------------------------------------
# Remember-me vs. browser-session cookie lifetime
# ---------------------------------------------------------------------------


def test_login_without_remember_me_issues_browser_session_cookie(
    registered_user,
):
    """remember=false → RememberMeMiddleware strips Max-Age/Expires, so
    the browser discards the cookie on close (Flask-era behavior)."""
    client, username = registered_user
    resp = _login(client, username, remember="false")
    assert resp.status_code == 302, (
        f"login failed: {resp.status_code} {resp.text[:300]}"
    )

    cookies = _session_set_cookies(resp)
    assert cookies, "login set no session cookie"
    cookie = cookies[0]
    assert _cookie_attr(cookie, "max-age") is None, cookie
    assert _cookie_attr(cookie, "expires") is None, cookie


def test_login_without_remember_me_keeps_other_cookie_attributes(
    registered_user,
):
    """The attribute-stripping rewrite must not eat Path/HttpOnly/
    SameSite along with Max-Age."""
    client, username = registered_user
    resp = _login(client, username, remember="false")
    assert resp.status_code == 302

    cookie = _session_set_cookies(resp)[0]
    assert _cookie_attr(cookie, "path") == "/", cookie
    assert _cookie_attr(cookie, "httponly") is not None, cookie
    assert (_cookie_attr(cookie, "samesite") or "").lower() == "lax", cookie
    # And the cookie still carries a non-empty signed value.
    assert len(cookie.split(";")[0].partition("=")[2]) > 0, cookie


def test_login_with_remember_me_issues_30_day_persistent_cookie(
    registered_user,
):
    """remember=true → the 30-day Max-Age from SessionMiddleware's
    config survives to the client (RememberMeMiddleware must not strip
    it when _remember_me is True)."""
    client, username = registered_user
    resp = _login(client, username, remember="true")
    assert resp.status_code == 302, (
        f"login failed: {resp.status_code} {resp.text[:300]}"
    )

    cookies = _session_set_cookies(resp)
    assert cookies, "login set no session cookie"
    cookie = cookies[0]
    max_age = _cookie_attr(cookie, "max-age")
    assert max_age is not None, f"remember-me cookie not persistent: {cookie}"
    assert int(max_age) == THIRTY_DAYS_SECONDS, cookie
    # Persistence must not come at the cost of the hardening attributes.
    assert _cookie_attr(cookie, "httponly") is not None, cookie
    assert (_cookie_attr(cookie, "samesite") or "").lower() == "lax", cookie


# ---------------------------------------------------------------------------
# SameSite / HttpOnly on every session cookie
# ---------------------------------------------------------------------------


def test_unauthenticated_session_cookie_is_lax_and_httponly(app):
    """Even the pre-login session cookie (created when the CSRF token is
    stamped on GET /auth/login) must be SameSite=lax + HttpOnly."""
    client = _fresh_client(app)
    resp = client.get("/auth/login")
    assert resp.status_code == 200

    cookies = _session_set_cookies(resp)
    assert cookies, "GET /auth/login set no session cookie"
    cookie = cookies[0]
    assert _cookie_attr(cookie, "httponly") is not None, cookie
    assert (_cookie_attr(cookie, "samesite") or "").lower() == "lax", cookie


# ---------------------------------------------------------------------------
# Secure flag follows the request scheme (full middleware stack)
# ---------------------------------------------------------------------------


def _built_secure_cookie_middleware(app):
    """Walk the built middleware stack to the live SecureCookieMiddleware
    instance (fastapi_app constructs it with testing=True under pytest,
    which disables the Secure flag entirely)."""
    from local_deep_research.web.fastapi_app import SecureCookieMiddleware

    node = app.middleware_stack
    seen = set()
    while node is not None and id(node) not in seen:
        seen.add(id(node))
        if isinstance(node, SecureCookieMiddleware):
            return node
        node = getattr(node, "app", None)
    return None


@pytest.fixture
def secure_cookie_mw_active(app, monkeypatch):
    """Force one request so the middleware stack is built, then flip the
    live SecureCookieMiddleware to production mode regardless of the explicit
    LDR_TEST_MODE setting. Monkeypatch restores its original value afterwards."""
    _fresh_client(app).get("/auth/login")
    mw = _built_secure_cookie_middleware(app)
    assert mw is not None, (
        "SecureCookieMiddleware not found in the built middleware stack"
    )
    monkeypatch.setattr(mw, "testing", False)
    # The http-scheme test below trips the one-shot "serving HTTP to a
    # public client" warning; restore its latch so the shared app
    # instance is left exactly as found.
    monkeypatch.setattr(
        mw, "_warned_insecure_public", mw._warned_insecure_public
    )
    return app


def test_https_request_gets_secure_session_cookie(secure_cookie_mw_active):
    app = secure_cookie_mw_active
    client = _fresh_client(app, base_url="https://testserver")
    resp = client.get("/auth/login")
    assert resp.status_code == 200

    cookies = _session_set_cookies(resp)
    assert cookies, "GET /auth/login set no session cookie"
    cookie = cookies[0]
    assert _cookie_attr(cookie, "secure") is not None, (
        f"https session cookie missing Secure flag: {cookie}"
    )


def test_http_request_does_not_get_secure_session_cookie(
    secure_cookie_mw_active,
):
    """Secure over plain HTTP would make browsers DROP the cookie
    (login loop, #3849) — it must stay scheme-gated."""
    app = secure_cookie_mw_active
    client = _fresh_client(app, base_url="http://testserver")
    resp = client.get("/auth/login")
    assert resp.status_code == 200

    cookies = _session_set_cookies(resp)
    assert cookies, "GET /auth/login set no session cookie"
    cookie = cookies[0]
    assert _cookie_attr(cookie, "secure") is None, (
        f"http session cookie must not be Secure: {cookie}"
    )