From 41a1aa00febf29619e375a8510a328317fc05b56 Mon Sep 17 00:00:00 2001 From: fl0l0u Date: Sun, 15 Nov 2026 17:02:53 +0000 Subject: [PATCH] fleet: the admin routes read all three legs, as the engine's guards do The fleet front guarded its admin routes with the loopback socket alone. The socket is the one leg that forges: a DNS-rebinding page is same-origin with its victim's browser and dials through loopback, and a localhost proxy delivers remote clients on loopback sockets carrying the public Host. With CORS open by default, a web page in the operator's browser could unload and park any model, reload the fleet, and read the topology -- disk paths in the body -- from outside the machine. The three-leg rule (loopback socket, no foreign Origin, a Host that names this box) was Server::local_operator's, locked inside Server. Hoist it to server::local_operator(request, bind_host, local_hosts) beside the request whose fields it reads, keep the member as the delegate it now is, and let the fleet's guard call the very same predicate with its own config's host and --allow-host names. One rule, two callers, no forked wording. --- core/fleet/fleet.cpp | 19 ++++++++++++------- core/server/http.cpp | 30 ++++++++++++++++++++++++++++++ core/server/http.h | 6 ++++++ core/server/server.cpp | 21 +++------------------ core/server/server.h | 9 ++------- tests/server_test.cpp | 30 ++++++++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 32 deletions(-) diff --git a/core/fleet/fleet.cpp b/core/fleet/fleet.cpp index 430616b8..2895508f 100644 --- a/core/fleet/fleet.cpp +++ b/core/fleet/fleet.cpp @@ -2581,13 +2581,17 @@ json Fleet::status() { } void Fleet::add_routes() { - /* The fleet's own routes. Without a bearer key they answer loopback callers only, as the - * engine's other admin routes do. */ + /* The fleet's own routes. Without a bearer key they answer the local operator only, on the + * very rule the engine's other admin routes apply: loopback socket, no foreign Origin, and a + * Host that names this box. The socket alone forges -- a rebinding page or a localhost proxy + * is loopback for any caller -- so all three legs are read here too (docs/GUIDE.md). */ auto guard = [this](const server::HttpRequest& q) -> std::optional { - if (!fc_.models.front().cfg.api_key.empty() || q.peer_is_loopback()) return std::nullopt; + const Config& c = fc_.models.front().cfg; + if (!c.api_key.empty() || server::local_operator(q, c.host, c.allow_hosts)) + return std::nullopt; return server::HttpResponse::json_body( 403, server::error_body(q.path, 403, "permission_error", - "the fleet's admin routes answer loopback callers only " + "the fleet's admin routes answer the local operator only " "unless an api key is configured")); }; auto ok = [](json j) { return server::HttpResponse::json_body(200, j.dump()); }; @@ -2602,14 +2606,15 @@ void Fleet::add_routes() { * its finished requests and, as `live`, its latest /stats object; without it the fleet's, with * /admin/fleet's object as `live`. `since` (unix seconds) asks for what came after. Every byte * comes from the sampler's last read, so a page that polls this reads nothing of the engine: - * it neither brings a model back nor costs a running one anything. A model's history is as - * open as its /stats, the fleet's as /admin/fleet. */ + * it neither brings a model back nor costs a running one anything. Both shapes are dashboard + * telemetry -- per-model figures name their model and its activity -- so the guard runs + * before EITHER answer, exactly like the routes below. */ front_.route("GET", "/history", [this, guard, ok](const server::HttpRequest& q) { + if (auto g = guard(q)) return *g; double since = 0; if (auto it = q.query.find("since"); it != q.query.end()) since = std::strtod(it->second.c_str(), nullptr); if (auto it = q.query.find("model"); it != q.query.end() && !it->second.empty()) return server::HttpResponse::json_body(200, history_.model_body(it->second, since)); - if (auto g = guard(q)) return *g; return server::HttpResponse::json_body(200, history_.fleet_body(since)); }); /* A command run by the scheduler thread, its answer awaited. */ diff --git a/core/server/http.cpp b/core/server/http.cpp index c56d3135..1cd32a1e 100644 --- a/core/server/http.cpp +++ b/core/server/http.cpp @@ -579,6 +579,36 @@ bool HttpRequest::host_is_loopback() const { return inet_pton(AF_INET6, h.c_str(), &v6) == 1 && IN6_IS_ADDR_LOOPBACK(&v6); } +/* The identity the keyless "loopback only" guards accept: the socket is loopback, the request + * carries no foreign Origin, and the Host names the loopback, one of `local_hosts` (--allow-host), + * or the address `bind_host` names. Two ways forge the socket alone: a DNS-rebinding page is + * same-origin with the server and its victim's browser *is* the loopback peer -- only the Host + * still tells which page asked for it -- and a localhost TLS proxy puts every remote client + * behind the loopback socket, carrying the public Host. An attacker at the console of this + * machine is a local user either way, and stays admitted. Server::local_operator is the member + * form; the fleet's front calls this one directly (fleet.cpp). */ +bool local_operator(const HttpRequest& req, const std::string& bind_host, + const std::vector& local_hosts) { + if (!req.peer_is_loopback() || req.is_cross_origin()) return false; + if (req.host_is_loopback()) return true; + const std::string host = req.host_authority(); + if (host.empty()) return false; /* a socket always carries a Host; the in-process case + passed host_is_loopback on its empty peer */ + for (const std::string& a : local_hosts) { + std::string want = a; + for (char& ch : want) ch = (char)std::tolower((unsigned char)ch); + if (want == host) return true; + } + /* Bound to one concrete address, the box answers to that name on itself. */ + std::string bind = bind_host; + for (char& ch : bind) ch = (char)std::tolower((unsigned char)ch); + if (!bind.empty() && bind.front() == '[' && bind.back() == ']') + bind = bind.substr(1, bind.size() - 2); + if (!bind.empty() && bind != "0.0.0.0" && bind != "::" && bind != "*") + return bind == host; + return false; +} + /* The API key a browser's socket carries. A browser cannot set a header on a WebSocket, so * OpenAI's browser clients offer it as a subprotocol, `openai-insecure-api-key.`, beside * `realtime`; the subprotocol chosen is never that one. Compared in constant time, as the header diff --git a/core/server/http.h b/core/server/http.h index c7b4a506..8b51c63d 100644 --- a/core/server/http.h +++ b/core/server/http.h @@ -89,6 +89,12 @@ bool cidr_valid(const std::string& cidr); * happens to embed it. */ bool ip_in_cidr(const std::string& addr, const std::string& cidr); +/* The identity the keyless "loopback only" guards accept -- the three legs the engine's admin + * routes read (see the definition in http.cpp for why the socket alone forges). Every guard that + * answers loopback callers keyless reads this, the fleet's front included. */ +bool local_operator(const HttpRequest& req, const std::string& bind_host, + const std::vector& local_hosts); + /* The caller a proxy chain vouches for. `raw_addr` is the socket peer (no port), `xff` the * request's X-Forwarded-For value ("" when there is none), `trusted` the vouched CIDRs. * Nothing is read from the header unless the socket itself belongs to a vouched CIDR -- an diff --git a/core/server/server.cpp b/core/server/server.cpp index 64556c7a..0d8c7c9f 100644 --- a/core/server/server.cpp +++ b/core/server/server.cpp @@ -62,24 +62,9 @@ Server::Server(const Deps& d, const ServerOptions& o) } bool Server::local_operator(const HttpRequest& req) const { - if (!req.peer_is_loopback() || req.is_cross_origin()) return false; - if (req.host_is_loopback()) return true; - const std::string host = req.host_authority(); - if (host.empty()) return false; /* a socket always carries a Host; the in-process case - passed host_is_loopback on its empty peer */ - for (const std::string& a : opt_.local_hosts) { - std::string want = a; - for (char& ch : want) ch = (char)std::tolower((unsigned char)ch); - if (want == host) return true; - } - /* Bound to one concrete address, the box answers to that name on itself. */ - std::string bind = opt_.host; - for (char& ch : bind) ch = (char)std::tolower((unsigned char)ch); - if (!bind.empty() && bind.front() == '[' && bind.back() == ']') - bind = bind.substr(1, bind.size() - 2); - if (!bind.empty() && bind != "0.0.0.0" && bind != "::" && bind != "*") - return bind == host; - return false; + /* The three-leg rule itself lives beside the request it reads (server::local_operator, + * http.cpp) so the fleet's front applies the very same one. */ + return server::local_operator(req, opt_.host, opt_.local_hosts); } Server::~Server() { stop(); } diff --git a/core/server/server.h b/core/server/server.h index 55a6b518..8f3f1a3f 100644 --- a/core/server/server.h +++ b/core/server/server.h @@ -257,13 +257,8 @@ public: /* Renders each --reasoning-effort-map target as a request's effort. Empty when the template * takes all of them; otherwise the first refusal, naming the target. */ std::string check_reasoning_effort_map() const; - /* The identity the keyless "loopback only" guards accept: the socket is loopback, the - * request carries no foreign Origin, and the Host names the loopback, one of - * ServerOptions::local_hosts, or the address --host names. Two ways forge the socket alone: - * a DNS-rebinding page is same-origin with the server and its victim's browser *is* the - * loopback peer — only the Host still tells which page asked for it — and a localhost TLS - * proxy puts every remote client behind the loopback socket, carrying the public Host. An - * attacker at the console of this machine is a local user either way, and stays admitted. */ + /* The identity the keyless "loopback only" guards accept: loopback socket, no foreign + * Origin, and a Host that names this box (server::local_operator, http.cpp). */ bool local_operator(const HttpRequest& req) const; void stop(); /* stop() without the transport: for a server behind a Front (front.h), which owns the socket. diff --git a/tests/server_test.cpp b/tests/server_test.cpp index 781581cf..d0e84ea8 100644 --- a/tests/server_test.cpp +++ b/tests/server_test.cpp @@ -2896,6 +2896,36 @@ TEST(the_models_root_path_follows_the_key) { std::string("/models/m.rad")); } +/* The predicate every keyless "loopback only" guard reads -- hoisted so the fleet's front applies + * the very rule the engine's routes do (fleet.cpp: the admin/fleet guard). The three legs: + * loopback socket, no foreign Origin, a Host that names this box. */ +TEST(local_operator_reads_all_three_legs) { + const std::vector pinned = {"box.example"}; + auto ask = [&](const char* peer, const char* host, const char* origin, + const char* bind) { + HttpRequest q; + q.peer = q.raw_peer = peer; + if (host) q.headers["Host"] = host; + if (origin) q.headers["Origin"] = origin; + return local_operator(q, bind, pinned); + }; + /* The machine dialling itself: loopback socket, loopback Host. */ + CHECK(ask("127.0.0.1:50122", "localhost:8000", nullptr, "0.0.0.0")); + /* A pinned --allow-host name and a concrete --host bind answer for the box. */ + CHECK(ask("127.0.0.1:50122", "box.example:8000", nullptr, "0.0.0.0")); + CHECK(ask("127.0.0.1:50122", "10.3.1.5", nullptr, "10.3.1.5")); + /* The rebinding page: same-origin with itself, so no Origin leg fires -- the Host leg is + * what refuses it. */ + CHECK(!ask("127.0.0.1:50122", "evil.test:8000", "http://evil.test:8000", "0.0.0.0")); + /* The localhost proxy: every remote client arrives on a loopback socket carrying the + * public Host, and no leg names that name. */ + CHECK(!ask("127.0.0.1:50122", "public.example:8000", nullptr, "0.0.0.0")); + /* A foreign Origin never rides a loopback socket into a guard, whatever the Host says. */ + CHECK(!ask("127.0.0.1:50122", "localhost:8000", "http://evil.test", "0.0.0.0")); + /* A remote socket is no operator, however it names itself. */ + CHECK(!ask("192.168.1.20:50122", "localhost:8000", nullptr, "0.0.0.0")); +} + TEST(the_model_entry_and_server_info_report_the_same_capabilities) { Fixture f; const json info = json::parse(f.get("/server_info").body)["capabilities"];