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.

40.1 KB
Raw Blame
"""Credential lifetime at logout / password change, and the registration
username charset guard.

The Flask -> FastAPI migration deleted ``tests/web/auth/`` (15 files). The
auth ROUTES were re-tested elsewhere on this branch, but four security
properties of ``web/routers/auth.py`` came through with nothing asserting
them at all. Each is a call the handler makes whose whole purpose is to end
the lifetime of a credential or a connection; a silent regression in any of
them leaves a plaintext SQLCipher password (or a live, pre-authorised
socket) alive past the session that authorised it.

WHAT IS PINNED HERE
-------------------
A1. ``logout`` -> ``scheduler.unregister_user(username)``. The news
    scheduler keeps the user's plaintext DB password in a process-global
    in-memory store so background jobs can open the encrypted database.
    Logout is the only thing that evicts it. Asserted as an OBSERVABLE
    effect on the real ``BackgroundJobScheduler`` singleton (the credential
    is actually gone from ``_credential_store``), not as a mock call — and
    paired with a second user whose credential must SURVIVE, so a
    regression to "clear everything" fails too.

A2. ``logout`` -> ``_disconnect_session_sockets(session_id)`` and NOT
    ``disconnect_user(username)``. Both directions matter: sockets are
    authorised once at handshake and never re-checked (so the logged-out
    tab's sockets must go), but logout is per-session, so the user's OTHER
    live sessions must keep theirs. The spy asserts the exact session id
    the real login minted, and the availability half is additionally
    asserted against ``session_manager``'s own records.

A3. ``change-password`` -> ``_disconnect_user_sockets(username)``, the
    all-sessions scope, and NOT the per-session one. This is the
    compromise-response path: a password change means every socket
    authorised under the old credential must be severed.

D.  ``change-password`` with the WRONG current password must be refused and
    must NOT rekey. Asserted through the encryption itself: the ORIGINAL
    password still opens the database and the attempted new one does not.

C.  The registration username charset guard. The username becomes a
    filesystem name (``<data>/encrypted_databases/<username>.db``) and a
    database key, so this is path safety, not cosmetics. Nothing tested it
    with a traversal / separator / NUL / trailing-dot corpus, and nothing
    tested the PARITY the downstream library guard's docstring claims (see
    ``research_library/utils/_reject_unsafe_username_component``, which
    says it "mirrors registration's *exact* predicate ... so the two checks
    can never diverge"). Divergence in either direction is a bug: loosen
    registration and the library guard rejects a legitimately provisioned
    account; loosen the library guard and a username becomes a traversal
    primitive.

DELIBERATELY NOT DUPLICATED (already covered on this branch)
------------------------------------------------------------
* ``allow_registrations = False`` on ``GET`` and ``POST /auth/register``
  (the original GAP B) is covered by
  ``tests/security/test_auth_routes_fastapi.py`` ::
  ``test_register_page_redirects_when_registrations_disabled`` and
  ``test_register_post_creates_no_user_when_registrations_disabled`` —
  including the "no auth row / no encrypted DB / no session" assertions.
* The happy-path change-password lifecycle (real rekey, other sessions
  killed, old password dead, data preserved) —
  ``tests/web/test_long_integration_flows_followup.py``.
* Logout destroying the server-side session and clearing the cookie —
  ``tests/web/routers/test_auth_flow_gaps.py``.
* Logout clearing the per-thread credential cache —
  ``tests/security/test_logout_clears_thread_credentials.py``.
* The idle-connection sweeper's ``unregister_user`` / ``disconnect_user``
  calls — ``tests/web/auth/test_connection_cleanup.py``. That is a
  DIFFERENT call site from the two route handlers pinned here.
* ``_reject_unsafe_username_component`` as reached through
  ``apply_user_subdir`` — ``tests/research_library/services/
  test_per_user_library_root.py``. This file tests the REGISTRATION side of
  that same corpus, which had no test, plus the parity between the two.

HARNESS
-------
``TestClient(app, raise_server_exceptions=False)`` over the function-scoped
``app`` fixture from ``tests/conftest.py``. CSRF is ASGI middleware with no
off switch, so every POST carries a token minted by ``GET
/auth/csrf-token``. Rate limits are per client IP, so every client gets its
own ``X-Forwarded-For`` (``REGISTRATION_RATE_LIMIT`` is enforced per IP).
"""

import re
import itertools
import uuid
from datetime import datetime, UTC

import pytest
from fastapi.testclient import TestClient

TEST_PASSWORD = "LogoutPolicyPass123"  # noqa: S105

# register.html embeds the SAME validation strings twice: once server-side,
# as a flashed error, and once in the page's client-side JS
# (``setFieldInvalid(this, usernameError, 'Username must be at least 3
# characters')``). A bare ``"..." in resp.text`` therefore matches on EVERY
# render of that page and asserts nothing at all — including on a 400 raised
# by CSRF, the rate limiter, or password strength. Flashes render into
# ``alert`` divs, so scope the search to those. (The two auth templates use
# different alert markup — ``alert alert-dismissible`` vs
# ``alert alert-danger alert-dismissible fade show`` — hence the loose class
# match. Static alert blocks in the templates are captured too; that only
# ever adds entries, never removes the one being asserted.)
_FLASH_RE = re.compile(r'<div class="alert[^"]*"[^>]*>\s*([^<]+?)\s*<')


def _flashed_messages(html: str) -> list:
    """The server-side alert text on a rendered page."""
    return [m.strip() for m in _FLASH_RE.findall(html)]


# ---------------------------------------------------------------------------
# Harness
# ---------------------------------------------------------------------------


# Monotonic source of forwarded IPs. RANDOM addresses are not safe here:
# the limiter buckets registrations per IP, and with a few hundred
# clients in one session the birthday odds of two of them landing on the same
# random /32 are high enough to hit reliably. When that happened the second
# client got a 429 and the assertions below — which allow only 302/400 —
# failed for a reason that had nothing to do with the guard under test.
# A counter makes the bucket per-client by construction.
_IP_COUNTER = itertools.count(1)


def _client(app) -> TestClient:
    """A TestClient with its own cookie jar and its own forwarded IP.

    ``_get_client_ip`` trusts ``X-Forwarded-For`` from the TestClient
    sentinel peer, so a distinct address per client means a distinct
    rate-limit bucket — otherwise the per-IP registration limit can turn the
    parametrized cases below into 429s that assert nothing.
    """
    n = next(_IP_COUNTER)
    client = TestClient(app, raise_server_exceptions=False)
    client.headers.update(
        {
            # 10.<n/65536>.<n/256>.<n%256> — unique for the first ~16M clients.
            "X-Forwarded-For": (
                f"10.{(n >> 16) & 0xFF}.{(n >> 8) & 0xFF}.{n & 0xFF}"
            )
        }
    )
    return client


def _csrf(client: TestClient) -> str:
    """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 _unique(prefix: str) -> str:
    return f"{prefix}_{uuid.uuid4().hex[:10]}"


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


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


def _logout(client: TestClient):
    return client.post(
        "/auth/logout",
        data={"csrf_token": _csrf(client)},
        follow_redirects=False,
    )


def _register_and_login(app, username: str, password: str) -> TestClient:
    client = _client(app)
    resp = _register(client, username, password)
    assert resp.status_code == 302, (
        f"setup registration failed: {resp.status_code} {resp.text[:400]}"
    )
    return client


def _session_ids_for(username: str) -> set:
    """The REAL server-side session ids the app minted for ``username``.

    ``session_manager.get_user_sessions()`` truncates the id to 8 chars, so
    read the map directly — the assertions below need the exact value that
    was handed to ``_disconnect_session_sockets``.
    """
    from local_deep_research.web.auth.session_manager import session_manager

    return {
        sid
        for sid, data in session_manager.sessions.items()
        if data["username"] == username
    }


def _auth_row_exists(username: str) -> bool:
    from local_deep_research.database.auth_db import get_auth_db_session
    from local_deep_research.database.models.auth import User

    auth_db = get_auth_db_session()
    try:
        return (
            auth_db.query(User).filter_by(username=username).first() is not None
        )
    finally:
        auth_db.close()


@pytest.fixture
def socket_spies(monkeypatch):
    """Record calls to the ASGI socket-teardown functions.

    The auth router imports these lazily INSIDE ``_disconnect_user_sockets``
    / ``_disconnect_session_sockets`` (``from ..services.socketio_asgi
    import ...``), so the attribute is re-read at call time and patching the
    definition module is what the handler actually sees.

    A recorder rather than a MagicMock so the tests can assert on the exact
    ARGUMENT, and both functions are patched together so every test can
    assert the direction that was *not* taken as well as the one that was.
    """
    import local_deep_research.web.services.socketio_asgi as socketio_asgi

    calls = {"user": [], "session": []}

    def _fake_disconnect_user(username: str) -> bool:
        calls["user"].append(username)
        return True

    def _fake_disconnect_session(session_id: str) -> bool:
        calls["session"].append(session_id)
        return True

    monkeypatch.setattr(socketio_asgi, "disconnect_user", _fake_disconnect_user)
    monkeypatch.setattr(
        socketio_asgi, "disconnect_session", _fake_disconnect_session
    )
    return calls


@pytest.fixture
def running_scheduler():
    """The real ``BackgroundJobScheduler`` singleton, marked as running.

    ``logout`` and ``change_password`` both guard their
    ``unregister_user()`` call with ``if sched.is_running``, so a stopped
    scheduler makes the behaviour under test unreachable and the assertions
    vacuous. ``tests/conftest.py``'s autouse ``reset_all_singletons`` drops
    the instance around every test; the flag is restored here anyway so
    nothing depends on that ordering.
    """
    from local_deep_research.scheduler.background import (
        get_background_job_scheduler,
    )

    sched = get_background_job_scheduler()
    original = sched.is_running
    sched.is_running = True
    try:
        yield sched
    finally:
        sched.is_running = original


def _seed_scheduler_credential(sched, username: str, password: str) -> None:
    """Register ``username`` with the scheduler exactly as
    ``update_user_info`` would, minus the subscription scheduling.

    ``update_user_info`` also calls ``_schedule_user_subscriptions``, which
    opens the user's database and talks to APScheduler — irrelevant to what
    is under test and a large amount of incidental machinery. The two pieces
    of state that matter (the session entry and the PLAINTEXT PASSWORD in
    the credential store) are seeded directly, and ``unregister_user`` is
    then exercised for real through the HTTP handler.
    """
    with sched.lock:
        sched.user_sessions[username] = {
            "last_activity": datetime.now(UTC),
            "scheduled_jobs": set(),
        }
    sched._credential_store.store(username, password)


# ---------------------------------------------------------------------------
# A1. Logout must evict the scheduler's copy of the plaintext DB password
# ---------------------------------------------------------------------------


def test_logout_evicts_the_schedulers_plaintext_password(
    app, running_scheduler
):
    """``POST /auth/logout`` must unregister the user from the news
    scheduler, which is what drops their plaintext SQLCipher password from
    the scheduler's process-global in-memory credential store.

    Without it the credential outlives the session that authorised it: the
    scheduler keeps opening that user's encrypted database on every tick,
    indefinitely, after they have logged out.

    Both directions are pinned:

    * the logging-out user's credential is GONE (retrievable before,
      ``None`` after); and
    * a second, still-logged-in user's credential SURVIVES. Without that
      control, a regression that cleared the whole store — or a scheduler
      ``stop()`` slipped into the logout path — would pass.
    """
    sched = running_scheduler

    victim = _unique("sched_out")
    bystander = _unique("sched_stay")
    bystander_password = "BystanderPass456"  # noqa: S105

    client = _register_and_login(app, victim, TEST_PASSWORD)

    _seed_scheduler_credential(sched, victim, TEST_PASSWORD)
    _seed_scheduler_credential(sched, bystander, bystander_password)

    # Positive baseline: the plaintext password really is retrievable, so
    # the "it is gone" assertion below is about logout and not about a
    # store that never held anything.
    assert sched._credential_store.retrieve(victim) == TEST_PASSWORD
    assert sched._credential_store.retrieve(bystander) == bystander_password
    assert victim in sched.user_sessions

    resp = _logout(client)
    assert resp.status_code == 302, (
        f"logout did not complete: {resp.status_code} {resp.text[:400]}"
    )
    assert resp.headers.get("location") == "/auth/login"

    assert sched._credential_store.retrieve(victim) is None, (
        "logout left the user's PLAINTEXT SQLCipher password in the "
        "scheduler's credential store — the credential outlives the session"
    )
    assert victim not in sched.user_sessions, (
        "logout left the user registered with the scheduler; their "
        "subscription jobs keep running after logout"
    )

    assert sched._credential_store.retrieve(bystander) == bystander_password, (
        "one user's logout wiped a DIFFERENT user's scheduler credential — "
        "the eviction must be scoped to the user logging out"
    )
    assert bystander in sched.user_sessions


# ---------------------------------------------------------------------------
# A2. Logout is per-SESSION socket teardown, never per-user
# ---------------------------------------------------------------------------


def test_logout_disconnects_only_this_sessions_sockets(app, socket_spies):
    """Logout must sever the sockets of the session being logged out, and
    only those.

    A Socket.IO connection is authorised once, at handshake, and never
    re-checked — so a socket opened before logout keeps delivering that
    user's events (including ``settings_changed``, which carries plaintext
    secrets) unless logout explicitly disconnects it. That is the
    confidentiality half.

    The availability half is the opposite direction: logout is a
    SINGLE-session operation, so calling ``disconnect_user`` here would kill
    the user's other tabs and devices. Both are asserted, and the
    per-session call is checked against the exact session id the real login
    minted — a value a bare "was it called?" spy cannot fake.
    """
    username = _unique("sockscope")

    client_a = _register_and_login(app, username, TEST_PASSWORD)
    sessions_after_a = _session_ids_for(username)
    assert len(sessions_after_a) == 1, (
        f"expected exactly one server-side session after registration, "
        f"got {len(sessions_after_a)}"
    )
    (session_a,) = sessions_after_a

    # A second, independent session for the SAME user (another tab/device).
    client_b = _client(app)
    assert _login(client_b, username, TEST_PASSWORD).status_code == 302
    session_b = (_session_ids_for(username) - {session_a}).pop()

    # Nothing has been disconnected yet — so the assertions below are about
    # logout and not about setup noise.
    assert socket_spies["session"] == []
    assert socket_spies["user"] == []

    resp = _logout(client_a)
    assert resp.status_code == 302, (
        f"logout did not complete: {resp.status_code} {resp.text[:400]}"
    )

    # Confidentiality: this session's sockets were severed, by id.
    assert socket_spies["session"] == [session_a], (
        "logout did not disconnect the logged-out session's sockets "
        f"(expected exactly [{session_a!r}], got "
        f"{socket_spies['session']!r}) — a socket authorised before logout "
        "keeps receiving this user's events"
    )

    # Availability: the user's OTHER sessions were not collateral damage.
    assert socket_spies["user"] == [], (
        "logout called disconnect_user() — logging out of one tab tore "
        "down every other tab/device this user has open. Logout is "
        "per-session; disconnect_user is for password change and the idle "
        "sweep, where every session is destroyed anyway"
    )

    remaining = _session_ids_for(username)
    assert session_a not in remaining, (
        "logout did not destroy its own server-side session"
    )
    assert session_b in remaining, (
        "logout destroyed a DIFFERENT session of the same user — logout "
        "must not be an all-sessions operation"
    )


# ---------------------------------------------------------------------------
# A3. Change password is the all-sessions socket teardown
# ---------------------------------------------------------------------------


def test_change_password_disconnects_every_socket_for_the_user(
    app, socket_spies
):
    """A password change must sever EVERY socket the user holds.

    This is the compromise-response path: the user is changing their
    password precisely because the old credential may be known to someone
    else. Every session is destroyed by the change, so every socket
    authorised under one of them must go — the all-sessions scope
    (``disconnect_user``), not logout's per-session one.

    Asserted positively (called exactly once, with this username) and in
    the opposite direction (the per-session helper was NOT used, which
    would leave the other sessions' sockets alive), plus a real 302 and a
    genuinely emptied session table so a 500 cannot pass this test.
    """
    username = _unique("pwsock")
    new_password = "RotatedSockPass456"  # noqa: S105

    client = _register_and_login(app, username, TEST_PASSWORD)

    # Second session, so "all sessions" is a meaningful claim.
    client_b = _client(app)
    assert _login(client_b, username, TEST_PASSWORD).status_code == 302
    assert len(_session_ids_for(username)) == 2

    assert socket_spies["user"] == []
    assert socket_spies["session"] == []

    resp = client.post(
        "/auth/change-password",
        data={
            "current_password": TEST_PASSWORD,
            "new_password": new_password,
            "confirm_password": new_password,
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )
    assert resp.status_code == 302, (
        f"the password change itself failed, so nothing below proves "
        f"anything: {resp.status_code} {resp.text[:400]}"
    )
    assert resp.headers.get("location") == "/auth/login"
    assert _session_ids_for(username) == set(), (
        "a successful password change must destroy every session for the "
        "user (the precondition for the all-sessions socket teardown)"
    )

    assert socket_spies["user"] == [username], (
        "change-password did not disconnect the user's live sockets "
        f"(expected exactly [{username!r}], got {socket_spies['user']!r}). "
        "Sockets are authorised once at handshake, so one held open across "
        "the change keeps streaming this user's events under the "
        "compromised credential"
    )
    assert socket_spies["session"] == [], (
        "change-password used the PER-SESSION socket teardown; that severs "
        "only one session's sockets and leaves every other session's "
        "sockets connected under the old credential"
    )


# ---------------------------------------------------------------------------
# D. A wrong current password must be refused and must not rekey
# ---------------------------------------------------------------------------


def test_change_password_with_wrong_current_password_is_refused_and_no_rekey(
    app, socket_spies
):
    """``POST /auth/change-password`` with the wrong current password must
    be rejected, and must leave the encryption key untouched.

    The account has no password hash anywhere — authentication IS the
    SQLCipher decryption — so "did it rekey?" is answered by the encryption
    itself rather than by a status code:

    * the ORIGINAL password must still open the database (a positive
      assertion: a fresh client logs in with it), and
    * the attacker's proposed new password must NOT open it.

    The second assertion alone would pass if the account had simply been
    destroyed; the first alone would pass if the rekey silently ran and
    also kept the old key. Together they pin "nothing changed".

    The encryption-state assertions come BEFORE the status-code one on
    purpose. A regression that skips the current-password check (rekeying
    with the credential the server already holds for the session, say)
    manifests as BOTH a 302 and a real rekey; asserting the key state first
    reports the serious fact — "a wrong password rotated the encryption
    key" — instead of a status-code mismatch. The status assertion still
    guards the case where the endpoint merely 500s and never rekeys at all.
    """
    username = _unique("wrongcur")
    attempted_new = "AttackerRotated456"  # noqa: S105

    client = _register_and_login(app, username, TEST_PASSWORD)
    sessions_before = _session_ids_for(username)
    assert len(sessions_before) == 1

    resp = client.post(
        "/auth/change-password",
        data={
            "current_password": "TotallyWrongPass789",
            "new_password": attempted_new,
            "confirm_password": attempted_new,
            "csrf_token": _csrf(client),
        },
        follow_redirects=False,
    )

    # No rekey: the ORIGINAL password still decrypts the database...
    original_still_works = _login(_client(app), username, TEST_PASSWORD)
    assert original_still_works.status_code == 302, (
        "a REFUSED password change rekeyed (or destroyed) the database — "
        "the user's real password no longer works: "
        f"{original_still_works.status_code} "
        f"{original_still_works.text[:400]}"
    )

    # ...and the password the caller tried to set does not.
    attempted_works = _login(_client(app), username, attempted_new)
    assert attempted_works.status_code == 401, (
        "the database was rekeyed to the password supplied alongside a "
        f"WRONG current password: {attempted_works.status_code}"
    )

    assert resp.status_code in (400, 401), (
        f"a wrong current password must be refused, got {resp.status_code}: "
        f"{resp.text[:400]}"
    )
    assert "Current password is incorrect" in _flashed_messages(resp.text), (
        "the refusal must say why, server-side; flashed messages were "
        f"{_flashed_messages(resp.text)!r}"
    )

    # A refused change is not a session teardown either: the success path
    # destroys every session for the user, the failure path must not.
    assert sessions_before <= _session_ids_for(username), (
        "a refused password change destroyed the caller's session"
    )
    assert socket_spies["user"] == [] and socket_spies["session"] == [], (
        "a refused password change tore down live sockets — a wrong guess "
        "would then be a denial-of-service primitive against the account"
    )


# ---------------------------------------------------------------------------
# C. The registration username charset guard is a path-safety guard
# ---------------------------------------------------------------------------

# Usernames that MUST be refused.
#
# The encrypted database file itself is named from a sha256 of the username
# (``get_user_database_filename`` -> ``ldr_user_<hash>.db``), so that one
# consumer is already immune. The RAW username is still joined into a
# filesystem path by ``apply_user_subdir`` (the per-user library directory,
# where downloaded PDFs land) and is used as a bare key into several
# process-global credential maps. Registration is the chokepoint that keeps
# every one of those raw-join consumers safe, so each entry below is a
# candidate arbitrary-path primitive rather than a style nit.
_UNSAFE_USERNAMES = [
    pytest.param("../evil", id="parent-traversal"),
    pytest.param("../../etc/passwd", id="deep-traversal"),
    pytest.param("foo/../bar", id="embedded-traversal"),
    pytest.param("a/b", id="posix-separator"),
    pytest.param("/etc/passwd", id="absolute-path"),
    pytest.param("a\\b", id="windows-separator"),
    pytest.param("..\\..\\x", id="windows-traversal"),
    pytest.param("a\x00b", id="nul-byte"),
    pytest.param("alice\x00.txt", id="nul-truncation"),
    pytest.param(".hidden", id="leading-dot"),
    pytest.param("alice.", id="trailing-dot"),
    pytest.param("alice.db", id="extension-lookalike"),
    pytest.param("alice bob", id="embedded-space"),
    pytest.param("...", id="dots-only"),
    pytest.param("D:evil", id="windows-drive-relative"),
    pytest.param("alice:stream", id="ntfs-alternate-data-stream"),
    pytest.param("a%2fb", id="percent-encoded-separator"),
    pytest.param("---", id="punctuation-only"),
    pytest.param("___", id="underscores-only"),
    pytest.param("al*ce", id="glob-metacharacter"),
    pytest.param("al\nice", id="newline"),
]

# Usernames the guard MUST keep accepting. Without these the whole corpus
# above is satisfied by a guard that simply rejects everything, which would
# be a total loss of function dressed up as security. The non-ASCII entries
# are deliberate: ``str.isalnum()`` is Unicode-aware, so these DO register
# today, and the downstream per-user library path guard documents that it
# accepts exactly the same set. Homoglyph confusability is a known,
# documented non-goal of this charset check.
_SAFE_USERNAMES = [
    pytest.param("alice", id="lowercase"),
    pytest.param("Bob2", id="mixed-case-digits"),
    pytest.param("user_name", id="underscore"),
    pytest.param("user-name", id="hyphen"),
    pytest.param("a_b-c1", id="mixed-separators"),
    pytest.param("Müller", id="latin-1-supplement"),
    pytest.param("田中太郎", id="cjk"),
]


def _register_raw(client: TestClient, username: str):
    """Register ``username`` verbatim — no ``_unique`` decoration, because
    the exact bytes are the thing under test."""
    return _register(client, username, TEST_PASSWORD)


@pytest.mark.parametrize("username", _UNSAFE_USERNAMES)
def test_registration_refuses_path_unsafe_usernames(app, username):
    """A username that is not a single safe path component must be refused
    by ``POST /auth/register``, and must create nothing anywhere.

    Rejection is asserted on EFFECTS, not on the status code alone: a
    handler that validated after provisioning would still answer 400 while
    leaving a database (possibly outside the per-user directory) on disk.
    So this asserts no auth row, no user database, no session — and that
    the encrypted-database directory contains exactly the files it
    contained before the request, which is the assertion that would catch
    an actual stray write.

    The reason is checked against the FLASHED message specifically, not
    against the page body: the register template also embeds every
    validation string in its client-side JS, so ``in resp.text`` matches on
    any render of the page and would let a rejection-for-the-wrong-reason
    (or a rejection by CSRF, or by the rate limiter) pass unnoticed.
    """
    from local_deep_research.database.encrypted_db import db_manager

    db_dir = db_manager.data_dir
    db_dir.mkdir(parents=True, exist_ok=True)
    before = sorted(p.name for p in db_dir.iterdir())

    client = _client(app)
    resp = _register_raw(client, username)

    assert resp.status_code == 400, (
        f"registration accepted the path-unsafe username {username!r}: "
        f"{resp.status_code} {resp.text[:400]}"
    )
    flashed = _flashed_messages(resp.text)
    assert any(
        m.startswith("Username can only contain")
        or m.startswith("Username must be at least 3 characters")
        for m in flashed
    ), (
        f"the 400 for {username!r} was not raised by the username guard — "
        f"server-side messages were {flashed!r}. Some other rule (CSRF, the "
        "rate limiter, password strength) is producing this 400 and the "
        "charset guard may no longer be reached at all"
    )

    assert not _auth_row_exists(username), (
        f"a refused username {username!r} still got an auth-DB row"
    )
    assert not db_manager.user_exists(username)
    check = client.get("/auth/check")
    assert check.status_code == 401, (
        f"a refused username {username!r} still opened a session"
    )

    # What must hold: no file with an ATTACKER-CONTROLLED name appeared.
    #
    # Deliberately not `after == before`. The data directory is shared with
    # every other test in the session, which legitimately creates databases
    # while this one runs — that assertion failed in a full-suite run against
    # a directory full of other users' `ldr_user_*.db` and SQLite `-shm`
    # sidecars, for reasons having nothing to do with the guard under test.
    #
    # The property that actually matters survives: a username is only ever
    # used as a filesystem name via its SHA-256 digest
    # (`ldr_user_<16 hex>.db`), so a traversal or injection that reached the
    # filesystem would appear as an entry that does NOT match that shape.
    # Concurrent writes are all hashed; an escape is not.
    safe_shape = re.compile(r"^ldr_user_[0-9a-f]{16}\.db([-.][A-Za-z0-9]+)?$")
    added = sorted(set(p.name for p in db_dir.iterdir()) - set(before))
    attacker_shaped = [name for name in added if not safe_shape.match(name)]

    assert not attacker_shaped, (
        f"registering {username!r} created {attacker_shaped!r} in the "
        f"encrypted database directory ({db_dir}). Every legitimate entry is "
        "a SHA-256 digest of the username; anything else means a rejected "
        "username reached the filesystem through a path it controls"
    )


@pytest.mark.parametrize("username", _SAFE_USERNAMES)
def test_registration_still_accepts_safe_usernames(app, username):
    """Control for the corpus above: the charset guard must not be a
    blanket reject.

    Each of these is a value the guard is specified to accept, and each
    must provision a real, usable account — auth row, encrypted database,
    and an authenticated session — with the username stored verbatim (no
    silent sanitisation, which would let two distinct registrations
    collapse onto one database file).
    """
    from local_deep_research.database.encrypted_db import db_manager

    client = _client(app)
    resp = _register_raw(client, username)

    assert resp.status_code == 302, (
        f"the guard rejected the legitimate username {username!r}: "
        f"{resp.status_code} {resp.text[:400]}"
    )
    assert _auth_row_exists(username), (
        f"{username!r} registered but no auth row was written verbatim — "
        "the username was silently rewritten somewhere"
    )
    assert db_manager.user_exists(username)
    check = client.get("/auth/check")
    assert check.status_code == 200 and check.json().get("username") == (
        username
    ), f"registration did not log {username!r} in: {check.text[:300]}"


@pytest.mark.parametrize(
    "username", _UNSAFE_USERNAMES + _SAFE_USERNAMES + [pytest.param("ab")]
)
def test_registration_and_library_path_guard_never_diverge(app, username):
    """Registration's charset check and the per-user library path guard
    must agree, name for name.

    ``research_library/utils/_reject_unsafe_username_component`` exists
    because ``apply_user_subdir`` joins the username into a filesystem
    path, and its docstring states that it mirrors registration's *exact*
    predicate "so the two checks can never diverge". Nothing tested that
    claim, and divergence is a real defect in both directions:

    * looser registration -> an account is provisioned whose library path
      the guard then refuses to build, permanently breaking that user's
      downloads (and, if the guard were ever relaxed to match, handing them
      a traversal primitive); and
    * looser library guard -> the guard stops being a backstop for a
      username that registration would never have produced.

    Asserted as an equality between the two decisions rather than against a
    hardcoded expectation, so the test keeps meaning something if the
    accepted charset is deliberately changed on both sides at once. ``ab``
    is included as a name registration refuses for a NON-charset reason
    (too short) that the path guard accepts — the one legitimate asymmetry,
    which is why the comparison below is on the charset decision only.
    """
    from local_deep_research.research_library.utils import (
        _reject_unsafe_username_component,
    )

    # "too short" is a LENGTH rule, not a charset rule, and the path guard
    # has no length rule to mirror. Decided from the input, never from the
    # response body: the register template embeds that same sentence in its
    # client-side JS, so a text match would fire on every 400 and silently
    # skip the entire hostile corpus.
    if len(username.strip()) < 3:
        pytest.skip("length rule, not the charset rule under comparison")

    # Judge BOTH sides on the same, session-unique string. The corpus
    # entries are fixed names ("alice", "user_name", ...) registered
    # verbatim, so a name already taken earlier in the pytest session comes
    # back 400 for a DUPLICATE — a non-charset reason — and the comparison
    # below then reports a divergence that does not exist. Appending digits
    # cannot change any entry's charset verdict: every safe name stays
    # alnum-after-stripping-[_-], and no unsafe name is made safe by them.
    probe = f"{username}{uuid.uuid4().int % 10**8:08d}"

    client = _client(app)
    resp = _register_raw(client, probe)
    assert resp.status_code in (302, 400), (
        f"unexpected status for {probe!r}: {resp.status_code} {resp.text[:400]}"
    )

    registration_accepts = resp.status_code == 302

    try:
        _reject_unsafe_username_component(probe)
        path_guard_accepts = True
    except ValueError:
        path_guard_accepts = False

    assert registration_accepts == path_guard_accepts, (
        f"registration and the per-user library path guard disagree about "
        f"{probe!r} (from {username!r}): registration "
        f"{'accepts' if registration_accepts else 'rejects'} it, the path "
        f"guard {'accepts' if path_guard_accepts else 'rejects'} it. "
        "_reject_unsafe_username_component documents that it mirrors "
        "registration's exact predicate; one of the two has moved"
    )


def test_over_long_username_is_never_half_provisioned(app):
    """A username far longer than a filesystem path component must leave
    the account either fully usable or entirely absent — never in between.

    Registration has NO explicit length cap: ``User.username`` is
    ``String(80)`` (SQLite does not enforce it) and the charset guard is
    length-agnostic. It survives today only because the encrypted database
    is named from a sha256 of the username, so its length never reaches the
    filesystem *there*. Asserted as an invariant rather than as a policy so
    the test stays correct whichever way a cap is decided later.

    The half-provisioned state is the security-relevant failure: an auth
    row committed while database creation failed makes the name
    permanently taken by an account nobody can log into — a self-inflicted
    denial of service on that username (the same invariant
    ``tests/security/test_auth_routes_fastapi.py`` pins for injected
    create-database failures, reached here through input length instead).

    NOTE (documented gap, not asserted here because it is not the
    registration route's behaviour): ``apply_user_subdir`` joins the RAW
    username into the per-user library directory, and a 300-character
    component exceeds the 255-byte limit on ext4 — so an account of this
    shape registers cleanly and then fails to build a library path.
    """
    from local_deep_research.database.encrypted_db import db_manager

    username = "a" * 300
    client = _client(app)
    resp = _register_raw(client, username)

    if resp.status_code == 302:
        # Accepted: the account must be genuinely complete and usable.
        assert _auth_row_exists(username)
        assert db_manager.user_exists(username)
        check = client.get("/auth/check")
        assert check.status_code == 200
        assert check.json().get("username") == username
        relogin = _login(_client(app), username, TEST_PASSWORD)
        assert relogin.status_code == 302, (
            "an over-long username was accepted at registration but its "
            f"database will not reopen: {relogin.status_code} — the name "
            "is now taken by an account that cannot log in"
        )
    else:
        # Refused: nothing may survive the refusal.
        assert resp.status_code in (400, 500), (
            f"unexpected status for an over-long username: "
            f"{resp.status_code} {resp.text[:300]}"
        )
        assert not _auth_row_exists(username), (
            "an over-long username was refused but left an auth-DB row "
            "behind — the name is now permanently un-registerable"
        )
        assert not db_manager.user_exists(username)
        assert client.get("/auth/check").status_code == 401


def test_registration_stores_the_stripped_username_as_the_path_component(app):
    """Surrounding whitespace is stripped BEFORE any guard runs, so the
    value that becomes a path component and a credential-map key is the
    stripped one.

    Two properties, both load-bearing for the guard corpus above:

    * the account is keyed by the stripped name, so the raw padded string
      never reaches ``apply_user_subdir`` (``" alice"`` is not a safe path
      component and the downstream guard rejects it); and
    * the padded form is not a second, distinct account — otherwise
      ``" alice"``, ``"alice "`` and ``"alice"`` would be three usernames
      that all resolve to one identity downstream.
    """
    from local_deep_research.database.encrypted_db import db_manager
    from local_deep_research.research_library.utils import (
        _reject_unsafe_username_component,
    )

    base = _unique("padded")
    padded = f"  {base}  "

    client = _client(app)
    resp = _register_raw(client, padded)
    assert resp.status_code == 302, (
        f"a padded username was refused outright: {resp.status_code} "
        f"{resp.text[:400]}"
    )

    assert _auth_row_exists(base), (
        "the account was not stored under the stripped username"
    )
    assert not _auth_row_exists(padded), (
        "the raw padded string was stored as the username — it is not a "
        "safe path component and the per-user library guard rejects it"
    )
    assert db_manager.user_exists(base)
    assert not db_manager.user_exists(padded)
    check = client.get("/auth/check")
    assert check.status_code == 200
    assert check.json().get("username") == base

    # The stored form is a value the downstream path guard accepts; the
    # padded form would not have been.
    _reject_unsafe_username_component(base)
    with pytest.raises(ValueError):
        _reject_unsafe_username_component(padded)