~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Unauthenticated-reachability census over the mounted FastAPI route table.
What this file is for
---------------------
Flask spelled authentication ``@login_required`` on the view function. The
FastAPI port re-spells it ``username: str = Depends(require_auth)`` in the
handler signature, by hand, on ~317 routes across 21 router modules. That is
a mechanical translation with no compiler behind it: a decorator that goes
missing does not break an import, does not break a template, and does not
break any test that only exercises the authenticated path. It just makes a
route reachable by anyone.
So this module answers two questions statically, over the WHOLE route table:
1. **Authentication presence.** Every mounted FastAPI route must depend on
``require_auth`` (directly or transitively) or be exactly one of the
reviewed entries in :data:`DECLARED_PUBLIC`. Set equality makes both a new
anonymous route and a stale public exception fail closed. Historical
Flask-to-FastAPI route and authentication parity belongs to
``tests/web/test_route_table_parity.py`` and its single reviewed snapshot;
this census deliberately does not duplicate that legacy table.
2. **The exemption intersection.** Three surfaces let a request skip a
control: the CSRF middleware's ``_SKIP_EXACT_PATHS`` /
``_SKIP_PATH_PREFIXES``, ``@limiter.exempt``, and ``/api/v1``'s
``require_api_access`` gate. An exemption on an *authenticated* route is a
tuning decision. An exemption on an *unauthenticated* route is an
unprotected, unmetered, anonymous entry point — that intersection is
enumerated explicitly and pinned
(:func:`test_no_unauthenticated_route_is_csrf_exempt`,
:func:`test_rate_limit_exemptions_on_unauthenticated_routes`,
:func:`test_api_v1_surface_is_gated_and_not_csrf_exempt`).
Relationship to the sibling census
----------------------------------
``test_cross_user_isolation_census.py`` established that the *routers* expose
exactly 10 unauthenticated routes and that all are legitimately public. This
file does not redo that argument. It differs in two ways that matter:
* it enumerates from ``fastapi_app._mount_all`` rather than globbing
``web/routers/*.py``, so it also covers the three routes registered directly
on the app object (``/``, ``/favicon.ico``, ``/static/{path:path}``) — which
is where two of the three ``@limiter.exempt`` unauthenticated routes live;
* its subject is auth PRESENCE over the mounted app, and the exemption
intersection, not identity threading.
Static, not executed
--------------------
Everything here is AST analysis of the installed package source. No app boot,
no TestClient, no database: a census must be exhaustive, and a request-driven
one can only ever cover the routes somebody remembered to request. The
package is located through ``importlib.util.find_spec``, so pointing
``PYTHONPATH`` at a mutated copy of ``local_deep_research`` re-runs the whole
census against that copy — which is how the negative control for this file
was exercised by hand (strip ``Depends(require_auth)`` from one handler; the
census names it).
Anti-vacuity
------------
Every headline assertion here is "this list is empty", which is exactly the
shape that passes when the analyzer silently resolves nothing. Three guards:
* :func:`test_route_table_is_mount_driven_and_complete` pins the mounted
module set, a floor on route count, and specific routes that must resolve.
* :func:`test_auth_closure_resolves_the_real_dependency_graph` pins the
transitive ``require_auth`` closure both ways — names that must be in it and
names that must not.
* :func:`test_census_flags_synthetic_vulnerable_handlers` is the positive
control: four hand-written handlers, each vulnerable in a different way, go
through the SAME functions the census uses and must each be flagged.
"""
from __future__ import annotations
# allow: no-sut-import — a static census over the production SOURCE. It
# deliberately does not IMPORT local_deep_research: importing boots routers,
# settings and the socket layer, and the census must read every mounted
# 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 this file's negative control is run).
import ast
import functools
import importlib.util
from dataclasses import dataclass, field
from pathlib import Path
# ---------------------------------------------------------------------------
# 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
WEB_DIR = PKG_ROOT / "web"
ROUTERS_DIR = WEB_DIR / "routers"
FASTAPI_APP = WEB_DIR / "fastapi_app.py"
CSRF_DEP = WEB_DIR / "dependencies" / "csrf.py"
#: The one dependency that means "authenticated". Everything else counts as
#: auth only by transitively depending on it.
AUTH_MODULE = "local_deep_research.web.dependencies.auth"
AUTH_ROOT = (AUTH_MODULE, "require_auth")
HTTP_METHODS = frozenset(
{"get", "post", "put", "patch", "delete", "head", "options", "api_route"}
)
#: Methods the CSRF middleware guards (mirrors ``_UNSAFE_METHODS``).
UNSAFE_METHODS = frozenset({"POST", "PUT", "PATCH", "DELETE"})
# ---------------------------------------------------------------------------
# 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):
"""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 _depends_target(default) -> str | None:
"""Return the callable name inside ``Depends(...)``/``Security(...)``."""
if (
isinstance(default, ast.Call)
and isinstance(default.func, ast.Name)
and default.func.id in ("Depends", "Security")
):
arg = default.args[0] if default.args else None
if isinstance(arg, ast.Name):
return arg.id
if isinstance(arg, ast.Attribute):
return arg.attr
return None
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``.
"""
if not isinstance(annotation, ast.Subscript):
return
head = annotation.value
is_annotated = (isinstance(head, ast.Name) and head.id == "Annotated") or (
isinstance(head, ast.Attribute) and head.attr == "Annotated"
)
if not is_annotated:
return
sl = annotation.slice
elts = sl.elts if isinstance(sl, ast.Tuple) else [sl]
# elts[0] is the wrapped type; everything after it is metadata.
yield from elts[1:]
def _depends_targets(default, annotation) -> 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.
"""
targets = []
t = _depends_target(default)
if t is not None:
targets.append(t)
for meta in _annotated_metadata(annotation):
t = _depends_target(meta)
if t is not None:
targets.append(t)
return targets
def _imports_of(tree: ast.Module, current_module: str) -> dict:
"""Map local binding name -> ``(absolute module, original name)``.
Walks the whole tree, not just the module body: these routers import
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
def _decorator_names(node) -> frozenset:
"""Bare names of every decorator on ``node`` (``limit``, ``exempt``...)."""
names = set()
for dec in node.decorator_list:
target = dec.func if isinstance(dec, ast.Call) else dec
if isinstance(target, ast.Attribute):
names.add(target.attr)
elif isinstance(target, ast.Name):
names.add(target.id)
return frozenset(names)
# ---------------------------------------------------------------------------
# Transitive require_auth closure, computed over the whole package
# ---------------------------------------------------------------------------
@functools.lru_cache(maxsize=1)
def _package_index():
"""``(functions, imports)`` keyed by absolute module name.
``functions`` maps ``(module, name) -> FunctionDef``; ``imports`` maps
``module -> {local name: (module, original name)}``.
"""
functions: dict[tuple[str, str], ast.AST] = {}
imports: dict[str, dict] = {}
for path in sorted(PKG_ROOT.rglob("*.py")):
try:
tree = _parse(path)
except (SyntaxError, UnicodeDecodeError): # pragma: no cover
continue
module = _module_name(path)
imports[module] = _imports_of(tree, module)
for node in ast.walk(tree):
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
functions[(module, node.name)] = node
return functions, imports
def _resolve(module: str, local_name: str, imports=None) -> tuple[str, str]:
"""Resolve a name used in ``module`` to its ``(module, name)`` origin.
``imports`` defaults to the package-wide index. It is passed explicitly
when analysing source that is not part of the installed package (the
positive control below), so that control resolves its ``from
..dependencies.auth import require_auth`` exactly as a real router does.
"""
if imports is None:
_, index = _package_index()
imports = index.get(module, {})
target = imports.get(local_name)
if target is not None and target[1] is not None:
return (target[0], target[1])
return (module, local_name)
@functools.lru_cache(maxsize=1)
def auth_closure() -> frozenset:
"""Every ``(module, name)`` that transitively depends on ``require_auth``.
``require_api_access`` (api_v1) is the real case: it declares
``username: str = Depends(require_auth)`` and returns that username after
an extra kill-switch check, so a route depending on it IS authenticated.
Computed as a fixpoint so any depth of chaining is covered.
"""
functions, _ = _package_index()
resolved = {AUTH_ROOT}
changed = True
while changed:
changed = False
for key, fn in functions.items():
if key in resolved:
continue
for _, default, annotation in _params(fn):
targets = _depends_targets(default, annotation)
if any(_resolve(key[0], t) in resolved for t in targets):
resolved.add(key)
changed = True
break
return frozenset(resolved)
def _requires_auth(module: str, dep_targets, imports=None) -> bool:
closure = auth_closure()
return any(_resolve(module, t, imports) in closure for t in dep_targets)
# ---------------------------------------------------------------------------
# The mounted route table
# ---------------------------------------------------------------------------
@dataclass(frozen=True)
class Route:
module: str # file basename, e.g. "notes.py"
func: str
lineno: int
method: str
path: str
dep_targets: tuple # every Depends(...) callable name, in order
decorators: frozenset
requires_auth: bool
module_path: str = field(default="", repr=False)
@property
def key(self) -> tuple:
return (self.method, self.path)
@property
def where(self) -> str:
return f"web/{self.module}:{self.lineno} {self.func}()"
@property
def limiter_exempt(self) -> bool:
"""``@limiter.exempt`` / ``@_limiter.exempt`` on the handler."""
return "exempt" in self.decorators
def _router_prefix(tree: ast.Module) -> str:
"""The ``prefix=`` of the module-level ``router = APIRouter(...)``."""
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 routes_from_tree(
tree: ast.Module,
label: str,
module: str,
prefix: str,
owner: str,
) -> list:
"""Every route registered on ``owner`` (``router`` or ``app``) in ``tree``.
``owner`` is the decorator receiver name, so this picks up both the
router-mounted routes and the handful registered directly on the ``app``
object inside ``fastapi_app.py``.
"""
out = []
imports = _imports_of(tree, module)
for node in ast.walk(tree):
if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
continue
route_decs = [
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
and isinstance(d.func.value, ast.Name)
and d.func.value.id == owner
]
if not route_decs:
continue
deps = tuple(
t
for _, default, annotation in _params(node)
for t in _depends_targets(default, annotation)
)
decorators = _decorator_names(node)
authed = _requires_auth(module, deps, imports)
for dec in route_decs:
raw = (
dec.args[0].value
if dec.args and isinstance(dec.args[0], ast.Constant)
else "<dynamic>"
)
out.append(
Route(
module=label,
func=node.name,
lineno=node.lineno,
method=dec.func.attr.upper(),
path=prefix + raw,
dep_targets=deps,
decorators=decorators,
requires_auth=authed,
module_path=module,
)
)
return out
@functools.lru_cache(maxsize=1)
def mounted_router_modules() -> tuple:
"""Router module basenames that ``_mount_all`` actually includes.
Read from the ``_router_modules`` list literal plus the ``api_v1`` router
imported directly at the top of ``_mount_all``. Enumerating from the mount
function rather than globbing ``routers/*.py`` means a module that is
present on disk but NOT mounted is not counted as covered, and a module
added to the mount list is picked up automatically.
"""
tree = _parse(FASTAPI_APP)
names: list[str] = []
for node in ast.walk(tree):
if not (
isinstance(node, ast.FunctionDef) and node.name == "_mount_all"
):
continue
for inner in ast.walk(node):
if isinstance(inner, ast.Assign) and any(
isinstance(t, ast.Name) and t.id == "_router_modules"
for t in inner.targets
):
for element in inner.value.elts:
names.append(element.elts[1].value.rsplit(".", 1)[-1])
elif isinstance(inner, ast.ImportFrom) and inner.module:
if inner.module.startswith("routers."):
names.append(inner.module.rsplit(".", 1)[-1])
return tuple(sorted(set(names)))
@functools.lru_cache(maxsize=1)
def route_table() -> tuple:
"""Every HTTP route the assembled app serves, mount-order independent."""
routes = []
for name in mounted_router_modules():
path = ROUTERS_DIR / f"{name}.py"
tree = _parse(path)
routes.extend(
routes_from_tree(
tree,
label=f"routers/{path.name}",
module=_module_name(path),
prefix=_router_prefix(tree),
owner="router",
)
)
app_tree = _parse(FASTAPI_APP)
routes.extend(
routes_from_tree(
app_tree,
label="fastapi_app.py",
module=_module_name(FASTAPI_APP),
prefix="",
owner="app",
)
)
return tuple(routes)
def unauthenticated_routes() -> tuple:
return tuple(r for r in route_table() if not r.requires_auth)
# ---------------------------------------------------------------------------
# Exemption surface 1: the CSRF middleware skip lists
# ---------------------------------------------------------------------------
def _const_strings(node) -> list:
"""Every string constant reachable from a literal container node."""
return [
n.value
for n in ast.walk(node)
if isinstance(n, ast.Constant) and isinstance(n.value, str)
]
@functools.lru_cache(maxsize=1)
def csrf_skip_sets() -> tuple:
"""``(exact_paths, path_prefixes)`` as declared in ``dependencies/csrf.py``.
Read from source rather than imported so that a mutated copy on
``PYTHONPATH`` is what gets analysed, and so widening either list shows up
here as a diff instead of silently loosening the middleware.
"""
tree = _parse(CSRF_DEP)
exact: list = []
prefixes: list = []
for node in tree.body:
if not isinstance(node, ast.Assign):
continue
for target in node.targets:
if not isinstance(target, ast.Name):
continue
if target.id == "_SKIP_EXACT_PATHS":
exact = _const_strings(node.value)
elif target.id == "_SKIP_PATH_PREFIXES":
prefixes = _const_strings(node.value)
return (frozenset(exact), tuple(prefixes))
def csrf_exempt(path: str, exact=None, prefixes=None) -> bool:
"""Exactly the middleware's own predicate (``CSRFMiddleware.__call__``)."""
if exact is None or prefixes is None:
exact, prefixes = csrf_skip_sets()
return path in exact or any(path.startswith(p) for p in prefixes)
# ---------------------------------------------------------------------------
# Declared public surface — asserted by EQUALITY
# ---------------------------------------------------------------------------
#: The complete set of routes that depend on no auth dependency, with the
#: reason each is safe. Asserted by set equality, so a newly unauthenticated
#: route fails the census whether or not anyone updates this list.
#:
#: The routers-only subset of this (everything except the last three) was
#: independently reviewed by the cross-user isolation census; the three
#: app-level entries are added here because this census enumerates from
#: ``_mount_all`` rather than from ``routers/*.py``.
DECLARED_PUBLIC = frozenset(
{
# Liveness probe. Process-level status only; the richer `resources`
# block inside is gated on an authenticated session.
("GET", "/api/v1/health"),
# Pre-login CSRF token issuance — cannot require a token to mint one.
("GET", "/auth/csrf-token"),
# Login / registration must work while logged out.
("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, touches no database.
("POST", "/auth/validate-password"),
# Logout and the session probe act 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}"),
# Registered directly on the app object in fastapi_app.py:
# the root page checks request.session itself and 302s to
# /auth/login when there is no username (same shape as main's
# app_factory.index, which also carried no @login_required).
("GET", "/"),
# Static assets. Public by definition on a self-hosted web app;
# both were unauthenticated AND @limiter.exempt on the Flask baseline.
("GET", "/favicon.ico"),
("GET", "/static/{path:path}"),
}
)
# ---------------------------------------------------------------------------
# Anti-vacuity guard 1: the route table itself
# ---------------------------------------------------------------------------
#: Router modules ``_mount_all`` includes. Pinned so a module silently
#: dropped from the mount list (its routes vanish) or added to it (its routes
#: must be censused) fails here rather than shrinking the census in silence.
EXPECTED_MOUNTED_MODULES = frozenset(
{
"api",
"api_v1",
"auth",
"benchmark",
"chat",
"context_overflow_api",
"followup",
"history",
"library",
"library_delete",
"library_search",
"metrics",
"news_flask_api",
"news_pages",
"notes",
"rag",
"research",
"scheduler",
"settings",
"unified_search",
"zotero",
}
)
#: Routes the analyzer MUST resolve, with the auth verdict it must reach.
#: Spread across ten modules and both registration styles (router and app),
#: so an import-resolution or prefix bug cannot leave the census "passing"
#: over a table it failed to build.
MUST_RESOLVE = {
("POST", "/api/start_research"): True,
("GET", "/history/api"): True,
("PUT", "/settings/api/{key}"): True,
("GET", "/metrics/api/metrics"): True,
("POST", "/api/v1/quick_summary"): True,
("GET", "/library/api/documents"): True,
("POST", "/notes/api/notes"): True,
("GET", "/news/api/feed"): True,
("POST", "/benchmark/api/start"): True,
("GET", "/chat/{session_id}"): True,
("POST", "/auth/login"): False,
("GET", "/"): False,
("GET", "/static/{path:path}"): False,
}
MIN_ROUTES = 300
def test_route_table_is_mount_driven_and_complete():
"""Anti-vacuity: the census must have a real, full route table.
Every emptiness assertion below is only as good as this. Pins the mounted
module set, a floor on the number of routes, that every mounted module
contributes at least one, and that a spread of specific routes resolves to
the expected auth verdict.
"""
assert set(mounted_router_modules()) == EXPECTED_MOUNTED_MODULES
table = route_table()
assert len(table) >= MIN_ROUTES, (
f"only {len(table)} routes enumerated; the census would be nearly "
"vacuous. Did _mount_all or the APIRouter decorator shape change?"
)
by_module: dict[str, int] = {}
for route in table:
by_module[route.module] = by_module.get(route.module, 0) + 1
for name in EXPECTED_MOUNTED_MODULES:
label = f"routers/{name}.py"
assert by_module.get(label, 0) >= 1, (
f"{label} is mounted but contributed no routes to the census"
)
assert by_module.get("fastapi_app.py", 0) >= 3, (
"the routes registered directly on the app object (/, /favicon.ico, "
"/static/{path:path}) were not picked up"
)
resolved = {r.key: r.requires_auth for r in table}
missing = sorted(k for k in MUST_RESOLVE if k not in resolved)
assert not missing, f"census failed to resolve known routes: {missing}"
wrong = {
k: (expected, resolved[k])
for k, expected in MUST_RESOLVE.items()
if resolved[k] != expected
}
assert not wrong, f"auth verdict wrong (expected, got): {wrong}"
def test_auth_closure_resolves_the_real_dependency_graph():
"""Anti-vacuity: the transitive ``require_auth`` closure is real.
Pinned in BOTH directions. If the fixpoint over-approximated (everything
counts as authenticated) the census could never report a finding; if it
under-approximated it would report every route as a finding and someone
would "fix" it by loosening the check.
"""
closure = auth_closure()
routers = "local_deep_research.web.routers"
deps = "local_deep_research.web.dependencies.auth"
must_be_auth = {
(deps, "require_auth"),
# Depends(require_auth) + the app.enable_api kill-switch.
(f"{routers}.api_v1", "require_api_access"),
# Chained: get_settings_manager_dep -> get_db_session_dep ->
# require_auth. Proves the fixpoint follows more than one hop.
(deps, "get_db_session_dep"),
(deps, "get_settings_manager_dep"),
}
assert must_be_auth <= closure, (
f"auth closure lost known members: {sorted(must_be_auth - closure)}"
)
must_not_be_auth = {
# Returns request.session.get("username") or None; never raises.
(deps, "get_session_username"),
# A body-size/shape gate, not an auth gate.
(f"{routers}.notes", "_notes_json_body"),
# A settings kill-switch. Every route using it ALSO declares
# Depends(require_auth); on its own it authenticates nothing.
(f"{routers}.news_flask_api", "require_scheduler_control"),
}
overreach = must_not_be_auth & closure
assert not overreach, (
f"auth closure over-approximated, these are not auth "
f"dependencies: {sorted(overreach)}"
)
authed = [r for r in route_table() if r.requires_auth]
assert len(authed) >= MIN_ROUTES - len(DECLARED_PUBLIC), (
f"only {len(authed)} routes resolved as authenticated"
)
# ---------------------------------------------------------------------------
# Current unauthenticated surface and CSRF intersection
# ---------------------------------------------------------------------------
def test_no_unauthenticated_route_is_csrf_exempt():
"""The dangerous combination: no auth AND no CSRF token required.
A CSRF exemption on an authenticated route is a deliberate trade (the
caller proved identity some other way). On an *unauthenticated* mutating
route it means a cross-site form can drive the endpoint outright.
Also pins the two skip lists themselves, because widening either one is
how this becomes true later: ``_SKIP_PATH_PREFIXES`` uses ``startswith``,
so adding a short prefix silently exempts everything beneath it.
"""
exact, prefixes = csrf_skip_sets()
assert exact == frozenset({"/auth/csrf-token"}), (
f"CSRF exact-skip list changed: {sorted(exact)}. /auth/login, "
"/auth/register and /auth/validate-password must stay OFF it — "
"each renders a token via template injection, and exempting login "
"re-opens login-CSRF (OWASP A07)."
)
assert prefixes == ("/ws/", "/ws"), (
f"CSRF skip prefixes changed: {prefixes}. These are startswith "
"matches, so any addition exempts an entire subtree."
)
exempt_mutators = sorted(
(r.method, r.path, r.where)
for r in route_table()
if not r.requires_auth
and r.method in UNSAFE_METHODS
and csrf_exempt(r.path, exact, prefixes)
)
assert not exempt_mutators, (
"UNAUTHENTICATED **and** CSRF-EXEMPT state-changing routes:\n "
+ "\n ".join(f"{m} {p} ({w})" for m, p, w in exempt_mutators)
)
# The bare "/ws" prefix is a startswith match, so a route named e.g.
# /wsearch would ship CSRF-exempt. csrf.py documents this as a standing
# constraint; this is the check that enforces it.
stray = sorted(
r.path
for r in route_table()
if r.path.startswith("/ws") and r.path != "/ws"
)
assert not stray, (
f"routes under the bare '/ws' CSRF skip prefix: {stray}. Only the "
"Socket.IO ASGI mount may live there."
)
def test_unauthenticated_routes_are_exactly_the_declared_public_set():
"""Set equality, so a new unauthenticated route cannot slip in unnoticed.
Equality rather than a subset check in both directions: an entry that
stops being public (someone added auth) must also force a re-read of the
justification, because the comment beside it is now wrong.
"""
actual = frozenset(
(r.method, r.path) for r in route_table() if not r.requires_auth
)
new = sorted(actual - DECLARED_PUBLIC)
gone = sorted(DECLARED_PUBLIC - actual)
assert not new, (
"routes reachable without authentication that are not on the "
f"reviewed public list: {new}"
)
assert not gone, (
f"declared-public routes that no longer exist or gained auth: {gone}."
" Remove the stale entries (and their justifications) from "
"DECLARED_PUBLIC."
)
# ---------------------------------------------------------------------------
# Rate-limit and API-v1 exemption intersections
# ---------------------------------------------------------------------------
#: ``@limiter.exempt`` routes that are ALSO unauthenticated — the full
#: intersection, asserted by equality. Both are static asset handlers that
#: were exempt and unauthenticated on the Flask baseline too (``app_factory.favicon`` /
#: ``app_serve_static``). They read no database and return no user data;
#: ``/static`` resolves through ``PathValidator.validate_safe_path``.
UNAUTHENTICATED_AND_RATE_LIMIT_EXEMPT = frozenset(
{
("GET", "/favicon.ico"),
("GET", "/static/{path:path}"),
}
)
def test_rate_limit_exemptions_on_unauthenticated_routes():
"""Anonymous + unmetered is the combination worth naming.
``@limiter.exempt`` removes a route from the global default limit
entirely. On an authenticated route that costs a logged-in user their own
quota; on an unauthenticated one it is an unmetered anonymous endpoint.
Pinned by equality so a new exempt public route is a test failure.
"""
table = route_table()
exempt = [r for r in table if r.limiter_exempt]
assert len(exempt) >= 6, (
f"only {len(exempt)} @limiter.exempt routes found; the decorator "
"scan is not working, so the intersection below is vacuous"
)
intersection = frozenset(
(r.method, r.path) for r in exempt if not r.requires_auth
)
assert intersection == UNAUTHENTICATED_AND_RATE_LIMIT_EXEMPT, (
"the unauthenticated x rate-limit-exempt intersection changed.\n"
f" newly in it: "
f"{sorted(intersection - UNAUTHENTICATED_AND_RATE_LIMIT_EXEMPT)}\n"
f" no longer in it: "
f"{sorted(UNAUTHENTICATED_AND_RATE_LIMIT_EXEMPT - intersection)}"
)
# No exempt route may also be a mutator: an unmetered POST is a
# denial-of-service primitive whether or not it authenticates.
exempt_mutators = sorted(
(r.method, r.path, r.where) for r in exempt if r.method != "GET"
)
assert not exempt_mutators, (
f"@limiter.exempt on non-GET routes: {exempt_mutators}"
)
def test_api_v1_surface_is_gated_and_not_csrf_exempt():
"""``/api/v1`` authenticates by session cookie, so CSRF must apply.
On main the whole ``api_v1`` blueprint was ``csrf.exempt(...)``-ed and its
four endpoints carried no ``@login_required`` at all. The port gates them
with ``require_api_access`` (``require_auth`` plus the ``app.enable_api``
kill-switch) and leaves them under the CSRF middleware. That pairing is
the load-bearing part: cookie authentication with a CSRF exemption is
exactly the combination CSRF protects against, so if ``/api/v1`` is ever
moved to real token auth, the exemption and the auth change have to land
together.
"""
v1 = [r for r in route_table() if r.path.startswith("/api/v1")]
assert len(v1) >= 5, f"only {len(v1)} /api/v1 routes enumerated"
unauth = sorted((r.method, r.path) for r in v1 if not r.requires_auth)
assert unauth == [("GET", "/api/v1/health")], (
f"unexpected unauthenticated /api/v1 routes: {unauth}"
)
gated = {
(r.method, r.path) for r in v1 if "require_api_access" in r.dep_targets
}
assert gated == {
("GET", "/api/v1/"),
("POST", "/api/v1/quick_summary"),
("POST", "/api/v1/generate_report"),
("POST", "/api/v1/analyze_documents"),
}, f"the require_api_access kill-switch surface changed: {sorted(gated)}"
still_protected = [r for r in v1 if not csrf_exempt(r.path)]
assert len(still_protected) == len(v1), (
"a /api/v1 route is CSRF-exempt while authenticating by session "
"cookie: "
f"{sorted(r.path for r in v1 if csrf_exempt(r.path))}"
)
# ---------------------------------------------------------------------------
# Anti-vacuity guard 4: positive control
# ---------------------------------------------------------------------------
#: Four handlers, each vulnerable in a different way, written in exactly the
#: idiom this codebase uses. They are fed through the SAME functions the
#: census uses, at the same module path a real router would occupy (so the
#: relative import resolves to the real ``dependencies.auth``).
VULNERABLE_ROUTER_SOURCE = '''
from fastapi import APIRouter, Depends, Request
from ..dependencies.auth import require_auth, get_session_username
from ..dependencies.rate_limit import limiter
router = APIRouter(prefix="/vuln")
@router.post("/dropped-decorator")
def dropped_decorator(request: Request):
"""The migration failure: Depends(require_auth) simply not written."""
return {"ok": True}
@router.get("/optional-identity")
def optional_identity(username=Depends(get_session_username)):
"""Reads a username but never requires one -- looks authed, is not."""
return {"user": username}
@router.post("/unmetered")
@limiter.exempt
def unmetered(request: Request):
"""Unauthenticated AND removed from the global rate limit."""
return {"ok": True}
@router.post("/authed")
def authed(request: Request, username: str = Depends(require_auth)):
"""The control's control: this one is correct and must NOT be flagged."""
return {"user": username}
'''
SYNTHETIC_MODULE = "local_deep_research.web.routers._synthetic_control"
def _synthetic_routes():
tree = ast.parse(VULNERABLE_ROUTER_SOURCE)
return routes_from_tree(
tree,
label="routers/_synthetic_control.py",
module=SYNTHETIC_MODULE,
prefix=_router_prefix(tree),
owner="router",
)
def test_census_flags_synthetic_vulnerable_handlers():
"""Positive control: the census's own predicates must catch each defect.
Without this, every "the list is empty" assertion above could be empty
because the analyzer resolves nothing rather than because the code is
sound.
"""
routes = {(r.method, r.path): r for r in _synthetic_routes()}
assert len(routes) == 4, f"control source mis-parsed: {sorted(routes)}"
# 1. The missing-decorator case, which is the migration's actual risk.
assert not routes[("POST", "/vuln/dropped-decorator")].requires_auth
# 2. An optional-identity dependency must not read as authentication.
assert not routes[("GET", "/vuln/optional-identity")].requires_auth
# 3. Unauthenticated AND rate-limit exempt: the intersection test's
# predicate must select it.
unmetered = routes[("POST", "/vuln/unmetered")]
assert unmetered.limiter_exempt and not unmetered.requires_auth
# 4. The correct handler must NOT be flagged, or the census would report
# findings everywhere and get loosened.
assert routes[("POST", "/vuln/authed")].requires_auth
# And the two set-shaped census checks must actually name them.
public_leak = sorted(
(r.method, r.path)
for r in routes.values()
if not r.requires_auth and (r.method, r.path) not in DECLARED_PUBLIC
)
assert public_leak == [
("GET", "/vuln/optional-identity"),
("POST", "/vuln/dropped-decorator"),
("POST", "/vuln/unmetered"),
]
exempt_intersection = sorted(
(r.method, r.path)
for r in routes.values()
if r.limiter_exempt and not r.requires_auth
)
assert exempt_intersection == [("POST", "/vuln/unmetered")]
def test_census_flags_a_widened_csrf_skip_prefix():
"""Positive control for the exemption intersection.
A one-character widening of ``_SKIP_PATH_PREFIXES`` (``/auth`` instead of
the exact ``/auth/csrf-token``) exempts every unauthenticated auth
mutator. Run through :func:`csrf_exempt` — the same predicate the census
uses — it must select them.
"""
real_exact, real_prefixes = csrf_skip_sets()
widened = real_prefixes + ("/auth",)
caught = sorted(
(r.method, r.path)
for r in route_table()
if not r.requires_auth
and r.method in UNSAFE_METHODS
and csrf_exempt(r.path, real_exact, widened)
)
assert caught == [
("POST", "/auth/login"),
("POST", "/auth/logout"),
("POST", "/auth/register"),
("POST", "/auth/validate-password"),
], f"the widened-prefix control caught {caught}"
# With the real prefixes the same expression is empty -- which is the
# census result, and the reason the control above is needed.
assert not [
r
for r in route_table()
if not r.requires_auth
and r.method in UNSAFE_METHODS
and csrf_exempt(r.path, real_exact, real_prefixes)
]