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.

7.9 KB
Raw Blame
"""UUID shape fences on parameterized page routes.

``_validated_collection_id`` (rag.py) established the pattern: page
routes that interpolate a path parameter into templates must validate
the id's *shape* at the boundary, because collection/note/document ids
are server-generated UUID4 strings — anything else is a typo or an
injection attempt, and shape validation is strictly more robust than
fixing any one template context (it covers inline-JS interpolation,
``onclick`` handlers, and anything added later).

This change fences two page routes:

* ``GET /notes/{note_id}`` renders the page for *any* string — the raw
  id reaches the template context (``window.noteId = ...``), currently
  contained only by ``| tojson`` escaping in that one template.
* ``GET /library/document/{document_id}/chunks`` queries before any
  shape check — malformed ids already 404 via the missing-row path,
  but only after a needless DB roundtrip and without the boundary
  guarantee the collection fix documents.

The fence does not close the class: other page routes still pass a
raw path parameter into template contexts unfenced —
``GET /chat/{session_id}`` and the library document pages
``GET /library/document/{document_id}`` and
``GET /library/document/{document_id}/txt``. None is a known live
vulnerability (autoescaped attribute contexts, the same containment
tier the fenced routes are triaged at); they remain to be fenced.

These tests pin the fence: malformed ids (including script-payload
shapes) get a 404 from both page routes, while well-formed UUIDs keep
rendering exactly as before — and the source-inspection test below
fails if either fence call is silently reverted.
"""

import inspect
from uuid import uuid4

from local_deep_research.database import session_context
from local_deep_research.web.routers import notes, rag

_HOSTILE_ID = 'x"><img src=x onerror=alert(1)>'


def _spy_on_user_db_session(monkeypatch):
    """Patch ``get_user_db_session`` with a pass-through spy; return calls.

    ``view_document_chunks`` resolves its DB session by importing
    ``database.session_context.get_user_db_session`` *inside the
    function body* at call time, so the patchable seam is the source
    module's attribute -- patching ``rag.get_user_db_session`` would
    touch nothing.
    """
    calls = []
    real_factory = session_context.get_user_db_session

    def _spying_factory(*args, **kwargs):
        calls.append((args, kwargs))
        return real_factory(*args, **kwargs)

    monkeypatch.setattr(session_context, "get_user_db_session", _spying_factory)
    return calls


def test_note_page_rejects_malformed_note_id(authenticated_client):
    """The notes page must not render for a non-UUID note id.

    Pre-fence the route rendered any string into the template context
    and relied on the one template's ``| tojson`` escaping. The
    boundary fence makes that hold for every future context.
    """
    response = authenticated_client.get(f"/notes/{_HOSTILE_ID}")

    assert response.status_code == 404, (
        "note page rendered for a malformed note_id; the id reached the "
        "template context relying only on per-template escaping"
    )


def test_note_page_renders_for_well_formed_uuid(authenticated_client):
    """A UUID-shaped id keeps rendering the page as before."""
    response = authenticated_client.get(f"/notes/{uuid4()}")

    assert response.status_code == 200, response.text[:200]


def test_document_chunks_page_rejects_malformed_document_id(
    authenticated_client,
    monkeypatch,
):
    """The chunks page must 404 malformed ids AT THE FENCE, before the
    route ever opens its DB session -- not via the route's own
    missing-row branch.

    A bare status-code check cannot tell the two apart: the
    missing-row branch also answers 404 (``rag.py``:
    ``return HTMLResponse("Document not found", status_code=404)``).
    If the fence call were ever moved after the DB query, a
    status-code-only test would stay green while silently losing the
    "skips its DB roundtrip" guarantee.

    This test originally pinned which branch answered by *body*: the
    fence's 404 arrived as the plain ``"Not found"`` text and the
    missing-row 404 as ``"Document not found"``. That premise changed
    when the branded error pages landed on main (PR #5424): the
    fence's ``HTTPException(404, ...)`` is dispatched by exact status
    code to ``fastapi_app.py``'s ``@app.exception_handler(404)``,
    which now renders ``templates/pages/error.html`` for browser
    requests, so the old plain-body pin went stale and failed CI on
    every branch. Body discrimination is fragile in both directions
    (the missing-row branch could be branded the same way later), so
    the test is body-independent now: it spies on the session factory
    the route resolves at call time and asserts NO DB session was
    created for a hostile id. Nothing else in this request's path
    opens a user session through that seam (``require_auth`` checks
    the connection and the server-side session without it), so a
    recorded call means the fence did not answer first. The
    well-formed companion test below proves the same spy IS reached
    when the fence passes.
    """
    session_calls = _spy_on_user_db_session(monkeypatch)

    response = authenticated_client.get(
        f"/library/document/{_HOSTILE_ID}/chunks"
    )

    assert response.status_code == 404, (
        "chunks page did not 404 a malformed document_id; the shape "
        "fence did not answer"
    )
    assert session_calls == [], (
        f"the shape fence did not run before the DB: get_user_db_session "
        f"was called {len(session_calls)} time(s) for a malformed "
        'document_id -- the fence\'s "skips its DB roundtrip" guarantee '
        "is gone and the 404 came from the route's missing-row branch"
    )


def test_document_chunks_page_opens_its_db_session_for_well_formed_unknown_id(
    authenticated_client,
    monkeypatch,
):
    """Positive control for the fence test above: the same spy must be
    reached when the fence passes.

    ``get_user_db_session`` never being called proves nothing about a
    fence that has silently moved unless the seam is demonstrably live
    for a well-formed id: a UUID that matches no row must open the
    user's session, because the missing-row 404 answers only after
    the query -- exactly the branch the fence must pre-empt for
    malformed ids. Asserting both directions through one seam keeps
    the pair body-independent: neither test cares which 404 body
    renders, so branding (or un-branding) either page cannot stale
    this file again.
    """
    session_calls = _spy_on_user_db_session(monkeypatch)

    response = authenticated_client.get(f"/library/document/{uuid4()}/chunks")

    assert response.status_code == 404, response.text[:200]
    assert session_calls, (
        "the spy on database.session_context.get_user_db_session was "
        "never reached for a well-formed document_id -- the chunks page "
        "no longer resolves its DB session through that module, so the "
        "fence test above is pinning a dead seam"
    )


def test_fenced_page_routes_call_the_uuid_validator():
    """The fence call itself must survive a silent revert.

    A status-code probe alone cannot catch deletion of the chunks-page
    fence: the route's own missing-row branch 404s a malformed id too.
    The DB-session spy above catches that at the HTTP level; this check
    additionally pins the call site for the notes page, and inspecting
    the route source — the idiom
    test_both_collection_page_routes_validate uses for the collection
    pages (tests/security/test_collection_id_xss.py) — fails the
    moment either call site disappears.
    """
    for fn, param in (
        (rag.view_document_chunks, "document_id"),
        (notes.note_detail_page, "note_id"),
    ):
        src = inspect.getsource(fn)
        assert f"validated_uuid_path_param({param}" in src, (
            f"{fn.__name__} no longer fences {param} with "
            "validated_uuid_path_param"
        )