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

+15 -5
View File
@@ -509,14 +509,15 @@ HttpResponse Server::handle_diagnostics(const HttpRequest& req) {
return error_response(err_not_implemented(
"the server was not given a diagnostics provider; this build has no engine attached"),
req.peer);
if (opt_.api_key.empty() && (!req.peer_is_loopback() || req.is_cross_origin())) {
/* A cross-origin browser is not a loopback client even when its own machine serves
* this port: the drive-by that guard exists to refuse. */
if (opt_.api_key.empty() && !local_operator(req)) {
/* A cross-origin browser is not a loopback client even when the victim's own machine
* serves this port, and under rebinding or a localhost proxy the socket alone says
* loopback for anyone: the Host is what still names who the client asked. */
ApiError e;
e.status = 403;
e.type = "permission_error";
e.message = "without --api-key, /diagnostics answers only loopback clients; start the "
"server with --api-key to read it from elsewhere";
e.message = "without --api-key, /diagnostics answers only a loopback client naming this "
"machine; start the server with --api-key to read it from elsewhere";
return error_response(e, req.peer);
}
metrics_.observe_http(200);
@@ -530,6 +531,15 @@ HttpResponse Server::handle_reset_prefix_cache(const HttpRequest& req) {
if (!deps_.sched)
return error_response(err_not_implemented("the server has no engine attached"), req.peer);
/* Wiping every client's cached prefix is an operator act: keyless, it belongs to the
* machine, not to whichever browser or page can reach the port. */
if (opt_.api_key.empty() && !local_operator(req))
return error_response(
err_forbidden("without --api-key, /reset_prefix_cache runs only from a loopback "
"client naming this machine; start the server with --api-key to run "
"it from elsewhere"),
req.peer);
const int64_t n = deps_.sched->reset_prefix_cache();
if (n < 0)
return error_response(err_not_implemented(
+21 -14
View File
@@ -43,6 +43,18 @@ namespace server {
* and answers with the same object. A deployment with no vector loaded answers 404 on both: it has
* no such feature, and its requests refuse `control_vector: true` by name. */
HttpResponse Server::handle_control_vector(const HttpRequest& req) {
/* Keyless, the vector and its switch belong to the machine: a rebinding page, a page on
* any origin a localhost proxy fronts, or a plain remote client reads neither the state
* nor the 404-that-means-no-vector. With --api-key both verbs are behind the key anyway. */
if (opt_.api_key.empty() && !local_operator(req)) {
ApiError e;
e.status = 403;
e.type = "permission_error";
e.message = "without --api-key, the control vector is read and its default changed only "
"from a loopback client naming this machine; start the server with --api-key "
"to reach it from elsewhere";
return error_response(e, req.peer);
}
const bool have = deps_.control_vector.available && deps_.control_vector.available();
if (!have) {
/* 404, like a model the server does not serve: the route exists, this deployment has nothing
@@ -56,20 +68,6 @@ HttpResponse Server::handle_control_vector(const HttpRequest& req) {
}
if (req.method == "POST") {
/* The default applies to every client's requests that name no choice, so a server
* started without --api-key takes the change from a loopback client only, as it does a
* runtime adapter load. A browser page on another origin does not qualify even when the
* victim's own browser makes it loopback: that is the drive-by this guard must refuse.
*/
if (opt_.api_key.empty() && (!req.peer_is_loopback() || req.is_cross_origin())) {
ApiError e;
e.status = 403;
e.type = "permission_error";
e.message = "without --api-key, the control-vector default is changed only from a "
"loopback client; start the server with --api-key to change it from "
"elsewhere";
return error_response(e, req.peer);
}
bool over = false, deep = false;
json body = parse_json_bounded(req.body.empty() ? "{}" : req.body, 0, &over, &deep);
if (body.is_discarded()) return error_response(err_bad("body", "not JSON"), req.peer);
@@ -195,6 +193,15 @@ HttpResponse Server::handle_session_release(const HttpRequest& req) {
if (!deps_.sched)
return error_response(err_not_implemented("the server has no engine attached"), req.peer);
/* Releasing a session destroys another client's cached conversation: keyless, that is an
* operator act, not something any page or client that reaches the port may do. */
if (opt_.api_key.empty() && !local_operator(req))
return error_response(
err_forbidden("without --api-key, sessions are released only from a loopback client "
"naming this machine; start the server with --api-key to do it from "
"elsewhere"),
req.peer);
const std::string label = req.param("label");
uint64_t id = 0;
const std::string id_s = req.param("id");
+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
+9
View File
@@ -57,6 +57,15 @@ struct HttpRequest {
* SDK. Through a proxy that rewrites Host, every browser reads as cross-origin -- which is
* exactly the case the "loopback-only" guards must not wave through. */
bool is_cross_origin() const;
/* The Host header's authority, lowercased with the port stripped; "" when there is no Host.
* The name a browser believes it is talking to — under DNS rebinding the attacker's own, and
* through a proxy the public one, neither of which the peer's address shows. */
std::string host_authority() const;
/* True when the Host names this machine as the loopback: "localhost", 127.0.0.1/8, [::1].
* A rebinding page is same-origin with the server and its victim's browser is the loopback
* peer — only the Host still says what page asked for it. An absent Host reads as local for
* an in-process dispatch and as remote for a socket, which always carries one. */
bool host_is_loopback() const;
};
struct HttpResponse {
+8
View File
@@ -186,6 +186,14 @@ ApiError err_unsupported(const std::string& param, const std::string& why) {
return e;
}
ApiError err_forbidden(const std::string& why) {
ApiError e;
e.status = 403;
e.type = "permission_error";
e.message = why;
return e;
}
ApiError err_not_implemented(const std::string& what) {
ApiError e;
e.status = 501;
+1
View File
@@ -44,6 +44,7 @@ struct ApiError {
ApiError err_bad(const std::string& param, const std::string& why);
ApiError err_unsupported(const std::string& param, const std::string& why);
ApiError err_forbidden(const std::string& why);
ApiError err_not_implemented(const std::string& what);
ApiError err_overloaded(const std::string& why);
ApiError err_internal(const std::string& why);
+28 -3
View File
@@ -14,6 +14,7 @@
#include <nlohmann/json.hpp>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <chrono>
#include <thread>
@@ -57,6 +58,27 @@ Server::Server(const Deps& d, const ServerOptions& o)
register_routes();
}
bool Server::local_operator(const HttpRequest& req) const {
if (!req.peer_is_loopback() || req.is_cross_origin()) return false;
if (req.host_is_loopback()) return true;
const std::string host = req.host_authority();
if (host.empty()) return false; /* a socket always carries a Host; the in-process case
passed host_is_loopback on its empty peer */
for (const std::string& a : opt_.local_hosts) {
std::string want = a;
for (char& ch : want) ch = (char)std::tolower((unsigned char)ch);
if (want == host) return true;
}
/* Bound to one concrete address, the box answers to that name on itself. */
std::string bind = opt_.host;
for (char& ch : bind) ch = (char)std::tolower((unsigned char)ch);
if (!bind.empty() && bind.front() == '[' && bind.back() == ']')
bind = bind.substr(1, bind.size() - 2);
if (!bind.empty() && bind != "0.0.0.0" && bind != "::" && bind != "*")
return bind == host;
return false;
}
Server::~Server() { stop(); }
/* A sink may be freed only after the scheduler reports its request terminal, since
@@ -2251,12 +2273,15 @@ HttpResponse Server::handle_lora_admin(const HttpRequest& req, bool load) {
if (!opt_.lora_load || !opt_.lora_unload)
return fail(404, "not_found_error", "this server was started without runtime LoRA (--lora or "
"--max-loras)");
if (opt_.api_key.empty() && (!req.peer_is_loopback() || req.is_cross_origin()))
if (opt_.api_key.empty() && !local_operator(req))
/* A cross-origin browser counts as neither loopback nor local: the victim's own browser
* is loopback to this port, and that is exactly how a hostile page would reach here. */
* is loopback to this port, and that is exactly how a hostile page would reach here —
* under DNS rebinding it even arrives with a matching Host unless the Host names the
* loopback, and through a localhost proxy every remote client does. Reading a path on
* this machine is a local operator's act. */
return fail(403, "permission_error", "loading an adapter reads a path on this machine, and a "
"server started without --api-key takes it only from a "
"loopback client");
"loopback client naming this machine");
/* Bounded like a generation body: this parses client text the same way, and a deep bracket
* nest would overflow this worker's stack when the tree is torn down. */
bool over = false, deep = false;
+11
View File
@@ -183,6 +183,9 @@ struct ServerOptions {
std::function<std::vector<std::string>()> gen_lora_names;
std::string api_key;
/* Extra Host authorities the keyless "loopback only" guards accept (--allow-host): names an
* operator reaches the box by that the loopback IP check cannot know. Case-insensitive. */
std::vector<std::string> local_hosts;
bool cors = true;
/* The transport's timeouts and body bound. See TransportOptions. */
@@ -227,6 +230,14 @@ public:
/* Renders each --reasoning-effort-map target as a request's effort. Empty when the template
* takes all of them; otherwise the first refusal, naming the target. */
std::string check_reasoning_effort_map() const;
/* The identity the keyless "loopback only" guards accept: the socket is loopback, the
* request carries no foreign Origin, and the Host names the loopback, one of
* ServerOptions::local_hosts, or the address --host names. Two ways forge the socket alone:
* a DNS-rebinding page is same-origin with the server and its victim's browser *is* the
* loopback peer — only the Host still tells which page asked for it — and a localhost TLS
* proxy puts every remote client behind the loopback socket, carrying the public Host. An
* attacker at the console of this machine is a local user either way, and stays admitted. */
bool local_operator(const HttpRequest& req) const;
void stop();
int port() const { return transport_.port(); } /* 0 until run() is accepting */