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.

70.4 KB
Raw Blame
#!/usr/bin/env python3
"""
Pre-commit hook: user-data constructions pass a username.

Scope: this hook is a syntax fence against ACCIDENTAL or IDIOMATIC
code -- the shapes a contributor writes without meaning to bypass
anything. Deliberate evasion by a contributor is out of scope and is
left to code review (CODEOWNERS); the hook does not try to be a
sandbox. Examples of deliberate evasion it does NOT catch (the list is
not exhaustive): a default supplied from outside the class body or
signature -- by a metaclass, decorator or ``__init_subclass__``, or set
after the class body (``Cls.attr = Depends()``, ``f.__defaults__ =
...``); walrus bindings in subscript targets or in
other annotations; module-level string or type aliases of a fenced name
(``Svc = "NoteService"`` then ``svc: Svc = Depends()``); and the known
gaps listed below.

LDR's cross-user object isolation is architectural: notes, documents,
downloads and research rows live in per-user *encrypted* databases, so
IDOR is impossible only while every access goes through a
service/session constructed for the requesting user.

What this hook checks is deliberately shallow -- a fast pre-commit
smoke check: every call to a constructor in ``USER_DATA_CONSTRUCTORS``
(by bare name, under an import alias such as ``from ... import
LibraryService as LS``, or as a module attribute ``mod.NoteService``)
under ``ISOLATION_SCAN_DIRS`` (the routers, ``research_library/`` and
``web/services/``) must pass a username explicitly: a positional
``username`` / ``self.username``-style name, or a keyword whose name
contains ``username`` (``username=``, ``owner_username=``). Any keyword
whose name contains ``username`` bound to a literal constant (``None``,
``"alice"``, a placeholder-free ``f"alice"``) is flagged, whatever the
other arguments are. A username-named *value* under another keyword
(``NoteService(owner=username)``) does not count. It catches a
defaulted or shared-session construction. Any non-literal ``username=``
expression is accepted, so constant-valued expressions such as
``username=str(None)`` or ``username=None or x`` are NOT flagged; nor
are dynamic lookups (``getattr``, ``importlib``).

A constructor must not leave a scanned file under a name this hook
does not recognise, since no rule follows a name across modules: an
import alias that is not itself a constructor name (``from x import
get_user_db_session as open_session``; leading underscores are
ignored, so ``as _get_user_db_session`` is fine and its calls are
checked) and any loaded non-call reference (``make = NoteService``,
``partial(get_user_db_session, ...)``, ``class Mine(NoteService)``)
are flagged, except in a type-only annotation. An annotation is NOT
type-only -- it counts as a reference, and a constructor quoted in a
string inside it counts too -- when it is evaluated into more than a
type: a parameter or class-body field annotation (wherever the field
sits in the class body, also under ``if`` / ``try``, and whatever
binds its name there: ``svc: NoteService`` then ``svc = Depends()``)
whose default is not a constant (``svc: NoteService = Depends()``, ``= Depends(None)``,
``= dep``: FastAPI then constructs the annotated class itself, with
``username`` filled from the request, e.g. ``?username=``), an
``Annotated[...]`` with a non-constant metadata element
(``Annotated[NoteService, Depends()]``, ``Annotated[T,
Depends(get_user_db_session)]``), a starred element
(``Annotated[*(NoteService, dep)]``) or a non-tuple slice, any call,
walrus or lambda inside it (``x: (s := get_user_db_session) = 1`` binds
the constructor in the enclosing scope), a class field with no binding
in its own class body on a class with a base other than ``object`` /
``Generic`` / ``Protocol`` (it may inherit a ``Depends()`` default), or
a string, other than an exempt one (below), that does not parse. Every
string inside such an annotation is parsed and checked like unquoted
code, recursively, since FastAPI evaluates it -- a wholly quoted
``"Annotated[NoteService, Depends()]"`` is as live as the unquoted form
-- unless it is exempt. Exemption is an allowlist of shapes (see
``_approved_children``), so whatever it does not name fails closed: a
string is exempt only if every step of its path from the annotation's
root is approved -- subscripts of a proven generic head (``Optional``,
``Union``, ``list`` ...) and the first element of a proven ``Annotated``
whose metadata is inert (each element a constant, a call of a proven
listed callee or a dict display: pydantic hands the first element to any
other metadata object's ``__get_pydantic_core_schema__``), then one last
step -- and it ends as a string member of a proven ``Literal[...]``; as
metadata of such an ``Annotated`` (an element after the first, where the
key has two or more elements and none is starred); as a string passed
directly, in that metadata or in a type slot, to an argument
``VALUE_ARGUMENTS`` lists, of a proven callee
(``Query(description="...")``, ``NewType("Name", str)``,
``ForwardRef("T", module="...")``); or as a string key or value of a
dict display in that metadata. Any other node on the path -- an
attribute read, a subscript of another head, an operator or comparison,
an unlisted call, a lambda, walrus, conditional, starred or formatted
value -- ends it, and every string below it is checked, whatever reads
the string back out: ``Literal["NoteService"].__args__[0]``, ``G |
Literal["NoteService"]``, ``G[Literal["NoteService"]]``,
``f"{Literal['NoteService']}"``, ``Annotated[*(), "NoteService",
Depends()]`` and ``Annotated[Literal["NoteService"], G]`` can each hand
the string on to a type position, which FastAPI resolves. ``Literal``
and ``Annotated`` are proven only when imported from ``typing`` or
``typing_extensions`` (``PLAIN_FORMS``), a ``VALUE_ARGUMENTS`` callee
only when imported from its own module (``from fastapi import Query [as
Q]``, or ``fastapi.Query`` after ``import fastapi``), and a generic head
only from ``typing``, ``typing_extensions`` or ``collections.abc`` or as
an unbound builtin, each under a name nothing else in the file binds
(after ``from typing import Annotated as Literal``,
``Literal["NoteService", Depends()]`` is checked as the ``Annotated`` it
is). Nothing in a file is proven if it could change what a proven name
denotes (see ``_proven_names``): a star import; a reference to a name
that can look up or rewrite a namespace dynamically (such as
``setattr``, ``vars``, ``globals``, ``locals``, ``__dict__``,
``sys.modules``, ``importlib``, ``exec`` or ``eval``); an attribute or
item store or delete rooted in an imported name (``typing.Literal =
...``), in an unbound name or in an expression; on any object, a store
or delete whose target attribute is named like something a proof rests
on (``mod.Literal = ...``), or whose target goes through a dunder or a
private attribute of anything but ``self`` / ``cls``; a proven module,
or one a proof rests on reached as an attribute, used other than to read
an attribute off it (``mod = typing``, ``mod =
typing_extensions.typing``), or a private name of one; or a proven form,
listed callee or subscripted generic head used outside a type position
(``L = Literal``, ``type(typing.List)``). Every other call argument is
checked, so an unknown call fails closed: ``typing.cast(object,
"NoteService")`` hands its string on to a type position, which FastAPI
resolves. ``ForwardRef``'s type argument is a type position, under any
name it is imported as or qualified. A string is parsed as it is (as
``ForwardRef`` compiles it, so ``"NoteService"`` followed by a line
continuation is checked) and stripped. A string in a type position that
parses neither way (``"*[NoteService]"`` is a valid ForwardRef but not
an expression), or one nested deeper than ``_MAX_QUOTE_DEPTH``, is also
reported on its own, as an annotation string that cannot be checked;
elsewhere -- a call argument, or a ``Literal`` member or ``Annotated``
metadata that is not exempt -- a string is also parsed the way
``ForwardRef`` reads ``"*[NoteService]"``, and one that parses no way is
not reported (``typing`` cannot resolve it; it may be a regex). An
attribute read off a constructor is a non-call reference too
(``NoteService.__call__``,
``get_user_db_session.__wrapped__``, ``NoteService.__new__(...)``,
``FollowUpResearchService.__call__()``, ``NoteService.some_method``),
since the attribute can be, or can produce, the unchecked constructor;
the only exemptions are an ALL_CAPS constant (``^[A-Z][A-Z0-9_]*$``,
e.g. ``NoteAIService.MAX_CLAIMS_PER_NOTE``) and the
(constructor, attribute) pairs pinned in ``ALLOWED_ATTRIBUTE_READS``.
An ALL_CAPS attribute is exempt by its name alone, whatever it holds
(a callable bound to ``NoteService.FACTORY`` would not be seen).
Known gaps (NOT fenced): a rename, re-export, wrapper or dependency
factory in a module OUTSIDE ISOLATION_SCAN_DIRS (e.g. one in
``web/dependencies/`` that builds a ``NoteService`` from a request
parameter; for the routers, injecting such an identity-taking callable
via ``Depends`` is caught by the census test
``test_no_dependency_takes_a_user_identity_from_the_request``); dynamic
lookups (``getattr``, ``importlib``, ``__import__``, ``globals()`` /
``module.__dict__[...]``, ``eval``); ``Annotated`` reached through an
assignment alias (``A = Annotated``) with non-call metadata
(``svc: A[NoteService, dep]``; an ``import ... as`` alias is
recognised); a ``VALUE_ARGUMENTS`` callee, ``PLAIN_FORMS`` form or
generic head changed from another module, through a dynamic lookup that
uses none of the names above (``getattr(m, "Lit" + "eral")``), through a
name used only inside a quoted annotation, or through an object derived
from it at runtime (``type(Literal["x"])``), whose exempt strings stay
unparsed; code that rewrites ``__annotations__`` after the fact, such as
a decorator, which no check of an annotation as written can see; a class
name assembled from pieces as the annotation is evaluated (``"Note" +
"Service"``, ``f"{'Note'}Service"``), since each string is checked on
its own; and constant-valued non-literal ``username=`` expressions
(above). It
does NOT check where that username came from: ``NoteService(
victim_username)`` or ``get_user_db_session(username=request.
query_params["u"])`` pass here. Provenance (the value must be the
``require_auth``-derived username) is enforced by the census in
tests/security/test_cross_user_isolation_census.py
(``test_identity_sinks_only_ever_receive_the_authenticated_username``,
``test_every_get_user_db_session_call_is_passed_an_auth_username``)
for call sites in ``web/routers/*.py`` only; outside the routers this
hook's shape check is the only fence.

A file that cannot be read or parsed (syntax error, undecodable bytes,
nesting too deep for the parser, an OS error) is reported as an
offender, never skipped, and never crashes the run. Files are parsed
from bytes, so a UTF-8 BOM or a PEP 263 coding cookie is honoured.

The companion contract (tests/security/test_architecture_fences.py)
runs this same scanner over ISOLATION_SCAN_DIRS, pre-commit style
(repo-root cwd, repo-relative paths). Every path argument is made
absolute (relative ones against the cwd) and taken relative to the
repository root twice -- lexically (``..`` collapsed, symlinks not
followed) and resolved (symlinks followed) -- and the file is scanned
if EITHER form is in ISOLATION_SCAN_DIRS, so ``..`` segments, a
non-root cwd, or a symlink inside a scanned dir pointing elsewhere
cannot move a file out of scope. Repo files outside
ISOLATION_SCAN_DIRS (both forms) are skipped; files outside the
repository (mutation fixtures) are always scanned.
"""

import ast
import re
import sys
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parent.parent

#: Constructors whose instances mediate per-user data. The deletion
#: services are destructive; ``FollowUpResearchService`` defaults its
#: ``username`` to None, so a bare ``FollowUpResearchService()`` is
#: exactly the defaulted construction this hook exists to catch.
USER_DATA_CONSTRUCTORS = frozenset(
    {
        "NoteService",
        "LibraryService",
        "DownloadService",
        "get_user_db_session",
        "DocumentDeletionService",
        "CollectionDeletionService",
        "BulkDeletionService",
        "NoteAIService",
        "FollowUpResearchService",
    }
)

#: Attribute reads off a constructor that are NOT ALL_CAPS constants
#: but are known not to construct anything, as (constructor, attribute).
#: Every other non-constant attribute read off a constructor is flagged
#: (fail closed): a dunder (``__call__``, ``__wrapped__``, ``__new__``,
#: ``__init__``, ``__func__``) or a classmethod could build the object
#: without a username under a name no rule checks.
ALLOWED_ATTRIBUTE_READS = frozenset(
    {
        # staticmethod taking an existing session; constructs nothing.
        ("NoteService", "_rollback_quietly"),
    }
)

#: Attribute names read as plain constants (``MAX_CLAIMS_PER_NOTE``).
#: No leading underscore, so no dunder ever matches.
_CONSTANT_ATTRIBUTE = re.compile(r"[A-Z][A-Z0-9_]*")

#: Trees (repo-relative, trailing slash) where user-data constructions
#: must pass a username: the routers plus the service layers they call.
ISOLATION_SCAN_DIRS = (
    "src/local_deep_research/web/routers/",
    "src/local_deep_research/research_library/",
    "src/local_deep_research/web/services/",
)


def _names_username(node: ast.AST) -> bool:
    # Bare names (username, alice_username) and bound attributes
    # (self.username) both count as a username-named value.
    if isinstance(node, ast.Name):
        return "username" in node.id
    if isinstance(node, ast.Attribute):
        return "username" in node.attr
    return False


def _is_literal(node: ast.AST) -> bool:
    # A constant, or an f-string with no placeholder (f"alice").
    if isinstance(node, ast.Constant):
        return True
    return isinstance(node, ast.JoinedStr) and not any(
        isinstance(part, ast.FormattedValue) for part in node.values
    )


def _mentions_username(call: ast.Call) -> bool:
    username_kws = [
        kw for kw in call.keywords if kw.arg and "username" in kw.arg
    ]
    # username=None / username="alice" / username=f"alice" is flagged
    # even if another argument names a username.
    if any(_is_literal(kw.value) for kw in username_kws):
        return False
    return bool(username_kws) or any(_names_username(arg) for arg in call.args)


def _constructor(name: str | None) -> str | None:
    """The constructor ``name`` denotes, ignoring leading underscores
    (``_get_user_db_session`` -> ``get_user_db_session``), or None."""
    if name is None:
        return None
    stripped = name.lstrip("_")
    return stripped if stripped in USER_DATA_CONSTRUCTORS else None


def _constructor_aliases(tree: ast.AST) -> dict[str, str]:
    """Local alias -> constructor name (``LS`` -> ``LibraryService``)."""
    aliases: dict[str, str] = {}
    for node in ast.walk(tree):
        if isinstance(node, ast.ImportFrom):
            for alias in node.names:
                real = _constructor(alias.name)
                if alias.asname and real:
                    aliases[alias.asname] = real
    return aliases


def _resolve(node: ast.AST, aliases: dict[str, str]) -> str | None:
    if isinstance(node, ast.Name):
        return aliases.get(node.id) or _constructor(node.id)
    if isinstance(node, ast.Attribute):
        return _constructor(node.attr)
    return None


#: Annotation content that is evaluated into something other than a
#: type (see ``_annotations``): a call (``Depends(get_user_db_session)``), a walrus (binds in
#: the enclosing scope, since annotations are evaluated eagerly) or a
#: lambda.
_EVALUATED = (ast.Call, ast.NamedExpr, ast.Lambda)


def _annotation_names(tree: ast.AST, name: str) -> set[str]:
    """``name`` plus every local name it is imported as."""
    names = {name}
    for node in ast.walk(tree):
        if isinstance(node, ast.ImportFrom):
            names.update(
                alias.asname
                for alias in node.names
                if alias.name == name and alias.asname
            )
    return names


#: Nesting bound for quoted annotations (a string inside a string ...):
#: a string nested deeper than this counts as evaluated (fail closed)
#: and is reported as unchecked (an exempt string is never parsed).
_MAX_QUOTE_DEPTH = 8

#: Offender text for a string the hook cannot check: one in a type
#: position that does not parse, or one nested too deep.
_UNCHECKED_STRING = (
    "annotation string that cannot be checked (it does not parse as an "
    "expression, or quotes nest deeper than "
    f"{_MAX_QUOTE_DEPTH}); write the annotation unquoted or as a valid "
    "forward reference"
)


def _is_literal_head(node: ast.AST) -> bool:
    """True if ``node`` is a subscript of a name spelled ``Literal`` (bare
    or qualified), whatever that name is bound to. Only a ``Literal``
    that ``_plain_form`` proves can hold exempt strings (see
    ``_approved_children``)."""
    if not isinstance(node, ast.Subscript):
        return False
    head = node.value
    return (isinstance(head, ast.Name) and head.id == "Literal") or (
        isinstance(head, ast.Attribute) and head.attr == "Literal"
    )


def _is_annotated_head(node: ast.AST, annotated: set[str]) -> bool:
    """True if ``node`` is a subscript of a name spelled ``Annotated``
    (under any import alias, or qualified as ``typing.Annotated``),
    whatever that name is bound to. ``_has_evaluated_metadata`` checks
    every such subscript (fail closed); only an ``Annotated`` that
    ``_plain_form`` proves can hold exempt strings (see
    ``_approved_children``)."""
    if not isinstance(node, ast.Subscript):
        return False
    head = node.value
    return (isinstance(head, ast.Name) and head.id in annotated) or (
        isinstance(head, ast.Attribute) and head.attr == "Annotated"
    )


#: Call arguments that only ever hold a plain value -- documentation, an
#: alias, a regex, example values, a ``NewType`` name or a module name --
#: which the callee (from FastAPI, pydantic or ``typing``) itself never
#: evaluates as a type and never calls, as ``{callee: positions}`` (an
#: index for a positional argument, a name for a keyword). A string
#: literal passed there directly is exempt from parsing only where the
#: call sits on an approved path and the callee is provably the listed
#: one (see ``_approved_children`` and ``_proven_names``); every other
#: argument is parsed and checked, so an unknown call fails closed. The
#: entries match the signatures of FastAPI 0.136, pydantic 2.13 and
#: Python 3.12 to 3.14; ``test_value_arguments_match_the_signatures`` in
#: tests/security/test_architecture_fences.py checks them against the
#: installed ones. ``ForwardRef``'s position 2 is ``module`` before
#: Python 3.14, which makes it keyword-only.
_PARAMETER_TEXT = frozenset(
    {"alias", "title", "description", "pattern", "regex", "example", "examples"}
)
VALUE_ARGUMENTS: dict[str, frozenset[int | str]] = {
    **{
        f"fastapi.{name}": _PARAMETER_TEXT
        for name in (
            "Query",
            "Path",
            "Header",
            "Cookie",
            "Body",
            "Form",
            "File",
        )
    },
    "fastapi.Depends": frozenset({"scope"}),
    "pydantic.Field": frozenset(
        {"alias", "title", "description", "pattern", "examples"}
    ),
    "pydantic.constr": frozenset({"pattern"}),
    "typing.NewType": frozenset({0, "name"}),
    "typing.ForwardRef": frozenset({2, "module"}),
}

#: Modules whose import makes ``module.callee`` a ``VALUE_ARGUMENTS`` callee.
_VALUE_MODULES = frozenset(
    callee.rpartition(".")[0] for callee in VALUE_ARGUMENTS
)

#: Subscript forms that can hold exempt strings, as ``module.name``: the
#: string members of ``Literal[...]`` and the metadata of
#: ``Annotated[...]`` (its elements after the first), which ``typing``
#: and FastAPI keep as values. Like a ``VALUE_ARGUMENTS`` callee, a form
#: counts only under a name ``_proven_names`` proves is imported from
#: its module, and only on an approved path (see ``_approved_children``).
PLAIN_FORMS = frozenset(
    f"{module}.{form}"
    for module in ("typing", "typing_extensions")
    for form in ("Literal", "Annotated")
)

#: Modules whose import makes ``module.Literal`` / ``module.Annotated`` a
#: ``PLAIN_FORMS`` form.
_FORM_MODULES = frozenset(form.rpartition(".")[0] for form in PLAIN_FORMS)

#: Modules whose subscriptable names (``Optional``, ``List``,
#: ``Sequence``, ``Literal`` ...) keep the key of a subscript a type
#: argument: subscripting one never hands its key to the project's code,
#: so it is a step of an approved path (see ``_approved_children``).
_GENERIC_MODULES = frozenset({"typing", "typing_extensions", "collections.abc"})

#: Builtins that keep the key of a subscript a type argument
#: (``list[T]``), where nothing in the file binds the name. Their types
#: cannot be changed, only the names rebound.
_GENERIC_BUILTINS = frozenset(
    {"list", "dict", "set", "frozenset", "tuple", "type"}
)

#: What ``_proven_names`` can prove a name denotes (and any name of a
#: ``_GENERIC_MODULES`` module).
_PROVABLE = frozenset(
    {
        *VALUE_ARGUMENTS,
        *_VALUE_MODULES,
        *PLAIN_FORMS,
        *_FORM_MODULES,
        *_GENERIC_MODULES,
    }
)

#: The modules a proof can rest on, and the last part of each module's
#: name, by which an attribute read can reach the module from another
#: one (``typing_extensions.typing``).
_PROOF_MODULES = _VALUE_MODULES | _FORM_MODULES | _GENERIC_MODULES
_PROOF_MODULE_NAMES = frozenset(
    module.rpartition(".")[2] for module in _PROOF_MODULES
)

#: Names through which a file can reach or rewrite a namespace -- a
#: module's, a class's, the builtins or ``sys.modules`` -- without naming
#: what it replaces (``setattr(typing, "Literal", ...)``,
#: ``vars(typing)[...]``, ``typing.__dict__[...]``, ``sys.modules[...]``,
#: ``importlib.import_module("typing")``, ``exec(...)``). A reference to
#: one, as a name, an attribute of anything or a part of an import,
#: voids every proof in the file (see ``_proven_names``).
_DYNAMIC_NAMES = frozenset(
    {
        "__builtins__",
        "__delattr__",
        "__dict__",
        "__import__",
        "__setattr__",
        "delattr",
        "eval",
        "exec",
        "globals",
        "import_module",
        "importlib",
        "locals",
        "modules",
        "setattr",
        "vars",
    }
)

#: Definitions that bind their own name.
_NAMED_DEFINITIONS = (
    ast.FunctionDef,
    ast.AsyncFunctionDef,
    ast.ClassDef,
    ast.ExceptHandler,
    ast.MatchAs,
    ast.MatchStar,
    ast.TypeVar,
    ast.ParamSpec,
    ast.TypeVarTuple,
)


def _proven_names(tree: ast.AST) -> dict[str, str]:
    """Local names that provably denote a ``VALUE_ARGUMENTS`` callee, a
    ``PLAIN_FORMS`` form, a name of a ``_GENERIC_MODULES`` module, a
    ``_GENERIC_BUILTINS`` builtin, or one of these modules: ``{"Q":
    "fastapi.Query", "fastapi": "fastapi", "Literal": "typing.Literal",
    "list": "builtins.list"}``.

    A name counts only if each import binding it in the file is the same
    ``from <module> import <name> [as <local>]`` (or ``import <module>
    [as <local>]``; a builtin, only if nothing imports it) and nothing
    else in the file binds it -- an assignment or ``del``, a ``def``,
    ``class``, parameter, ``for`` / ``with`` / ``except`` / ``match``
    target, walrus, ``global`` / ``nonlocal`` or type parameter (no
    scope tracking; fail closed) -- so a local pass-through that happens
    to be called ``Query`` is an unknown callee, and ``Literal =
    typing.Annotated`` is not a ``Literal``.

    Nothing in the file is proven at all (fail closed: every string is
    then checked) if it contains any of these, through which it could
    change what a proven name denotes:

    * a star import;
    * a ``_DYNAMIC_NAMES`` name, as a name, as an attribute of anything
      or imported by name (``setattr(typing, "Literal", ...)``,
      ``vars(typing)[...]``, ``typing.__dict__[...]``,
      ``sys.modules[...]``, ``exec(...)``, ``from importlib import
      import_module``);
    * a store or delete (an assignment, augmented assignment or
      ``del``, a loop or ``with`` target) of an attribute or item that
      is rooted in an imported name (``typing.Literal = ...``,
      ``builtins.list = ...``, ``Query.__code__ = ...``), in a name the
      file never binds, or in anything but a name
      (``type(Literal).__getitem__ = ...``);
    * a store or delete, on any object, of an attribute named like
      anything a proof could rest on -- the last part of a
      ``PLAIN_FORMS`` or ``VALUE_ARGUMENTS`` name, a
      ``_GENERIC_BUILTINS`` name, or the last part of a name proven here
      or read off a proven module (``mod.Literal = ...``, ``b.list =
      ...``, ``t.Optional = ...``) -- or one whose target reads a dunder
      attribute (``c.__getitem__ = ...``, ``a.__class__.x = ...``) or a
      ``_``-private attribute of anything but ``self`` or ``cls``
      (``L._getitem = ...``);
    * a proven module referenced other than as the object of an
      attribute read (``mod = typing``, ``getattr(typing, ...)``), an
      attribute named like a module a proof rests on (``typing``,
      ``typing_extensions``, ``fastapi``, ``pydantic``, ``abc``) read
      other than as the object of another attribute read
      (``t = typing_extensions.typing``), or a ``_``-private name read
      off a proven module or imported from a module a proof rests on
      (``typing._SpecialForm``, ``from typing import _GenericAlias``);
    * a ``PLAIN_FORMS`` form, a ``VALUE_ARGUMENTS`` callee or a generic
      head the file subscripts, referenced -- under a name proven to
      denote it, or as an attribute of a proven module -- other than in
      a type position: the head of a subscript, the callee of a call, a
      whole annotation (``-> Dict``), or the key, or an element of the
      key, of a subscript whose head is proven (``List[Dict]``,
      ``dict[str, Dict]``). So ``L = Literal``, ``type(typing.List)``,
      ``partial(Query, ...)`` or ``isinstance(x, Mapping)`` in a file
      that subscripts ``Mapping`` voids every proof.

    Not seen (deliberate evasion, beyond a per-file syntax check): a
    form, callee or generic head changed from another module, through a
    dynamic lookup (``getattr(m, "Lit" + "eral")``), through a name used
    only inside a quoted annotation, or through an object derived from it
    at runtime (``type(Literal["x"])``) whose public attributes are then
    replaced."""
    imported: dict[str, set[str]] = {}
    bound: set[str] = set()
    # Attribute and item store / delete targets; every load of a name or
    # attribute with the node holding it; annotation roots; and each
    # tuple that is a subscript's key, with the subscript.
    targets: list[ast.AST] = []
    loads: list[tuple[ast.AST, ast.AST]] = []
    annotations: set[int] = set()
    keys: dict[int, ast.Subscript] = {}
    for node in ast.walk(tree):
        if isinstance(node, ast.ImportFrom):
            module = f"{'.' * node.level}{node.module or ''}"
            for alias in node.names:
                if (
                    alias.name == "*"
                    or alias.name in _DYNAMIC_NAMES
                    or (alias.name.startswith("_") and module in _PROOF_MODULES)
                ):
                    return {}
                imported.setdefault(alias.asname or alias.name, set()).add(
                    f"{module}.{alias.name}"
                )
        elif isinstance(node, ast.Import):
            for alias in node.names:
                local = alias.asname or alias.name.partition(".")[0]
                source = alias.name if alias.asname else local
                imported.setdefault(local, set()).add(source)
        elif isinstance(node, ast.arg):
            bound.add(node.arg)
            if node.annotation is not None:
                annotations.add(id(node.annotation))
        elif isinstance(node, (ast.Global, ast.Nonlocal)):
            bound.update(node.names)
        elif isinstance(node, ast.MatchMapping):
            if node.rest:
                bound.add(node.rest)
        elif isinstance(node, ast.AnnAssign):
            annotations.add(id(node.annotation))
        elif isinstance(node, ast.Subscript) and isinstance(
            node.slice, ast.Tuple
        ):
            keys[id(node.slice)] = node
        if isinstance(node, _NAMED_DEFINITIONS) and node.name:
            bound.add(node.name)
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and (
            node.returns is not None
        ):
            annotations.add(id(node.returns))
        for child in ast.iter_child_nodes(node):
            if isinstance(child, ast.Name):
                spelled = child.id
            elif isinstance(child, ast.Attribute):
                spelled = child.attr
            elif isinstance(child, ast.Subscript):
                if not isinstance(child.ctx, ast.Load):
                    targets.append(child)
                continue
            else:
                continue
            if spelled in _DYNAMIC_NAMES:
                return {}
            if isinstance(child.ctx, ast.Load):
                loads.append((child, node))
            elif isinstance(child, ast.Name):
                bound.add(child.id)
            else:
                targets.append(child)
    names: dict[str, str] = {}
    for name, sources in imported.items():
        if len(sources) != 1 or name in bound:
            continue
        (source,) = sources
        if source in _PROVABLE or source.rpartition(".")[0] in _GENERIC_MODULES:
            names[name] = source
    for name in _GENERIC_BUILTINS - imported.keys() - bound:
        names[name] = f"builtins.{name}"
    modules = {
        name for name, source in names.items() if source in _PROOF_MODULES
    }

    def denotes(node: ast.AST) -> str:
        """What ``node`` denotes by ``names``, or ``""``."""
        return _proven_name(node, names, _PROOF_MODULES) or ""

    # The last part of everything a proof here could rest on, and of the
    # forms, callees and generic heads to be used only in type positions.
    relied = {
        source.rpartition(".")[2]
        for source in (*VALUE_ARGUMENTS, *PLAIN_FORMS, *names.values())
    } | _GENERIC_BUILTINS
    used = {
        source.rpartition(".")[2] for source in (*VALUE_ARGUMENTS, *PLAIN_FORMS)
    }
    for node, parent in loads:
        if (
            isinstance(node, ast.Attribute)
            and isinstance(node.value, ast.Name)
            and node.value.id in modules
        ):
            relied.add(node.attr)
            if node.attr.startswith("_"):
                return {}
        if isinstance(parent, ast.Subscript) and parent.value is node:
            source = denotes(node)
            if (
                source
                and source not in _PROOF_MODULES
                and not (source.startswith("builtins."))
            ):
                used.add(source.rpartition(".")[2])
    for target in targets:
        root = target
        while isinstance(root, (ast.Attribute, ast.Subscript)):
            root = root.value
        if (
            not isinstance(root, ast.Name)
            or root.id in imported
            or root.id not in bound
        ):
            return {}
        if isinstance(target, ast.Attribute) and target.attr in relied:
            return {}
        step = target
        while isinstance(step, (ast.Attribute, ast.Subscript)):
            if (
                isinstance(step, ast.Attribute)
                and step.attr.startswith("_")
                and (step.attr.endswith("__") or root.id not in ("self", "cls"))
            ):
                return {}
            step = step.value
    for node, parent in loads:
        if isinstance(parent, ast.Attribute) and parent.value is node:
            if isinstance(node, ast.Name) and node.id in modules:
                continue
        elif isinstance(node, ast.Name) and node.id in modules:
            return {}
        elif (
            isinstance(node, ast.Attribute) and node.attr in _PROOF_MODULE_NAMES
        ):
            return {}
        source = denotes(node)
        if (
            not source
            or source in _PROOF_MODULES
            or source.startswith("builtins.")
            or source.rpartition(".")[2] not in used
        ):
            continue
        subscript = keys.get(id(parent), parent)
        if not (
            (isinstance(parent, ast.Subscript) and parent.value is node)
            or (isinstance(parent, ast.Call) and parent.func is node)
            or id(node) in annotations
            or (
                isinstance(subscript, ast.Subscript)
                and node is not subscript.value
                and denotes(subscript.value)
            )
        ):
            return {}
    return names


def _proven_name(
    node: ast.AST, proven: dict[str, str], modules: frozenset[str]
) -> str | None:
    """What ``node`` provably denotes (see ``_proven_names``): a proven
    name, or an attribute of a name proven to be one of ``modules``
    (``fastapi.Query``), as ``module.name``; else None."""
    if isinstance(node, ast.Name):
        return proven.get(node.id)
    if (
        isinstance(node, ast.Attribute)
        and isinstance(node.value, ast.Name)
        and proven.get(node.value.id) in modules
    ):
        return f"{proven[node.value.id]}.{node.attr}"
    return None


def _value_callee(func: ast.AST, proven: dict[str, str]) -> str | None:
    """The ``VALUE_ARGUMENTS`` callee ``func`` provably names (``Query``,
    an alias ``Q``, or ``fastapi.Query``; see ``_proven_names``), or
    None."""
    callee = _proven_name(func, proven, _VALUE_MODULES)
    return callee if callee in VALUE_ARGUMENTS else None


def _plain_form(node: ast.AST, proven: dict[str, str]) -> str | None:
    """``"Literal"`` or ``"Annotated"`` if ``node`` is a subscript of that
    ``PLAIN_FORMS`` form under a name ``_proven_names`` proves
    (``Literal``, an alias ``L``, or ``typing.Literal``), else None."""
    if not isinstance(node, ast.Subscript):
        return None
    form = _proven_name(node.value, proven, _FORM_MODULES)
    return form.rpartition(".")[2] if form in PLAIN_FORMS else None


def _generic_head(head: ast.AST, proven: dict[str, str]) -> bool:
    """True if ``head`` provably names something of a ``_GENERIC_MODULES``
    module or a ``_GENERIC_BUILTINS`` builtin (``Optional``,
    ``typing.List``, ``list``; see ``_proven_names``), whose subscript
    keeps its key a type argument."""
    name = _proven_name(head, proven, _GENERIC_MODULES)
    return name is not None and (
        name.rpartition(".")[0] in _GENERIC_MODULES
        or name.startswith("builtins.")
    )


def _is_forward_ref(func: ast.AST, forward_refs: set[str]) -> bool:
    """True if ``func`` is named ``ForwardRef``: bare, under any name it
    is imported as, or qualified (``typing.ForwardRef``)."""
    return (isinstance(func, ast.Name) and func.id in forward_refs) or (
        isinstance(func, ast.Attribute) and func.attr == "ForwardRef"
    )


def _call_arguments(call: ast.Call) -> list[tuple[ast.AST, int | str | None]]:
    """Each argument of ``call`` with the parameter it binds: an index, a
    keyword, or None where that is unknown.

    Literal unpacking is followed, nested too: ``*("a", "b")`` binds
    indexes and ``**{"k": v}`` binds keyword ``k``. Anything else that is
    unpacked (``*args``, ``**dict(k=v)``, a key that is not a string
    constant) binds None, and so does every positional argument after a
    ``*`` of unknown length. The unpacking syntax itself (the starred
    node, the unpacked tuple, list or dict, and a dict's keys) is listed
    with None too."""
    found: list[tuple[ast.AST, int | str | None]] = []
    index: int | None = 0
    positional = list(reversed(call.args))
    while positional:
        arg = positional.pop()
        if isinstance(arg, ast.Starred):
            found.extend([(arg, None), (arg.value, None)])
            if isinstance(arg.value, (ast.Tuple, ast.List)):
                positional.extend(reversed(arg.value.elts))
            else:
                index = None
            continue
        found.append((arg, index))
        if index is not None:
            index += 1
    mappings: list[ast.AST] = []
    for kw in call.keywords:
        if kw.arg is None:
            mappings.append(kw.value)
        else:
            found.append((kw.value, kw.arg))
    while mappings:
        mapping = mappings.pop()
        found.append((mapping, None))
        if not isinstance(mapping, ast.Dict):
            continue
        for key, value in zip(mapping.keys, mapping.values):
            if key is None:
                mappings.append(value)
                continue
            found.append((key, None))
            named = isinstance(key, ast.Constant) and isinstance(key.value, str)
            found.append((value, key.value if named else None))
    return found


def _parse_expression(text: str) -> ast.AST | None:
    """``text`` parsed as an expression, or None if it does not parse."""
    try:
        return ast.parse(text, mode="eval").body
    except (SyntaxError, ValueError, RecursionError, MemoryError):
        return None


def _parse_string(text: str, star: bool) -> list[ast.AST]:
    """The distinct trees ``text`` parses to as an expression, trying it
    as it is (``ForwardRef`` compiles the raw string, so one ending in a
    line continuation -- a backslash and a newline -- is valid there)
    and with surrounding whitespace stripped (``eval`` strips leading
    spaces). With ``star``, each of the two that does not parse is also
    tried the way ``ForwardRef`` reads a string starting with ``*``, as
    ``({text},)[0]`` (``"*[T]"``), which also tolerates leading
    whitespace before a line continuation. Empty only if every attempt
    fails."""
    trees: dict[str, ast.AST] = {}
    for form in (text, text.strip()):
        parsed = _parse_expression(form)
        if parsed is None and star:
            parsed = _parse_expression(f"({form},)[0]")
        if parsed is not None:
            trees.setdefault(ast.dump(parsed), parsed)
    return list(trees.values())


def _is_string(node: ast.AST) -> bool:
    """True if ``node`` is a string literal."""
    return isinstance(node, ast.Constant) and isinstance(node.value, str)


#: Where a node sits on an approved path from an annotation's root (see
#: ``_approved_children``): a type slot, an ``Annotated`` metadata slot,
#: or an exempt string. A node that has none is off the path.
_TYPE_SLOT, _METADATA_SLOT, _EXEMPT = "type slot", "metadata slot", "exempt"


def _listed_arguments(call: ast.Call, proven: dict[str, str]) -> list[ast.AST]:
    """The arguments ``VALUE_ARGUMENTS`` lists for the callee that
    ``_value_callee`` proves ``call`` names, passed directly: a keyword
    by its name, a positional argument by its index before any ``*``
    unpacking (nothing inside a ``*`` or ``**`` is listed)."""
    listed = VALUE_ARGUMENTS.get(_value_callee(call.func, proven), frozenset())
    found: list[ast.AST] = []
    for index, argument in enumerate(call.args):
        if isinstance(argument, ast.Starred):
            break
        if index in listed:
            found.append(argument)
    found.extend(kw.value for kw in call.keywords if kw.arg in listed)
    return found


def _inert_metadata(node: ast.AST, proven: dict[str, str]) -> bool:
    """True if ``node``, as ``Annotated`` metadata, is an object FastAPI
    and pydantic never hand the annotated type to project code through:
    a constant, a call of a callee ``_value_callee`` proves, or a dict
    display with no ``**`` unpacking. Any other object may define
    ``__get_pydantic_core_schema__``, which pydantic calls with the
    type (``Annotated[Literal["T"], G]`` hands ``Literal["T"]`` to
    ``G``, which can read ``"T"`` back out into the type it validates
    with), so its ``Annotated`` approves nothing."""
    return (
        isinstance(node, ast.Constant)
        or (
            isinstance(node, ast.Call)
            and _value_callee(node.func, proven) is not None
        )
        or (isinstance(node, ast.Dict) and None not in node.keys)
    )


def _approved_children(
    node: ast.AST, slot: str, proven: dict[str, str]
) -> dict[int, str]:
    """The allowlist: ``{id(descendant): slot}`` for what ``node``, on an
    approved path in ``slot`` (see ``_annotation_nodes``), passes the
    path on to. A string literal is exempt from parsing only where a
    rule here makes it ``_EXEMPT``. Whatever no rule names is off the
    path, and so is everything below it, whatever the node kind -- an
    attribute read, an operator, a comparison, an unlisted call, a
    subscript of another head, a lambda, a walrus, a conditional, a
    starred or formatted value -- so every string there is parsed and
    checked. The rules, where proven means provably the ``typing``,
    ``typing_extensions``, ``collections.abc``, builtin, FastAPI or
    pydantic name (see ``_proven_names``):

    * in a type slot, a subscript of a proven ``Literal``: each string
      member of its key is exempt;
    * in a type slot, a subscript of a proven ``Annotated`` whose key is
      a tuple of two or more elements, none starred, with only inert
      metadata (see ``_inert_metadata``): the first element is a type
      slot, and each later one is exempt if it is a string, else a
      metadata slot;
    * in a type slot, a subscript of another proven generic head
      (``Optional``, ``Union``, ``list`` ...; see ``_generic_head``):
      each element of its key is a type slot;
    * in a type or metadata slot, a call of a proven ``VALUE_ARGUMENTS``
      callee: each string passed directly in a listed position is exempt
      (see ``_listed_arguments``);
    * in a metadata slot, a dict display: each string key and value is
      exempt (one with ``**`` unpacking is not inert metadata, so its
      ``Annotated`` approves nothing)."""
    if slot == _TYPE_SLOT and isinstance(node, ast.Subscript):
        key = node.slice
        elements = key.elts if isinstance(key, ast.Tuple) else [key]
        form = _plain_form(node, proven)
        if form == "Literal":
            return {
                id(member): _EXEMPT for member in elements if _is_string(member)
            }
        if form == "Annotated":
            if (
                not isinstance(key, ast.Tuple)
                or len(elements) < 2
                or any(isinstance(e, ast.Starred) for e in elements)
                or not all(_inert_metadata(m, proven) for m in elements[1:])
            ):
                return {}
            return {
                id(elements[0]): _TYPE_SLOT,
                **{
                    id(meta): _EXEMPT if _is_string(meta) else _METADATA_SLOT
                    for meta in elements[1:]
                },
            }
        if _generic_head(node.value, proven):
            return {id(element): _TYPE_SLOT for element in elements}
        return {}
    if isinstance(node, ast.Call):
        return {
            id(argument): _EXEMPT
            for argument in _listed_arguments(node, proven)
            if _is_string(argument)
        }
    if slot == _METADATA_SLOT and isinstance(node, ast.Dict):
        return {
            id(item): _EXEMPT
            for item in (*node.keys, *node.values)
            if _is_string(item)
        }
    return {}


#: How ``_annotation_nodes`` checks a string that is not exempt, by where
#: it sits: in a type position it must parse (it is reported if it does
#: not); in the argument context -- a call argument, a ``Literal``
#: member or ``Annotated`` metadata -- it is also parsed the way
#: ``ForwardRef`` reads a starred string, and not reported if it does
#: not parse at all, since it may be a value such as a regex.
_TYPE, _ARGUMENT = "type", "argument"


def _child_contexts(
    node: ast.AST,
    context: str,
    annotated: set[str],
    forward_refs: set[str],
    proven: dict[str, str],
) -> list[tuple[ast.AST, str]]:
    """``(descendant, context)`` pairs for what ``node``, seen in
    ``context``, contains. The caller keeps the first pair given for a
    node, so the specific ones come first: the string members of a
    ``Literal`` and the metadata of an ``Annotated`` (the form
    ``_plain_form`` proves, else the name it is spelled with; see
    ``_is_literal_head`` and ``_is_annotated_head``) and every argument
    of a call are in the argument context, except a ``ForwardRef``'s
    type argument, which is a type position; a call's callee is in the
    call's context. Every other child inherits ``context``."""
    given: list[tuple[ast.AST, str]] = []
    form = _plain_form(node, proven)
    if form == "Literal" or (form is None and _is_literal_head(node)):
        members = (
            node.slice.elts
            if isinstance(node.slice, ast.Tuple)
            else [node.slice]
        )
        given.extend(
            (member, _ARGUMENT) for member in members if _is_string(member)
        )
    elif (
        form == "Annotated"
        or (form is None and _is_annotated_head(node, annotated))
    ) and isinstance(node.slice, ast.Tuple):
        given.extend((meta, _ARGUMENT) for meta in node.slice.elts[1:])
    elif isinstance(node, ast.Call):
        forward_ref = _is_forward_ref(node.func, forward_refs)
        given.append((node.func, context))
        for argument, position in _call_arguments(node):
            if forward_ref and position in (0, "arg", None):
                given.append((argument, _TYPE))
            else:
                given.append((argument, _ARGUMENT))
    given.extend((child, context) for child in ast.iter_child_nodes(node))
    return given


def _annotation_nodes(
    root: ast.AST,
    annotated: set[str],
    forward_refs: set[str],
    proven: dict[str, str],
) -> tuple[list[tuple[ast.AST, int | None]], bool, list[int]]:
    """Every node of an annotation, quoted parts included.

    Returns ``(nodes, unparsable, unchecked)``. ``nodes`` holds
    ``(node, line)`` pairs: ``line`` is None for a node of ``root``
    itself and, for a node parsed out of a string, the line of the
    outermost string literal holding it. FastAPI (via
    ``typing.get_type_hints``) evaluates a quoted annotation, so every
    string in it is parsed as an expression, recursively
    (``"Annotated[NoteService, Depends()]"``, ``Optional["NoteService"]``),
    and the parsed tree is inspected exactly like an unquoted one --
    unless the string is exempt.

    A string is exempt only if every step of its path from the
    annotation's root is one ``_approved_children`` approves (an
    allowlist: what it does not name fails closed). ``root`` is a type
    slot, and a string in a type slot is parsed into a tree whose root
    is a type slot again; the path ends at any other node. So
    ``Literal["NoteService"]``, ``Optional[Literal["NoteService"]]``,
    ``Annotated[str, "NoteService"]``, ``Annotated[str,
    Query(description="NoteService")]`` and ``Annotated[str,
    {"description": "NoteService"}]`` hold exempt strings, and the
    wholly quoted ``"Annotated[Literal['NoteService'], Query()]"`` too,
    while ``Literal["NoteService"].__args__[0]``, ``G |
    Literal["NoteService"]``, ``G[Literal["NoteService"]]`` (``G`` not a
    proven generic head), ``f"{Literal['NoteService']}"``,
    ``Annotated[*(), "NoteService", Depends()]``,
    ``Annotated[Literal["NoteService"], G]`` (see ``_inert_metadata``)
    and ``typing.cast(object, "NoteService")`` do not: below a node no
    rule approves, every string is checked, whatever reads it out, and a
    tree parsed out of a string that is not exempt is off the path as a
    whole.

    A string that is not exempt is parsed (see ``_parse_string``) and
    checked in one of two contexts (see ``_child_contexts``). In a type
    position -- ``root``, and whatever is reached from one other than as
    a call argument, a ``Literal`` member or ``Annotated`` metadata, and
    a ``ForwardRef``'s type argument (the first positional, ``arg=``, or
    an argument whose position is unknown; under any name it is imported
    as or qualified) wherever the call sits -- it is parsed as it is and
    stripped, and one that parses neither way, or that nests deeper than
    ``_MAX_QUOTE_DEPTH``, makes ``unparsable`` True (fail closed: the
    caller treats the annotation as evaluated) and is listed in
    ``unchecked`` by the line of its outermost string literal, which the
    caller reports as an offender. ``"*[NoteService]"`` is valid as a
    ``ForwardRef`` but not as an expression, so in a type position it is
    unchecked. In the argument context it is also parsed the way
    ``ForwardRef`` reads it, as ``(*[NoteService],)[0]``
    (``typing.cast(object, "*[NoteService]")`` hands it on to a type
    position); one that parses no way at all makes ``unparsable`` True
    but is not reported, since ``typing`` cannot resolve it and it may
    be a value, such as a regex, and one nested too deep is reported.
    Literal ``*`` / ``**`` unpacking of call arguments is followed to
    find a ``ForwardRef``'s type argument (see ``_call_arguments``).

    Not seen: a name assembled from pieces as the annotation is
    evaluated (``"Note" + "Service"``, ``f"{'Note'}Service"``), since
    each string is checked on its own; a form, callee or generic head
    changed in a way ``_proven_names`` does not see; and code that
    rewrites ``__annotations__`` after the fact, such as a decorator,
    which no check of the annotation as written can see.
    """
    nodes: list[tuple[ast.AST, int | None]] = []
    unparsable = False
    unchecked: list[int] = []
    pending: list[tuple[ast.AST, str, str | None, int, int | None]] = [
        (root, _TYPE, _TYPE_SLOT, 0, None)
    ]
    while pending:
        current, base, base_slot, depth, line = pending.pop()
        # Contexts and approved-path slots by node id. ast.walk is
        # breadth first, so a node's parent (or the subscript or call
        # whose key or arguments hold it) sets both before the node is
        # reached; a node with no slot is off the path.
        contexts = {id(current): base}
        slots = {id(current): base_slot} if base_slot else {}
        for node in ast.walk(current):
            nodes.append((node, line))
            context = contexts.get(id(node), _TYPE)
            slot = slots.get(id(node))
            if slot in (_TYPE_SLOT, _METADATA_SLOT):
                slots.update(_approved_children(node, slot, proven))
            for child, given in _child_contexts(
                node, context, annotated, forward_refs, proven
            ):
                contexts.setdefault(id(child), given)
            if slot == _EXEMPT or not _is_string(node):
                continue
            here = node.lineno if line is None else line
            if depth >= _MAX_QUOTE_DEPTH:
                unparsable = True
                unchecked.append(here)
                continue
            trees = _parse_string(node.value, star=context == _ARGUMENT)
            if not trees:
                unparsable = True
                if context == _TYPE:
                    unchecked.append(here)
                continue
            inner = _TYPE_SLOT if slot == _TYPE_SLOT else None
            for tree in trees:
                pending.append((tree, context, inner, depth + 1, here))
    return nodes, unparsable, unchecked


def _has_evaluated_metadata(
    nodes: list[tuple[ast.AST, int | None]], annotated: set[str]
) -> bool:
    """True if an ``Annotated[...]`` among ``nodes`` is anything but
    ``Annotated[T, constant, ...]``: a non-constant metadata element
    (``Annotated[T, dep]`` with ``dep = Depends()`` hands FastAPI a
    dependency without a call), a starred element (``Annotated[*(T,
    dep)]`` unpacks to the same thing) or a slice that is not a tuple
    (``Annotated[args]`` with ``args = (T, dep)``)."""
    for node, _line in nodes:
        if not _is_annotated_head(node, annotated):
            continue
        if not isinstance(node.slice, ast.Tuple):
            return True
        elements = node.slice.elts
        if any(isinstance(element, ast.Starred) for element in elements):
            return True
        if any(not isinstance(meta, ast.Constant) for meta in elements[1:]):
            return True
    return False


#: Nodes that open their own scope: a store inside them never binds a
#: class attribute.
_NEW_SCOPES = (
    ast.FunctionDef,
    ast.AsyncFunctionDef,
    ast.ClassDef,
    ast.Lambda,
    ast.ListComp,
    ast.SetComp,
    ast.DictComp,
    ast.GeneratorExp,
)
_BLOCKS = (ast.stmt, ast.excepthandler, ast.match_case)


def _class_body_blocks(cls: ast.ClassDef) -> list[ast.AST]:
    """Every statement (and ``except`` / ``case`` clause) executed in
    ``cls``'s own body, including those nested under ``if`` / ``try`` /
    ``with`` / ``for`` / ``while`` / ``match``, but not the bodies of a
    nested def or class (the def or class statement itself is
    included: it binds its name)."""
    blocks: list[ast.AST] = []
    pending: list[ast.AST] = list(cls.body)
    while pending:
        block = pending.pop()
        blocks.append(block)
        if not isinstance(block, _NEW_SCOPES):
            pending.extend(
                child
                for child in ast.iter_child_nodes(block)
                if isinstance(child, _BLOCKS)
            )
    return blocks


def _own_expressions(block: ast.AST) -> list[ast.AST]:
    """The expression nodes of one statement or clause, not descending
    into nested statements or into a nested scope."""
    nodes: list[ast.AST] = []
    pending = [
        child
        for child in ast.iter_child_nodes(block)
        if not isinstance(child, _BLOCKS)
    ]
    while pending:
        node = pending.pop()
        nodes.append(node)
        if not isinstance(node, _NEW_SCOPES):
            pending.extend(ast.iter_child_nodes(node))
    return nodes


#: Bases that never supply a class attribute (a field default).
_PLAIN_BASES = frozenset({"object", "Generic", "Protocol"})


def _may_inherit_defaults(cls: ast.ClassDef) -> bool:
    """True if ``cls`` has a base other than ``object`` / ``Generic`` /
    ``Protocol`` (bare, qualified or subscripted), so a field it leaves
    without a default may inherit one from that base."""
    for base in cls.bases:
        head = base.value if isinstance(base, ast.Subscript) else base
        if isinstance(head, ast.Name):
            name = head.id
        elif isinstance(head, ast.Attribute):
            name = head.attr
        else:
            return True
        if name not in _PLAIN_BASES:
            return True
    return False


def _class_field_defaults(cls: ast.ClassDef) -> dict[int, ast.AST]:
    """``id(field)`` -> its default, for every annotated assignment in
    ``cls``'s own body (see ``_class_body_blocks``).

    A dataclass (or FastAPI, via the generated ``__init__``) reads a
    field's default from the class attribute, so EVERY binding of the
    field's name in the class body counts, not just ``x: T = value``:
    ``svc: NoteService`` followed by ``svc = Depends()`` defaults the
    field to ``Depends()``. A plain ``name = value`` contributes
    ``value``; any other binding (augmented or unpacking assignment,
    loop / ``with`` / ``except`` / ``case`` target, import, def, class,
    walrus) contributes the binding statement itself, which is never a
    constant (fail closed). The default is the first non-constant
    contribution, else the field's own value. A field with no binding
    at all in the class body of a class with any base other than
    ``object`` / ``Generic`` / ``Protocol`` may inherit its default
    from that base, so its default is the class itself, which is never
    a constant (fail closed).
    """
    blocks = _class_body_blocks(cls)
    bound: dict[str, list[ast.AST]] = {}

    def bind(name: str | None, value: ast.AST) -> None:
        if name:
            bound.setdefault(name, []).append(value)

    fields: list[ast.AnnAssign] = []
    for block in blocks:
        if isinstance(block, ast.AnnAssign):
            fields.append(block)
            if block.value is not None and isinstance(block.target, ast.Name):
                bind(block.target.id, block.value)
            if block.value is not None:
                for node in ast.walk(block.value):
                    if isinstance(node, ast.NamedExpr):
                        bind(node.target.id, block)
            continue
        if (
            isinstance(block, ast.Assign)
            and len(block.targets) == 1
            and isinstance(block.targets[0], ast.Name)
        ):
            bind(block.targets[0].id, block.value)
            for node in ast.walk(block.value):
                if isinstance(node, ast.NamedExpr):
                    bind(node.target.id, block)
            continue
        if isinstance(block, _NEW_SCOPES):
            bind(block.name, block)
        elif isinstance(block, (ast.Import, ast.ImportFrom)):
            for alias in block.names:
                bind(alias.asname or alias.name.split(".")[0], block)
        elif isinstance(block, ast.excepthandler):
            bind(block.name, block)
        for node in _own_expressions(block):
            if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Store):
                bind(node.id, block)
            elif isinstance(node, (ast.MatchAs, ast.MatchStar)):
                bind(node.name, block)
            elif isinstance(node, ast.MatchMapping):
                bind(node.rest, block)
    defaults: dict[int, ast.AST] = {}
    inherits = _may_inherit_defaults(cls)
    for field in fields:
        values = (
            bound.get(field.target.id, [])
            if isinstance(field.target, ast.Name)
            else []
        )
        chosen = next(
            (v for v in values if not isinstance(v, ast.Constant)),
            field.value,
        )
        if chosen is None and inherits:
            chosen = cls
        if chosen is not None:
            defaults[id(field)] = chosen
    return defaults


def _annotations(tree: ast.AST) -> list[tuple[ast.AST, bool]]:
    """Every annotation as ``(annotation, type_only)``.

    An annotation is type-only -- never a rebinding -- unless:

    * it contains a call, walrus or lambda anywhere
      (``Annotated[T, Depends(get_user_db_session)]`` hands the callable to
      FastAPI; ``x: (s := get_user_db_session) = 1`` binds it in the enclosing
      scope, since annotations are evaluated eagerly before 3.14);
    * it is a parameter's or a class-body field's annotation (a
      dataclass field becomes an ``__init__`` parameter) whose default
      is not a constant: ``svc: NoteService = Depends()``, ``= Depends(None)``
      or ``= dep`` with ``dep = Depends()`` makes FastAPI CALL the
      annotated class, filling its parameters from the request;
    * an ``Annotated[...]`` in it (under any import alias) carries a
      non-constant metadata element (``Annotated[class, dep]``), a
      starred element (``Annotated[*(class, dep)]``) or a slice that is
      not a tuple (``Annotated[args]``);
    * a string in it that is not exempt does not parse (see
      ``_annotation_nodes``), or nests too deep (fail closed; one in a type
      position, or nested too deep, is also reported itself, see
      ``_string_annotation_hits``);
    * it is a class field with no binding in its own class body, on a
      class with a base other than ``object`` / ``Generic`` /
      ``Protocol``: the field may inherit a non-constant default.

    Every string that is not exempt is parsed and checked like
    unquoted code, recursively (see ``_annotation_nodes``), so a wholly
    quoted ``"Annotated[class, Depends()]"`` is not type-only either. A class
    field counts wherever it sits in the class body, also under ``if`` /
    ``try`` / ``with`` / loops, and its default is any binding of its
    name there (``svc: T`` then ``svc = Depends()``; see
    ``_class_field_defaults``).

    Callee names are not matched here, so ``Depends`` imported under
    another name cannot hide a dependency (fail closed). Return
    annotations and module- or function-level variable annotations have
    no default.
    """
    annotated = _annotation_names(tree, "Annotated")
    forward_refs = _annotation_names(tree, "ForwardRef")
    proven = _proven_names(tree)
    defaults: dict[int, ast.AST] = {}
    for node in ast.walk(tree):
        if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
            args = node.args
            positional = args.posonlyargs + args.args
            offset = len(positional) - len(args.defaults)
            for arg, default in zip(positional[offset:], args.defaults):
                defaults[id(arg)] = default
            for arg, default in zip(args.kwonlyargs, args.kw_defaults):
                if default is not None:
                    defaults[id(arg)] = default
        elif isinstance(node, ast.ClassDef):
            defaults.update(_class_field_defaults(node))
    found: list[tuple[ast.AST, bool]] = []
    for node in ast.walk(tree):
        if (
            isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef))
            and node.returns is not None
        ):
            root, default = node.returns, None
        elif isinstance(node, ast.arg) and node.annotation is not None:
            root, default = node.annotation, defaults.get(id(node))
        elif isinstance(node, ast.AnnAssign):
            root, default = node.annotation, defaults.get(id(node))
        else:
            continue
        nodes, unparsable, _unchecked = _annotation_nodes(
            root, annotated, forward_refs, proven
        )
        type_only = (
            (default is None or isinstance(default, ast.Constant))
            and not unparsable
            and not any(isinstance(n, _EVALUATED) for n, _line in nodes)
            and not _has_evaluated_metadata(nodes, annotated)
        )
        found.append((root, type_only))
    return found


def _annotation_ids(tree: ast.AST) -> set[int]:
    """ids of every node inside a type-only annotation (see
    ``_annotations``), which is never a rebinding."""
    return {
        id(child)
        for root, type_only in _annotations(tree)
        if type_only
        for child in ast.walk(root)
    }


def _string_annotation_hits(
    tree: ast.AST, aliases: dict[str, str]
) -> list[tuple[int, str]]:
    """Constructors named inside a STRING in a non-type-only annotation.

    FastAPI resolves a forward reference (``svc: "NoteService" =
    Depends()``, or a wholly quoted ``"Annotated[NoteService,
    Depends()]"``) in the module globals, so a quoted constructor name
    is as live there as a bare one. Strings are parsed recursively (see
    ``_annotation_nodes``); one in a type position that does not parse,
    or any that nests too deep, is reported itself, as an annotation
    string that cannot be checked."""
    annotated = _annotation_names(tree, "Annotated")
    forward_refs = _annotation_names(tree, "ForwardRef")
    proven = _proven_names(tree)
    hits: list[tuple[int, str]] = []
    for root, type_only in _annotations(tree):
        if type_only:
            continue
        nodes, _unparsable, unchecked = _annotation_nodes(
            root, annotated, forward_refs, proven
        )
        hits.extend((line, _UNCHECKED_STRING) for line in unchecked)
        for inner, line in nodes:
            if line is None:
                continue
            real = _resolve(inner, aliases)
            if real:
                hits.append(
                    (
                        line,
                        f"non-call reference to {real} (quoted in an "
                        "evaluated annotation; user-data constructors "
                        "must be called directly)",
                    )
                )
    return hits


def _rebindings(
    tree: ast.AST, aliases: dict[str, str], callees: set[int]
) -> list[tuple[int, str]]:
    """Ways a constructor escapes under a name this hook does not know.

    * ``from x import get_user_db_session as open_session``: any module
      importing ``open_session`` from here would call it unchecked, so
      an import alias must itself be a constructor name (leading
      underscores allowed: ``as _get_user_db_session``).
    * A loaded non-call reference (``make = NoteService``,
      ``partial(get_user_db_session, ...)``, ``return NoteService``,
      ``class Mine(NoteService)``), including the object of an
      attribute read (``NoteService.__call__``, ``get_user_db_session.
      __wrapped__``), whether or not that attribute is then called. Not
      counted: the callee of a call, a type-only annotation (see
      ``_annotations``: e.g. one with a non-constant default, or holding
      a call, walrus or lambda, still counts), and the object of
      an ALL_CAPS constant read (``NoteAIService.MAX_CLAIMS_PER_NOTE``)
      or of an ``ALLOWED_ATTRIBUTE_READS`` pair.
    * A constructor quoted in a non-type-only annotation
      (``svc: "NoteService" = Depends()``).
    """
    hits: list[tuple[int, str]] = _string_annotation_hits(tree, aliases)
    exempt = _annotation_ids(tree) | callees
    # id(constructor node) -> the non-exempt attribute read off it.
    read_attr: dict[int, str] = {}
    for node in ast.walk(tree):
        if isinstance(node, ast.ImportFrom):
            for alias in node.names:
                real = _constructor(alias.name)
                if real and alias.asname and not _constructor(alias.asname):
                    hits.append(
                        (
                            node.lineno,
                            f"{real} imported as {alias.asname} (an alias "
                            "must keep a constructor name, e.g. "
                            f"_{real})",
                        )
                    )
        elif isinstance(node, ast.Attribute):
            owner = _resolve(node.value, aliases)
            if owner is None:
                continue
            if (
                _CONSTANT_ATTRIBUTE.fullmatch(node.attr)
                or (owner, node.attr) in ALLOWED_ATTRIBUTE_READS
            ):
                exempt.add(id(node.value))
            else:
                read_attr[id(node.value)] = node.attr
    for node in ast.walk(tree):
        if not isinstance(node, (ast.Name, ast.Attribute)):
            continue
        if not isinstance(node.ctx, ast.Load) or id(node) in exempt:
            continue
        real = _resolve(node, aliases)
        if real:
            via = (
                f" (via .{read_attr[id(node)]}: only ALL_CAPS constants "
                "and ALLOWED_ATTRIBUTE_READS may be read off a constructor)"
                if id(node) in read_attr
                else ""
            )
            hits.append(
                (
                    node.lineno,
                    f"non-call reference to {real}{via} (user-data "
                    "constructors must be called directly)",
                )
            )
    return hits


def _parse(path: Path) -> tuple[ast.AST | None, str]:
    """Parse ``path``, or return ``(None, offender line)``.

    Fail closed: an unreadable or unparsable file is reported, never
    skipped, and a parser failure (too-deep nesting raises RecursionError
    or MemoryError) never crashes the run.
    """
    try:
        source = path.read_bytes()
    except OSError as exc:
        return None, f"{path}:1: could not read ({type(exc).__name__}: {exc})"
    try:
        # Bytes, not text: ast.parse then honours a UTF-8 BOM and a PEP
        # 263 coding cookie exactly as the interpreter does.
        return ast.parse(source, filename=str(path)), ""
    except (SyntaxError, ValueError, RecursionError, MemoryError) as exc:
        lineno = getattr(exc, "lineno", None) or 1
        return (
            None,
            f"{path}:{lineno}: could not parse ({type(exc).__name__}: {exc})",
        )


def scan_file(path: Path) -> list[str]:
    offenders: list[str] = []
    tree, error = _parse(path)
    if tree is None:
        return [error]
    aliases = _constructor_aliases(tree)
    callees: set[int] = set()
    for node in ast.walk(tree):
        if not isinstance(node, ast.Call):
            continue
        callees.add(id(node.func))
        name = _resolve(node.func, aliases)
        if name and not _mentions_username(node):
            offenders.append(
                f"{path}:{node.lineno}: {name}(...) without an explicit "
                "username argument"
            )
    for lineno, what in _rebindings(tree, aliases, callees):
        offenders.append(f"{path}:{lineno}: {what}")
    return offenders


def _repo_relative(path: Path) -> tuple[str | None, str | None]:
    """``(lexical, resolved)`` repo-relative posix paths of ``path``;
    each is None if that form of the path lies outside the repo.

    Both are absolute-ised against the cwd, so ``..`` segments and a cwd
    other than the repo root still yield the true repo-relative path.
    The lexical form only normalises ``..`` textually (symlinks are not
    followed); the resolved form follows symlinks. The caller scans a
    file if EITHER form is in scope, so a symlink inside a scanned dir
    whose target lies elsewhere is still scanned (fail closed).
    """

    def rel(candidate: Path) -> str | None:
        try:
            return candidate.relative_to(REPO_ROOT).as_posix()
        except ValueError:
            return None

    # Collapse ``..`` textually (pathlib keeps it; resolve() would
    # follow symlinks): ``a/link/../b`` -> ``a/b``.
    parts: list[str] = []
    for part in path.absolute().parts:
        if part == "..":
            if len(parts) > 1:
                parts.pop()
        elif part != ".":
            parts.append(part)
    lexical = rel(Path(*parts))
    return lexical, rel(path.resolve())


def _safe_print(line: str) -> None:
    """Print ``line`` without crashing on undecodable path bytes.

    Argv path arguments are decoded with surrogateescape, so an
    offender line built from a non-UTF-8 filename can carry lone
    surrogates that a strict stdout encoder refuses to write
    (``UnicodeEncodeError``). Round-tripping through the
    ``backslashreplace`` error handler turns any such surrogate into a
    literal, printable escape instead of crashing the hook.
    """
    encoding = sys.stdout.encoding or "utf-8"
    print(
        line.encode(encoding, "backslashreplace").decode(
            encoding, "backslashreplace"
        )
    )


def main(argv: list[str]) -> int:
    offenders: list[str] = []
    for arg in argv:
        path = Path(arg)
        if not path.suffix == ".py" or not path.is_file():
            continue
        in_repo = [r for r in _repo_relative(path) if r is not None]
        if in_repo and not any(
            r.startswith(ISOLATION_SCAN_DIRS) for r in in_repo
        ):
            continue
        offenders.extend(scan_file(path))
    if offenders:
        _safe_print(
            "Per-user isolation fence: user-data access without username:"
        )
        for offender in offenders:
            _safe_print(f"  {offender}")
        _safe_print(
            "Construct user-data services/sessions with the require_auth "
            "username (per-user encrypted DBs are the isolation boundary)."
        )
        return 1
    return 0


if __name__ == "__main__":
    sys.exit(main(sys.argv[1:]))