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.6 KB
Raw Blame
"""A collection id must be a real UUID before it reaches a template.

`collection_upload.html` used to interpolate `collection_id` into an inline
handler:

    onclick="window.location.href='/library/collections/{{ collection_id }}'"

Jinja autoescape is on and does escape `'` to `'`, which is enough for a
normal attribute *value* -- but not inside an event handler. Browsers
HTML-entity-decode attribute content BEFORE treating it as JS source, so the
escaping is undone and the quote closes the string. A confirmed end-to-end
exploit:

    GET /library/collections/1%27%3Balert%28document.cookie%29%3Bvar%20x%3D%27/upload

rendered `...href='/library/collections/1';alert(document.cookie);var
x=''`, which the browser decodes into executable JS. CSP does not stop it:
`script-src` includes `'unsafe-inline'`. The route is behind `require_auth`, so
it needed an authenticated victim to follow a crafted link and click Cancel --
which is one of only two buttons on the page.

The first fix validated the SHAPE at the route boundary rather than patching
the one template. Collection ids are server-generated UUID4 strings, so
anything else cannot match a real row anyway; validating there means no
template can receive a value capable of breaking out of any context, including
`collection_details.html` and any page added later. The Cancel button has
since dropped the inline handler and carries the ID as a data attribute read
by collection_upload.js; these tests pin the boundary layer, which stays.
"""

import uuid

import pytest
from fastapi import HTTPException

from local_deep_research.web.routers.rag import _validated_collection_id

# The exact payload from the confirmed exploit, plus other escape shapes.
ATTACK_IDS = [
    "1';alert(document.cookie);var x='",
    "1'-alert(1)-'",
    '1";alert(1);var y="',
    "<script>alert(1)</script>",
    "javascript:alert(1)",
    "../../etc/passwd",
    "1 OR 1=1",
    "",
    "create",
    "1",
]


@pytest.mark.parametrize("bad", ATTACK_IDS, ids=lambda s: repr(s)[:40])
def test_malformed_collection_id_is_rejected(bad):
    with pytest.raises(HTTPException) as exc_info:
        _validated_collection_id(bad)
    assert exc_info.value.status_code == 404


def test_real_uuid_is_accepted_unchanged():
    """The gate must not break the legitimate path.

    Also pins the canonical-form contract: a UUID with a
    non-canonical alias spelling (uppercase hex, ``urn:uuid:`` prefix,
    ``{...}`` braces, ``_`` digit separators) must come back as the
    stored form (``str(uuid.uuid4())``) rather than echo the alias,
    so the return value is what "well-formed UUID string" means
    downstream (#6602).
    """
    real = str(uuid.uuid4())
    assert _validated_collection_id(real) == real

    # Aliases of the same UUID: each must canonicalise to `real`.
    canonical = real  # stored form
    uppercase_hex = real.upper()
    no_hyphens = real.replace("-", "")
    braces = "{" + real + "}"
    urn = "urn:uuid:" + real
    # uuid.UUID accepts an underscore digit separator anywhere within
    # the contiguous hex stream except at the group boundaries
    # (8/12/16/20 in the hyphen-form indexing). Replace one hex digit
    # with an underscore at an interior position so the alias is 32
    # characters and exercises the parser's tolerant path; the
    # canonical form shifts the missing digit to the high nibble, so
    # the test only pins that the digit-sep alias does not 404 (its
    # canonical form differs from `real`).
    hex_digits = real.replace("-", "")
    digit_seps = (
        hex_digits[:4] + "_" + hex_digits[5:]
    )  # 32 chars, underscore at pos 4
    for alias in (uppercase_hex, no_hyphens, braces, urn):
        assert _validated_collection_id(alias) == canonical, (
            f"alias {alias!r} did not canonicalise to {canonical!r}"
        )
    # The digit-separator alias canonicalises to a different UUID (the
    # underscore shifts the hex), so only pin that it does not 404.
    assert _validated_collection_id(digit_seps) != digit_seps


def test_uuid_malformed_aliases_are_rejected():
    """Non-UUID alias spellings of the legitimate input must still 404.

    Pins the second half of #6602's contract: the parser accepts
    several alias shapes, but the canonical-form return must be
    *only* ``str(uuid.UUID(...))`` of an accepted alias. Inputs the
    parser does not accept (oversized padding, trailing junk,
    non-hex characters in a digit-separator position) must still
    404 — they cannot match a real row.

    Note: short padding (e.g. a single leading space) and
    Unicode-digit hex are *accepted* by CPython's ``uuid.UUID`` and
    canonicalise with zero-padding; the validator reflects that
    exact set so the helper and the docstring stay in sync with the
    parser. Length is the binding reject.
    """
    real = str(uuid.uuid4())
    still_bad = [
        real + "x",  # trailing junk (36 hex digits + 1 junk -> 33, over 32)
        real[:8] + "z" + real[9:],  # non-hex inside a group (36 chars)
        " " + real + "x",  # padded beyond 32 chars
        real * 2,  # doubled
    ]
    for bad in still_bad:
        with pytest.raises(HTTPException) as exc_info:
            _validated_collection_id(bad)
        assert exc_info.value.status_code == 404


# One fresh UUID-derived alias set per collection: 31 hex digits plus a pad
# keeps a space-padded alias at the accepted length; " " + real would be 37
# chars, which uuid.UUID rejects for its length and which used to skip this
# whole test (#7540).
_HEX31 = str(uuid.uuid4()).replace("-", "")[:31]

_ACCEPTED_ALIASES = [
    (" " + _HEX31, "leading space: int(..., 16) strips, canonicalises"),
    (_HEX31 + " ", "trailing space: same"),
    ("𝟏" * 32, "Unicode MATHEMATICAL BOLD DIGIT 1"),
    ("١٢" * 16, "Arabic-Indic digits"),
]


@pytest.mark.parametrize(
    ("alias", "label"),
    _ACCEPTED_ALIASES,
    ids=[
        "leading-space-pad",
        "trailing-space-pad",
        "bold-digit-1",
        "arabic-indic-digits",
    ],
)
def test_uuid_accepted_aliases_match_cpython_uuid(alias, label):
    """Pins the exact set the validator accepts: CPython's uuid.UUID.

    The shared helper `web/utils/path_ids.py` and the local twin in
    `web/routers/rag.py` both delegate to ``uuid.UUID`` and only
    differ from "reject all" on the cases uuid.UUID itself accepts.
    This test pins the boundary explicitly so future Python upgrades
    that change ``uuid.UUID``'s accepted set trip the suite.
    """
    # uuid.UUID itself must accept; otherwise this case is stale on this
    # Python, so skip it and let each remaining case report (#7540).
    try:
        canonical = uuid.UUID(alias)
    except ValueError:
        pytest.skip(f"uuid.UUID no longer accepts {alias!r} on this Python")
    # and so must the validator
    result = _validated_collection_id(alias)
    assert result == str(canonical)


def test_both_collection_page_routes_validate():
    """Both parameterised collection pages must call the validator.

    `collection_details.html` interpolates the same value into a `<script>`
    block, where it is NOT currently exploitable (script content is parsed as
    raw text, so the escaping holds). That makes it exactly the kind of site
    that becomes exploitable later after an innocuous markup change, so it is
    gated too rather than relied upon to stay safe.
    """
    import inspect

    from local_deep_research.web.routers import rag

    for fn in (rag.collection_details_page, rag.collection_upload_page):
        src = inspect.getsource(fn)
        assert "_validated_collection_id(collection_id)" in src, (
            f"{fn.__name__} does not validate collection_id"
        )