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
"""Direct unit tests for ``validated_uuid_path_param`` (web/utils/path_ids.py).

Mirrors ``tests/security/test_collection_id_xss.py:50-60``, the direct
unit test for ``_validated_collection_id`` -- the sibling helper this
one's docstring is modelled on. ``test_page_route_uuid_fences.py``
already exercises this function indirectly through the two page routes
over HTTP; these tests drive it directly, including the property that
distinguishes it from a bare shape check: the return value is the
*canonical* form of the UUID, not an echo of whatever alias spelling
was submitted (path_ids.py: ``canonical = str(uuid.UUID(str(value)))``).
"""

import uuid

import pytest
from fastapi import HTTPException

from local_deep_research.web.utils.path_ids import validated_uuid_path_param

_CANON = "00000000-0000-0000-0000-000000000000"
_HEX32 = "0" * 32

# Malformed / hostile shapes: none of these is a well-formed UUID under
# any spelling, so every one must 404 -- whatever the payload.
REJECTED_VALUES = [
    "<script>alert(1)</script>",
    "1';alert(document.cookie);var x='",
    '1";alert(1);var y="',
    "../../etc/passwd",
    "1 OR 1=1",
    "",
    "not-a-uuid",
    7,
    None,
    # ``uuid.UUID`` strips only braces and hyphens before its 32-character
    # length guard, so padding a full 32-digit hex string leaves 33
    # characters and is rejected -- ``int(hex, 16)`` would tolerate the
    # whitespace, but it never runs. (Whitespace plus 31 hex digits does
    # parse, as a *different* value, so it is not an alias of this one.)
    (" " + _HEX32),
    (_HEX32 + "\n"),
]


@pytest.mark.parametrize("bad", REJECTED_VALUES, ids=lambda v: repr(v)[:40])
def test_malformed_value_is_rejected(bad):
    """Every non-UUID shape 404s, carrying the fence's own status code."""
    with pytest.raises(HTTPException) as exc_info:
        validated_uuid_path_param(bad, "Note")
    assert exc_info.value.status_code == 404


# Alias spellings of the SAME UUID value: must be accepted, and the
# return value must be that UUID's canonical form -- not the alias
# verbatim. (Pre-tidy, ``validated_uuid_path_param`` returned
# ``str(value)``, so every one of these reached the template exactly
# as submitted; see gate report R5.)
ALIAS_VALUES = [
    ("urn_uuid_prefix", "urn:uuid:" + _CANON),
    ("braces", "{" + _CANON + "}"),
    ("hyphen_free_hex", _HEX32),
    ("sixteen_kb_hyphens_plus_hex", "-" * 16384 + _HEX32),
    ("unicode_digits_arabic_indic", "٠" * 32),
    ("uppercase_hex", "A" * 32),
]


@pytest.mark.parametrize(
    "value", [v for _, v in ALIAS_VALUES], ids=[n for n, _ in ALIAS_VALUES]
)
def test_alias_shape_is_accepted_and_canonicalised(value):
    result = validated_uuid_path_param(value, "Note")
    # Same UUID value as the alias input...
    assert uuid.UUID(result) == uuid.UUID(str(value))
    # ...returned in canonical form: idempotent under re-parsing, so it
    # is not just "a" valid spelling but *the* canonical one.
    assert result == str(uuid.UUID(result))


def test_real_uuid4_round_trips_unchanged():
    """Stored ids are already ``str(uuid.uuid4())``: canonicalising a
    real id must be a no-op, or every existing note/document link
    breaks.
    """
    real = str(uuid.uuid4())
    assert validated_uuid_path_param(real, "Note") == real