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.

3.2 KB
Raw Blame
"""UUID shape validation for parameterized page-route ids.

Page routes that interpolate a path parameter into templates validate
the id's *shape* at the boundary: collection/note/document ids are
server-generated UUID4 strings (``String(36)`` columns assigned from
``str(uuid.uuid4())``), so anything else is a typo or an injection
attempt. Shape validation is strictly more robust than fixing any one
template's escaping — it holds for every interpolation context,
including inline-JS handlers where HTML-escaping is not sufficient.

Same pattern as ``_validated_collection_id`` (the stored-reflection XSS
fix for the collection pages), but that helper is not reused here: it
stays a local twin in ``rag.py`` so the route-table parity scanner
(which only follows same-module dependency closures) still sees its
404 for the collection routes. This shared helper is for page-route
fences outside ``rag.py`` instead. Not every page route is fenced yet:
``GET /chat/{session_id}`` and ``GET
/library/document/{document_id}/txt`` still pass their raw path
parameter (``session_id``, ``document_id``) into template contexts.
``GET /library/document/{document_id}`` is unfenced too, but does not
pass the raw id into its template — it looks the document up first and
passes the object instead (``context={"request": request, "document":
document}``, library.py). No known live vulnerability at the two
raw-id sites (autoescaped attribute contexts) — they are the same
hardening tier and remain to be fenced.
"""

import uuid

from fastapi import HTTPException
from loguru import logger


def validated_uuid_path_param(value: object, label: str) -> str:
    """Return *value*'s canonical UUID form if well-formed, else 404.

    Returns ``str(uuid.UUID(str(value)))`` rather than echoing *value*
    back verbatim, so alias spellings of the same UUID — for example an
    ``urn:uuid:`` prefix, ``{...}`` braces, hyphen-free hex or uppercase
    hex; ``uuid.UUID`` tolerates a few more, such as ``_`` digit
    separators — come back canonical, which is what makes "well-formed
    UUID string" above actually true of the return value. Length is the
    binding reject: a padded input that pushes the total past 32
    characters fails the parser and 404s; shorter padded inputs
    (e.g. leading/trailing whitespace) are accepted by ``int(..., 16)``
    and canonicalise with zero-padding, and CPython's base-16 parser
    also accepts Unicode digit characters (`"𝟏"` / Arabic-Indic
    digits), so this validator is NOT a strict whitelist of ASCII
    hex — the canonical output is pure ASCII hex, which is what makes
    it safe to interpolate in any template context. Ids as stored are
    already ``str(uuid.uuid4())`` (lowercase, hyphenated, no wrapper),
    so canonicalising a real id is a no-op round-trip.

    A malformed id cannot match a real row, so 404 is the honest
    answer for both typos and injection attempts.
    """
    try:
        canonical = str(uuid.UUID(str(value)))
    except (ValueError, AttributeError, TypeError):
        value_preview = repr(value)[:100]
        logger.warning("Rejected malformed {}: {}", label, value_preview)
        raise HTTPException(
            status_code=404, detail=f"{label} not found"
        ) from None
    return canonical