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.

15.6 KB
Raw Blame
"""Per-user keying fences for the notes router's slowapi rate-limit buckets.

Main (Flask-Limiter) pinned these invariants in
``tests/notes/test_notes_routes_review_fixes.py`` (deleted with the
FastAPI migration). This file restores them against the slowapi wiring in
``web/routers/notes.py`` + ``web/dependencies/rate_limit.py``:

  * Every notes bucket keys per authenticated USER (``key_func=_user_key``),
    not per client IP. Without it, the Limiter default (client IP) makes
    users behind one NAT/proxy IP drain each other's LLM/write budgets.
  * The six buckets have DISTINCT scopes — in particular the read bucket
    (``notes_search``) never shares a window with the mutate buckets
    (``notes_write`` / ``notes_save_as_note`` / ``notes_factcheck``), so a
    burst of saves cannot lock a user out of reading their notes.
  * The rate numbers are pinned (3/min factcheck < 5/min synthesize <
    10/min ai & save-as-note < 60/min search & write ordering is
    deliberate, cost-tiered).
  * The wiring is real: the callable FastAPI registered as the route
    endpoint IS the slowapi wrapper. A decorator-order slip
    (``@_notes_write_limit`` above ``@router.post``) would leave the limit
    registered in the limiter but never enforced.

Enforcement tests drive slowapi's real ``_check_request_limit`` (the exact
call the decorator wrapper makes) against the real registered limits, the
real key funcs and the real storage — no re-declared limits, so they fail
if the router's buckets regress to per-IP keying or to a merged scope.
"""

# allow: no-sut-import — the SUT is imported dynamically via
# ``importlib.import_module(NOTES_MODULE)`` below (and patched by qualified
# name), so there is no literal ``import local_deep_research...`` statement
# for the shadow-test scanner to find. This file does exercise the real
# ``web/routers/notes.py`` and ``web/dependencies/rate_limit.py``.
import importlib
import uuid

import pytest
from slowapi.errors import RateLimitExceeded
from starlette.requests import Request

NOTES_MODULE = "local_deep_research.web.routers.notes"

# Bucket -> (amount, granularity) pinned from web/routers/notes.py.
EXPECTED_BUCKET_RATES = {
    "notes_ai": (10, "minute"),
    "notes_search": (60, "minute"),
    "notes_synthesize": (5, "minute"),
    "notes_write": (60, "minute"),
    "notes_save_as_note": (10, "minute"),
    "notes_factcheck": (3, "minute"),
}

# Every rate-limited notes endpoint -> its bucket scope. Read endpoints
# must land in notes_search; mutations in notes_write (or their tighter
# cost-aware buckets). An endpoint moving to the wrong bucket is a real
# regression (e.g. create_note in the read bucket would let scripted
# writes ride the read budget).
EXPECTED_ENDPOINT_BUCKETS = {
    # -- reads: cheap lookups / FAISS / listing --------------------------
    "list_notes": "notes_search",
    "semantic_search_notes": "notes_search",
    "ask_context": "notes_search",
    "search_notes_for_linking": "notes_search",
    "get_research_notes": "notes_search",
    "get_research_annotations": "notes_search",
    "get_document_notes": "notes_search",
    "get_document_annotations": "notes_search",
    "get_similar_notes": "notes_search",
    "get_backlinks": "notes_search",
    "get_outgoing_links": "notes_search",
    "get_suggested_links": "notes_search",
    "get_unlinked_mentions": "notes_search",
    "similar_passages": "notes_search",
    "resolve_link": "notes_search",
    # -- mutations: generic write budget ---------------------------------
    "create_note": "notes_write",
    "update_note": "notes_write",
    "delete_note": "notes_write",
    "add_note_to_collection": "notes_write",
    "remove_note_from_collection": "notes_write",
    "link_research_to_note": "notes_write",
    "patch_note_research": "notes_write",
    "reorder_note_research": "notes_write",
    "create_research_note": "notes_write",
    "create_research_annotation": "notes_write",
    "delete_research_annotation": "notes_write",
    "create_document_note": "notes_write",
    "create_document_annotation": "notes_write",
    "delete_document_annotation": "notes_write",
    "index_note_to_collection": "notes_write",
    "accept_suggested_link": "notes_write",
    "restore_note_version": "notes_write",
    # -- LLM-cost endpoints ----------------------------------------------
    "summarize_note": "notes_ai",
    "extract_research_questions": "notes_ai",
    "suggest_tags": "notes_ai",
    "extract_key_concepts": "notes_ai",
    "get_semantic_diff": "notes_ai",
    # -- multi-note synthesis (most expensive LLM path) ------------------
    "preview_synthesis": "notes_synthesize",
    "synthesize_notes": "notes_synthesize",
    # -- whole-report copy (disk-write vector) ---------------------------
    "save_research_as_note": "notes_save_as_note",
    # -- kicks off a full research run downstream ------------------------
    "fact_check_note": "notes_factcheck",
    "grade_note_fact_check": "notes_factcheck",
}


@pytest.fixture()
def notes_mod():
    """The notes router module as currently loaded.

    Resolved per-test (idiom from tests/web/dependencies/
    test_rate_limit_keys.py): a sibling test file may reload the
    rate_limit module, so all assertions use the limiter / _user_key
    objects the notes module actually bound at ITS import — the ones its
    routes are registered with.
    """
    return importlib.import_module(NOTES_MODULE)


def _limits_for(notes_mod, endpoint_name):
    """The slowapi Limit objects registered for a notes endpoint."""
    qualified = f"{NOTES_MODULE}.{endpoint_name}"
    limits = notes_mod.limiter._route_limits.get(qualified, [])
    assert limits, (
        f"{qualified} has no registered rate limit — the bucket decorator "
        "was removed or renamed"
    )
    return limits


def make_request(username=None, ip="10.201.7.1", method="POST", path="/x"):
    """Minimal Starlette Request from a raw ASGI scope.

    ``username=None`` means no session key at all (anonymous / no
    SessionMiddleware), which exercises the per-IP fallback path.
    """
    scope = {
        "type": "http",
        "method": method,
        "path": path,
        "query_string": b"",
        "headers": [],
        "client": (ip, 51234),
    }
    if username is not None:
        scope["session"] = {"username": username}
    return Request(scope)


def _unique_user(tag):
    return f"nrlk-{tag}-{uuid.uuid4().hex[:12]}"


class TestPerUserKeying:
    """Regression guard: notes buckets must key per user, not per IP."""

    @pytest.mark.parametrize("endpoint", sorted(EXPECTED_ENDPOINT_BUCKETS))
    def test_endpoint_bucket_keys_per_user(self, notes_mod, endpoint):
        for lim in _limits_for(notes_mod, endpoint):
            assert lim.key_func is notes_mod._user_key, (
                f"{endpoint} must pass key_func=_user_key — got "
                f"{lim.key_func!r}. Without per-user keying, users behind "
                "a shared NAT/proxy IP starve each other's notes budgets."
            )

    def test_no_notes_bucket_uses_the_limiter_default_ip_key(self, notes_mod):
        """The Limiter default key (_get_client_ip) is per-IP; no notes
        bucket may fall back to it."""
        default_key = notes_mod.limiter._key_func
        for endpoint in EXPECTED_ENDPOINT_BUCKETS:
            for lim in _limits_for(notes_mod, endpoint):
                assert lim.key_func is not default_key, (
                    f"{endpoint} is keyed by the limiter default (client "
                    "IP) — per-user keying was dropped"
                )

    def test_user_key_separates_two_users_on_the_same_ip(self, notes_mod):
        """The key func the buckets are wired with must yield different
        bucket keys for different logged-in users on ONE IP."""
        key_func = _limits_for(notes_mod, "create_note")[0].key_func
        key_a = key_func(make_request(username="alice", ip="10.201.7.9"))
        key_b = key_func(make_request(username="bob", ip="10.201.7.9"))
        assert key_a != key_b
        assert "alice" in key_a and "bob" in key_b


class TestBucketScopes:
    """The six buckets are distinct; reads never share a mutate window."""

    @pytest.mark.parametrize(
        "endpoint,bucket", sorted(EXPECTED_ENDPOINT_BUCKETS.items())
    )
    def test_endpoint_is_in_its_bucket(self, notes_mod, endpoint, bucket):
        scopes = {lim.scope for lim in _limits_for(notes_mod, endpoint)}
        assert scopes == {bucket}, (
            f"{endpoint} must be in the '{bucket}' shared bucket, got {scopes}"
        )

    def test_exactly_six_distinct_scopes(self, notes_mod):
        """No two buckets may share a scope: slowapi keeps ONE counter per
        (key, scope), so a scope collision silently merges budgets."""
        scopes = set()
        for endpoint in EXPECTED_ENDPOINT_BUCKETS:
            for lim in _limits_for(notes_mod, endpoint):
                scopes.add(lim.scope)
        assert scopes == set(EXPECTED_BUCKET_RATES), (
            f"Expected the 6 notes scopes {sorted(EXPECTED_BUCKET_RATES)}, "
            f"got {sorted(scopes)}"
        )

    def test_read_scope_differs_from_every_mutate_scope(self, notes_mod):
        read_scopes = {
            lim.scope for lim in _limits_for(notes_mod, "list_notes")
        }
        for mutator in (
            "create_note",
            "save_research_as_note",
            "fact_check_note",
        ):
            mutate_scopes = {
                lim.scope for lim in _limits_for(notes_mod, mutator)
            }
            assert not (read_scopes & mutate_scopes), (
                f"read bucket shares a scope with {mutator} — a burst of "
                "writes would lock the user out of reading their notes"
            )

    @pytest.mark.parametrize(
        "bucket,expected", sorted(EXPECTED_BUCKET_RATES.items())
    )
    def test_bucket_rate_is_pinned(self, notes_mod, bucket, expected):
        amount, granularity = expected
        seen = set()
        for endpoint, b in EXPECTED_ENDPOINT_BUCKETS.items():
            if b != bucket:
                continue
            for lim in _limits_for(notes_mod, endpoint):
                seen.add((lim.limit.amount, lim.limit.GRANULARITY.name))
        assert seen == {(amount, granularity)}, (
            f"{bucket} must be {amount} per {granularity}, got {seen}"
        )


class TestDecoratorWiring:
    """The limits are wired into what FastAPI actually calls."""

    def test_registry_covers_exactly_the_expected_endpoints(self, notes_mod):
        registered = {
            name.rsplit(".", 1)[1]
            for name in notes_mod.limiter._route_limits
            if name.startswith(NOTES_MODULE + ".")
        }
        expected = set(EXPECTED_ENDPOINT_BUCKETS)
        missing = expected - registered
        assert not missing, f"buckets lost from: {sorted(missing)}"
        extra = registered - expected
        assert not extra, (
            f"new rate-limited notes endpoints {sorted(extra)} — add them "
            "to EXPECTED_ENDPOINT_BUCKETS with their intended bucket"
        )

    def test_route_endpoints_are_the_slowapi_wrappers(self, notes_mod):
        """FastAPI must have registered the slowapi WRAPPER (which runs
        _check_request_limit), not the raw handler. If the decorators were
        stacked in the wrong order (@limit above @router.method), the
        limiter registry would still list the route while the app served
        the unlimited raw function — a silent no-op limit."""
        checked = set()
        for route in notes_mod.router.routes:
            endpoint = getattr(route, "endpoint", None)
            name = getattr(endpoint, "__name__", None)
            if name not in EXPECTED_ENDPOINT_BUCKETS:
                continue
            assert hasattr(endpoint, "__wrapped__"), (
                f"route for {name} serves the raw handler — the rate-limit "
                "wrapper is not in the call path"
            )
            checked.add(name)
        assert checked == set(EXPECTED_ENDPOINT_BUCKETS), (
            "router is missing routes for: "
            f"{sorted(set(EXPECTED_ENDPOINT_BUCKETS) - checked)}"
        )

    def test_no_notes_endpoint_uses_a_dynamic_limit(self, notes_mod):
        """A callable limit value would move the route into
        _dynamic_route_limits, un-exempting it from SlowAPIMiddleware —
        which runs outside SessionMiddleware, where _user_key collapses
        to per-IP. Notes limits must stay static."""
        dynamic = [
            name
            for name in notes_mod.limiter._dynamic_route_limits
            if name.startswith(NOTES_MODULE + ".")
        ]
        assert dynamic == []


class TestEnforcement:
    """Drive slowapi's real check (real limits, key funcs, storage).

    Uses the same private entry point the decorator wrapper calls
    (``_check_request_limit(request, endpoint_func, in_middleware=False)``)
    so nothing about the buckets is re-declared in the test. Usernames are
    uuid-unique per run, so the shared in-memory counters never collide
    with other tests. ``limiter.enabled`` is monkeypatched (restored by
    pytest) because the test env may run with rate limiting disabled.
    """

    @pytest.fixture()
    def check(self, notes_mod, monkeypatch):
        monkeypatch.setattr(notes_mod.limiter, "enabled", True)

        def _check(endpoint_name, username, ip="10.201.9.1"):
            request = make_request(username=username, ip=ip)
            notes_mod.limiter._check_request_limit(
                request, getattr(notes_mod, endpoint_name), False
            )

        return _check

    def test_one_users_exhausted_factcheck_does_not_block_another_on_same_ip(
        self, check
    ):
        """The NAT scenario: user A burns the 3/min factcheck budget; user
        B on the SAME IP must still get through. Fails if the buckets
        regress to per-IP keying."""
        user_a = _unique_user("fca")
        user_b = _unique_user("fcb")
        ip = "10.201.9.77"

        for _ in range(3):
            check("fact_check_note", user_a, ip=ip)
        with pytest.raises(RateLimitExceeded):
            check("fact_check_note", user_a, ip=ip)

        # Same IP, different session user: separate bucket, still allowed.
        check("fact_check_note", user_b, ip=ip)

        # And user A stays blocked (the 429 above did not reset anything).
        with pytest.raises(RateLimitExceeded):
            check("fact_check_note", user_a, ip=ip)

    def test_exhausted_write_bucket_blocks_writes_but_not_reads(self, check):
        """Read vs mutate separation, behaviorally: exhausting the 60/min
        write budget must (a) block sibling write routes — they SHARE the
        bucket — while (b) leaving the read and save-as-note buckets
        untouched."""
        user = _unique_user("rw")

        for _ in range(60):
            check("create_note", user)
        with pytest.raises(RateLimitExceeded):
            check("create_note", user)

        # (a) update_note draws from the same shared notes_write bucket.
        with pytest.raises(RateLimitExceeded):
            check("update_note", user)

        # (b) reads and the separately-budgeted save-as-note still pass.
        check("list_notes", user)
        check("save_research_as_note", user)

    def test_anonymous_requests_fall_back_to_per_ip_buckets(self, check):
        """No session -> the key degrades to the client IP (each anonymous
        IP gets its own bucket; two IPs don't share)."""
        ip_a = "10.202.1.1"
        ip_b = "10.202.1.2"
        for _ in range(3):
            check("fact_check_note", None, ip=ip_a)
        with pytest.raises(RateLimitExceeded):
            check("fact_check_note", None, ip=ip_a)
        # A different anonymous IP is a different bucket.
        check("fact_check_note", None, ip=ip_b)