From b83d53cf196119cdd9358e1df74c655590428124 Mon Sep 17 00:00:00 2001 From: fl0l0u Date: Sun, 15 Nov 2026 17:04:06 +0000 Subject: [PATCH] front: a refused request does not wake a model to be refused Every forwarded request leased its model, and the lease fired demand for a cold or parked one before any handler ran. On the guarded routes -- the lora admin, /diagnostics, /reset_prefix_cache, DELETE /sessions, the control vector -- the handler then answered 403 keyless: the wake had already asked the scheduler for a real load, which can fetch the repository and evict residents to make room. Loop one cheap GET over an enumerated model name and the fleet churns for an audience that never gets an answer. The front already knows reads that report on a model are no reason to bring one back; the same courtesy is now owed to a request the guard will refuse. The fleet hands the front its operator rule -- the very predicate its admin guard reads -- and a guarded route from anyone else reads the not-open state and stops. Open models still dispatch, keyed boxes still wake everything, and the operator keeps the diagnostics that bring a parked model forward. --- core/fleet/fleet.cpp | 6 +++++ core/server/front.cpp | 60 ++++++++++++++++++++++++++++++++++++++----- core/server/front.h | 20 ++++++++++++++- tests/front_test.cpp | 36 ++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 8 deletions(-) diff --git a/core/fleet/fleet.cpp b/core/fleet/fleet.cpp index 2895508f..e796ecf1 100644 --- a/core/fleet/fleet.cpp +++ b/core/fleet/fleet.cpp @@ -2824,6 +2824,12 @@ int Fleet::run() { if (required) preload.push_back(e.name); } front_.set_demand([this](const std::string& n) { demand(n); }); + /* The guarded routes wake models only for callers their own handlers would admit: the + * same three-leg rule add_routes' guard reads (fleet.cpp), keyed deployments always. */ + front_.set_operator_rule([this](const server::HttpRequest& q) { + const Config& c = fc_.models.front().cfg; + return !c.api_key.empty() || server::local_operator(q, c.host, c.allow_hosts); + }); front_.set_describe([this](const std::string& n) { return describe(n); }); front_.set_metrics([this] { return metrics(); }); add_routes(); diff --git a/core/server/front.cpp b/core/server/front.cpp index 2d0d1185..fa1fe96a 100644 --- a/core/server/front.cpp +++ b/core/server/front.cpp @@ -447,7 +447,7 @@ Front::Entry* Front::resolve(const std::string& model) { Front::Got Front::lease(const std::string& model, const std::string& path, int64_t wait_ms, uint64_t* until_ns, Entry** out, std::shared_ptr* held, - HttpResponse* refusal) { + HttpResponse* refusal, bool operator_ok) { std::unique_lock lk(mu_); *out = nullptr; Entry* e = resolve(model); @@ -490,8 +490,11 @@ Front::Got Front::lease(const std::string& model, const std::string& path, int64 return Got::Refused; } if (e->state == State::Failed) { + /* The failure prose quotes fetch errors and filesystem paths: operator-facing. A + * stranger learns the model failed -- that is public anyway -- never WHY (A-05). */ *refusal = refuse(path, 503, "service_unavailable", - "model '" + e->name + "' could not be loaded: " + e->why); + "model '" + e->name + "' could not be loaded" + + (operator_ok ? ": " + e->why : std::string())); const uint64_t t = rad_mono_ns(); refusal->set_header("Retry-After", std::to_string(e->retry_ns > t ? (e->retry_ns - t) / 1000000000ull + 1 : 1)); @@ -550,10 +553,39 @@ std::vector>> Front::hold_open() return held; } +/* The path Router::match actually dispatches on: split_path (http.cpp) drops empty segments, + * so "//stats" and "/stats/" ARE the route "/stats". Every guard that compares path text must + * read the canonical form, or a slash turns any exact match into a bypass. */ +static std::string path_canonical(const std::string& p) { + std::string out; + out.reserve(p.size()); + for (const char c : p) + if (c != '/' || out.empty() || out.back() != '/') out += c; + if (out.size() > 1 && out.back() == '/') out.pop_back(); + return out; +} + bool Front::dashboard_read(const HttpRequest& q) { if (q.method != "GET") return false; - return q.path == "/stats" || q.path == "/sessions" || q.path == "/graph" || - q.path == "/server_info" || q.path == "/admin/control-vector"; + const std::string p = path_canonical(q.path); + return p == "/stats" || p == "/sessions" || p == "/graph" || + p == "/server_info" || p == "/admin/control-vector"; +} + +/* The routes Server::register_routes guards with local_operator when no key is configured. + * handle_control_vector guards BOTH verbs the router registers for its path, so GET counts too: + * a keyless GET /admin/control-vector for a parked model used to wake it for a 403 alone. */ +bool Front::guarded_route(const HttpRequest& q) { + const std::string p = path_canonical(q.path); + if (q.method == "GET") return p == "/diagnostics" || p == "/admin/control-vector"; + if (q.method == "POST") return p == "/reset_prefix_cache" || p == "/admin/control-vector" || + p == "/v1/load_lora_adapter" || p == "/v1/unload_lora_adapter"; + if (q.method == "DELETE") + /* Both shapes the router registers and the handler guards: /sessions and its :label + * form (server.cpp registers DELETE /sessions/:label next to DELETE /sessions). */ + return p == "/sessions" || (p.size() > 10 && p.compare(0, 10, "/sessions/") == 0 && + p.find('/', 10) == std::string::npos); + return false; } void Front::keep_job(const std::string& id, int64_t expires_at, int64_t max_kept, Handler answer) { @@ -631,6 +663,19 @@ HttpResponse Front::forward(HttpRequest q) { Entry* e = nullptr; std::shared_ptr l; HttpResponse refusal; + /* A guarded route asked by someone the handler will refuse brings its model back for a + * 403 alone: the wake fetched, evicted, and charged for nothing. Answer the not-open + * state here and the stranger's request never touches the scheduler. An open model still + * dispatches -- its handler is the guard that answers -- and so does everything once the + * operator asks, rule or no rule. */ + if (guarded_route(q) && operator_rule_ && !operator_rule_(q)) { + std::lock_guard lk(mu_); + Entry* r = resolve(model); + if (r && r->state != State::Open) + return refuse(q.path, 409, "model_not_open", "model '" + r->name + "' is " + + state_name(r->state) + + "; this request does not bring it back"); + } /* A streaming request for a model that is not open gets its event stream at once. */ if (keepalive_ms_ > 0 && wants_stream(q)) { bool open = false; @@ -641,7 +686,7 @@ HttpResponse Front::forward(HttpRequest q) { } if (!open) return forward_waiting(std::move(q), model); } - const Got g = lease(model, q.path, -1, nullptr, &e, &l, &refusal); + const Got g = lease(model, q.path, -1, nullptr, &e, &l, &refusal, caller_is_operator(q)); if (g != Got::Leased) { if (model.empty() && id_route(q.path) && refusal.status == 400) { /* Every loaded model that might hold the job, in turn; the first that knows it @@ -690,7 +735,8 @@ HttpResponse Front::forward_waiting(HttpRequest q, const std::string& model) { } Entry* e = nullptr; HttpResponse refusal; - const Got g = lease(w->model, w->q.path, keepalive_ms_, &w->until, &e, &w->lease, &refusal); + const Got g = lease(w->model, w->q.path, keepalive_ms_, &w->until, &e, &w->lease, &refusal, + caller_is_operator(w->q)); if (g == Got::Waiting) { chunk = ": loading model " + w->model + "\n\n"; return true; @@ -724,7 +770,7 @@ void Front::forward_socket(const HttpRequest& q, WsConn& c) { Entry* e = nullptr; std::shared_ptr l; HttpResponse refusal; - if (lease(model, q.path, -1, nullptr, &e, &l, &refusal) != Got::Leased) { + if (lease(model, q.path, -1, nullptr, &e, &l, &refusal, caller_is_operator(q)) != Got::Leased) { c.close(1008, model.empty() ? "name a model with ?model=" : "model '" + model + "' is not served here"); return; } diff --git a/core/server/front.h b/core/server/front.h index 8a8e4816..cf022ded 100644 --- a/core/server/front.h +++ b/core/server/front.h @@ -163,6 +163,19 @@ public: /* A read the dashboard polls. It reports on a model and is no reason to bring one back, so it * is refused while that model is not open, where a request for work would wait for it. */ static bool dashboard_read(const HttpRequest& q); + /* The per-model routes whose handlers answer the local operator keyless (the lora admin, + * /diagnostics, /reset_prefix_cache, DELETE /sessions, /admin/control-vector). A refused + * request that woke its model first churned the fleet for nothing -- wake, HF fetch, + * eviction of residents -- and churn priced by a stranger is a DoS. Kept in step with + * Server::register_routes. */ + static bool guarded_route(const HttpRequest& q); + /* Which requests speak for the machine's operator -- the fleet sets this to its admin + * guard's predicate. Set, the guarded routes above bring a cold or parked model back only + * for those callers; unset, they wake as any request does (no fleet, no guards to precede). */ + void set_operator_rule(std::function f) { operator_rule_ = std::move(f); } + /* The caller-side view of the rule: a wired rule decides; no rule at all (the front used + * without fleet wiring, as in its own tests) trusts the caller. */ + bool caller_is_operator(const HttpRequest& q) const { return !operator_rule_ || operator_rule_(q); } private: struct Entry { @@ -199,8 +212,12 @@ private: * end is written back so later calls continue the same wait. Refused with *refusal set when * there is no such model, it failed, or the whole wait passed; Waiting when only `wait_ms` * passed. */ + /* `operator_ok` is the caller's verdict of operator_rule_ on its request: a Failed model's + * refusal prose (the last load error, which names paths and fetch URLs) goes to operators + * only; a stranger gets the state without the story. */ Got lease(const std::string& model, const std::string& path, int64_t wait_ms, - uint64_t* until_ns, Entry** out, std::shared_ptr* held, HttpResponse* refusal); + uint64_t* until_ns, Entry** out, std::shared_ptr* held, HttpResponse* refusal, + bool operator_ok); std::shared_ptr lease_entry(Entry* e); std::shared_ptr lease_quiet(Entry* e); HttpResponse forward(HttpRequest q); @@ -219,6 +236,7 @@ private: HttpTransport transport_; std::atomic stopping_{false}; std::function demand_; + std::function operator_rule_; std::function describe_; std::function fleet_metrics_; diff --git a/tests/front_test.cpp b/tests/front_test.cpp index 5c7f2791..cc74ed56 100644 --- a/tests/front_test.cpp +++ b/tests/front_test.cpp @@ -52,6 +52,12 @@ TEST(the_dashboards_reads_do_not_count_as_wanting_a_model) { q.path = path; q.method = "GET"; CHECK(Front::dashboard_read(q)); + /* The router dispatches a doubled or trailing slash to the same route; so must the + * read's exemption, or the read wakes a model for a slash. */ + q.path = std::string("/") + path; + CHECK(Front::dashboard_read(q)); + q.path = std::string(path) + "/"; + CHECK(Front::dashboard_read(q)); } q.path = "/admin/control-vector"; q.method = "POST"; @@ -63,6 +69,36 @@ TEST(the_dashboards_reads_do_not_count_as_wanting_a_model) { CHECK(!Front::dashboard_read(q)); } +/* The set that must not wake a model for a caller its handler will refuse: exactly the routes + * Server::register_routes guards with local_operator keyless, and nothing beside them. */ +TEST(guarded_routes_are_the_local_operator_ones) { + HttpRequest q; + q.method = "GET"; q.path = "/diagnostics"; CHECK(Front::guarded_route(q)); + /* The router collapses "//x" and "x/" into "/x" (split_path, http.cpp) -- the guard sees + * the same path the handler reaches, or a slash buys the wake the guard denies. */ + q.path = "//diagnostics"; CHECK(Front::guarded_route(q)); + q.path = "/diagnostics/"; CHECK(Front::guarded_route(q)); + q.method = "POST"; q.path = "/reset_prefix_cache"; CHECK(Front::guarded_route(q)); + q.path = "/admin/control-vector"; CHECK(Front::guarded_route(q)); + q.method = "GET"; q.path = "/admin/control-vector"; CHECK(Front::guarded_route(q)); + q.method = "DELETE"; q.path = "/sessions"; CHECK(Front::guarded_route(q)); + /* The :label form is registered and guarded next to it (server.cpp); the front must not + * wake a model for a stranger's DELETE /sessions/whatever either. */ + q.path = "/sessions/abc"; CHECK(Front::guarded_route(q)); + q.method = "POST"; q.path = "/v1/load_lora_adapter"; CHECK(Front::guarded_route(q)); + q.path = "/v1/unload_lora_adapter"; CHECK(Front::guarded_route(q)); + /* The rest is unguarded: the open dashboard reads stay as they are, and inference keeps + * waking its model for whoever asks -- that is what a keyless box is for. */ + q.method = "GET"; q.path = "/sessions"; CHECK(!Front::guarded_route(q)); + q.path = "/stats"; CHECK(!Front::guarded_route(q)); + q.method = "POST"; q.path = "/v1/chat/completions"; CHECK(!Front::guarded_route(q)); + q.method = "GET"; q.path = "/v1/models"; CHECK(!Front::guarded_route(q)); + /* Shapes the :label pattern cannot match are no route at all, so they wake nothing -- but a + * trailing slash is not such a shape: the router collapses it to the guarded bare route. */ + q.method = "DELETE"; q.path = "/sessions/"; CHECK(Front::guarded_route(q)); + q.path = "/sessions/a/b"; CHECK(!Front::guarded_route(q)); +} + TEST(a_finished_job_is_answered_by_its_id_with_no_model) { Front f(TransportOptions{}, 0); const int64_t later = (int64_t)std::time(nullptr) + 100;