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.

64.6 KB
Raw Blame
"""Cross-user isolation (IDOR) census over the REAL FastAPI route table.

Why a census and not a pile of request tests
--------------------------------------------
This app is multi-tenant with per-user *encrypted* SQLCipher databases. There
is no owner column to forget: isolation is enforced by *which database file
gets opened*, and that is keyed entirely by one string — the username. Flask
enforced it through ``@login_required`` plus a request-global username; the
FastAPI port re-expressed the same rule as ``Depends(require_auth)`` plus an
explicitly threaded ``username`` argument, by hand, across the whole web
package. The failure mode that migration introduces is not "a route 500s", it
is "one route out of 314 threads the wrong string", and a behavioural suite
only finds that if someone thought to write a test for that exact route.

So this module enumerates EVERY route from source and proves, per route, that:

  1. it authenticates (or is on an explicit, exhaustively-asserted public
     allowlist) — :func:`test_every_route_authenticates_or_is_declared_public`;
  2. no user identity is ever accepted from request input — path, query, body,
     header or form — outside login/register
     (:func:`test_no_route_accepts_a_user_identity_from_request_input`);
  3. every call that takes a ``username`` / ``user_id`` / ``owner`` parameter
     receives the value produced by the auth dependency and nothing else
     (:func:`test_identity_sinks_only_ever_receive_the_authenticated_username`);
  4. every ``get_user_db_session(...)`` — the single chokepoint that decides
     which encrypted database is opened — is passed an authenticated username
     (:func:`test_every_get_user_db_session_call_is_passed_an_auth_username`);
  5. routes that take an object id reach a user-scoped access, or are on an
     allowlist of render-only page shells
     (:func:`test_object_id_routes_reach_a_user_scoped_access`);
  6. the id-keyed PROCESS-GLOBAL research registry (``web/research_state.py``,
     shared by every user) is only touched after an owner-scoped database
     lookup in the same handler
     (:func:`test_global_research_state_is_only_touched_after_an_owner_lookup`).

Everything here is pure AST analysis over the installed package source: no app
boot, no TestClient, no database. That is deliberate — it makes the census
exhaustive (it cannot skip a route it forgot to request) and cheap enough to
run on every commit.

Anti-vacuity
------------
A static census fails open if its analyzer silently resolves nothing, so two
guards are built in:

* :func:`test_census_analyzer_actually_resolves_known_identity_sinks` pins a
  floor on how much the analyzer resolved and names specific call sites it
  must have found. If an import-resolution or indexing bug blanks the
  analysis, that test fails instead of the census passing empty.
* :func:`test_census_flags_deliberately_unscoped_handlers` is the positive
  control: it feeds hand-written vulnerable handlers (username from a query
  param, an unscoped ``get_user_db_session``, a missing auth dependency, a
  global-registry read with no owner lookup) through the SAME functions the
  census uses and asserts each one is reported.

The analyzer locates the package via ``importlib.util.find_spec`` rather than
a path relative to this file, so pointing ``PYTHONPATH`` at a mutated copy of
``local_deep_research`` re-runs the whole census against that copy — which is
how the negative controls above were exercised by hand.
"""

from __future__ import annotations

# allow: no-sut-import — a static census over the production SOURCE of
# local_deep_research.web.routers. It deliberately does not IMPORT the
# package: importing would boot routers, settings and the socket layer,
# and a census must read every route module including any that fails to
# import. The package is located with importlib.util.find_spec, so the
# analysis follows PYTHONPATH to whichever copy is on the path (which is
# how the negative controls for this file are run).


import ast
import functools
import importlib.util
import re
from dataclasses import dataclass, field
from pathlib import Path

import pytest

# ---------------------------------------------------------------------------
# Locating the package under census
# ---------------------------------------------------------------------------

_SPEC = importlib.util.find_spec("local_deep_research")
assert _SPEC is not None and _SPEC.origin is not None, (
    "local_deep_research is not importable; the census cannot run"
)
PKG_ROOT = Path(_SPEC.origin).parent
ROUTERS_DIR = PKG_ROOT / "web" / "routers"
FASTAPI_APP = PKG_ROOT / "web" / "fastapi_app.py"
AUTH_DEPS = PKG_ROOT / "web" / "dependencies" / "auth.py"

HTTP_METHODS = frozenset(
    {"get", "post", "put", "patch", "delete", "head", "options", "api_route"}
)

# Parameter names that carry a user identity. A value reaching one of these
# decides which encrypted database is opened / which rows are returned.
IDENTITY_PARAMS = frozenset({"username", "user_id", "owner", "owner_username"})

# The one function that turns a username into an open per-user database.
DB_CHOKEPOINT = "get_user_db_session"

# Process-global, id-keyed research registry shared by ALL users
# (web/research_state.py). Reaching these with an attacker-supplied id and no
# prior owner check is a cross-user read/write.
GLOBAL_RESEARCH_STATE = frozenset(
    {
        "get_active_research_snapshot",
        "is_research_active",
        "get_research_field",
        "set_termination_flag",
        "clear_termination_flag",
        "is_termination_requested",
        "update_active_research",
        "remove_active_research",
        "cleanup_research",
        "is_research_thread_alive",
        "update_progress_if_higher",
        "update_progress_and_check_active",
        "append_research_log",
        "set_active_research",
    }
)


# ---------------------------------------------------------------------------
# Allowlists — every entry is a claim, with the reason it is safe
# ---------------------------------------------------------------------------

#: Routes with no auth-derived dependency. Asserted by EQUALITY, so a newly
#: unauthenticated route fails the census even if someone forgets this list.
PUBLIC_ROUTES = frozenset(
    {
        # Liveness probe. Returns process-level status only; the extra
        # `resources` block is gated on an authenticated session inside.
        ("GET", "/api/v1/health"),
        # Pre-login CSRF token issuance.
        ("GET", "/auth/csrf-token"),
        # Login / registration must be reachable while logged out. These are
        # the ONLY two routes permitted to read a username from request input
        # (see IDENTITY_FROM_INPUT_ALLOWED).
        ("GET", "/auth/login"),
        ("POST", "/auth/login"),
        ("GET", "/auth/register"),
        ("POST", "/auth/register"),
        # Password-strength check on the registration form; takes a password,
        # never a username, and touches no database.
        ("POST", "/auth/validate-password"),
        # Logout and session probe operate on request.session's OWN username;
        # requiring auth would make logout un-callable from a dead session.
        ("POST", "/auth/logout"),
        ("GET", "/auth/check"),
        # Legacy static-asset URL; serves the asset directly (no redirect),
        # no user data.
        ("GET", "/redirect-static/{path:path}"),
    }
)

#: (module, function) pairs allowed to bind an identity from request input.
IDENTITY_FROM_INPUT_ALLOWED = frozenset(
    {
        ("auth.py", "login"),
        ("auth.py", "register"),
    }
)

#: Authenticated routes that take an object id in the path but never use the
#: authenticated username. Each was read: they render a template shell and
#: echo the id back into the page; the data is fetched afterwards by the
#: user-scoped API routes. None of them reads user data.
TEMPLATE_ONLY_ID_ROUTES = frozenset(
    {
        ("GET", "/chat/{session_id}"),
        ("GET", "/notes/{note_id}"),
        ("GET", "/library/collections/{collection_id}"),
        ("GET", "/progress/{research_id}"),
        ("GET", "/details/{research_id}"),
        ("GET", "/results/{research_id}"),
        # Returns the literal string "/library/api/document/{id}/pdf" plus a
        # constant title. Zero database access; the /pdf route it names IS
        # scoped (library.py view_pdf_page opens the caller's own database and
        # 404s on a document row that is not there).
        ("GET", "/library/api/document/{document_id}/pdf-url"),
        # Model pricing is operator-level reference data, not user data.
        ("GET", "/metrics/api/pricing/{model_name}"),
        # news.api.research_news_item is an unimplemented stub that raises
        # NotImplementedException (501) and reads nothing.
        ("POST", "/news/api/research/{card_id}"),
        # Public; listed in PUBLIC_ROUTES too.
        ("GET", "/redirect-static/{path:path}"),
    }
)

#: Identity sinks fed a value the analyzer cannot prove is the auth username.
#: Keyed as (module, enclosing function, callee, parameter).
IDENTITY_SINK_ALLOWED = frozenset(
    {
        # rag.py get_available_models: `username_from_snapshot(snapshot) or
        # username`. The snapshot is built inside this handler from
        # get_user_db_session(username) -> get_settings_manager(...), so its
        # "_username" (when present) is the caller's own. The value feeds an
        # EGRESS-policy context, not a database scope.
        (
            "rag.py",
            "get_available_models",
            "context_from_snapshot",
            "username",
        ),
    }
)

#: Routers permitted to open the central auth database (ldr_auth.db, which
#: holds usernames only — no user content). Account lifecycle only.
AUTH_DB_ALLOWED_MODULES = frozenset({"auth.py"})


# ---------------------------------------------------------------------------
# AST helpers
# ---------------------------------------------------------------------------


def _parse(path: Path) -> ast.Module:
    return ast.parse(path.read_text(encoding="utf-8"), filename=str(path))


def _module_name(path: Path) -> str:
    return ".".join(path.relative_to(PKG_ROOT.parent).with_suffix("").parts)


def _params(fn: ast.FunctionDef | ast.AsyncFunctionDef):
    """Yield ``(name, default_node_or_None, annotation_node_or_None)`` for
    every declared parameter.
    """
    a = fn.args
    positional = a.posonlyargs + a.args
    defaults = [None] * (len(positional) - len(a.defaults)) + list(a.defaults)
    for p, d in zip(positional, defaults):
        yield p.arg, d, p.annotation
    for p, d in zip(a.kwonlyargs, a.kw_defaults):
        yield p.arg, d, p.annotation


def _dependency_name(node) -> str | None:
    """Name of the callable ``node`` denotes: ``f`` / ``mod.f`` -> ``f``;
    a dunder read off it (``get_user_db_session.__wrapped__``) is the
    callable itself; a quoted forward reference (``"NoteService"``) is
    resolved the way FastAPI resolves it, by name."""
    if isinstance(node, ast.Constant) and isinstance(node.value, str):
        try:
            node = ast.parse(node.value.strip(), mode="eval").body
        except SyntaxError:
            return None
    while isinstance(node, ast.Attribute) and node.attr.startswith("__"):
        node = node.value
    if isinstance(node, ast.Name):
        return node.id
    if isinstance(node, ast.Attribute):
        return node.attr
    return None


def _depends_target(default, annotation=None) -> str | None:
    """Return the callable name inside ``Depends(...)``/``Security(...)``.

    Both the bare and the qualified spelling (``fastapi.Depends``) and the
    ``dependency=`` keyword count. With no dependency (``Depends()`` /
    ``Depends(None)``) FastAPI calls the parameter's ANNOTATION itself, so
    the target is the annotated type's name (``svc: NoteService =
    Depends()`` -> ``NoteService``).
    """
    if not isinstance(default, ast.Call):
        return None
    func = default.func
    name = (
        func.id
        if isinstance(func, ast.Name)
        else func.attr
        if isinstance(func, ast.Attribute)
        else None
    )
    if name not in ("Depends", "Security"):
        return None
    arg = default.args[0] if default.args else None
    if arg is None:
        arg = next(
            (kw.value for kw in default.keywords if kw.arg == "dependency"),
            None,
        )
    if arg is None or (isinstance(arg, ast.Constant) and arg.value is None):
        arg = annotation
    return _dependency_name(arg)


#: Nesting bound for a quoted annotation (a string inside a string ...).
_MAX_QUOTE_DEPTH = 8


def _unquote(annotation, imports=None):
    """A wholly quoted annotation parsed the way FastAPI evaluates it:
    ``"Annotated[NoteService, Depends()]"`` -> the ``Annotated`` subscript
    (recursively, up to ``_MAX_QUOTE_DEPTH``). A literal ``typing.ForwardRef``
    type argument is handled the same way, using the module's import bindings
    for both constructor and module aliases. Other call arguments and
    annotation metadata remain data. Unresolvable expressions are left as is.
    """
    imports = imports or {}
    for _ in range(_MAX_QUOTE_DEPTH):
        if isinstance(annotation, ast.Call):
            constructor = annotation.func
            is_forward_ref = (
                isinstance(constructor, ast.Name)
                and imports.get(constructor.id) == ("typing", "ForwardRef")
            ) or (
                isinstance(constructor, ast.Attribute)
                and constructor.attr == "ForwardRef"
                and isinstance(constructor.value, ast.Name)
                and imports.get(constructor.value.id) == ("typing", None)
            )
            if not is_forward_ref:
                break
            argument = (
                annotation.args[0]
                if annotation.args
                else next(
                    (kw.value for kw in annotation.keywords if kw.arg == "arg"),
                    None,
                )
            )
            if not (
                isinstance(argument, ast.Constant)
                and isinstance(argument.value, str)
            ):
                break
            annotation = argument
        if not (
            isinstance(annotation, ast.Constant)
            and isinstance(annotation.value, str)
        ):
            break
        try:
            annotation = ast.parse(annotation.value.strip(), mode="eval").body
        except (SyntaxError, ValueError, RecursionError, MemoryError):
            break
    return annotation


def _annotated_elements(annotation) -> list:
    """The elements of ``Annotated[...]``'s subscript, with a starred
    tuple or list literal unpacked as Python unpacks it
    (``Annotated[*(T, Depends(f))]`` -> ``[T, Depends(f)]``)."""
    sl = annotation.slice
    elements = []
    for element in sl.elts if isinstance(sl, ast.Tuple) else [sl]:
        if isinstance(element, ast.Starred) and isinstance(
            element.value, (ast.Tuple, ast.List)
        ):
            elements.extend(element.value.elts)
        else:
            elements.append(element)
    return elements


def _annotated_type(annotation):
    """The wrapped type of ``Annotated[T, ...]``, else ``annotation``."""
    if _is_annotated(annotation):
        elements = _annotated_elements(annotation)
        return elements[0] if elements else annotation
    return annotation


def _is_annotated(annotation) -> bool:
    if not isinstance(annotation, ast.Subscript):
        return False
    head = annotation.value
    return (isinstance(head, ast.Name) and head.id == "Annotated") or (
        isinstance(head, ast.Attribute) and head.attr == "Annotated"
    )


def _annotated_metadata(annotation):
    """Yield each metadata expression inside ``Annotated[T, meta, ...]``.

    Handles both the bare-name (``Annotated[...]``) and qualified
    (``typing.Annotated[...]``) spellings. ``ast.Subscript.slice`` is the
    expression directly on the Python versions this project targets (3.9+),
    so a multi-element subscript is an ``ast.Tuple``; a starred tuple or
    list literal in it is unpacked (see ``_annotated_elements``).
    """
    if not _is_annotated(annotation):
        return
    # The first element is the wrapped type; everything after it is
    # metadata.
    yield from _annotated_elements(annotation)[1:]


def _depends_targets(default, annotation, imports=None) -> list:
    """Every ``Depends(...)``/``Security(...)`` callable name for one
    parameter, whichever spelling is used.

    A handler may express its dependency either the legacy way
    (``x: T = Depends(f)``) or via ``Annotated``
    (``x: Annotated[T, Depends(f)]``) — both must be detected or the census
    silently blinds itself to every ``Annotated``-spelled route. A wholly
    quoted annotation (``x: "Annotated[T, Depends(f)]"``) is evaluated by
    FastAPI, so it is parsed first (see ``_unquote``).
    """
    targets = []
    annotation = _unquote(annotation, imports)
    wrapped = _unquote(_annotated_type(annotation), imports)
    t = _depends_target(default, wrapped)
    if t is not None:
        targets.append(t)
    for meta in _annotated_metadata(annotation):
        t = _depends_target(meta, wrapped)
        if t is not None:
            targets.append(t)
    return targets


def _router_prefix(tree: ast.Module) -> str:
    for node in tree.body:
        if (
            isinstance(node, ast.Assign)
            and any(
                isinstance(t, ast.Name) and t.id == "router"
                for t in node.targets
            )
            and isinstance(node.value, ast.Call)
        ):
            for kw in node.value.keywords:
                if kw.arg == "prefix" and isinstance(kw.value, ast.Constant):
                    return kw.value.value
    return ""


def _resolve_imports(tree: ast.Module, current_module: str) -> dict:
    """Map local binding name -> (absolute module, original name).

    Walks the whole tree, not just the module body, because these routers
    import heavily inside function bodies to break import cycles.
    """
    out: dict[str, tuple[str, str | None]] = {}
    parts = current_module.split(".")
    for node in ast.walk(tree):
        if isinstance(node, ast.ImportFrom):
            if node.level:
                base = ".".join(parts[: len(parts) - node.level])
                module = f"{base}.{node.module}" if node.module else base
            else:
                module = node.module or ""
            for alias in node.names:
                out[alias.asname or alias.name] = (module, alias.name)
        elif isinstance(node, ast.Import):
            for alias in node.names:
                out[alias.asname or alias.name] = (alias.name, None)
    return out


# ---------------------------------------------------------------------------
# Package-wide index of callables that take a user identity
# ---------------------------------------------------------------------------


@functools.lru_cache(maxsize=1)
def identity_param_index() -> dict:
    """``module -> {callable name: [(positional index, param name), ...]}``.

    Covers plain functions and class constructors (indexed under the CLASS
    name, since that is what a call site writes: ``NoteService(username)``).
    """
    index: dict[str, dict[str, list]] = {}
    for path in sorted(PKG_ROOT.rglob("*.py")):
        try:
            tree = _parse(path)
        except (SyntaxError, UnicodeDecodeError):  # pragma: no cover
            continue
        module = _module_name(path)
        bucket = index.setdefault(module, {})

        def _record(name, arg_nodes):
            names = [a.arg for a in arg_nodes]
            hits = [(i, n) for i, n in enumerate(names) if n in IDENTITY_PARAMS]
            if hits:
                bucket.setdefault(name, []).extend(hits)

        for node in ast.walk(tree):
            if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
                args = node.args.posonlyargs + node.args.args
                if args and args[0].arg in ("self", "cls"):
                    args = args[1:]
                _record(node.name, args)
            elif isinstance(node, ast.ClassDef):
                for stmt in node.body:
                    if (
                        isinstance(
                            stmt, (ast.FunctionDef, ast.AsyncFunctionDef)
                        )
                        and stmt.name == "__init__"
                    ):
                        _record(
                            node.name,
                            (stmt.args.posonlyargs + stmt.args.args)[1:],
                        )
    return index


def _identity_params_of(local_name, imports, current_module):
    """Identity parameters of whatever ``local_name`` refers to, or None."""
    index = identity_param_index()
    target = imports.get(local_name)
    if target is not None:
        module, original = target
        if original is None:
            return None
        return index.get(module, {}).get(original)
    return index.get(current_module, {}).get(local_name)


# ---------------------------------------------------------------------------
# Route model
# ---------------------------------------------------------------------------


@dataclass
class Route:
    module: str  # file basename, e.g. "notes.py"
    lineno: int
    func: str
    method: str
    path: str
    auth_params: frozenset  # parameter names bound to an auth dependency
    node: ast.FunctionDef | ast.AsyncFunctionDef = field(repr=False)
    tree: ast.Module = field(repr=False)
    module_path: str = field(repr=False, default="")

    @property
    def key(self):
        return (self.method, self.path)

    @property
    def where(self):
        return f"{ROUTERS_DIR / self.module}:{self.lineno} {self.func}()"

    @property
    def path_params(self):
        return re.findall(r"\{([A-Za-z_][A-Za-z0-9_]*)", self.path)


def _auth_dependency_names(router_tree: ast.Module) -> set:
    """Names of dependency callables that transitively require ``require_auth``.

    ``require_api_access`` (api_v1) is the real-world case: it is a dependency
    that itself declares ``username: str = Depends(require_auth)`` and returns
    that username after an extra kill-switch check.
    """
    auth_tree = _parse(AUTH_DEPS)
    local = {
        n.name: n
        for n in router_tree.body
        if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef))
    }
    shared = {
        n.name: n
        for n in auth_tree.body
        if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef))
    }
    candidates = [
        (name, fn, imports)
        for definitions, imports in (
            (local, _resolve_imports(router_tree, "")),
            (shared, _resolve_imports(auth_tree, "")),
        )
        for name, fn in definitions.items()
    ]

    resolved = {"require_auth"}
    changed = True
    while changed:
        changed = False
        for name, fn, imports in candidates:
            if name in resolved:
                continue
            for _, default, annotation in _params(fn):
                targets = _depends_targets(default, annotation, imports)
                if any(t in resolved for t in targets):
                    resolved.add(name)
                    changed = True
                    break
    return resolved


def _routes_in(tree: ast.Module, module_label: str) -> list:
    prefix = _router_prefix(tree)
    auth_names = _auth_dependency_names(tree)
    imports = _resolve_imports(tree, "")
    routes = []
    for node in ast.walk(tree):
        if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
            continue
        decorators = [
            d
            for d in node.decorator_list
            if isinstance(d, ast.Call)
            and isinstance(d.func, ast.Attribute)
            and d.func.attr in HTTP_METHODS
        ]
        if not decorators:
            continue
        auth_params = frozenset(
            name
            for name, default, annotation in _params(node)
            if any(
                t in auth_names
                for t in _depends_targets(default, annotation, imports)
            )
        )
        for dec in decorators:
            path = (
                dec.args[0].value
                if dec.args and isinstance(dec.args[0], ast.Constant)
                else "<dynamic>"
            )
            routes.append(
                Route(
                    module=module_label,
                    lineno=node.lineno,
                    func=node.name,
                    method=dec.func.attr.upper(),
                    path=prefix + path,
                    auth_params=auth_params,
                    node=node,
                    tree=tree,
                )
            )
    return routes


@functools.lru_cache(maxsize=1)
def route_table() -> tuple:
    routes = []
    for path in sorted(ROUTERS_DIR.glob("*.py")):
        if path.name == "__init__.py":
            continue
        tree = _parse(path)
        for route in _routes_in(tree, path.name):
            route.module_path = _module_name(path)
            routes.append(route)
    return tuple(routes)


@functools.lru_cache(maxsize=1)
def mounted_router_modules() -> frozenset:
    """Router module basenames the app actually mounts.

    Read out of ``fastapi_app._mount_all``'s ``_router_modules`` list plus the
    separately-imported ``api_v1`` router, so a router added to the app but
    not reached by this census fails
    :func:`test_census_covers_every_mounted_router`.
    """
    tree = _parse(FASTAPI_APP)
    names = {"api_v1"}
    for node in ast.walk(tree):
        if isinstance(node, ast.Assign) and any(
            isinstance(t, ast.Name) and t.id == "_router_modules"
            for t in node.targets
        ):
            for element in ast.walk(node.value):
                if isinstance(element, ast.Constant) and isinstance(
                    element.value, str
                ):
                    if element.value.startswith(".routers."):
                        names.add(element.value.split(".")[-1])
    return frozenset(f"{n}.py" for n in names)


# ---------------------------------------------------------------------------
# Per-handler provenance
# ---------------------------------------------------------------------------


def _names_equal_to_auth(node, auth_params) -> set:
    """Local names provably holding the authenticated username.

    Seeded with the auth-bound parameters and grown across plain
    ``alias = username`` aliasing, which several handlers do
    (``user_id = username`` in news_flask_api.py, for instance).
    """
    safe = set(auth_params)
    changed = True
    while changed:
        changed = False
        for stmt in ast.walk(node):
            if (
                isinstance(stmt, ast.Assign)
                and isinstance(stmt.value, ast.Name)
                and stmt.value.id in safe
            ):
                for target in stmt.targets:
                    if isinstance(target, ast.Name) and target.id not in safe:
                        safe.add(target.id)
                        changed = True
    return safe


def _rebound_auth_params(node, auth_params) -> set:
    """Auth parameters reassigned to something other than themselves.

    A handler that does ``username = data["username"]`` still LOOKS scoped at
    every call site below it; this is what makes such a bug survive review.
    """
    safe = _names_equal_to_auth(node, auth_params)
    rebound = set()
    for stmt in ast.walk(node):
        if isinstance(stmt, ast.Assign):
            for target in stmt.targets:
                if isinstance(target, ast.Name) and target.id in auth_params:
                    if not (
                        isinstance(stmt.value, ast.Name)
                        and stmt.value.id in safe
                    ):
                        rebound.add(target.id)
        elif isinstance(stmt, (ast.AugAssign, ast.AnnAssign)):
            if (
                isinstance(stmt.target, ast.Name)
                and stmt.target.id in auth_params
            ):
                rebound.add(stmt.target.id)
        elif isinstance(stmt, ast.NamedExpr):
            if (
                isinstance(stmt.target, ast.Name)
                and stmt.target.id in auth_params
            ):
                rebound.add(stmt.target.id)
    return rebound


def identity_sinks(route: Route) -> list:
    """Every call in a handler that feeds a user-identity parameter.

    Returns ``(callee, param, source_text, lineno, is_auth)`` tuples. Only
    call targets that resolve to a real function/class in this package are
    considered positionally — a bare ``d.get("username", {})`` must not be
    mistaken for an identity sink — while an explicit ``username=`` keyword is
    unambiguous on any callee.
    """
    imports = _resolve_imports(route.tree, route.module_path)
    safe = _names_equal_to_auth(route.node, route.auth_params)
    found = []
    for call in ast.walk(route.node):
        if not isinstance(call, ast.Call):
            continue
        pairs = []
        if isinstance(call.func, ast.Name):
            declared = _identity_params_of(
                call.func.id, imports, route.module_path
            )
            for index, param in declared or ():
                if index < len(call.args):
                    pairs.append((call.func.id, param, call.args[index]))
        for kw in call.keywords:
            if kw.arg in IDENTITY_PARAMS:
                callee = (
                    call.func.id
                    if isinstance(call.func, ast.Name)
                    else ast.unparse(call.func)
                )
                pairs.append((callee, kw.arg, kw.value))
        for callee, param, expr in pairs:
            is_auth = isinstance(expr, ast.Name) and expr.id in safe
            # `username=None` is an explicit opt-out, not an identity claim:
            # the callees that accept it fall back to the request contextvar
            # or return early.
            if isinstance(expr, ast.Constant) and expr.value is None:
                is_auth = True
            found.append(
                (
                    callee,
                    param,
                    ast.unparse(expr),
                    getattr(expr, "lineno", route.lineno),
                    is_auth,
                )
            )
    return found


def identity_from_request_input(route: Route) -> list:
    """Identity values bound from client-controlled request data.

    Two shapes: a handler PARAMETER named like an identity that FastAPI would
    fill from the path/query/form/body/header, and an explicit read out of
    ``request.query_params`` / ``path_params`` / ``headers`` / ``cookies``.
    """
    problems = []
    imports = _resolve_imports(route.tree, route.module_path)
    for name, default, annotation in _params(route.node):
        if name not in IDENTITY_PARAMS:
            continue
        if name in route.auth_params:
            continue
        if _depends_targets(default, annotation, imports):
            continue
        detail = ast.unparse(default) if default is not None else "<no default>"
        problems.append(("parameter", name, detail, route.lineno))

    containers = {"query_params", "path_params", "headers", "cookies"}
    for node in ast.walk(route.node):
        if (
            isinstance(node, ast.Call)
            and isinstance(node.func, ast.Attribute)
            and node.func.attr == "get"
            and isinstance(node.func.value, ast.Attribute)
            and node.func.value.attr in containers
            and node.args
            and isinstance(node.args[0], ast.Constant)
            and node.args[0].value in IDENTITY_PARAMS
        ):
            problems.append(
                (
                    "request-read",
                    node.args[0].value,
                    ast.unparse(node),
                    node.lineno,
                )
            )
    return problems


def db_chokepoint_calls(tree: ast.Module) -> list:
    """Every ``get_user_db_session(...)`` in a module, with its argument.

    Tracks the enclosing function chain so the argument can be validated
    against the parameters actually in scope — these calls appear inside
    nested helpers and streaming closures as well as directly in handlers.
    """
    results = []
    stack: list = []

    class Visitor(ast.NodeVisitor):
        def visit_FunctionDef(self, node):
            stack.append(node)
            self.generic_visit(node)
            stack.pop()

        visit_AsyncFunctionDef = visit_FunctionDef

        def visit_Call(self, node):
            if isinstance(node.func, ast.Name) and node.func.id == (
                DB_CHOKEPOINT
            ):
                arg = node.args[0] if node.args else None
                for kw in node.keywords:
                    if kw.arg == "username":
                        arg = kw.value
                in_scope = set()
                for fn in stack:
                    a = fn.args
                    in_scope |= {
                        p.arg for p in a.posonlyargs + a.args + a.kwonlyargs
                    }
                results.append(
                    (
                        node.lineno,
                        stack[-1].name if stack else "<module>",
                        arg,
                        frozenset(in_scope),
                    )
                )
            self.generic_visit(node)

    Visitor().visit(tree)
    return results


def injected_identity_callables(tree: ast.Module, current_module: str) -> list:
    """Dependencies FastAPI would call with a request-filled identity.

    FastAPI CALLS every ``Depends(f)`` / ``Security(f)`` target (for a bare
    ``Depends()``, the annotated class) and fills ITS parameters from the
    request. A target taking ``username`` / ``user_id`` / ``owner``
    (``NoteService``, ``get_user_db_session``) therefore gets that
    identity from ``?username=`` unless the target is itself an auth
    dependency (``require_auth``-derived, see ``_auth_dependency_names``).
    Covers handler and helper parameters (both spellings, via
    ``_depends_targets``) and ``Depends(...)`` anywhere else, such as a
    decorator's ``dependencies=[...]``. Returns ``(lineno, target)``.
    """
    imports = _resolve_imports(tree, current_module)
    auth_names = _auth_dependency_names(tree)
    found = []
    targets = []
    for node in ast.walk(tree):
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
            for _, default, annotation in _params(node):
                targets.extend(
                    (node.lineno, t)
                    for t in _depends_targets(default, annotation, imports)
                )
        elif isinstance(node, ast.Call):
            t = _depends_target(node)
            if t is not None:
                targets.append((node.lineno, t))
    for lineno, target in targets:
        if target in auth_names:
            continue
        if _identity_params_of(target, imports, current_module):
            found.append((lineno, target))
    return sorted(set(found))


def ungated_global_state_calls(route: Route) -> list:
    """Global-registry calls made with an id but no prior owner lookup.

    The registry in ``web/research_state.py`` is a process-wide dict keyed by
    research id alone, shared across every user. The only thing standing
    between it and an IDOR is that the handler first resolves the id inside
    the CALLER'S OWN encrypted database and bails out when the row is absent.
    """
    gate_linenos = [
        call.lineno
        for call in ast.walk(route.node)
        if isinstance(call, ast.Call)
        and isinstance(call.func, ast.Name)
        and call.func.id == DB_CHOKEPOINT
        and call.args
        and isinstance(call.args[0], ast.Name)
        and call.args[0].id
        in _names_equal_to_auth(route.node, route.auth_params)
    ]
    earliest_gate = min(gate_linenos, default=None)

    ungated = []
    for call in ast.walk(route.node):
        if not (
            isinstance(call, ast.Call)
            and isinstance(call.func, ast.Name)
            and call.func.id in GLOBAL_RESEARCH_STATE
        ):
            continue
        # A zero-argument accessor cannot be steered by a client-supplied id
        # (e.g. get_active_research_ids()); it is a whole-registry read.
        if not call.args:
            continue
        if earliest_gate is None or call.lineno <= earliest_gate:
            ungated.append((call.func.id, ast.unparse(call), call.lineno))
    return ungated


# ---------------------------------------------------------------------------
# Reporting
# ---------------------------------------------------------------------------


def _report(title, rows):
    lines = [title, ""]
    lines += [f"  - {r}" for r in rows]
    return "\n".join(lines)


# ---------------------------------------------------------------------------
# The census
# ---------------------------------------------------------------------------


def test_census_covers_every_mounted_router():
    """Every router the app mounts must be reached by this census.

    Without this, adding a router is enough to make every other test in this
    file pass vacuously for its routes.
    """
    mounted = mounted_router_modules()
    censused = {r.module for r in route_table()}
    missing = sorted(mounted - censused)
    assert not missing, _report(
        "Routers mounted by fastapi_app._mount_all but not covered by the "
        "census (their routes were never checked for user scoping):",
        missing,
    )
    assert len(route_table()) >= 300, (
        f"Census found only {len(route_table())} routes across "
        f"{len(censused)} modules; the route table has ~314. A parsing "
        "regression would silently shrink every check in this file."
    )


def test_route_decorators_use_only_the_idioms_the_census_understands():
    """The census reads auth off the handler SIGNATURE.

    It understands a dependency spelled either the legacy way
    (``x: T = Depends(require_auth)``) or inline via ``Annotated``
    (``x: Annotated[T, Depends(require_auth)]``) — see ``_depends_targets``.
    One idiom would still make it blind: a router- or route-level
    ``dependencies=[Depends(require_auth)]``, which puts the dependency on
    the decorator instead of any parameter, so there is nothing in the
    signature to read at all. This branch uses neither dependencies= on a
    route/router nor any other unrecognised spelling. If that changes, the
    analyzer must be taught the new shape — failing here is the signal to do
    it, rather than letting routes quietly drop out of the authenticated
    set.

    (Note: an ``Annotated`` dependency expressed through a module-level
    *alias* referenced by bare name — e.g. notes.py's ``body: _NotesBody``
    — is intentionally NOT flagged here: ``_NotesBody`` wraps a body-shape
    validator, not an auth dependency, and every notes.py route also
    declares ``Depends(require_auth)`` on its own ``username`` parameter, so
    the alias carries no auth information the census needs.)
    """
    blind_spots = []
    for path in sorted(ROUTERS_DIR.glob("*.py")):
        tree = _parse(path)
        for node in ast.walk(tree):
            if isinstance(node, ast.Call) and any(
                kw.arg == "dependencies" for kw in node.keywords
            ):
                func = node.func
                name = (
                    func.attr
                    if isinstance(func, ast.Attribute)
                    else getattr(func, "id", "")
                )
                if name in HTTP_METHODS or name == "APIRouter":
                    blind_spots.append(
                        f"{path.name}:{node.lineno} dependencies= on {name}()"
                    )
    assert not blind_spots, _report(
        "Auth is declared in a way this census cannot see. Teach the "
        "analyzer these shapes before merging:",
        blind_spots,
    )


def test_every_route_authenticates_or_is_declared_public():
    """Exact set equality against PUBLIC_ROUTES.

    Equality (not subset) in both directions: a new unauthenticated route
    fails, and so does a stale allowlist entry that no longer matches a real
    route — which would otherwise hide the route it was meant to describe.
    """
    unauthenticated = {r.key for r in route_table() if not r.auth_params}
    unexpected = sorted(unauthenticated - PUBLIC_ROUTES)
    stale = sorted(PUBLIC_ROUTES - unauthenticated)
    detail = []
    for route in route_table():
        if route.key in unexpected:
            detail.append(f"{route.key} at {route.where}")
    assert not unexpected, _report(
        "Routes with NO authentication dependency that are not on the "
        "public allowlist. Each serves whatever the handler reaches to any "
        "anonymous caller:",
        detail,
    )
    assert not stale, _report(
        "PUBLIC_ROUTES entries that match no route in the table. Remove "
        "them; a stale entry can mask a real route added later at the same "
        "method+path:",
        stale,
    )


def test_no_route_accepts_a_user_identity_from_request_input():
    """The highest-severity IDOR shape: identity taken from the client.

    Under per-user encrypted databases, a username read from a path, query,
    body, header or form IS the authorization decision — it selects the
    database file to decrypt and read. Only login and register may do it.
    """
    violations = []
    for route in route_table():
        if (route.module, route.func) in IDENTITY_FROM_INPUT_ALLOWED:
            continue
        for kind, name, detail, lineno in identity_from_request_input(route):
            violations.append(
                f"{route.method} {route.path} — {route.module}:{lineno} "
                f"{route.func}() binds '{name}' from request input "
                f"({kind}: {detail})"
            )
    assert not violations, _report(
        "CROSS-USER: a user identity is taken from client-controlled input "
        "instead of the auth dependency:",
        violations,
    )


def test_identity_sinks_only_ever_receive_the_authenticated_username():
    """Every username/user_id/owner argument must be the auth dependency's.

    This is the check that would catch the hand-translation slip this
    migration is exposed to: a call site that keeps the right shape
    (``Service(x)``) but threads the wrong string.
    """
    violations = []
    for route in route_table():
        if route.key in PUBLIC_ROUTES:
            # Login/register/logout/check_auth legitimately handle a username
            # that is not yet (or no longer) an authenticated one; they are
            # pinned by test_every_route_authenticates_or_is_declared_public
            # and by the dedicated auth suites.
            continue
        for callee, param, source, lineno, is_auth in identity_sinks(route):
            if is_auth:
                continue
            if (route.module, route.func, callee, param) in (
                IDENTITY_SINK_ALLOWED
            ):
                continue
            violations.append(
                f"{route.method} {route.path} — {route.module}:{lineno} "
                f"{route.func}() calls {callee}({param}={source})"
            )
    assert not violations, _report(
        "CROSS-USER: an identity parameter is fed a value that is not "
        "provably the authenticated username:",
        violations,
    )


def test_no_handler_rebinds_its_authenticated_username():
    """``username`` must still mean the auth dependency at the bottom of the
    handler, not only at the top."""
    violations = []
    for route in route_table():
        if route.key in PUBLIC_ROUTES:
            continue
        rebound = _rebound_auth_params(route.node, route.auth_params)
        if rebound:
            violations.append(
                f"{route.method} {route.path} — {route.where} rebinds "
                f"{sorted(rebound)}"
            )
    assert not violations, _report(
        "CROSS-USER: a handler reassigns its authenticated username, so "
        "call sites below the assignment are scoped to something else:",
        violations,
    )


def test_every_get_user_db_session_call_is_passed_an_auth_username():
    """The single chokepoint that decides which encrypted DB is opened.

    Checked across the whole routers package (not only handler bodies), since
    these calls also appear in nested helpers and streaming closures. The
    argument must be an identity-named parameter that is in scope — a
    literal, an attribute read or an expression here would mean the database
    choice is computed rather than inherited from authentication.
    """
    violations = []
    total = 0
    for path in sorted(ROUTERS_DIR.glob("*.py")):
        tree = _parse(path)
        for lineno, func, arg, in_scope in db_chokepoint_calls(tree):
            total += 1
            ok = (
                isinstance(arg, ast.Name)
                and arg.id in in_scope
                and arg.id in IDENTITY_PARAMS
            )
            if not ok:
                rendered = ast.unparse(arg) if arg is not None else "<none>"
                violations.append(
                    f"{path.name}:{lineno} {func}() -> "
                    f"{DB_CHOKEPOINT}({rendered})"
                )
    assert total >= 150, (
        f"Only {total} {DB_CHOKEPOINT}() calls found in {ROUTERS_DIR}; the "
        "routers make ~174. A resolution regression would make this test "
        "pass while checking almost nothing."
    )
    assert not violations, _report(
        f"CROSS-USER: {DB_CHOKEPOINT}() opens a per-user encrypted database "
        "from a value that is not an in-scope authenticated username:",
        violations,
    )


def test_routers_open_no_database_outside_the_per_user_scope():
    """No router may reach a database except through the per-user chokepoint.

    ``auth_db_session`` (ldr_auth.db — usernames only, no user content) is
    allowed in the auth router for the account lifecycle. Anything else
    (a raw engine, a sessionmaker, the metrics session) would be a store not
    keyed by the caller.
    """
    forbidden = {
        "auth_db_session": AUTH_DB_ALLOWED_MODULES,
        "get_auth_db_session": AUTH_DB_ALLOWED_MODULES,
        "create_engine": frozenset(),
        "sessionmaker": frozenset(),
        "get_metrics_session": frozenset(),
        "get_db_session": frozenset(),
    }
    violations = []
    for path in sorted(ROUTERS_DIR.glob("*.py")):
        tree = _parse(path)
        for node in ast.walk(tree):
            if not (
                isinstance(node, ast.Call) and isinstance(node.func, ast.Name)
            ):
                continue
            allowed = forbidden.get(node.func.id)
            if allowed is None:
                continue
            if path.name in allowed:
                continue
            violations.append(f"{path.name}:{node.lineno} {node.func.id}(...)")
    assert not violations, _report(
        "CROSS-USER: a router opens a database session that is not scoped "
        "to the authenticated user:",
        violations,
    )


def test_no_dependency_takes_a_user_identity_from_the_request():
    """``svc: NoteService = Depends()``, ``Annotated[T,
    Depends(get_user_db_session)]`` and ``Depends(NoteService)`` make
    FastAPI construct the user-data object itself, with ``username``
    filled from the request (``?username=victim``). No router may inject
    an identity-taking callable that is not an auth dependency."""
    violations = []
    for path in sorted(ROUTERS_DIR.glob("*.py")):
        tree = _parse(path)
        for lineno, target in injected_identity_callables(
            tree, _module_name(path)
        ):
            violations.append(f"{path.name}:{lineno} Depends({target})")
    assert not violations, _report(
        "CROSS-USER: a router injects a callable that takes a user "
        "identity, which FastAPI then fills from request input:",
        violations,
    )


def test_object_id_routes_reach_a_user_scoped_access():
    """A route that takes an object id must resolve it against the caller.

    With per-user databases the owner predicate IS the database choice, so
    "scoped" means the authenticated username is threaded into something. A
    handler that takes ``{note_id}`` and never mentions the username either
    renders a shell (allowlisted, each one read) or resolves an id against a
    store it did not scope.
    """
    violations = []
    for route in route_table():
        if not route.path_params:
            continue
        if route.key in TEMPLATE_ONLY_ID_ROUTES:
            continue
        if not route.auth_params:
            continue
        safe = _names_equal_to_auth(route.node, route.auth_params)
        used = False
        for node in ast.walk(route.node):
            if isinstance(node, ast.Name) and node.id in safe:
                if not (
                    isinstance(getattr(node, "ctx", None), ast.Store)
                    and node.id not in route.auth_params
                ):
                    if node.lineno > route.lineno:
                        used = True
                        break
        if not used:
            violations.append(
                f"{route.method} {route.path} — {route.where} takes "
                f"{route.path_params} and never uses the authenticated user"
            )
    assert not violations, _report(
        "CROSS-USER: an object-id route resolves the id without scoping to "
        "the authenticated user:",
        violations,
    )
    live = {r.key for r in route_table()}
    stale = sorted(TEMPLATE_ONLY_ID_ROUTES - live)
    assert not stale, _report(
        "TEMPLATE_ONLY_ID_ROUTES entries matching no live route — remove "
        "them so they cannot exempt a future route at the same path:",
        stale,
    )


def test_global_research_state_is_only_touched_after_an_owner_lookup():
    """``web/research_state.py`` is keyed by research id ALONE, process-wide.

    Every entry in it belongs to some user, and nothing in the accessor
    signature says which. A handler may only reach it after resolving the id
    inside the caller's own encrypted database, which is what turns "unknown
    id" into a 404 instead of another user's progress log.
    """
    violations = []
    checked = 0
    for route in route_table():
        ungated = ungated_global_state_calls(route)
        checked += 1
        for name, source, lineno in ungated:
            violations.append(
                f"{route.method} {route.path} — {route.module}:{lineno} "
                f"{route.func}() calls {source} with no preceding "
                f"{DB_CHOKEPOINT}(<auth username>) in the same handler "
                f"(accessor: {name})"
            )
    assert not violations, _report(
        "CROSS-USER: the shared, id-keyed research registry is reached "
        "without an owner-scoped lookup first:",
        violations,
    )


def test_analytics_helpers_never_query_without_a_username():
    """Aggregate endpoints (metrics, cost, links, journals, ratings).

    These roll rows up across a table, which is exactly where a dropped
    filter produces a plausible-looking number containing other users' data.
    Every module-level helper in metrics.py that opens a database must take a
    username, and must open it with that username.
    """
    path = ROUTERS_DIR / "metrics.py"
    tree = _parse(path)
    violations = []
    helpers_checked = 0
    for node in tree.body:
        if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
            continue
        opens = [
            call
            for call in ast.walk(node)
            if isinstance(call, ast.Call)
            and isinstance(call.func, ast.Name)
            and call.func.id == DB_CHOKEPOINT
        ]
        if not opens:
            continue
        helpers_checked += 1
        declared = {name for name, _, _ in _params(node)}
        if "username" not in declared:
            violations.append(
                f"metrics.py:{node.lineno} {node.name}() opens a database "
                "but declares no username parameter"
            )
            continue
        for call in opens:
            arg = call.args[0] if call.args else None
            if not (isinstance(arg, ast.Name) and arg.id == "username"):
                violations.append(
                    f"metrics.py:{call.lineno} {node.name}() -> "
                    f"{DB_CHOKEPOINT}("
                    f"{ast.unparse(arg) if arg else '<none>'})"
                )
    assert helpers_checked >= 4, (
        f"Only {helpers_checked} analytics helpers found in metrics.py; "
        "expected the rating/link/strategy/rate-limiting rollups."
    )
    assert not violations, _report(
        "CROSS-USER: an analytics rollup queries without a per-user scope:",
        violations,
    )


# ---------------------------------------------------------------------------
# Anti-vacuity guards
# ---------------------------------------------------------------------------


def test_census_analyzer_actually_resolves_known_identity_sinks():
    """Pin that the analyzer resolves real call sites, not an empty set.

    Every check above is an assertion that a list is EMPTY, so an analyzer
    that resolves nothing passes them all. This names call sites that must be
    found and puts a floor under the total.
    """
    resolved = set()
    total = 0
    for route in route_table():
        for callee, param, _source, _lineno, _is_auth in identity_sinks(route):
            resolved.add((route.module, callee, param))
            total += 1

    required = {
        # Per-user database chokepoint, threaded by keyword and positionally.
        ("notes.py", "NoteService", "username"),
        ("library.py", "LibraryService", "username"),
        ("metrics.py", "get_user_db_session", "username"),
        ("metrics.py", "get_rating_analytics", "username"),
        ("research.py", "get_user_db_session", "username"),
        ("history.py", "get_user_db_session", "username"),
        ("rag.py", "get_user_db_session", "username"),
    }
    missing = sorted(required - resolved)
    assert not missing, _report(
        "The analyzer failed to resolve identity sinks it must see; import "
        "resolution or the package index is broken and every check above is "
        "passing vacuously:",
        missing,
    )
    assert total >= 250, (
        f"Only {total} identity-sink call sites resolved across "
        f"{len(route_table())} routes; expected several hundred."
    )
    assert len(identity_param_index()) >= 200, (
        "Package-wide identity-parameter index is suspiciously small: "
        f"{len(identity_param_index())} modules."
    )


# --- Positive control -------------------------------------------------------
#
# Synthetic routers, written to be exactly the defects the census exists to
# catch, pushed through the SAME functions the real census uses. If a future
# refactor blunts the analyzer, these fail before the real checks go quiet.

_CONTROL_HEADER = """
from fastapi import APIRouter, Depends, Request
from ...database.session_context import get_user_db_session
from ..dependencies.auth import require_auth
from ..research_state import get_active_research_snapshot

router = APIRouter(prefix="/ctl")
"""

_CONTROL_IDENTITY_FROM_QUERY = (
    _CONTROL_HEADER
    + '''
@router.get("/notes")
def leak_via_query(request: Request, username: str = None):
    """Identity taken straight from the client."""
    with get_user_db_session(username) as db:
        return db.query(object).all()
'''
)

_CONTROL_REBOUND_USERNAME = (
    _CONTROL_HEADER
    + '''
@router.get("/rebound")
def leak_via_rebind(request: Request, username: str = Depends(require_auth)):
    """Looks scoped; is not."""
    username = request.query_params.get("username")
    with get_user_db_session(username) as db:
        return db.query(object).all()
'''
)

_CONTROL_UNSCOPED_SESSION = (
    _CONTROL_HEADER
    + '''
@router.get("/unscoped")
def leak_via_literal(request: Request, username: str = Depends(require_auth)):
    """The exact negative control: the username filter is gone."""
    with get_user_db_session(request.path_params["owner"]) as db:
        return db.query(object).all()
'''
)

_CONTROL_NO_AUTH = (
    _CONTROL_HEADER
    + """
@router.get("/open")
def no_auth_at_all(request: Request):
    return {"secrets": True}
"""
)

_CONTROL_UNGATED_GLOBAL = (
    _CONTROL_HEADER
    + '''
@router.get("/progress/{research_id}")
def leak_via_global(
    request: Request, research_id, username: str = Depends(require_auth)
):
    """Reads the shared registry with a client-supplied id, no owner check."""
    return get_active_research_snapshot(research_id)
'''
)


_CONTROL_INJECTED_IDENTITY = """
from typing import Annotated
import fastapi
from fastapi import APIRouter, Depends, Request
from ...database.session_context import get_user_db_session
from ...research_library.notes.services.note_service import NoteService
from ..dependencies.auth import require_auth

router = APIRouter()


@router.get("/a")
def bare(svc: NoteService = Depends()):
    return svc


@router.get("/b")
def annotated_bare(svc: Annotated[NoteService, Depends()]):
    return svc


@router.get("/c")
def annotated_session(db: Annotated[object, Depends(get_user_db_session)]):
    return db


@router.get("/d")
def wrapped(db: object = fastapi.Depends(get_user_db_session.__wrapped__)):
    return db


@router.get("/e", dependencies=[Depends(NoteService)])
def via_decorator(username: str = Depends(require_auth)):
    return username


@router.get("/f")
def quoted(svc: "NoteService" = Depends(None)):
    return svc


@router.get("/g")
def quoted_annotated(svc: "Annotated[NoteService, Depends()]"):
    return svc


@router.get("/h")
def quoted_session(db: "Annotated[object, Depends(get_user_db_session)]"):
    return db


@router.get("/i")
def starred(svc: Annotated[*(NoteService, Depends())]):
    return svc


@router.get("/ok")
def ok(username: Annotated[str, Depends(require_auth)]):
    return NoteService(username)


@router.get("/ok2")
def ok_quoted(username: "Annotated[str, Depends(require_auth)]"):
    return NoteService(username)
"""


def _control_routes(source, label="control.py"):
    tree = ast.parse(source)
    routes = _routes_in(tree, label)
    for route in routes:
        # Resolve imports as if this module sat in web/routers/.
        route.module_path = "local_deep_research.web.routers.control"
        route.tree = tree
    return routes


def test_census_flags_deliberately_unscoped_handlers():
    """Positive control for every check that asserts an empty list."""
    # 1. Identity bound straight from request input.
    (route,) = _control_routes(_CONTROL_IDENTITY_FROM_QUERY)
    problems = identity_from_request_input(route)
    assert problems, (
        "Census did NOT flag a handler binding `username` from request "
        "input — test_no_route_accepts_a_user_identity_from_request_input "
        "would pass on a real leak."
    )
    assert problems[0][1] == "username"

    # 2. Auth username reassigned mid-handler.
    (route,) = _control_routes(_CONTROL_REBOUND_USERNAME)
    assert _rebound_auth_params(route.node, route.auth_params) == {
        "username"
    }, (
        "Census did NOT flag a rebound auth username — "
        "test_no_handler_rebinds_its_authenticated_username is blind."
    )

    # 3. The database chokepoint fed a client-controlled value. This is the
    #    negative control the brief calls for: the scoping is removed from
    #    one query and the census must report it.
    tree = ast.parse(_CONTROL_UNSCOPED_SESSION)
    calls = db_chokepoint_calls(tree)
    assert calls, "db_chokepoint_calls() found no get_user_db_session call"
    _lineno, _func, arg, in_scope = calls[0]
    unscoped = not (
        isinstance(arg, ast.Name)
        and arg.id in in_scope
        and arg.id in IDENTITY_PARAMS
    )
    assert unscoped, (
        "Census did NOT flag get_user_db_session() opening a database from "
        "request input — "
        "test_every_get_user_db_session_call_is_passed_an_auth_username "
        "is blind."
    )

    # 4. Route with no auth dependency at all.
    (route,) = _control_routes(_CONTROL_NO_AUTH)
    assert not route.auth_params, (
        "Census treated an unauthenticated handler as authenticated — "
        "test_every_route_authenticates_or_is_declared_public is blind."
    )
    assert route.key not in PUBLIC_ROUTES

    # 5. Shared id-keyed registry read with no owner lookup.
    (route,) = _control_routes(_CONTROL_UNGATED_GLOBAL)
    ungated = ungated_global_state_calls(route)
    assert ungated, (
        "Census did NOT flag an ungated read of the process-global research "
        "registry — "
        "test_global_research_state_is_only_touched_after_an_owner_lookup "
        "is blind."
    )
    assert ungated[0][0] == "get_active_research_snapshot"

    # 6. ...and the same handler WITH the owner lookup must NOT be flagged,
    #    so the check is discriminating rather than always-positive.
    gated = _CONTROL_UNGATED_GLOBAL.replace(
        "    return get_active_research_snapshot(research_id)",
        "    with get_user_db_session(username) as db:\n"
        "        if not db.query(object).filter_by(id=research_id).first():\n"
        "            return {'error': 'not found'}\n"
        "    return get_active_research_snapshot(research_id)",
    )
    (route,) = _control_routes(gated)
    assert not ungated_global_state_calls(route), (
        "Census flagged a properly owner-gated handler; the check is not "
        "discriminating and its clean result on the real tree is meaningless."
    )

    # 7. A dependency FastAPI would call with a request-filled username:
    #    every injection shape is reported, the auth dependency is not.
    tree = ast.parse(_CONTROL_INJECTED_IDENTITY)
    injected = injected_identity_callables(
        tree, "local_deep_research.web.routers.control"
    )
    source_lines = _CONTROL_INJECTED_IDENTITY.splitlines()
    flagged_defs = {
        source_lines[lineno - 1].split("(")[0].removeprefix("def ")
        for lineno, _name in injected
        if source_lines[lineno - 1].startswith("def ")
    }
    assert flagged_defs == {
        "bare",
        "annotated_bare",
        "annotated_session",
        "wrapped",
        "quoted",
        "quoted_annotated",
        "quoted_session",
        "starred",
    }, (
        "Census did NOT flag every Depends() injection of an identity-taking "
        f"callable (got {sorted(flagged_defs)}) — "
        "test_no_dependency_takes_a_user_identity_from_the_request is blind."
    )
    assert any(
        name == "NoteService"
        and source_lines[lineno - 1].startswith('@router.get("/e"')
        for lineno, name in injected
    ), "Census did NOT flag dependencies=[Depends(NoteService)]."
    assert "require_auth" not in {name for _lineno, name in injected}, (
        "Census flagged the auth dependency itself; the check is not "
        "discriminating."
    )


def test_census_reports_the_route_table(record_property):
    """Emit the census as a table so the result is inspectable in CI output.

    Not an assertion about behaviour — it records what was actually examined,
    which is the point of a census.
    """
    routes = route_table()
    by_module: dict[str, int] = {}
    for route in routes:
        by_module[route.module] = by_module.get(route.module, 0) + 1
    record_property("census_route_count", len(routes))
    record_property("census_module_count", len(by_module))
    record_property("census_public_routes", len(PUBLIC_ROUTES))
    scoped = sum(1 for r in routes if r.auth_params)
    record_property("census_authenticated_routes", scoped)
    assert len(by_module) >= 20, (
        f"Census only saw {len(by_module)} router modules: {sorted(by_module)}"
    )
    # Every route is accounted for in exactly one bucket: authenticated, or
    # on the public allowlist. A route in neither would mean the census
    # produced a verdict for a route it never classified.
    unclassified = [
        f"{r.key} at {r.where}"
        for r in routes
        if not r.auth_params and r.key not in PUBLIC_ROUTES
    ]
    assert not unclassified, _report(
        "Routes the census could not classify as authenticated or public:",
        unclassified,
    )
    assert scoped == len(routes) - sum(1 for r in routes if not r.auth_params)


@pytest.mark.parametrize(
    "module_name",
    sorted(mounted_router_modules()),
)
def test_each_mounted_router_has_every_route_scoped(module_name):
    """Per-router verdict, so a failure names the router that regressed."""
    routes = [r for r in route_table() if r.module == module_name]
    assert routes, f"No routes censused for mounted router {module_name}"
    unscoped = []
    for route in routes:
        if not route.auth_params and route.key not in PUBLIC_ROUTES:
            unscoped.append(f"{route.key} (no auth) at {route.where}")
            continue
        if route.key in PUBLIC_ROUTES:
            continue
        for callee, param, source, lineno, is_auth in identity_sinks(route):
            if is_auth:
                continue
            if (route.module, route.func, callee, param) in (
                IDENTITY_SINK_ALLOWED
            ):
                continue
            unscoped.append(
                f"{route.key} {callee}({param}={source}) at "
                f"{module_name}:{lineno}"
            )
    assert not unscoped, _report(
        f"CROSS-USER isolation defects in {module_name}:", unscoped
    )