From 66b0dcf019e9ab4544b6b74b40e3b8a179ca869a Mon Sep 17 00:00:00 2001 From: fl0l0u Date: Sun, 15 Nov 2026 19:36:57 +0000 Subject: [PATCH] server: the guarded-route set is declared once, at registration The front kept its own list of which routes answer the local operator keyless -- guarded_route spelled out the same eight (method, path) pairs Server::register_routes registers with a local_operator handler, and its comment admitted the copy ("Kept in step with Server::register routes"). A new guarded route meant editing the registration AND the front's list AND the test that pins the list against drift; miss one and the front wakes a parked model for a stranger who is then refused -- the exact A-05 bug class -- silently. Router::add now takes a `guarded` flag, set on the six registrations that guard (and their DELETE siblings). Router::guarded(method, path) asks the matcher -- the same split_path + :label match that reaches the handler -- and the front copies the flag as it mirrors each model route, so guarded_route is one line reading the front's own copied marks. One declaration per guarded route, at its registration; the matcher that decides the wake is the matcher that dispatches, so slash and :label forms cannot disagree with the handler and the drift risk is gone. front_test still pins every shape (guarded: slashes, /sessions/:label, the trailing-slash collapse; not: GET /sessions, /stats, inference, /sessions/a/b) and transport_test exercises real dispatch through them. Behaviour unchanged -- the same eight routes wake nothing for a stranger. --- core/server/front.cpp | 26 +++++++++++--------------- core/server/front.h | 7 ++++++- core/server/http.cpp | 10 ++++++++-- core/server/http.h | 13 ++++++++++++- core/server/server.cpp | 23 +++++++++++++---------- tests/front_test.cpp | 36 ++++++++++++++++++++---------------- 6 files changed, 70 insertions(+), 45 deletions(-) diff --git a/core/server/front.cpp b/core/server/front.cpp index fa1fe96a..875667f1 100644 --- a/core/server/front.cpp +++ b/core/server/front.cpp @@ -226,7 +226,10 @@ void Front::install_routes(const Router& model_router) { bool fleet = method == "GET" && (path == "/" || path == "/dashboard"); for (const auto& [p, fn] : own) fleet |= method == "GET" && path == p; if (fleet) continue; - router_.add(method, path, [this](const HttpRequest& q) { return forward(q); }); + /* Copy the guarded mark with the route: the front's wake-guard then reads the same fact + * the engine's registration set, through the same matcher. */ + router_.add(method, path, [this](const HttpRequest& q) { return forward(q); }, + model_router.guarded(method, path)); } for (const auto& [p, fn] : own) router_.get(p, [this, fn = fn](const HttpRequest& q) { return (this->*fn)(q); }); @@ -572,20 +575,13 @@ bool Front::dashboard_read(const HttpRequest& q) { 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; +/* The routes Server::register_routes marks guarded are the ones whose handlers answer the local + * operator only keyless. The front reads that mark back off its own copied routes (each guarded + * at registration in server.cpp) rather than re-listing it: one source, and the match is the + * router's -- so slash forms and the DELETE /sessions/:label shape decide exactly as the handler + * is reached, and a route cannot be guarded in one place and not the other. */ +bool Front::guarded_route(const HttpRequest& q) const { + return router_.guarded(q.method, q.path); } void Front::keep_job(const std::string& id, int64_t expires_at, int64_t max_kept, Handler answer) { diff --git a/core/server/front.h b/core/server/front.h index cf022ded..bc3890b8 100644 --- a/core/server/front.h +++ b/core/server/front.h @@ -168,7 +168,12 @@ public: * 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); + /* Whether the front's route for this request was registered guarded -- i.e. its handler + * answers the local operator only keyless -- so a stranger's call does not wake a parked + * model just to be refused. Derived from the router's own guarded marks (set at each route's + * registration in Server::register_routes), so the front and the handler cannot drift: the + * matcher that decides here is the one that reaches the handler. */ + bool guarded_route(const HttpRequest& q) const; /* 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). */ diff --git a/core/server/http.cpp b/core/server/http.cpp index eb900539..1f08c66d 100644 --- a/core/server/http.cpp +++ b/core/server/http.cpp @@ -250,8 +250,8 @@ static std::vector split_path(const std::string& p) { return out; } -void Router::add(std::string method, std::string path, Handler h) { - routes_.push_back(Route{std::move(method), split_path(path), std::move(h)}); +void Router::add(std::string method, std::string path, Handler h, bool guarded) { + routes_.push_back(Route{std::move(method), split_path(path), std::move(h), guarded}); } void Router::socket(std::string path, SocketHandler h) { @@ -346,6 +346,12 @@ bool Router::allows(const std::string& method, const std::string& path) const { return match(method, split_path(path), nullptr, &path_matched) != nullptr; } +bool Router::guarded(const std::string& method, const std::string& path) const { + bool path_matched = false; + const Route* r = match(method, split_path(path), nullptr, &path_matched); + return r && r->guarded; +} + HttpResponse Router::dispatch(HttpRequest req) const { bool path_matched = false; std::map caps; diff --git a/core/server/http.h b/core/server/http.h index 8b51c63d..86784edc 100644 --- a/core/server/http.h +++ b/core/server/http.h @@ -162,9 +162,15 @@ using SocketHandler = std::function; * entry's route ends in "*id"). No regex and no priority rules. */ class Router { public: - void add(std::string method, std::string path, Handler h); + void add(std::string method, std::string path, Handler h, bool guarded = false); void get(std::string path, Handler h) { add("GET", std::move(path), std::move(h)); } void post(std::string path, Handler h) { add("POST", std::move(path), std::move(h)); } + /* Registering a route whose handler answers the local operator only (no api key configured): + * the guard itself lives in the handler; marking it here lets the front read the SAME fact + * (see guarded()) so a guarded route is declared once, at its registration, and cannot be + * listed by the front's wake-guard without also being registered guarded -- and vice versa. */ + void get_guarded(std::string path, Handler h) { add("GET", std::move(path), std::move(h), true); } + void post_guarded(std::string path, Handler h) { add("POST", std::move(path), std::move(h), true); } /* A WebSocket route, exact path. An upgrade request to it becomes a connection the handler * is given; a plain GET to the same path is whatever add() registered there, if anything. */ void socket(std::string path, SocketHandler h); @@ -182,6 +188,10 @@ public: /* Whether a route takes `method` on `path`, as dispatch would match it. */ bool allows(const std::string& method, const std::string& path) const; + /* Whether the route taking `method` on `path` was registered guarded (get_guarded / + * post_guarded / add(..., true)), as dispatch would match it -- so slash and `:label` forms + * decide the same way the handler is reached. False when no route matches. */ + bool guarded(const std::string& method, const std::string& path) const; std::vector routes() const; @@ -190,6 +200,7 @@ private: std::string method; std::vector segs; /* ":name" is any one segment, a final "*name" the rest */ Handler h; + bool guarded = false; /* answered the local operator only (see add) */ }; /* The route taking `method` on the path `segs`, with its captures; null when none. * `*path_matched` says whether a route takes the path under any method. */ diff --git a/core/server/server.cpp b/core/server/server.cpp index 44daa3b7..d59c7a52 100644 --- a/core/server/server.cpp +++ b/core/server/server.cpp @@ -156,8 +156,8 @@ void Server::register_routes() { router_.get ("/v1/models", [this](const HttpRequest& q) { return handle_models(q); }); router_.get ("/v1/loras", [this](const HttpRequest& q) { return handle_loras(q); }); router_.get ("/v1/models/*id", [this](const HttpRequest& q) { return handle_models(q); }); - router_.post("/v1/load_lora_adapter", [this](const HttpRequest& q) { return handle_lora_admin(q, true); }); - router_.post("/v1/unload_lora_adapter", [this](const HttpRequest& q) { return handle_lora_admin(q, false); }); + router_.post_guarded("/v1/load_lora_adapter", [this](const HttpRequest& q) { return handle_lora_admin(q, true); }); + router_.post_guarded("/v1/unload_lora_adapter", [this](const HttpRequest& q) { return handle_lora_admin(q, false); }); /* The same routes without /v1, as llama-server answers them, for clients whose base URL is * the bare host. */ router_.post("/chat/completions", [this](const HttpRequest& q) { return handle_chat(q); }); @@ -212,13 +212,13 @@ void Server::register_routes() { router_.get ("/version", [this](const HttpRequest& q) { return handle_version(q); }); router_.get ("/server_info", [this](const HttpRequest& q) { return handle_server_info(q); }); router_.get ("/props", [this](const HttpRequest& q) { return handle_props(q); }); - router_.get ("/diagnostics", [this](const HttpRequest& q) { return handle_diagnostics(q); }); + router_.get_guarded ("/diagnostics", [this](const HttpRequest& q) { return handle_diagnostics(q); }); router_.post("/tokenize", [this](const HttpRequest& q) { return handle_tokenize(q); }); router_.post("/detokenize", [this](const HttpRequest& q) { return handle_detokenize(q); }); router_.get ("/get_tokenizer_info", [this](const HttpRequest& q) { return handle_tokenizer_info(q); }); - router_.post("/reset_prefix_cache", [this](const HttpRequest& q) { return handle_reset_prefix_cache(q); }); + router_.post_guarded("/reset_prefix_cache", [this](const HttpRequest& q) { return handle_reset_prefix_cache(q); }); /* The dashboard, also at `/`, since that is what a person with the host and port types. */ router_.get ("/", [this](const HttpRequest& q) { return handle_dashboard(q); }); @@ -227,17 +227,20 @@ void Server::register_routes() { router_.get ("/graph", [this](const HttpRequest& q) { return handle_graph(q); }); router_.get ("/sessions", [this](const HttpRequest& q) { return handle_sessions(q); }); /* The control vector's switch, for the dashboard's Model view and for automation: a - * deployment with no vector found answers 404 here. */ - router_.get ("/admin/control-vector", + * deployment with no vector found answers 404 here. Both verbs answer the local operator + * only keyless (handle_control_vector enforces it); registered guarded so the front reads + * that fact here too and will not wake a parked model for a stranger's GET. */ + router_.get_guarded ("/admin/control-vector", [this](const HttpRequest& q) { return handle_control_vector(q); }); - router_.post("/admin/control-vector", + router_.post_guarded("/admin/control-vector", [this](const HttpRequest& q) { return handle_control_vector(q); }); /* Forget one stored conversation: by its client label in the path (or `?label=`, for a - * label the path cannot carry), or by engine id with `?id=`. */ + * label the path cannot carry), or by engine id with `?id=`. Guarded like the other admin + * writes -- and both shapes, so the front does not wake a model to be refused. */ router_.add ("DELETE", "/sessions/:label", - [this](const HttpRequest& q) { return handle_session_release(q); }); + [this](const HttpRequest& q) { return handle_session_release(q); }, true); router_.add ("DELETE", "/sessions", - [this](const HttpRequest& q) { return handle_session_release(q); }); + [this](const HttpRequest& q) { return handle_session_release(q); }, true); } /* Workers beyond the admission ceiling. A generation holds its connection's worker for its whole diff --git a/tests/front_test.cpp b/tests/front_test.cpp index cc74ed56..4650f429 100644 --- a/tests/front_test.cpp +++ b/tests/front_test.cpp @@ -72,31 +72,35 @@ TEST(the_dashboards_reads_do_not_count_as_wanting_a_model) { /* 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) { + /* guarded_route now reads the front's own copied routes, so the decision is made by an + * instance's matcher -- a Front with no models installed still carries the full route table + * (its prototype does), which is exactly the set whose handlers guard. */ + Front f(TransportOptions{}, 0); HttpRequest q; - q.method = "GET"; q.path = "/diagnostics"; CHECK(Front::guarded_route(q)); + q.method = "GET"; q.path = "/diagnostics"; CHECK(f.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)); + q.path = "//diagnostics"; CHECK(f.guarded_route(q)); + q.path = "/diagnostics/"; CHECK(f.guarded_route(q)); + q.method = "POST"; q.path = "/reset_prefix_cache"; CHECK(f.guarded_route(q)); + q.path = "/admin/control-vector"; CHECK(f.guarded_route(q)); + q.method = "GET"; q.path = "/admin/control-vector"; CHECK(f.guarded_route(q)); + q.method = "DELETE"; q.path = "/sessions"; CHECK(f.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)); + q.path = "/sessions/abc"; CHECK(f.guarded_route(q)); + q.method = "POST"; q.path = "/v1/load_lora_adapter"; CHECK(f.guarded_route(q)); + q.path = "/v1/unload_lora_adapter"; CHECK(f.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)); + q.method = "GET"; q.path = "/sessions"; CHECK(!f.guarded_route(q)); + q.path = "/stats"; CHECK(!f.guarded_route(q)); + q.method = "POST"; q.path = "/v1/chat/completions"; CHECK(!f.guarded_route(q)); + q.method = "GET"; q.path = "/v1/models"; CHECK(!f.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)); + q.method = "DELETE"; q.path = "/sessions/"; CHECK(f.guarded_route(q)); + q.path = "/sessions/a/b"; CHECK(!f.guarded_route(q)); } TEST(a_finished_job_is_answered_by_its_id_with_no_model) {