~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
"""Auth must resolve before the notes body gate reads the request.
Every mutating notes route takes two dependencies::
username: str = Depends(require_auth),
body=Depends(_notes_json_body),
FastAPI collects dependencies by iterating the signature and solves them in
that order, so the parameter order decides which one runs first. It shipped
the other way round -- ``body`` first on all 24 routes -- which meant an
**unauthenticated** request was fully read and JSON-parsed before
``require_auth`` ran and answered 401.
That is worse here than it sounds, for three compounding reasons:
* ``_notes_json_body`` is ``async def``, so it runs ON the event loop, not
in the threadpool -- regardless of the endpoints themselves being ``def``.
* ``/notes/`` is the single entry in ``fastapi_app._LARGE_JSON_BODY_PREFIXES``,
so it is granted the 100 MB cap rather than the 16 MB default.
* ``json.loads`` measures ~110 ms/MB on adversarial-but-valid input, linearly,
so 100 MB is ~11 s of frozen event loop -- every other user's request, every
Socket.IO event and the health check included -- under single-worker uvicorn.
The slowapi rate limiter does not help: it decorates the endpoint function,
which FastAPI calls only AFTER dependency resolution, so even a request
destined for 429 pays the full parse. Nor does CSRF: ``/auth/csrf-token`` is
public and mints a token for any anonymous client.
On main this was safe -- ``@login_required`` was the outermost decorator, so
an unauthenticated request was rejected before any body was touched.
The first test is the durable one: it fails if any future notes route
declares the body gate ahead of auth. The second demonstrates on a
throwaway app that the ordering really is what decides it, so the first
test's premise cannot quietly stop being true.
"""
import ast
from pathlib import Path
import pytest
from local_deep_research.web.routers import notes as notes_module
NOTES_PATH = Path(notes_module.__file__).resolve()
def _annotated_aliases(tree):
"""Map ``NAME -> Annotated[...]`` for module-level ``NAME = Annotated[...]``
assignments, e.g. ``_NotesBody = Annotated[..., Depends(_notes_json_body)]``.
Routes reference the dependency through this alias
(``body: _NotesBody``) rather than spelling ``Annotated[...]`` inline, so
the scan below has to resolve it to see the wrapped ``Depends(...)``.
"""
aliases = {}
for node in tree.body:
if not (
isinstance(node, ast.Assign)
and len(node.targets) == 1
and isinstance(node.targets[0], ast.Name)
):
continue
value = node.value
head = value.value if isinstance(value, ast.Subscript) else None
is_annotated = (
isinstance(head, ast.Name) and head.id == "Annotated"
) or (isinstance(head, ast.Attribute) and head.attr == "Annotated")
if is_annotated:
aliases[node.targets[0].id] = value
return aliases
def _routes_with_both_deps():
"""Yield (func_name, body_index, auth_index) per decorated notes route."""
tree = ast.parse(NOTES_PATH.read_text(encoding="utf-8"))
aliases = _annotated_aliases(tree)
for node in tree.body:
if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
continue
if not any(
ast.unparse(d).startswith("router.") for d in node.decorator_list
):
continue
args = node.args.args
defaults = [None] * (len(args) - len(node.args.defaults)) + list(
node.args.defaults
)
body_i = auth_i = None
for i, (arg, default) in enumerate(zip(args, defaults)):
# A dependency may be spelled as a default (``x = Depends(f)``),
# inline ``Annotated`` (``x: Annotated[T, Depends(f)]``), or a
# module-level ``Annotated`` alias used as the annotation
# (``x: _NotesBody``) -- resolve the alias, then render both the
# (possibly-resolved) annotation and the default, so no spelling
# makes this scan blind.
annotation = arg.annotation
if isinstance(annotation, ast.Name) and annotation.id in aliases:
annotation = aliases[annotation.id]
rendered = " ".join(
ast.unparse(n) for n in (annotation, default) if n is not None
)
if "_notes_json_body" in rendered:
body_i = i
if "require_auth" in rendered:
auth_i = i
if body_i is not None and auth_i is not None:
yield node.name, body_i, auth_i
def test_auth_is_declared_before_the_body_gate_on_every_notes_route():
offenders = [
f"{name}: body at position {b}, require_auth at {a}"
for name, b, a in _routes_with_both_deps()
if b < a
]
assert not offenders, (
"A notes route reads and parses the request body before "
"authenticating. FastAPI solves dependencies in signature order, so "
"`username: str = Depends(require_auth)` must come BEFORE "
"`body=Depends(_notes_json_body)` -- otherwise an anonymous caller "
"can spend up to 100 MB of event-loop JSON parsing (~11 s) on a "
"request that ends in 401:\n " + "\n ".join(offenders)
)
def test_the_scan_found_the_notes_routes():
"""Premise guard: an empty scan would make the test above vacuous."""
found = list(_routes_with_both_deps())
assert len(found) >= 20, (
f"expected the notes router to expose many body-gated routes, "
f"found {len(found)} -- the guard above may be scanning nothing"
)
def test_a_body_gate_declared_first_really_does_run_first():
"""Executable proof of the premise, on a throwaway app.
Pins FastAPI's "dependencies resolve in signature order" behaviour, so
a future FastAPI upgrade that changed it would surface here rather
than silently making the guard above meaningless.
"""
fastapi = pytest.importorskip("fastapi")
from fastapi.testclient import TestClient
ran = []
async def body_dep(request: fastapi.Request):
payload = bytearray()
async for chunk in request.stream():
payload.extend(chunk)
ran.append("body")
return {}
def auth_dep():
ran.append("auth")
raise fastapi.HTTPException(status_code=401, detail="nope")
app = fastapi.FastAPI()
@app.post("/body-first")
def body_first(
body=fastapi.Depends(body_dep),
username: str = fastapi.Depends(auth_dep),
):
return {"ok": True}
@app.post("/auth-first")
def auth_first(
username: str = fastapi.Depends(auth_dep),
body=fastapi.Depends(body_dep),
):
return {"ok": True}
client = TestClient(app)
ran.clear()
assert client.post("/body-first", content=b'{"a": 1}').status_code == 401
assert ran == ["body", "auth"], (
"declaring the body gate first must read the body before auth -- "
f"got {ran}"
)
ran.clear()
assert client.post("/auth-first", content=b'{"a": 1}').status_code == 401
assert ran == ["auth"], (
"declaring auth first must short-circuit before the body is read "
f"-- got {ran}"
)
def test_large_bodies_are_parsed_off_the_event_loop():
"""The reorder alone still leaves an authenticated user able to stall."""
source = NOTES_PATH.read_text(encoding="utf-8")
assert "_parse_json_off_loop" in source
assert "asyncio.to_thread(json.loads" in source, (
"the notes body gate must hand a large json.loads to a worker "
"thread; on the event loop a 100 MB body is ~11 s of full-instance "
"freeze, and reordering auth only stops the ANONYMOUS case"
)