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

+31
View File
@@ -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