diff --git a/core/config.cpp b/core/config.cpp index 0db587fc..d9bf217f 100644 --- a/core/config.cpp +++ b/core/config.cpp @@ -769,7 +769,14 @@ int config_parse(int argc, char** argv, Config* c) { } else if (S("--host")) { if (!(v = need(i))) return RAD_E_INVAL; c->host = v; } else if (S("--port")) { if (!num(0, 65535, &c->port)) return RAD_E_INVAL; } - else if (S("--api-key")) { if (!(v = need(i))) return RAD_E_INVAL; c->api_key = v; } + else if (S("--api-key")) { + if (!(v = need(i))) return RAD_E_INVAL; + /* An empty key is no key: --api-key "" or --api-key "$UNSET_VAR" must not start a + * server that looks guarded and answers everything keyless. */ + if (!*v) { RAD_ERR("--api-key takes a non-empty key; an empty value guards nothing"); + return RAD_E_INVAL; } + c->api_key = v; + } else if (S("--served-model-name")) { if (!(v = need(i))) return RAD_E_INVAL; if (!*v) { RAD_ERR("--served-model-name takes a non-empty name"); return RAD_E_INVAL; } diff --git a/core/rad_internal.h b/core/rad_internal.h index 2b17bc83..a4c9e5c6 100644 --- a/core/rad_internal.h +++ b/core/rad_internal.h @@ -29,6 +29,12 @@ void log_emit(Log l, const char* file, int line, const char* fmt, ...) * the first. */ uint64_t log_last_line_ns(); +/* A copy safe to interpolate into a log line: every byte below 0x20 but '\t', and DEL, becomes + * '?'. Client-controlled text reaches the log — session labels, echoed request fields — and an + * unfiltered newline forges a line while an ESC sequence drives the terminal that reads it. + * Multi-byte UTF-8 is left alone; only control bytes are replaced. */ +std::string log_sanitize(const std::string& s); + /* Rank tags on log lines. Per-rank components (pools, planner, mover, sampler) print the same * line once per rank, so under TP each line needs its rank. LogRank sets the tag as a scope * because the printers are several layers down, where threading a rank argument through every diff --git a/core/server/admin.cpp b/core/server/admin.cpp index 85ad6ce1..8c607716 100644 --- a/core/server/admin.cpp +++ b/core/server/admin.cpp @@ -502,7 +502,9 @@ HttpResponse Server::handle_diagnostics(const HttpRequest& req) { return error_response(err_not_implemented( "the server was not given a diagnostics provider; this build has no engine attached"), req.peer); - if (opt_.api_key.empty() && !req.peer_is_loopback()) { + if (opt_.api_key.empty() && (!req.peer_is_loopback() || req.is_cross_origin())) { + /* A cross-origin browser is not a loopback client even when its own machine serves + * this port: the drive-by that guard exists to refuse. */ ApiError e; e.status = 403; e.type = "permission_error"; diff --git a/core/server/dashboard.cpp b/core/server/dashboard.cpp index e93fa793..32a2f185 100644 --- a/core/server/dashboard.cpp +++ b/core/server/dashboard.cpp @@ -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