~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
"""
REST API v1 router for Local Deep Research.
Ports Flask's api.py blueprint to FastAPI APIRouter.
Provides health check and programmatic research endpoints.
"""
import inspect
import os
import threading
import time
from typing import Any, Dict, Optional, Annotated
try:
import resource as _resource_mod
except ImportError:
_resource_mod = None # Windows: no POSIX resource limits
from fastapi import APIRouter, Depends, HTTPException, Request
from fastapi.responses import JSONResponse
from loguru import logger
from ...api.research_functions import analyze_documents
from ...database.session_context import get_user_db_session
from ...security.log_sanitizer import sanitize_error_for_client
from ...settings.exceptions import SettingsValueError
from ...utilities.db_utils import get_settings_manager
from ..dependencies.auth import get_session_username, require_auth
from ..dependencies.json_body import is_json_request
from ..dependencies.rate_limit import (
API_RATE_LIMIT_DEFAULT,
api_rate_limit,
set_request_api_rate_limit,
)
from ..dependencies.threadpool import run_db_sync, run_research_sync
from ..routes.research_validation import (
ITERATIONS_MAX,
ITERATIONS_MIN,
QUESTIONS_PER_ITERATION_MAX,
QUESTIONS_PER_ITERATION_MIN,
)
router = APIRouter(prefix="/api/v1", tags=["API v1"])
# Match the largest strategy-layer cap (_AGENT_ERROR_MAX_LEN = 500 in
# security/log_sanitizer.py) at the HTTP boundary so a message already
# scrubbed at the strategy layer is never re-truncated here, and
# categorizable exception tokens (e.g. "Connection refused" sitting deep in a
# long error) survive to the API client. The 200-char default of
# sanitize_error_for_client would truncate that signal prematurely.
_ERROR_BOUNDARY_MAX_LEN = 500
def _scrub_error_fields(results: Dict[str, Any]) -> None:
"""In-place defense-in-depth scrub for exception-derived fields about to
leave the API (CWE-209, CodeQL #8019).
Strategy-layer ``sanitize_error_for_agent``/``sanitize_error_for_client`` already
wraps exception text at the source; this is the final HTTP boundary. Only
fires on fields that start with the literal ``"Error:"`` marker so
legitimate research prose is never touched, and truncation is from the tail
so the marker survives the scrub.
Field names: strategies emit error text into ``current_knowledge``, but
``quick_summary()`` (research_functions.py) returns that value under the key
``summary`` — and ``analyze_documents()`` uses ``summary`` as well — so both
spellings are scrubbed here. Ports the Flask fix from #5032.
"""
if not isinstance(results, dict):
return
for field in ("current_knowledge", "summary", "formatted_findings"):
value = results.get(field)
if isinstance(value, str) and value.startswith("Error:"):
results[field] = sanitize_error_for_client(
value, max_length=_ERROR_BOUNDARY_MAX_LEN
)
for finding in results.get("findings", []):
content = finding.get("content")
if isinstance(content, str) and content.startswith("Error:"):
finding["content"] = sanitize_error_for_client(
content, max_length=_ERROR_BOUNDARY_MAX_LEN
)
# Body params /analyze_documents accepts beyond the positional
# query/collection_name. Derived from the real signature so the two can't
# drift: analyze_documents (unlike quick_summary/generate_report) has no
# **kwargs, so an unknown key would TypeError at call time — surfacing as
# an opaque 500. Validating up front turns that into a clear 400.
# username/settings_snapshot are excluded because they are server-set by
# _load_user_context_into_params (and overwritten if a body supplied
# them).
#
# ``programmatic_mode`` is ALSO subtracted here, even though it is a
# genuine declared parameter of analyze_documents (unlike
# username/settings_snapshot it would otherwise survive the allowlist
# derivation untouched). It is the same identity/audit-plumbing knob
# documented at length on ``_IDENTITY_PLUMBING_PARAMS`` below for
# quick_summary/generate_report: ``_load_user_context_into_params`` sets
# ``params.setdefault("programmatic_mode", False)`` specifically so
# authenticated REST calls default to DB-backed metrics/rate-limit
# persistence, and ``setdefault`` means an explicit body value is
# respected rather than overridden — so leaving it in this
# signature-derived allowlist would let a caller opt their own
# analyze_documents calls out of the audit trail and DB-backed
# rate-limit accounting, the identical gap closed for quick_summary/
# generate_report via ``_REJECTED_BODY_PARAMS``. Rejecting it here (via
# subtraction, since analyze_documents' allowlist is derive-then-permit
# rather than the reject-list ``_reject_unsafe_body_params`` uses) closes
# it on this third endpoint too.
#
# ``output_file`` is ALSO subtracted here for the identical reason it is
# rejected on quick_summary/generate_report (see ``_SERVER_PATH_PARAMS``
# below): analyze_documents (research_functions.py) reaches the same
# ``write_file_verified`` sink — with no containment or traversal check,
# and a leaf-symlink refusal (O_NOFOLLOW) that does not cover
# parent-directory symlinks or hardlinks — after its own full
# search-and-LLM run, behind the same unset-by-default
# ``api.allow_file_output`` gate, and the same opaque catch-all 500 when
# that gate is (correctly) closed. A server-side filesystem path posted in
# a public JSON body is no more REST-shaped here than it is on the other
# two endpoints; there is no substantive reason for this third endpoint to
# disagree. Found and closed in the same post-merge review pass that added
# ``_SERVER_PATH_PARAMS`` — an earlier version of this module reported the
# gap here without closing it ("separately-scoped... out of scope"); that
# scoping call was wrong, since the fix is the same one-token subtraction
# already used for ``programmatic_mode`` right above.
#
# ``research_id``/``research_context`` are not declared parameters of
# analyze_documents at all (checked against the signature below), so
# there is nothing else in ``_IDENTITY_PLUMBING_PARAMS`` to subtract
# here today — but any future analyze_documents parameter matching one of
# those names should be audited the same way before being allowed
# through.
_ANALYZE_DOCUMENTS_PARAMS = (
frozenset(inspect.signature(analyze_documents).parameters)
- {"query", "collection_name", "username", "settings_snapshot"}
- {
"research_id",
"programmatic_mode",
"research_context",
"output_file",
}
)
# Request-body keys that quick_summary/generate_report accept as declared
# Python parameters but must NEVER be forwarded from the HTTP path.
# ``retrievers``/``llms`` are registered into the retriever registry
# (web_search_engines/retriever_registry.py) and the LLM registry
# (llm/llm_registry.py) the moment they reach the research function.
#
# This is NOT a cross-user problem. Both registries are namespaced per user
# (#5539), and the ``username`` used at those registration sites is
# server-set from the authenticated session (``require_api_access`` /
# ``_load_user_context_into_params`` below) — never read from the request
# body. A value posted here can therefore only land in the poster's OWN
# namespace, and reads resolve own-namespace-first without ever surfacing
# another user's entries.
#
# What rejecting these keys prevents is a self-inflicted type confusion. A
# JSON body can only carry scalars, never a live BaseRetriever /
# BaseChatModel, so every value that can arrive here is the wrong type: a
# string registers successfully and then fails later, when something tries
# to use it as a retriever; a dict raises ValueError out of
# register_multiple, which the endpoint's catch-all turns into an opaque
# 500. Either way the bad entry persists in that user's namespace for the
# process lifetime, so the user's own later requests keep tripping over it.
# A 400 naming the offending key is the honest answer.
#
# It is also a regression guard: if the per-user namespacing were ever
# removed or bypassed, unvalidated body values would again reach a shared
# registry. Filtering at the HTTP boundary keeps that regression
# unreachable from the public API, and costs nothing, because no legitimate
# caller can express these objects in JSON to begin with.
#
# These must be rejected EXPLICITLY, not via the _ANALYZE_DOCUMENTS_PARAMS
# signature-derived-allowlist pattern: that pattern works there only
# because analyze_documents has no ``retrievers``/``llms`` parameter;
# reusing that derivation here would ADMIT them, since they ARE declared
# parameters of quick_summary/generate_report.
#
# Only the HTTP body path is filtered: in-process Python-SDK / internal
# callers still pass ``retrievers=``/``llms=`` directly (with real objects)
# and are unaffected.
_REGISTRY_PARAMS = frozenset({"retrievers", "llms"})
# ``progress_callback`` is Callable-typed on ``generate_report``'s own
# signature, and reaches ``_init_search_system`` from ``quick_summary`` too
# via its **kwargs pass-through — quick_summary never declares the
# parameter itself, but nothing strips it before it lands in
# ``_init_search_system(progress_callback=...)``. A JSON body can only carry
# scalars/dicts/lists/null, never a live callable, so this is the same
# "wrong type reaches a registry" shape as retrievers/llms above, except it
# fails even more quietly: the value is simply assigned to
# ``system.progress_callback`` (search_system.py:set_progress_callback) and
# only blows up with "... object is not callable" deep inside the strategy
# loop, mid-research — the exact opaque 500 this module exists to prevent.
_CALLABLE_PARAMS = frozenset({"progress_callback"})
# ``openai_endpoint_url`` is declared on ``_init_search_system`` (not on
# quick_summary/generate_report themselves — it reaches them via their
# **kwargs pass-through, same route as progress_callback above) and is
# forwarded UNCONDITIONALLY: ``_init_search_system`` passes it straight to
# ``get_llm(openai_endpoint_url=...)`` with no gate. This is NOT in the same
# bucket as ``provider``/``temperature``/``max_search_results`` below —
# those three are dead because they are only read inside quick_summary's/
# generate_report's own ``if "settings_snapshot" not in kwargs:`` branch,
# which the REST path always skips (``_load_user_context_into_params``
# unconditionally sets ``params["settings_snapshot"]`` before the research
# function runs). ``openai_endpoint_url`` never passes through that branch
# at all, so it is live on every REST call.
#
# Traced effect (config/llm_config.py ``get_llm``, ~lines 120-132): when the
# resolved ``provider == "openai_endpoint"``, a non-null
# ``openai_endpoint_url`` OVERLAYS ``settings_snapshot["llm.openai_endpoint.
# url"]`` for that call — the exact settings key the user's stored
# ``llm.openai_endpoint`` provider (URL *and* its API key) is read from. A
# caller whose stored ``llm.provider`` is ``openai_endpoint`` can therefore
# redirect that run's prompts, and the account's already-configured
# endpoint API key, to ANY host of the caller's choosing, gated only by the
# egress-policy PEP (which treats ``openai_endpoint`` as non-local and does
# not pin it to the stored URL). This is real credential/endpoint steering,
# not a self-inflicted type-confusion footgun like the classes above — it
# must be rejected outright, not merely documented as a no-op.
_CREDENTIAL_STEERING_PARAMS = frozenset({"openai_endpoint_url"})
# ``research_id``, ``programmatic_mode``, and ``research_context`` are
# identity/audit plumbing that the REST path already manages itself;
# accepting a caller-supplied value only reopens control the server
# deliberately took away.
#
# - ``research_id``: a declared parameter of ``quick_summary`` (reached via
# plain kwargs on ``generate_report``, which never declares it). When
# absent, quick_summary generates a fresh UUID and returns it in the
# response body for correlation — a caller never needs to supply one on
# this path. Worse, the two research functions disagree on what a
# caller-supplied value affects: quick_summary threads it into BOTH
# ``search_context["research_id"]`` (what search_tracker.py's SearchCall
# rows key on) AND ``_init_search_system(research_id=...)`` (what
# token_counter.py's TokenUsage rows key on), but generate_report always
# mints its own fresh UUID for ``search_context`` while still forwarding
# any caller-supplied ``research_id`` from kwargs to ``_init_search_system``
# alone. A caller-controlled value on generate_report therefore
# split-brains the two metrics tables for that run — SearchCall rows land
# under the server ID, TokenUsage rows land under the caller's ID — with
# no way for the caller to know that in advance. Per-user DB isolation
# (#5539) keeps this inside the poster's own account, but it is a
# needless, caller-invisible way to corrupt one's own metrics history for
# zero REST-path benefit.
# - ``programmatic_mode``: not declared on quick_summary/generate_report at
# all (kwargs-only, reaching ``_init_search_system`` and then
# ``AdvancedSearchSystem``). ``_load_user_context_into_params`` below
# uses ``setdefault("programmatic_mode", False)`` specifically so
# authenticated REST calls default to DB-backed persistence. Because
# ``setdefault`` only sets a value that is ABSENT, an explicit
# caller-supplied ``programmatic_mode`` reaching that line would be
# respected rather than overridden — which is exactly the gap this
# ``_REJECTED_BODY_PARAMS`` entry closes, by rejecting the key with a 400
# before ``_load_user_context_into_params`` ever runs, so no
# caller-supplied value can reach that ``setdefault`` at all.
# ``AdvancedSearchSystem.__init__`` logs
# "Running in programmatic mode - database operations and metrics
# tracking disabled. Rate limiting, search metrics, and persistence
# features will not be available." when true (search_system.py). A
# caller who sets this on the REST path can make their own calls opt out
# of the audit trail and DB-backed rate-limit accounting the authenticated
# path exists to provide — the request still counts against the slowapi
# per-route limiter, but leaves nothing else behind. The REST path should
# be the one deciding this, not the request body.
# - ``research_context``: a declared parameter of ``_init_search_system``
# forwarded via **kwargs from both quick_summary and generate_report.
# quick_summary is safe: it unconditionally overwrites
# ``init_kwargs["research_context"] = search_context`` (its own
# server-built metrics dict) right before calling
# ``_init_search_system``, so a caller-supplied value in the body is
# always clobbered. generate_report has no equivalent overwrite — it
# forwards ``**kwargs`` straight into ``_init_search_system``, so a
# caller-supplied ``research_context`` reaches
# ``get_llm(research_context=...)`` (config/llm_config.py) untouched.
# That is a caller-controlled value of unconstrained JSON type reaching
# code that both mutates it (``research_context["context_limit"] = ...``)
# — a non-dict, e.g. a string or int, raises TypeError there and
# surfaces as an opaque 500 — and, if it is a dict, hands it to
# ``TokenCounter`` (metrics/token_counter.py), which reads
# ``username``/``user_password``/``research_query``/``research_mode``
# etc. straight out of it for the metrics rows it writes. A caller can
# therefore inject arbitrary metadata (including a password-shaped
# value) into their own metrics history, or 500 the request outright,
# from a key the REST path never needed exposed. Same shape as
# ``research_id``/``programmatic_mode`` above: server-managed identity
# plumbing, not a lever the request body should control.
# - ``research_mode`` is read straight out of ``**kwargs`` by
# quick_summary (``kwargs.get("research_mode", "quick")``) and written
# into the metrics ``search_context`` as the row's mode label, while
# generate_report hardcodes its own value — so a body-supplied value
# only mislabels the caller's metrics history. Server-managed, same
# tier.
_IDENTITY_PLUMBING_PARAMS = frozenset(
{"research_id", "programmatic_mode", "research_context", "research_mode"}
)
# Declared parameters of quick_summary/generate_report (or bare **kwargs
# keys they read) that have NO effect when posted over the public REST
# path, so accepting them only invites confusion or, if the invariant below
# is ever weakened, reopens the exact settings-injection class already
# excluded for username/settings_snapshot elsewhere in this module:
#
# - ``settings``/``settings_override``/``api_key``/``provider``/
# ``max_search_results`` are only ever consumed inside
# quick_summary's/generate_report's own
# ``if "settings_snapshot" not in kwargs:`` branch, to build a snapshot
# from scratch. On the REST path that branch is always skipped, because
# ``_load_user_context_into_params`` below unconditionally overwrites
# ``params["settings_snapshot"]`` with the caller's own server-loaded
# snapshot BEFORE the research function ever runs — so these five are
# dead weight in the request body, not levers a caller can pull.
#
# ``max_search_results`` cannot be rescued by threading it into the
# settings snapshot instead (the way ``search_tool`` already works).
# NOT because of a ``get_search`` default shadowing it — that was traced
# to the wrong function. ``research_functions.py`` imports ``get_search``
# from ``config/search_config.py`` (``from ..config.search_config import
# get_search`` at the top of the file), whose signature (``search_tool``,
# ``llm_instance``, ``username``, ``settings_snapshot``,
# ``programmatic_mode``) has NO ``max_results`` parameter at all, and
# which reads the effective value straight out of the snapshot
# (``get_setting_from_snapshot("search.max_results", 10, ...)``) — the
# 10-default there is a snapshot fallback, not a shadowing kwarg.
# ``_init_search_system`` calls this ``get_search`` without ever passing
# ``max_results``. (``search_engine_factory.get_search`` — a different
# function, same name, in the retriever branch of the web-search-engines
# package, imported into ``config/search_config.py`` as
# ``factory_get_search`` — also defaults ``max_results`` to 10, and IS
# one frame down this call path: ``config/search_config.get_search``
# calls it with ``**params``, where ``params["max_results"]`` is the
# value already read from the snapshot above. So its own default is
# simply always overridden by that snapshot value before it can matter;
# a reader chasing "get_search" by name alone does still land on a
# second, differently-shaped function, but that function is reachable,
# not dead — the earlier version of this comment claiming it is "never
# on this call path" was wrong.) The real reason is simpler: even a caller-supplied
# ``max_search_results`` threaded into the snapshot would need to land
# under the key ``search.max_results`` — a different name — to have any
# effect, and nothing on the REST path performs that rename; only the
# user's own stored ``search.max_results`` setting (already in the
# snapshot ``_load_user_context_into_params`` loads) ever reaches
# ``get_search``. Renaming/threading it is a real internal-API gap
# shared by the programmatic SDK, the web app, and the MCP server, out
# of scope for a REST-boundary fix and not safe to patch here.
# ``provider`` is not blocked this way (``_init_search_system``'s own
# default is ``None``, so ``get_llm`` does fall back to the snapshot)
# but is rejected anyway for symmetry: neither has ever been a
# documented REST parameter (unlike ``temperature`` — see
# ``_ACCEPTED_BUT_INEFFECTIVE_PARAMS`` below), so there is no
# backward-compatibility reason to keep accepting either.
# - ``user_password``/``metadata`` are not declared parameters at all; they
# are read directly out of **kwargs. ``user_password`` gets stashed
# verbatim into the metrics ``search_context`` dict (a password-shaped
# value written to the metrics store); ``metadata`` is read nowhere and
# silently dropped. Neither does anything useful over HTTP either.
#
# No legitimate REST caller can rely on a no-op, so rejecting these costs
# nothing — same argument as for _REGISTRY_PARAMS.
_DEAD_OR_CONFUSING_PARAMS = frozenset(
{
"settings",
"settings_override",
"api_key",
"user_password",
"metadata",
"provider",
"max_search_results",
}
)
# ``temperature`` is dead on quick_summary/generate_report for the exact
# same reason as ``_DEAD_OR_CONFUSING_PARAMS`` above: it is only read
# inside quick_summary's/generate_report's own
# ``if "settings_snapshot" not in kwargs:`` branch, which the REST path
# always skips, AND ``_init_search_system`` calls ``get_llm(temperature=``
# with its OWN hardcoded ``temperature: float = 0.7`` default (never
# ``None``), so even threading a caller's value into the settings snapshot
# instead would not rescue it — ``get_llm``'s
# ``if temperature is None: <fall back to settings_snapshot>`` branch never
# fires.
#
# Unlike the rest of that set, though, ``temperature`` is a PUBLICLY
# DOCUMENTED REST parameter: it was listed in this module's own
# ``GET /api/v1`` ``parameters`` dict for both endpoints, and release notes
# 1.8.1 tells callers migrating off the removed ``quick_summary_test``
# endpoint to "call /quick_summary with search_tool, iterations, and
# temperature set explicitly". Hard-400ing it (as the rest of
# ``_DEAD_OR_CONFUSING_PARAMS`` correctly does — no legitimate caller ever
# relied on those) would therefore break existing callers who followed our
# own documentation. So ``temperature`` gets the honest-and-non-breaking
# treatment instead: the request still succeeds, but the value is popped
# out of ``params`` before the research function is called (see
# ``_pop_ineffective_params``) — it truly cannot reach quick_summary/
# generate_report — and the response carries a ``warnings`` entry naming
# it and pointing callers who actually need temperature control at
# ``POST /api/v1/analyze_documents``, where it IS honored
# (``analyze_documents`` reads it directly, outside the dead
# ``settings_snapshot``-from-scratch branch above).
_ACCEPTED_BUT_INEFFECTIVE_PARAMS = frozenset({"temperature"})
# ``output_file`` is a declared parameter of ``generate_report`` — it was
# advertised in this module's own ``GET /api/v1`` documentation (the
# ``api_documentation`` route's ``parameters`` dict, below) prior to this
# fix; that entry has been removed since a rejected parameter should not
# also be advertised as accepted. It lets the caller name a server-side
# filesystem path to write the finished report to (``research_functions.py``
# ``generate_report`` -> ``write_file_verified`` -> ``file_write_verifier.py``).
#
# It was NOT in the audit that produced the rest of this module's classes,
# because that audit was scoped to keys reaching ``_init_search_system``
# (registry/callable/credential-steering/identity-plumbing/dead-settings —
# see each class above), and ``output_file`` never reaches
# ``_init_search_system`` at all; it is consumed directly inside
# ``generate_report`` after the report is built. Rejecting it here closes
# that gap rather than widening the earlier audit's own scope claim.
#
# Two failure modes, both real:
# - No ``defaults/`` entry sets ``api.allow_file_output``. Precisely:
# ``write_file_verified`` calls ``get_setting_from_snapshot`` with no
# ``default``, so a missing key raises ``NoSettingsContextError``
# (``config/thread_settings.py``); it is ``file_write_verifier.py``'s own
# ``except Exception: actual_value = None`` that turns this into ``None``,
# which then does not equal the required ``True``. Either way
# ``write_file_verified`` raises ``FileWriteSecurityError`` — fail-closed,
# correctly — but only AFTER the
# full research-and-report run has already completed. The endpoint's
# catch-all ``except Exception`` has no special case for
# ``FileWriteSecurityError``, so the caller pays the entire run's cost for
# an opaque 500 that never mentions the real cause.
# - If an operator has explicitly turned ``api.allow_file_output`` on,
# ``write_file_verified``'s write path (``file_write_verifier.py``) has
# no containment: no restriction to a configured output directory, no
# traversal check. It refuses a symlink at the destination leaf (via
# O_NOFOLLOW) but that refusal does not cover a symlinked parent
# directory or a hardlinked leaf. A caller-chosen path is otherwise
# written verbatim wherever the server process has filesystem access.
#
# A server-side filesystem path posted in a public JSON body is not a
# REST-shaped parameter — same "caller cannot legitimately express this
# safely" rationale as ``_REGISTRY_PARAMS``/``_CALLABLE_PARAMS`` above, just
# for a path instead of a live object, so it is rejected outright rather
# than merely validated early. Only the HTTP body path is affected: the
# in-process Python API is unchanged, and a programmatic caller who trusts
# their own filesystem access can still pass ``output_file=`` directly to
# ``generate_report()``.
_SERVER_PATH_PARAMS = frozenset({"output_file"})
# The full class of request-body keys that must never reach
# quick_summary/generate_report from the public REST path. See
# _REGISTRY_PARAMS, _CALLABLE_PARAMS, _CREDENTIAL_STEERING_PARAMS,
# _IDENTITY_PLUMBING_PARAMS, _DEAD_OR_CONFUSING_PARAMS, and
# _SERVER_PATH_PARAMS above for what each group protects against.
#
# This audit spans all THREE REST endpoints that forward a request body
# into a research function, not just these two:
# - quick_summary / generate_report (api_quick_summary / api_generate_report
# below) enforce this class via ``_reject_unsafe_body_params`` and this
# ``_REJECTED_BODY_PARAMS`` reject-list.
# - analyze_documents (api_analyze_documents below) uses a different
# mechanism — a signature-derived ALLOWlist (``_ANALYZE_DOCUMENTS_PARAMS``)
# rather than a reject-list, because it has no **kwargs to smuggle
# unlisted keys through. Members of ``_IDENTITY_PLUMBING_PARAMS`` that
# are also declared parameters of ``analyze_documents`` (currently just
# ``programmatic_mode`` — see the comment above
# ``_ANALYZE_DOCUMENTS_PARAMS``) are explicitly subtracted from that
# allowlist so the same identity/audit-plumbing protection applies there
# too, even though they never appear in this reject-list union.
# ``_REGISTRY_PARAMS``/``_CALLABLE_PARAMS``/``_CREDENTIAL_STEERING_PARAMS``
# need no equivalent subtraction: none of retrievers/llms/
# progress_callback/openai_endpoint_url is a declared parameter of
# analyze_documents, so the allowlist derivation already excludes them.
# ``output_file`` IS a declared parameter of ``analyze_documents`` too,
# and — like ``programmatic_mode`` above — is now explicitly subtracted
# from ``_ANALYZE_DOCUMENTS_PARAMS`` rather than admitted by the
# allowlist derivation: that endpoint's own write call reaches the
# identical ``write_file_verified`` sink as quick_summary/generate_report
# (see ``_SERVER_PATH_PARAMS`` above and the comment on
# ``_ANALYZE_DOCUMENTS_PARAMS`` itself), so there was no substantive
# reason to leave a third endpoint exposed once the other two were fixed.
#
# The scope of "traced" below is deliberately narrow: it covers every OTHER
# **kwargs-forwarded key that reaches ``_init_search_system`` from the REST
# body — NOT the full set of keys quick_summary/generate_report accept via
# **kwargs, which is what ``output_file`` above demonstrates. And "traced"
# here means privilege-safe (no host/credential steering, no cross-user
# reach, no audit-trail bypass), NOT type-safe. Three of these are
# confirmed footguns of the exact same *shape* as the now-rejected
# ``progress_callback``: a JSON body can send a value of the wrong scalar
# type, and the first consumer performs an unconditional, un-type-checked
# operation on it that AttributeErrors/TypeErrors — an opaque 500.
# ``model_name``/``search_strategy``/``search_tool`` are therefore
# additionally covered by ``_TYPE_VALIDATED_PARAMS``/
# ``_validate_param_types`` below, so the wrong-type case now gets a clean
# 400 at the boundary instead of reaching that crash. They are not
# rejected outright (unlike ``progress_callback``) because, sent with the
# right type, they are ordinary, legitimate, already-documented knobs:
#
# - ``model_name`` — selects which model string to request from the
# caller's OWN already-configured provider/API key; no host or
# credential is chosen by this value. It is explicitly documented as a
# public parameter of POST /api/v1/generate_report in
# ``api_documentation`` above.
# - ``search_strategy`` — selects which strategy class runs
# (source_based/modular/etc.), all under the same user's settings
# snapshot and egress policy; no different privilege or destination.
# - ``search_tool`` — selects which search engine to use, same
# already-configured-account shape as the two above. It is explicitly
# documented as a public parameter of POST /api/v1/quick_summary in
# ``api_documentation`` above, and was NOT type-validated by the
# original #5533 audit despite having a confirmed footgun of the exact
# same shape: a truthy non-string value (e.g. a list) survives the
# ``search_tool or get_setting_from_snapshot(...)`` fallback in
# ``config/search_config.get_search``, then reaches
# ``retriever_registry.get(name, ...)``
# (``web_search_engines/retriever_registry.py``) — a plain dict
# ``.get()`` call, which raises ``TypeError: unhashable type`` for a
# list, or for a dict that has no ``value`` key, instead of returning
# ``None``. Be precise about the dict case: ``config/search_config.py``
# has an explicit ``isinstance(tool, dict)`` branch that unwraps
# ``tool["value"]``, so ``{"search_tool": {"value": "wikipedia"}}`` was a
# normal 200 before this change and is a 400 after it. Falsy non-strings
# (``0``, ``false``, ``[]``, ``{}``) were likewise omission-equivalent
# 200s via ``research_functions.py``'s ``if not search_tool`` and are
# 400s now. Those spellings are undocumented — the contract is
# ``"search_tool": "Search engine to use"``, a string, and the
# ``{"value": ...}`` form is a settings-snapshot debug shim, not an API
# spelling — so tightening them is intended here, but it IS a behaviour
# change and is called out rather than left for someone to discover.
# Found in post-merge review of this follow-up; added here rather than
# left as another silent gap in a set whose own comment claims
# completeness.
#
# ``iterations``/``questions_per_iteration`` are forwarded into
# ``_init_search_system`` when present. Omitted keys stay omitted so the
# snapshot's ``search.iterations`` / ``search.questions_per_iteration``
# are used. When present, they are boundary-validated in
# ``_validate_param_types`` (finite ints, 1..100 for iterations, 1..20 for
# questions_per_iteration) before reaching downstream consumers.
_REJECTED_BODY_PARAMS = (
_REGISTRY_PARAMS
| _CALLABLE_PARAMS
| _CREDENTIAL_STEERING_PARAMS
| _IDENTITY_PLUMBING_PARAMS
| _DEAD_OR_CONFUSING_PARAMS
| _SERVER_PATH_PARAMS
)
def _reject_unsafe_body_params(data: Dict[str, Any]) -> Optional[JSONResponse]:
"""Reject request-body keys that a JSON body cannot legitimately carry,
that steer a live call to a caller-chosen endpoint/credential, that are
identity/audit plumbing the REST path already manages itself, or that
are silent no-ops (and only a source of confusion) over the public REST
path.
Returns a 400 ``JSONResponse`` the endpoint must return when any
``_REJECTED_BODY_PARAMS`` key is present in the body, or ``None`` when
the body is clean. See ``_REJECTED_BODY_PARAMS`` for the rationale.
"""
present = sorted(_REJECTED_BODY_PARAMS & set(data))
if present:
return JSONResponse(
{
"error": (
"The following parameter(s) are not accepted via the "
f"REST API: {', '.join(present)}. They either require "
"a live Python object a JSON body cannot carry "
"(retrievers/llms/progress_callback), steer the call's "
"LLM endpoint/credentials to a caller-chosen host "
"(openai_endpoint_url), are identity/audit plumbing the "
"REST path already manages itself "
"(research_id/programmatic_mode/research_context/"
"research_mode), name a server-side filesystem path a "
"public API body should not control (output_file), or "
"have no effect "
"when set from the REST path "
"(settings/settings_override/api_key/user_password/"
"metadata/provider/max_search_results); "
"pass them via the in-process Python API instead."
)
},
status_code=400,
)
return None
def _pop_ineffective_params(params: Dict[str, Any]) -> list:
"""Strip ``_ACCEPTED_BUT_INEFFECTIVE_PARAMS`` keys out of ``params`` in
place and return a caller-facing warning string per key removed.
Unlike ``_reject_unsafe_body_params``, this never blocks the request —
these keys are accepted for backward compatibility (see
``_ACCEPTED_BUT_INEFFECTIVE_PARAMS``) but popped here so they truly
cannot reach the research function, same end state as a rejected
param, just without breaking existing callers who send them.
"""
warnings = []
for key in sorted(_ACCEPTED_BUT_INEFFECTIVE_PARAMS & set(params)):
params.pop(key, None)
warnings.append(
f"'{key}' was accepted but has no effect on this endpoint and "
"was ignored; it only affects POST /api/v1/analyze_documents."
)
return warnings
# Forwarded **kwargs keys with a confirmed unconditional, un-type-checked
# operation performed on them by their first consumer (see the
# ``model_name``/``search_strategy``/``search_tool`` bullets in the
# ``_REJECTED_BODY_PARAMS`` comment above for the exact call sites;
# ``iterations``/``questions_per_iteration`` are validated separately
# below in ``_validate_param_types`` for integer type and range). A JSON
# body sending the wrong scalar type for one of these three
# reaches that operation and 500s — the same opaque-500 shape
# ``_ANALYZE_DOCUMENTS_PARAMS`` exists to prevent for analyze_documents,
# applied here to the keys on this reject-list path with a confirmed
# footgun rather than every forwarded key speculatively.
_TYPE_VALIDATED_PARAMS: Dict[str, type] = {
"model_name": str,
"search_strategy": str,
"search_tool": str,
}
# ``model_name``/``search_tool`` treat an explicit JSON ``null`` as
# indistinguishable from omitting the key entirely — both resolve from the
# settings snapshot exactly as if the caller had not sent the key. The two
# reach that outcome by different routes, and the distinction matters if
# either consumer is ever changed:
# - ``model_name``: ``config/llm_config.py``'s ``get_llm`` has an
# explicit ``if model_name is None`` branch.
# - ``search_tool``: control never reaches ``config/search_config.py``'s
# ``get_search`` at all for ``None``. ``research_functions.py``'s
# ``if not search_tool`` resolves ``search.tool`` from the snapshot
# first and skips ``get_search`` entirely when it is unset. (An earlier
# revision of this comment cited ``get_search``'s
# ``search_tool or <fallback>`` as the deciding line; that is the wrong
# frame — the outcome is the same, the mechanism is not.) Both were accepted (200) before this module's type
# validation existed; excluding them here from the type check keeps that
# true instead of turning a documented no-op spelling into a new 400.
# ``search_strategy`` gets no equivalent carve-out: its consumer default
# is the non-None string ``"source_based"`` (``_init_search_system``), so
# an EXPLICIT ``null`` overrides that default with a real ``None`` and
# reaches ``strategy_name.lower()`` (search_system.py) unconditionally —
# that was a genuine pre-existing 500 (not a no-op), and this module's 400
# for it is a real fix, not an over-rejection.
#
# NOTE: this is a type-level fix only. ``model_name: ""`` still passes
# ``isinstance(value, str)``, and an empty string is NOT ``None``, so it
# skips ``get_llm``'s null-sentinel branch entirely, falls through
# ``if model_name: model_name = model_name.strip()...`` unstripped, and
# still hits ``if not model_name or not model_name.strip(): raise
# ValueError(...)`` (config/llm_config.py) — a 500, not a 400. That is a
# separate, pre-existing value-level gap this type-only pass does not
# close.
_NULL_IS_SENTINEL_PARAMS = frozenset({"model_name", "search_tool"})
def _validate_param_types(data: Dict[str, Any]) -> Optional[JSONResponse]:
"""Reject request-body values whose JSON type cannot survive the
unconditional operation their first consumer performs on them. See
``_TYPE_VALIDATED_PARAMS`` for the covered keys and rationale, and
``_NULL_IS_SENTINEL_PARAMS`` for the keys where ``null`` is deliberately
let through as equivalent to omission rather than treated as a type
error.
Returns a 400 ``JSONResponse`` naming every offending key, or ``None``
when every present key already has an acceptable type.
"""
errors = []
for key, expected_type in _TYPE_VALIDATED_PARAMS.items():
if key not in data:
continue
value = data[key]
if value is None and key in _NULL_IS_SENTINEL_PARAMS:
continue
if not isinstance(value, expected_type):
errors.append(
f"'{key}' must be a {expected_type.__name__}, got "
f"{type(value).__name__} instead"
)
if "iterations" in data:
val = data["iterations"]
if val is not None:
if type(val) is not int or not (
ITERATIONS_MIN <= val <= ITERATIONS_MAX
):
errors.append(
f"'iterations' must be an integer between {ITERATIONS_MIN} and {ITERATIONS_MAX}"
)
if "questions_per_iteration" in data:
val = data["questions_per_iteration"]
if val is not None:
if type(val) is not int or not (
QUESTIONS_PER_ITERATION_MIN
<= val
<= QUESTIONS_PER_ITERATION_MAX
):
errors.append(
f"'questions_per_iteration' must be an integer between "
f"{QUESTIONS_PER_ITERATION_MIN} and {QUESTIONS_PER_ITERATION_MAX}"
)
if errors:
return JSONResponse({"error": "; ".join(errors)}, status_code=400)
return None
async def require_api_access(
request: Request,
username: Annotated[str, Depends(require_auth)],
) -> str:
"""Port of main's api_access_control decorator.
Enforces the per-user ``app.enable_api`` kill-switch (403 when the
user disabled API access) and pre-caches ``app.api_rate_limit`` for
the dynamic ``api_rate_limit`` shared limit so the limiter never does
a second DB read. Async on purpose: the cached value lives in a
ContextVar, and a sync dependency would run in the threadpool where
the ContextVar write could not propagate back to the request task.
"""
def _read_settings():
with get_user_db_session(username) as db_session:
sm = get_settings_manager(db_session, username)
return (
sm.get_bool_setting("app.enable_api", True),
sm.get_setting("app.api_rate_limit", API_RATE_LIMIT_DEFAULT),
)
api_enabled, rate_limit_value = await run_db_sync(_read_settings)
if not api_enabled:
raise HTTPException(status_code=403, detail="API access is disabled")
set_request_api_rate_limit(rate_limit_value)
return username
@router.get("/health")
def health_check(
username: Annotated[str | None, Depends(get_session_username)],
):
"""Health check endpoint (no auth required).
Always returns "ok" if the process can serve a request — but exposes
a `subsystems` dict so an orchestrator's deep-probe can additionally
inspect specific components. The top-level "status" stays "ok" so
existing liveness probes (and tests) keep passing; consumers that
care about subsystem health should look at `subsystems`.
File-descriptor and thread diagnostics (`resources`) are only
included for authenticated users, to avoid leaking process internals
to anonymous callers. The basic ``status``/``message``/``timestamp``
fields stay public so the Docker healthcheck (which only inspects the
HTTP status code) keeps working.
"""
subsystems: dict[str, str] = {}
# Research queue processor — the worker thread should be alive
try:
from ..queue.processor_v2 import queue_processor
thread = getattr(queue_processor, "thread", None)
alive = bool(
getattr(queue_processor, "running", False)
and thread
and thread.is_alive()
)
subsystems["queue_processor"] = "ok" if alive else "not_started"
except Exception:
subsystems["queue_processor"] = "error"
# Database manager — the singleton should be importable and respond
try:
from ...database.encrypted_db import db_manager
_ = db_manager.has_encryption
subsystems["db_manager"] = "ok"
except Exception:
subsystems["db_manager"] = "error"
diagnostics: Dict[str, Any] = {
"status": "ok",
"message": "API is running",
"timestamp": time.time(),
}
# Only expose subsystem + resource diagnostics to authenticated users.
# Anonymous callers get exactly the status/message/timestamp triple main
# exposed — enough for a container healthcheck, while not telling an
# unauthenticated prober whether the queue worker or the DB is degraded.
if username:
diagnostics["subsystems"] = subsystems
# File descriptor count (Linux only; /proc not available on macOS)
try:
fd_count = len(os.listdir("/proc/self/fd"))
except OSError:
fd_count = None
# FD soft/hard limits (POSIX)
soft_limit = hard_limit = None
if _resource_mod is not None:
try:
soft_limit, hard_limit = _resource_mod.getrlimit(
_resource_mod.RLIMIT_NOFILE
)
if soft_limit == _resource_mod.RLIM_INFINITY:
soft_limit = None
if hard_limit == _resource_mod.RLIM_INFINITY:
hard_limit = None
except (AttributeError, ValueError, OSError):
pass
thread_count = threading.active_count()
fd_usage_percent = (
round(fd_count / soft_limit * 100, 1)
if fd_count is not None
and soft_limit is not None
and soft_limit > 0
else None
)
diagnostics["resources"] = {
"fd_count": fd_count,
"fd_soft_limit": soft_limit,
"fd_hard_limit": hard_limit,
"fd_usage_percent": fd_usage_percent,
"thread_count": thread_count,
}
if fd_usage_percent is not None and fd_usage_percent > 70:
diagnostics["status"] = "warning"
diagnostics["message"] = (
f"High FD usage: {fd_count}/{soft_limit} ({fd_usage_percent}%)"
)
return diagnostics
@router.get("/")
@api_rate_limit
def api_documentation(
request: Request, username: Annotated[str, Depends(require_api_access)]
):
"""Provide documentation on available API endpoints.
Hand-written rather than derived from FastAPI's OpenAPI schema. Review
asked why; the answer is three things that are each checkable today.
*The generated schema is currently thinner than this dict, not richer.*
No route in this app declares a Pydantic request body or a
``response_model`` — every handler parses ``await request.json()`` by hand
and returns a bare dict — so ``app.openapi()`` renders the three research
POSTs with **no** ``requestBody`` member at all and a ``{}`` (any JSON) 200
schema. It therefore never mentions ``query``, ``collection_name`` or
``allow_default_settings``, let alone that the first two are required, and
it carries no securityScheme because access here is a session cookie
checked by ``require_api_access`` rather than a declared scheme. Until
these routes gain request/response models, this dict is the only place a
caller learns the request shape.
*And the generated schema is off by default.* ``openapi_url`` is gated on
``LDR_EXPOSE_DOCS`` in ``fastapi_app.py`` so a multi-user deployment does
not publish its entire surface unauthenticated: ``/openapi.json`` 404s on a
default install, while this route is served and API-gated.
*It is also curated, not exhaustive.* It advertises the three research
endpoints and deliberately omits ``/health`` and this route itself, and its
parameter prose (the ``allow_default_settings`` egress-policy note in
particular) states a security consequence no schema would infer from types.
Callers already parse this shape.
Hand-written means it can drift, so it cannot:
``tests/web/routers/test_api_v1_documentation_is_current.py`` fails if a
documented path is not served, or if a POST route is added here without
being advertised. If the routes ever do gain request/response models, that
test is the seam to converge on: the generated schema becomes the source of
truth and this body can be derived from it.
"""
return {
"api_version": "v1",
"description": "REST API for Local Deep Research",
"endpoints": [
{
"path": "/api/v1/quick_summary",
"method": "POST",
"description": "Generate a quick research summary",
"parameters": {
"query": "Research query (required)",
"search_tool": "Search engine to use (optional)",
"iterations": "Number of research cycles. If omitted, uses the account's stored search.iterations setting (optional)",
"allow_default_settings": "Set to true to proceed with default settings (and NO egress policy) when your stored settings cannot be loaded; default is to refuse with 503 (optional)",
"temperature": "Accepted for backward compatibility but has NO effect on this endpoint (dropped before the research call; a 'warnings' entry is returned when set) — use POST /api/v1/analyze_documents for temperature control (optional)",
},
},
{
"path": "/api/v1/generate_report",
"method": "POST",
"description": "Generate a comprehensive research report",
"parameters": {
"query": "Research query (required)",
"searches_per_section": "Searches per report section (optional)",
"model_name": "LLM model to use (optional)",
"allow_default_settings": "Set to true to proceed with default settings (and NO egress policy) when your stored settings cannot be loaded; default is to refuse with 503 (optional)",
"temperature": "Accepted for backward compatibility but has NO effect on this endpoint (dropped before the research call; a 'warnings' entry is returned when set) — use POST /api/v1/analyze_documents for temperature control (optional)",
},
},
{
"path": "/api/v1/analyze_documents",
"method": "POST",
"description": "Search and analyze documents in a local collection",
"parameters": {
"query": "Search query (required)",
"collection_name": "Local collection name (required)",
"max_results": "Maximum results to return (optional)",
"temperature": "LLM temperature (optional)",
"force_reindex": "Force collection reindexing (optional)",
"allow_default_settings": "Set to true to proceed with default settings (and NO egress policy) when your stored settings cannot be loaded; default is to refuse with 503 (optional)",
},
},
],
}
def _load_user_context_into_params(
params: Dict[str, Any],
username: str | None,
allow_default_settings: bool = False,
) -> Optional[JSONResponse]:
"""Mutate ``params`` in place to thread the authenticated user's context
down to the research-function call.
All authenticated REST endpoints share the same shape: the user has an
encrypted DB whose settings snapshot must be loaded and passed through,
so calls honor the user's stored API keys, model preference, search
tool, and other config — not just the application defaults plus
``LDR_*`` env vars that the programmatic-API fallback would produce.
Sets ``username``, ``settings_snapshot``, and (for authenticated
requests) ``programmatic_mode=False`` so DB-backed rate-limit
estimates persist across requests. Still uses ``params.setdefault(...)``
(not a plain assignment) for ``programmatic_mode`` rather than an
unconditional overwrite, but on every current caller that distinction
is now moot: all three REST endpoints reject an explicit
``programmatic_mode`` in the request body before this function ever
runs — quick_summary/generate_report via ``_REJECTED_BODY_PARAMS``
(400), analyze_documents via its allowlist subtraction in
``_ANALYZE_DOCUMENTS_PARAMS`` (400) — so ``params`` never contains the
key by the time ``setdefault`` runs here, and the request body can no
longer influence this value at all. ``setdefault`` (rather than a plain
assignment) is kept anyway so a non-REST caller of this helper that
pre-populates ``params["programmatic_mode"]`` itself is not silently
overridden.
Returns ``None`` on success. If the settings snapshot cannot be loaded,
fails CLOSED: returns a 503 ``JSONResponse`` the endpoint must return to
the caller. Continuing with an empty snapshot would resolve to the
permissive default egress scope, silently downgrading a configured
PRIVATE_ONLY / require-local user — bypassing the very boundary they
configured. ``allow_default_settings=True`` is the caller's CONSCIOUS
opt-in to proceed with defaults (empty snapshot, no egress policy)
instead; it is logged loudly so it is never silent.
An invalid configured setting (``SettingsValueError``) also returns a
503, with ``reason`` ``invalid_setting`` and an ``error`` naming the
key, its source and the reason but not the value. The opt-in does not
apply to it.
Runs sync SQLAlchemy — call from a threadpool, not the event loop.
"""
if not username:
logger.debug("No username in session, skipping settings snapshot")
params["settings_snapshot"] = {}
return None
params["username"] = username
params.setdefault("programmatic_mode", False)
try:
with get_user_db_session(username) as db_session:
if db_session is None:
logger.warning(f"No database session for user: {username}")
params["settings_snapshot"] = {}
return None
settings_manager = get_settings_manager(db_session, username)
snapshot = settings_manager.get_settings_snapshot()
params["settings_snapshot"] = snapshot
logger.debug(
f"Loaded settings snapshot for user '{username}' "
f"with {len(snapshot)} settings"
)
return None
except SettingsValueError as error:
# Handled before the generic branch so the unavailable-database
# opt-in below cannot waive invalid configuration. The diagnostic
# names the key, its source and the reason, never the value or the
# parser's own exception, so it is the one part of this exception
# that may reach the client. It is read into a string here, and the
# body is built from that string: the response-leakage guard
# (tests/web/routers/test_error_response_leakage.py) rejects any use
# of a bound exception inside a response body.
diagnostic = error.diagnostic
logger.bind(policy_audit=True).warning(
"Research refused because of an invalid setting: {}",
diagnostic,
user=username,
)
return JSONResponse(
{
"error": (
"The research was REFUSED because a configured "
f"setting is invalid. {diagnostic}"
),
"how_to_fix": (
"Correct or remove the named setting in the "
"deployment environment or the saved settings, then "
"retry. allow_default_settings does not bypass an "
"invalid setting."
),
"reason": "invalid_setting",
},
status_code=503,
)
except Exception:
# logger.exception captures the traceback so the root cause
# (e.g. SQLCipher decrypt failure, settings table corruption,
# missing column after a migration) is visible. Without this
# the downstream error misleads — looks like "no provider",
# really was "couldn't read user settings".
logger.exception("Failed to load user settings snapshot")
if allow_default_settings:
# Caller explicitly opted in to run without their settings.
# Proceed with defaults (empty snapshot → permissive scope).
# Logged loudly so it's never a silent downgrade.
logger.bind(policy_audit=True).warning(
"Settings snapshot failed to load; proceeding with "
"DEFAULT settings because allow_default_settings=true "
"— this run is NOT bound by the user's egress policy",
user=username,
)
params["settings_snapshot"] = {}
return None
return JSONResponse(
{
"error": (
"Your settings could not be loaded, so the "
"research was REFUSED to avoid silently "
"running without your privacy/egress policy "
"(which could send your data to the cloud "
"when you meant to keep it local)."
),
"how_to_fix": (
"This is usually transient — try again. If "
"it persists, your encrypted settings "
"database may be unavailable (e.g. a session "
"/ password issue), so re-authenticate. To "
"deliberately run with default settings and "
"NO egress policy, resend the request with "
'"allow_default_settings": true.'
),
"reason": "settings_unavailable",
},
status_code=503,
)
@router.post("/quick_summary")
@api_rate_limit
async def api_quick_summary(
request: Request,
username: Annotated[str, Depends(require_api_access)],
):
"""Generate a quick research summary via REST API."""
if not is_json_request(request):
return JSONResponse(
{"error": "Query parameter is required"}, status_code=400
)
try:
data = await request.json()
except Exception:
return JSONResponse({"error": "Invalid JSON body"}, status_code=400)
if not isinstance(data, dict):
# A JSON array/string/number passes the membership checks below but
# has no .get(), so it used to reach data.get() and 500. main's
# require_json_body enforced an object.
return JSONResponse(
{"error": "Request body must be a JSON object"}, status_code=400
)
if not data or "query" not in data:
return JSONResponse(
{"error": "Query parameter is required"}, status_code=400
)
query = data.get("query")
if not isinstance(query, str):
return JSONResponse(
{"error": "Query must be a string"}, status_code=400
)
# Reject the whole unsafe-forwarding class (retrievers/llms/
# progress_callback/openai_endpoint_url/research_id/programmatic_mode/
# settings/settings_override/api_key/user_password/metadata/provider/
# max_search_results/output_file) BEFORE building params. See
# _REJECTED_BODY_PARAMS for why these can't be forwarded from the HTTP
# path. ``temperature`` is handled separately below: it is a documented
# public parameter, so it is accepted (not 400ed) but stripped before
# it can reach quick_summary — see _ACCEPTED_BUT_INEFFECTIVE_PARAMS.
unsafe_param_error = _reject_unsafe_body_params(data)
if unsafe_param_error is not None:
return unsafe_param_error
# Cheap type check for the forwarded knobs with a confirmed
# unconditional operation on them (see _TYPE_VALIDATED_PARAMS): turns
# an opaque 500 into a clear 400.
type_error = _validate_param_types(data)
if type_error is not None:
return type_error
# Opt-in escape hatch for programmatic callers: when settings can't be
# loaded, proceed with defaults (empty snapshot → permissive scope) instead
# of failing closed (503). Default False (fail closed) so a configured
# PRIVATE_ONLY user is never silently downgraded; setting it true is a
# CONSCIOUS "I'm fine running without my settings/egress policy" choice.
# Excluded from ``params`` so it isn't forwarded to quick_summary().
# Strict ``is True`` (not bool()): for a security-boundary flag we only opt
# in on a real JSON ``true`` — not on a truthy string like "false"/"0".
allow_default_settings = data.get("allow_default_settings") is True
params: Dict[str, Any] = {
k: v
for k, v in data.items()
if k not in ("query", "allow_default_settings")
}
# search_tool and iterations deliberately have no default here: when
# omitted, _init_search_system reads the user's configured values from
# the settings snapshot. Defaulting iterations to 1 would clobber
# stored search.iterations. ``temperature`` is popped out (not
# defaulted) here — quick_summary only reads its own temperature
# argument in the branch that builds a settings_snapshot from scratch,
# which the REST path never takes; see _pop_ineffective_params.
param_warnings = _pop_ineffective_params(params)
error = await run_db_sync(
_load_user_context_into_params, params, username, allow_default_settings
)
if error is not None:
return error
from ...security.egress.policy import PolicyDeniedError
try:
from ...api.research_functions import quick_summary
# Keep long research calls off the default executor used by every
# authenticated request's database check. The dedicated dispatch
# still cleans up worker-owned sessions and thread context.
result = await run_research_sync(quick_summary, query, **params)
# Serialize Document objects
converted = result.copy()
for finding in converted.get("findings", []):
for i, doc in enumerate(finding.get("documents", [])):
finding["documents"][i] = {
"metadata": doc.metadata,
"content": doc.page_content,
}
# CWE-209 / CodeQL #8019: scrub exception-derived fields before the
# response leaves. See _scrub_error_fields for rationale.
_scrub_error_fields(converted)
if param_warnings:
converted["warnings"] = param_warnings
return converted
except TimeoutError:
logger.exception("Request timed out")
return JSONResponse(
{
"error": "Request timed out. Please try with a simpler query or fewer iterations."
},
status_code=504,
)
except PolicyDeniedError as exc:
# A STRICT blank-primary (or other policy) denial from
# _init_search_system / the factory PEP must surface as a curated
# 400, not the generic 500 below (which would swallow it before
# the global PolicyDeniedError handler can render it). Keeps the
# error contract consistent with POST /api/start_research.
reason = getattr(getattr(exc, "decision", None), "reason", "denied")
logger.bind(policy_audit=True).warning(
"POST /api/v1/quick_summary policy denied",
reason=reason,
)
return JSONResponse(
{"error": f"Egress policy refused this request: {reason}"},
status_code=400,
)
except Exception:
logger.exception("Error in quick_summary API")
return JSONResponse(
{
"error": "An internal error has occurred. Please try again later."
},
status_code=500,
)
@router.post("/generate_report")
@api_rate_limit
async def api_generate_report(
request: Request,
username: Annotated[str, Depends(require_api_access)],
):
"""Generate a comprehensive research report via REST API."""
if not is_json_request(request):
return JSONResponse(
{"error": "Query parameter is required"}, status_code=400
)
try:
data = await request.json()
except Exception:
return JSONResponse({"error": "Invalid JSON body"}, status_code=400)
if not isinstance(data, dict):
# A JSON array/string/number passes the membership checks below but
# has no .get(), so it used to reach data.get() and 500. main's
# require_json_body enforced an object.
return JSONResponse(
{"error": "Request body must be a JSON object"}, status_code=400
)
if not data or "query" not in data:
return JSONResponse(
{"error": "Query parameter is required"}, status_code=400
)
query = data.get("query")
if not isinstance(query, str):
return JSONResponse(
{"error": "Query must be a string"}, status_code=400
)
# Reject the whole unsafe-forwarding class (retrievers/llms/
# progress_callback/openai_endpoint_url/research_id/programmatic_mode/
# settings/settings_override/api_key/user_password/metadata/provider/
# max_search_results/output_file) BEFORE building params. See
# _REJECTED_BODY_PARAMS for why these can't be forwarded from the HTTP
# path. ``temperature`` is handled separately below: see
# api_quick_summary and _ACCEPTED_BUT_INEFFECTIVE_PARAMS.
unsafe_param_error = _reject_unsafe_body_params(data)
if unsafe_param_error is not None:
return unsafe_param_error
# Cheap type check for the forwarded knobs with a confirmed
# unconditional operation on them — see api_quick_summary and
# _TYPE_VALIDATED_PARAMS.
type_error = _validate_param_types(data)
if type_error is not None:
return type_error
# See api_quick_summary for the allow_default_settings semantics
# (opt-in escape hatch, strict ``is True``, excluded from params).
allow_default_settings = data.get("allow_default_settings") is True
params = {
k: v
for k, v in data.items()
if k not in ("query", "allow_default_settings")
}
params.setdefault("searches_per_section", 1)
# ``temperature`` is popped out (not defaulted) here: see
# api_quick_summary for why it's dead on the REST path.
param_warnings = _pop_ineffective_params(params)
error = await run_db_sync(
_load_user_context_into_params, params, username, allow_default_settings
)
if error is not None:
return error
from ...security.egress.policy import PolicyDeniedError
try:
from ...api.research_functions import generate_report
# See quick_summary: isolate long research from login DB checks.
result = await run_research_sync(generate_report, query, **params)
if (
result
and "content" in result
and isinstance(result["content"], str)
and len(result["content"]) > 10000
):
result["content"] = (
result["content"][:2000] + "... [Content truncated]"
)
result["content_truncated"] = True
# CWE-209 / CodeQL #8019: same boundary scrub as api_quick_summary.
# Today's payload ({content, metadata, file_path}) carries none of the
# scrubbed field names, so this is precautionary — it keeps the
# every-response-sink boundary policy intact if the payload ever grows
# error-carrying fields.
_scrub_error_fields(result)
if param_warnings and isinstance(result, dict):
result["warnings"] = param_warnings
return result
except TimeoutError:
return JSONResponse(
{"error": "Request timed out. Please try with a simpler query."},
status_code=504,
)
except PolicyDeniedError as exc:
# Same 500-vs-400 contract as api_quick_summary above.
reason = getattr(getattr(exc, "decision", None), "reason", "denied")
logger.bind(policy_audit=True).warning(
"POST /api/v1/generate_report policy denied",
reason=reason,
)
return JSONResponse(
{"error": f"Egress policy refused this request: {reason}"},
status_code=400,
)
except Exception:
logger.exception("Error in generate_report API")
return JSONResponse(
{
"error": "An internal error has occurred. Please try again later."
},
status_code=500,
)
@router.post("/analyze_documents")
@api_rate_limit
async def api_analyze_documents(
request: Request,
username: Annotated[str, Depends(require_api_access)],
):
"""Search and analyze documents in a local collection via REST API."""
if not is_json_request(request):
return JSONResponse(
{"error": "Both query and collection_name parameters are required"},
status_code=400,
)
try:
data = await request.json()
except Exception:
return JSONResponse({"error": "Invalid JSON body"}, status_code=400)
if not isinstance(data, dict):
# A JSON array/string/number passes the membership checks below but
# has no .get(), so it used to reach data.get() and 500. main's
# require_json_body enforced an object.
return JSONResponse(
{"error": "Request body must be a JSON object"}, status_code=400
)
if not data or "query" not in data or "collection_name" not in data:
return JSONResponse(
{"error": "Both query and collection_name parameters are required"},
status_code=400,
)
query = data.get("query")
collection_name = data.get("collection_name")
if not isinstance(query, str):
return JSONResponse(
{"error": "Query must be a string"}, status_code=400
)
if not isinstance(collection_name, str):
return JSONResponse(
{"error": "Collection name must be a string"}, status_code=400
)
# See api_quick_summary for the allow_default_settings semantics
# (opt-in escape hatch, strict ``is True``, excluded from params).
allow_default_settings = data.get("allow_default_settings") is True
params = {
k: v
for k, v in data.items()
if k not in ("query", "collection_name", "allow_default_settings")
}
unknown_params = sorted(set(params) - _ANALYZE_DOCUMENTS_PARAMS)
if unknown_params:
return JSONResponse(
{
"error": (
f"Unknown parameter(s) for analyze_documents: "
f"{', '.join(unknown_params)}"
),
"allowed_parameters": sorted(_ANALYZE_DOCUMENTS_PARAMS),
},
status_code=400,
)
error = await run_db_sync(
_load_user_context_into_params, params, username, allow_default_settings
)
if error is not None:
return error
from ...security.egress.policy import PolicyDeniedError
try:
# Local-collection search can also be long-running.
result = await run_research_sync(
analyze_documents, query, collection_name, **params
)
# CWE-209 / CodeQL #8019: same boundary scrub as api_quick_summary.
# analyze_documents returns error text under the `summary` key.
_scrub_error_fields(result)
return result
except PolicyDeniedError as exc:
# Same 500-vs-400 contract as api_quick_summary/generate_report:
# a STRICT blank-primary (or other policy) denial from get_llm /
# get_search must surface as a curated 400, not the generic 500.
reason = getattr(getattr(exc, "decision", None), "reason", "denied")
logger.bind(policy_audit=True).warning(
"POST /api/v1/analyze_documents policy denied",
reason=reason,
)
return JSONResponse(
{"error": f"Egress policy refused this request: {reason}"},
status_code=400,
)
except Exception:
logger.exception("Error in analyze_documents API")
return JSONResponse(
{
"error": "An internal error has occurred. Please try again later."
},
status_code=500,
)