Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

6.7 KB
Raw Blame
"""The request checks and the 400 response of main's `@require_json_body`.

Main gated 46 route handlers with ``security.decorators.require_json_body``,
a Flask decorator that rejected any request whose parsed body was not a
``dict``:

    data = request.get_json(silent=True)
    if not isinstance(data, dict):
        return jsonify(...), 400

The decorator has no FastAPI equivalent, so each application had to be
re-expressed by hand during the port, and most were not. A handler that
skipped it reads its body with ``await request.json()`` and then calls
``data.get(...)``, which raises ``AttributeError`` on a JSON array, string,
number or ``null`` — surfacing to the client as a **500 "Server error"**
where main returned a clean **400**. That is a real contract change: a
malformed client request now looks like a backend outage, in logs and in
monitoring alike.

``read_json_dict`` is the way to parse a JSON object body. It answers a body
that will not parse and a body that parses to something other than an
object with the same 400, so a call site cannot forget the parse failure
the way the hand-written ports did:

    data, err = await read_json_dict(request, ...)
    if err is not None:
        return err

The call must not sit inside a broad ``except``, which would swallow what
the helper lets through on purpose: the request-size limit's exception and a
client disconnect. Its docstring has the details.

``get_json(silent=True)`` also returned ``None`` when the Content-Type was
not a JSON media type, so the decorator refused a JSON-looking
``text/plain`` body as well. ``request.json()`` ignores the header, so that
check is separate here. ``is_json_request`` is the media-type check, and
``read_json_dict`` parses the body and checks its shape without looking at
the media type. A route that needs both calls ``is_json_request`` first.

``error_format`` reproduces main's three response shapes exactly, because
the front-end branches on them: ``success`` is checked by the JS that drives
the chat, RAG and library-search views, while ``status`` is what the
settings and ratings pages expect. Getting the shape wrong would turn a
handled validation error into an unhandled one in the browser, which is why
the format is passed per call site rather than unified.
"""

from typing import Literal

from fastapi.responses import JSONResponse

ErrorFormat = Literal["simple", "status", "success"]

DEFAULT_MESSAGE = "Request body must be valid JSON"


def is_json_request(request) -> bool:
    """Return whether the request's Content-Type is a JSON media type.

    Same rule as Werkzeug's ``Request.is_json``: the media type, meaning
    the part before any ``;``, stripped and lowercased, must be
    ``application/json`` or ``application/*+json``. A missing header does
    not qualify. Call it before parsing; ``read_json_dict`` does not check
    the media type.
    """
    request_content_type = request.headers.get("content-type", "").split(
        ";", 1
    )[0]
    request_content_type = request_content_type.strip().lower()
    return request_content_type == "application/json" or (
        request_content_type.startswith("application/")
        and request_content_type.endswith("+json")
    )


def json_body_error(
    error_format: ErrorFormat = "simple",
    error_message: str = DEFAULT_MESSAGE,
) -> JSONResponse:
    """Build the 400 main's ``require_json_body`` returned.

    Mirrors ``security/decorators.py`` on ``origin/main``:

    * ``simple``  → ``{"error": msg}``
    * ``status``  → ``{"status": "error", "message": msg}``
    * ``success`` → ``{"success": False, "error": msg}``
    """
    if error_format == "status":
        payload = {"status": "error", "message": error_message}
    elif error_format == "success":
        payload = {"success": False, "error": error_message}
    else:
        payload = {"error": error_message}
    return JSONResponse(payload, status_code=400)


async def read_json_dict(
    request,
    error_format: ErrorFormat = "simple",
    error_message: str = DEFAULT_MESSAGE,
):
    """Parse the request body as a JSON object, failing with main's 400.

    Returns ``(data, None)`` on success and ``(None, JSONResponse)`` on
    failure, so callers stay a plain ``if err is not None: return err``.

    It does NOT check the media type. The body is parsed whatever the
    Content-Type says, so a JSON object sent as ``text/plain`` is accepted.
    main's decorator refused that request as well, because
    ``get_json(silent=True)`` returned ``None`` for a non-JSON media type.
    A route that needs that behaviour calls ``is_json_request`` first.

    main's ``@require_json_body`` collapsed BOTH failure modes into one 400:
    a body that will not parse, and a body that parses to something other
    than an object. Porting the decorator away split them — handlers kept an
    ``isinstance(data, dict)`` check but let ``await request.json()`` raise,
    so malformed JSON escaped to the generic handler and surfaced as a **500**
    on 9 of the 11 news body endpoints. A 500 tells the caller (and paging
    alerts) that the server is broken, when the client simply sent bad bytes.

    Only a body the CLIENT got wrong is answered here, so the caller cannot
    forget the parse error the way the hand-written ports did. That is what
    ``request.json()`` raises for a body it cannot parse: ``ValueError``
    (``json.JSONDecodeError`` for bad syntax, ``UnicodeDecodeError`` for
    undecodable bytes, and the plain ``ValueError`` ``json.loads`` raises for
    an integer literal past ``sys.get_int_max_str_digits()``) and
    ``RecursionError`` (nesting deeper than the parser can follow). The
    optional-body cleanup route in ``routers/settings.py`` catches the same
    pair around its own parse.

    Everything else propagates, on purpose. ``request.json()`` also reads the
    request stream, so it is where transport failures surface:
    ``BodySizeLimitMiddleware`` enforces its JSON cap on a body streamed
    without a Content-Length by raising from the receive channel, and answers
    413 (and logs the rejection) once that exception reaches it; a dropped
    client surfaces as ``ClientDisconnect``, which the app-level handler in
    ``fastapi_app.py`` logs at info level and answers 499. A broad
    ``except Exception`` here turned an over-cap body into the route's 400
    envelope and kept the rejection out of the log. For the same reason a
    caller must not wrap this call in a broad ``except`` of its own, which
    would swallow those exceptions one frame up.
    """
    try:
        data = await request.json()
    except (ValueError, RecursionError):
        return None, json_body_error(error_format, error_message)
    if not isinstance(data, dict):
        return None, json_body_error(error_format, error_message)
    return data, None