server: bound, gate and sanitize every request entry point

Prepared for exposing the dashboard and the API beyond the local machine. Each piece
answers a finding from the review; none changes what a legitimate client sees.

Parsing. Six entry points still called json::parse with no bound on the tree: the two
WebSocket sessions, the image form's loras field, the two re-parses of template kwargs,
the control-vector POST and the lora-admin body. A legal frame or body of brackets alone
built a tree whose teardown recurses once per level -- one worker stack, one process.
They all go through parse_json_bounded now, the body cap's own value and depth limits.
The same reasoning already refused deep bodies on the generation routes; frames and form
fields were the ones that never passed a gate.

Errors. ApiError::dump threw on invalid UTF-8, and URL-decoded params reach messages
without passing a JSON parser: a 404 becomes a 500, or worse a closed connection. The
dumps replace bad bytes instead of dying.

Logs. Session labels and echoed request text reach the log; an embedded newline forged a
line and an escape sequence drove the terminal reading it. log_sanitize filters control
bytes at the entry points -- labels, refusal logs, the exception handler -- while the
three loggers that print multi-line reports deliberately keep their newlines.

The browser. The dashboard page ships a CSP (its script talks only to its own origin)
and X-Frame-Options DENY; every response carries X-Content-Type-Options: nosniff. And
the loopback-only guards -- control-vector default, runtime LoRA, diagnostics -- now
treat a cross-origin browser as the remote client it is: a hostile page reaches them
through the victim's own browser, which is exactly loopback. Origin not matching Host
refuses; same-origin and non-browser traffic are untouched.

The gate. The API key's path exemptions are method-scoped (reads, plus the POST /ping of
the health-check convention), so an exemption can never ride a method no route answers.
An empty --api-key value is now a startup error rather than a server that looks guarded.

SSE. In the vendored httplib 0.46 an exception escaping a content provider aborts the
process; the provider ends the response instead. Upstream's 0.54 serve_guarded fixes the
cause -- the vendor lift is owed either way, and the catch is harmless belt until it.
This commit is contained in:
fl0l0u authored and StillDeadcode committed 2026-10-29 16:22:35 +01:00
1 parent a28221eb6a
commit b9c685a9c3
16 files changed
+245 -48

No files matched your search

+22 -8
View File
@@ -13,6 +13,7 @@
* collapsing while the pooled acceptance rate stays plausible.
*/
#include "server.h"
#include "oai.h"
#include "rad_internal.h"
@@ -57,8 +58,10 @@ HttpResponse Server::handle_control_vector(const HttpRequest& req) {
if (req.method == "POST") {
/* The default applies to every client's requests that name no choice, so a server
* started without --api-key takes the change from a loopback client only, as it does a
* runtime adapter load. */
if (opt_.api_key.empty() && !req.peer_is_loopback()) {
* runtime adapter load. A browser page on another origin does not qualify even when the
* victim's own browser makes it loopback: that is the drive-by this guard must refuse.
*/
if (opt_.api_key.empty() && (!req.peer_is_loopback() || req.is_cross_origin())) {
ApiError e;
e.status = 403;
e.type = "permission_error";
@@ -67,12 +70,12 @@ HttpResponse Server::handle_control_vector(const HttpRequest& req) {
"elsewhere";
return error_response(e, req.peer);
}
json body;
try {
body = json::parse(req.body.empty() ? "{}" : req.body);
} catch (const std::exception& e) {
return error_response(err_bad("body", std::string("not JSON: ") + e.what()), req.peer);
}
bool over = false, deep = false;
json body = parse_json_bounded(req.body.empty() ? "{}" : req.body, 0, &over, &deep);
if (body.is_discarded()) return error_response(err_bad("body", "not JSON"), req.peer);
if (over || deep)
return error_response(err_bad("body", "expected {\"enabled\": true|false} and "
"nothing deeper than that"), req.peer);
const auto it = body.find("enabled");
if (it == body.end() || !it->is_boolean())
return error_response(err_bad("enabled", "a boolean is required"), req.peer);
@@ -3669,6 +3672,17 @@ HttpResponse Server::handle_dashboard(const HttpRequest&) {
/* No caching: the page must match the binary's /stats shape, and a stale copy shows only
* blank panels. */
r.set_header("Cache-Control", "no-store");
/* The page is fixed markup that talks only to its own origin, and CSP is how that promise
* becomes enforceable: even a future markup bug could not phone an injected payload's
* findings home, and could not reach the key beyond this origin. script/style 'unsafe-inline'
* is the cost of shipping as one compiled-in literal with its own <style> and <script>;
* frame-ancestors keeps a hostile page from framing the Model view and clickjacking its
* guardrail toggle. */
r.set_header("Content-Security-Policy",
"default-src 'none'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; "
"connect-src 'self'; img-src 'self'; frame-ancestors 'none'; base-uri 'none'; "
"form-action 'none'");
r.set_header("X-Frame-Options", "DENY");
return r;
}