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.
This commit is contained in:
fl0l0u authored and StillDeadcode committed 2026-10-31 07:50:43 +01:00
1 parent 1c967b2f14
commit ad3f03dbc6
6 files changed
+105 -8

No files matched your search

+25 -3
View File
@@ -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))