server: stop treating the loopback socket as identity

Three guards read a loopback peer plus a matching Origin as "this is
the machine's operator". Two real topologies forge exactly that:

DNS rebinding hands a hostile page its victim's own browser, so the
peer IS loopback, and the page is same-origin with the server, so the
Origin/Host compare stays silent. Only the Host still says which name
the client asked for, and the attacker's domain cannot be made to read
as the loopback.

The documented TLS-proxy topology puts every remote client behind a
loopback socket, and a non-browser client sends no Origin at all; the
proxy passes the public Host, which the new check refuses.

Server::local_operator now guards the keyless routes: the socket, the
Origin, and a Host naming the loopback, --allow-host, or a concrete
bind address. GET /admin/control-vector takes the guard POST already
had, placed before the 404-that-means-no-vector so a remote page can
probe neither; keyless DELETE /sessions and /reset_prefix_cache --
which destroy other clients' cached conversations -- answer the
machine only. --allow-host (repeatable) admits names an operator
reaches the box by from the box itself.

A local user at the console stays admitted; nothing about a key-set
server changes. GUIDE 6.6 states the new posture.
This commit is contained in:
fl0l0u authored and StillDeadcode committed 2026-10-31 07:50:43 +01:00
1 parent d54f3b9e6e
commit ec53bce5dc
14 files changed
+257 -43

No files matched your search

+25
View File
@@ -441,6 +441,31 @@ bool HttpRequest::is_cross_origin() const {
return lower_ascii(auth) != lower_ascii(header("Host"));
}
std::string HttpRequest::host_authority() const {
std::string h = lower_ascii(header("Host"));
if (h.empty()) return h;
if (h.front() == '[') {
/* "[::1]:8000": the address is what the brackets hold. */
const size_t close = h.find(']');
return close == std::string::npos ? h.substr(1) : h.substr(1, close - 1);
}
const size_t colon = h.rfind(':');
if (colon != std::string::npos && h.find(':') == colon &&
h.find_first_not_of("0123456789", colon + 1) == std::string::npos)
h.erase(colon); /* "localhost:8000" -> "localhost"; a bare "::1" keeps its colons */
return h;
}
bool HttpRequest::host_is_loopback() const {
const std::string h = host_authority();
if (h.empty()) return peer.empty(); /* no Host: an in-process dispatch, never a socket */
if (h == "localhost" || h == "ip6-localhost" || h == "ip6-loopback") return true;
in_addr v4{};
if (inet_pton(AF_INET, h.c_str(), &v4) == 1) return (ntohl(v4.s_addr) >> 24) == 127;
in6_addr v6{};
return inet_pton(AF_INET6, h.c_str(), &v6) == 1 && IN6_IS_ADDR_LOOPBACK(&v6);
}
/* The API key a browser's socket carries. A browser cannot set a header on a WebSocket, so
* OpenAI's browser clients offer it as a subprotocol, `openai-insecure-api-key.<key>`, beside
* `realtime`; the subprotocol chosen is never that one. Compared in constant time, as the header