From ad3f03dbc622488a0f89f5ceb7930c17652f14bd Mon Sep 17 00:00:00 2001 From: fl0l0u Date: Fri, 30 Oct 2026 12:42:18 +0000 Subject: [PATCH] server: no internals on the wire, no client text in the log, no paths for strangers Four ways the server told more about itself than the caller earned. The exception handler logged e.what() sanitized and then put the raw string on the wire: a 500 whose message echoes request text is a reflection point, and one that quotes how a handler broke names the server's internals to whoever prodded it. The wire gets the generic sentence now; the reason stays in the log. The realtime and speech sockets caught their session's exception the same way -- raw what() to the log and onto the client's error event -- and follow. Their WebSocket wrapper logged e.what() and the request path unsanitized: the path is fully client text, and a client that can put CRLF and an escape sequence into a log line can forge the next one. A security-relevant header sent twice is a request built to be read two ways: this server reads the first copy, a proxy in front may read the last or join them, and whichever copy the disagreement lands on, the attacker picked it. Host, Authorization, X-Api-Key and Origin are refused whole at 400 before either reader -- the key gate included -- even when every copy carries a valid key. The lookup is case-insensitive, so an Authorization beside an authorization counts. And the model card's `root` is a path on this machine, served to anyone /v1/models admits. Keyless, it now belongs to the local operator, the rule /diagnostics and the LoRA admin already follow; with a key it travels with the key. server_test: the_models_root_path_follows_the_key. transport_test: a_duplicated_security_header_is_refused. ASan+UBSan: server/transport/ wire/stream/speech/speech_stream/dashboard, 7/7. --- core/server/http.cpp | 28 ++++++++++++++++++++++++--- core/server/realtime.cpp | 6 ++++-- core/server/server.cpp | 6 +++++- core/server/speech_stream.cpp | 6 ++++-- tests/server_test.cpp | 36 +++++++++++++++++++++++++++++++++++ tests/transport_test.cpp | 31 ++++++++++++++++++++++++++++++ 6 files changed, 105 insertions(+), 8 deletions(-) diff --git a/core/server/http.cpp b/core/server/http.cpp index 73a7173b..3ef8c323 100644 --- a/core/server/http.cpp +++ b/core/server/http.cpp @@ -533,10 +533,12 @@ int HttpTransport::serve(const Router& router, const TransportOptions& o) { catch (const std::exception& e) { what = e.what(); } catch (...) {} /* e.what() can carry request text (a path, a header): keep it off the log as an - * escape-sequence vector. */ + * escape-sequence vector, and off the wire entirely -- the message a 500 carries back + * must not echo the request or name how the server broke; that is what the log is for. */ RAD_ERR("server: %s", log_sanitize(what).c_str()); res.status = 500; - res.set_content(error_body(req.path, 500, "internal_server_error", what), + res.set_content(error_body(req.path, 500, "internal_server_error", + "the server faulted on this request; the reason is in its log"), "application/json"); }); @@ -782,7 +784,10 @@ int HttpTransport::serve(const Router& router, const TransportOptions& o) { try { h(req, conn); } catch (const std::exception& e) { - RAD_ERR("server: %s on %s", e.what(), req.path.c_str()); + /* The path is client text and e.what() can carry some: both are log + * vectors until sanitized, exactly as in the exception handler above. */ + RAD_ERR("server: %s on %s", log_sanitize(e.what()).c_str(), + log_sanitize(req.path).c_str()); conn.close(1011, "internal error"); } }, @@ -802,6 +807,23 @@ int HttpTransport::serve(const Router& router, const TransportOptions& o) { * its body being read. */ svr.set_pre_routing_handler([&router, gate, socket_gate, bridge, preflight](const httplib::Request& q, httplib::Response& s) { + /* A security-relevant header sent twice is a request built to be read two ways: this + * server reads the first copy, a proxy in front may read the last or join them, and any + * disagreement over a key, an Origin or the Host the keyless routes pin to depends on + * which reader you ask. Refuse the whole request rather than pick a reader. + * (Duplicated Content-Length is httplib's own rejection, in read_headers.) The lookup + * is case-insensitive, so `Authorization` beside `authorization` counts as two. */ + for (const char* h : {"Host", "Authorization", "X-Api-Key", "Origin"}) { + if (q.get_header_value_count(h) > 1) { + RAD_INFO("req refused from %s:%d -> 400 invalid_request_error: duplicate %s header", + q.remote_addr.c_str(), q.remote_port, h); + s.status = 400; + s.set_content(error_body(q.path, 400, "invalid_request_error", + std::string("the ") + h + " header was sent twice"), + "application/json"); + return httplib::Server::HandlerResponse::Handled; + } + } /* A WebSocket upgrade to a socket route is left to httplib, which then answers 101 and * runs the route's socket handler. */ if (is_socket_upgrade(q) && router.socket_for(q.path)) diff --git a/core/server/realtime.cpp b/core/server/realtime.cpp index 73d087d2..c9788901 100644 --- a/core/server/realtime.cpp +++ b/core/server/realtime.cpp @@ -285,12 +285,14 @@ void Server::realtime_run(RealtimeSession& s, WsConn& ws) { try { server_closed = realtime_loop(s, ws); } catch (const std::exception& e) { - RAD_ERR("realtime %s failed: %s", s.id.c_str(), e.what()); + /* e.what() is server-internal text that may quote client frames: sanitized to the log, + * and the socket's client gets the generic sentence, not the reason. */ + RAD_ERR("realtime %s failed: %s", s.id.c_str(), log_sanitize(e.what()).c_str()); ojson o = event("error"); ojson err; err["type"] = "server_error"; err["code"] = "internal_error"; - err["message"] = std::string("the session failed: ") + e.what(); + err["message"] = "the session failed; the reason is in the server log"; err["param"] = nullptr; err["event_id"] = nullptr; o["error"] = std::move(err); diff --git a/core/server/server.cpp b/core/server/server.cpp index d4c032c4..f413bb46 100644 --- a/core/server/server.cpp +++ b/core/server/server.cpp @@ -2230,7 +2230,11 @@ HttpResponse Server::handle_models(const HttpRequest& req) { c.id = opt_.model_id; c.created = started_at_; c.max_model_len = opt_.max_ctx; - c.root = opt_.model_path; + /* `root` is a path on this machine -- the one field of the card that describes the server's + * filesystem, and /v1/models is the first route a keyless scanner reaches. Keyless, it + * belongs to the operator alone, the rule /diagnostics and the LoRA admin follow; with a + * key, everyone who can pay the key already owns this model's card. */ + if (!opt_.api_key.empty() || local_operator(req)) c.root = opt_.model_path; c.capabilities = capabilities().dump(); metrics_.observe_http(200); std::vector cards{ c }; diff --git a/core/server/speech_stream.cpp b/core/server/speech_stream.cpp index eca62719..9c3e0602 100644 --- a/core/server/speech_stream.cpp +++ b/core/server/speech_stream.cpp @@ -181,9 +181,11 @@ void Server::speech_stream_run(SpeechSession& s, WsConn& ws) { try { speech_stream_loop(s, ws); } catch (const std::exception& e) { - RAD_ERR("speech stream %s failed: %s", s.id.c_str(), e.what()); + /* e.what() is server-internal text that may quote client frames: sanitized to the log, + * and the socket's client gets the generic sentence, not the reason. */ + RAD_ERR("speech stream %s failed: %s", s.id.c_str(), log_sanitize(e.what()).c_str()); ojson o = event("error"); - o["message"] = std::string("the session failed: ") + e.what(); + o["message"] = "the session failed; the reason is in the server log"; ws.send(json_dump(o)); ws.close(1011, "internal error"); } diff --git a/tests/server_test.cpp b/tests/server_test.cpp index e7d0a1e0..043d8dcd 100644 --- a/tests/server_test.cpp +++ b/tests/server_test.cpp @@ -2748,6 +2748,42 @@ TEST(the_model_entry_carries_the_context_length_the_server_was_started_with) { CHECK(!json::parse(unknown.get("/v1/models").body)["data"][0].contains("max_model_len")); } +/* `root` names where the model sits on the server's disk. Keyless it is the operator's own + * information -- the rule /diagnostics follows -- and with a key it travels with the key: the + * gate admitted the caller, so the card is theirs. */ +TEST(the_models_root_path_follows_the_key) { + Fixture f(true, 262144, "/models/m.rad"); + HttpRequest remote; + remote.path = "/v1/models"; + remote.peer = "192.168.1.20:50122"; + remote.headers["Host"] = "box.example:8000"; + CHECK(!json::parse(f.srv->router().dispatch(remote).body)["data"][0].contains("root")); + /* The rebinding page: a loopback socket, an attacker's Origin and Host -- the pair the + * loopback predicate exists for, and no path to the disk path. */ + HttpRequest rebinding; + rebinding.path = "/v1/models"; + rebinding.peer = "127.0.0.1:50122"; + rebinding.headers["Host"] = "evil.test:8000"; + rebinding.headers["Origin"] = "http://evil.test:8000"; + CHECK(!json::parse(f.srv->router().dispatch(rebinding).body)["data"][0].contains("root")); + HttpRequest local; + local.path = "/v1/models"; + local.peer = "127.0.0.1:50122"; + local.headers["Host"] = "localhost:8000"; + CHECK_EQ(json::parse(f.srv->router().dispatch(local).body)["data"][0] + .value("root", std::string()), + std::string("/models/m.rad")); + /* Keyed. The gate ran before the router, so the caller is admitted whoever dialled them; + * in-process dispatch stands in for the gate here, and `remote` now reads its path. */ + Fixture k(Fixture::Tweak{ [](ServerOptions& o, Deps&) { + o.model_path = "/models/m.rad"; + o.api_key = "k"; + } }); + CHECK_EQ(json::parse(k.srv->router().dispatch(remote).body)["data"][0] + .value("root", std::string()), + std::string("/models/m.rad")); +} + TEST(the_model_entry_and_server_info_report_the_same_capabilities) { Fixture f; const json info = json::parse(f.get("/server_info").body)["capabilities"]; diff --git a/tests/transport_test.cpp b/tests/transport_test.cpp index a26e53af..1c2e25db 100644 --- a/tests/transport_test.cpp +++ b/tests/transport_test.cpp @@ -325,6 +325,37 @@ TEST(the_api_key_gate_holds_every_route_but_the_probes) { CHECK_EQ(ask("HEAD / HTTP/1.1\r\nHost: t\r\n\r\n", /*head_only=*/true).status, 200); } +/* A security-relevant header sent twice is a request built to be read two ways: this server + * reads the first copy, a proxy in front may read the last or join the two, and whichever one + * the disagreement lands on, the attacker picked it. Refused whole, before either reader — + * the key gate included — even when every copy carries a valid key. */ +TEST(a_duplicated_security_header_is_refused) { + LiveServer s(4, 1, [](ServerOptions& o) { o.api_key = "k"; }); + REQUIRE(s.srv->port() > 0); + s.sched.hang = false; + s.sched.reply = "ok"; + auto ask = [&](const std::string& req, bool head_only = false) { + return ::ask(s, req, head_only); + }; + const Reply dup = ask(get("/v1/models", + "Authorization: Bearer k\r\nAuthorization: Bearer k\r\n")); + CHECK_EQ(dup.status, 400); + CHECK(has(json::parse(dup.body, nullptr, false)["error"].value("message", std::string()), + "twice")); + /* Case variants count as two: they land on one lookup here, but no front-end need agree. */ + CHECK_EQ(ask(get("/v1/models", + "Authorization: Bearer k\r\nauthorization: Bearer k\r\n")).status, 400); + /* The probe exemption reads no header, and still does not carry a duplicated one: an + * Origin or X-Api-Key split is refused wherever it lands. */ + CHECK_EQ(ask(get("/ping", "X-Api-Key: k\r\nX-Api-Key: wrong\r\n")).status, 400); + CHECK_EQ(ask("GET /ping HTTP/1.1\r\nHost: t\r\nOrigin: http://a\r\nOrigin: http://b\r\n\r\n") + .status, + 400); + /* One copy of each, however spelled, still works. */ + CHECK_EQ(ask(get("/v1/models", "Authorization: Bearer k\r\n")).status, 200); + CHECK_EQ(ask(get("/v1/models", "Authorization: Bearer k\r\nX-Api-Key: k\r\n")).status, 200); +} + /* A 401 from the key gate ends the connection. The refused request's body was never read, and * keeping the socket alive makes those bytes somebody's problem: draining them hands the * refusing client a way to pace a worker — one byte per socket timeout, forever — and not