server: name the caller a vouched proxy carried, without arming the guard

An operator running radiance behind a reverse proxy sees one address in
every log line: the proxy's. --trusted-proxy CIDR (repeatable) vouches
for the networks whose sockets may say who they carried. An
X-Forwarded-For from inside a vouched network is walked from the right:
vouched hops are skipped, the first address no vouched CIDR covers is
the caller, a chain of only vouched hops ends at its leftmost, and
anything else falls back to the socket. From an unvouched socket the
header names no one, because it has not been vouched for.

Attribution is not authority, and the split that keeps them apart is
mechanical. HttpRequest carries `peer`, the attributed caller, sanitized
where it is built because a named address arrives in a header and the
log keeps no client text otherwise -- and `raw_peer`, the socket as the
kernel saw it, which is now the only address peer_is_loopback() reads.
A proxy on loopback therefore hands the keyless loopback-only routes to
none of its clients; it only names them where the proxy used to stand.
transport_test pins both halves: the walk at the unit level, and over a
real socket that a vouched proxy plus X-Forwarded-For leaves the
loopback guard answering 200 and the cross-origin 403 untouched.

httplib's own set_trusted_proxies -- new this side of the vendor bump --
is deliberately unarmed. It rewrites req.remote_addr, the very field the
guards would read as the socket peer, so one over-broad vouched network
would move the guard rather than the log line. Radiance reads the header
itself, and ip_in_cidr refuses across families: a vouched ::ffff:0:0/96
vouches for no IPv4 socket. Config rejects anything but a numeric
network with a prefix at startup, because a typo there would silently
unvouch a proxy; the refusals the transport answers before the router
log the attributed caller too, so the audit trail stays one line per
request whoever the socket belongs to.
This commit is contained in:
fl0l0u authored and StillDeadcode committed 2026-10-31 07:50:43 +01:00
1 parent 40e1b3073b
commit b6acbd69e4
11 files changed
+352 -22

No files matched your search

+39 -2
View File
@@ -36,7 +36,16 @@ struct HttpRequest {
std::map<std::string, std::string, CiLess> headers;
std::map<std::string, std::string> query;
std::map<std::string, std::string> params; /* from a ":name" route segment */
std::string peer; /* "addr:port"; empty in-process */
std::string peer; /* "addr:port"; empty in-process. The
ATTRIBUTED caller: with --trusted-proxy
and an X-Forwarded-For, the address the
proxy chain names; the port is always
the last hop's -- the chain carries no
port. For logs and audit, never for
authority. */
std::string raw_peer; /* "addr:port" of the socket peer as the
kernel saw it; empty in-process. The
only address the guards read. */
/* Steady-clock nanoseconds at which the whole body had arrived. The server's own time for
* the request starts here, before parsing, so network time is excluded. -1 in-process. */
int64_t t_received_ns = -1;
@@ -50,7 +59,9 @@ struct HttpRequest {
/* The first form part named `k` (a field or a file), or null. */
const FormPart* part(const std::string& k) const;
bool peer_gone() const { return closed && closed(); }
/* True for 127.0.0.0/8, ::1, an IPv4-mapped 127.x address, and an in-process request. */
/* True when raw_peer is 127.0.0.0/8, ::1, an IPv4-mapped 127.x address, or absent (the
* in-process request). Reads the socket, never the attributed peer: a proxy an operator
* vouched for can name any caller it likes; that changes the log line, not the guard. */
bool peer_is_loopback() const;
/* True when the request comes from a browser on another origin: an `Origin` header whose
* authority differs from `Host`. Scripts send no Origin, so this never fires for curl or an
@@ -68,6 +79,28 @@ struct HttpRequest {
bool host_is_loopback() const;
};
/* Whether `cidr` is in the one form --trusted-proxy takes: numeric "addr/prefix", prefix within
* its family. Config refuses anything else at startup, so a typo cannot silently unvouch a
* proxy; ip_in_cidr treats the same rejection as simply no match. */
bool cidr_valid(const std::string& cidr);
/* True when the numeric address `addr` (no port, no zone) falls inside `cidr`. Families must
* agree: this is not RFC 6052 translation, an IPv4 peer is not inside an IPv6 network that
* happens to embed it. */
bool ip_in_cidr(const std::string& addr, const std::string& cidr);
/* The caller a proxy chain vouches for. `raw_addr` is the socket peer (no port), `xff` the
* request's X-Forwarded-For value ("" when there is none), `trusted` the vouched CIDRs.
* Nothing is read from the header unless the socket itself belongs to a vouched CIDR -- an
* unvouched peer can name any address it likes and names none. Then the chain is walked from
* the right (the hop nearest us, which our vouched proxy appended): the first entry that no
* vouched CIDR covers is the caller; a chain of only vouched hops ends at its leftmost entry,
* the original client as the outermost proxy saw it. Malformed entries stop the walk where
* they stand -- the chain is reported, not interpreted, and the log sanitizer owns the bytes.
* Fallback is `raw_addr`, always. */
std::string attributed_client(const std::string& raw_addr, const std::string& xff,
const std::vector<std::string>& trusted);
struct HttpResponse {
int status = 200;
std::string content_type = "application/json; charset=utf-8";
@@ -203,6 +236,10 @@ struct TransportOptions {
size_t max_inflight_body_bytes = 0;
std::string api_key; /* "" disables the check */
bool cors = true;
/* --trusted-proxy, repeatable: CIDRs whose sockets may vouch for their caller through
* X-Forwarded-For. Empty: no socket vouches for anything and peer always equals raw_peer.
* Attribution rewrites the logged caller, never an authority -- see HttpRequest::peer. */
std::vector<std::string> trusted_proxies;
};
/* Idle workers kept for TransportOptions::n_threads: that many, one per hardware thread for 0,