| @@ -51,7 +51,12 @@ import std/result |
| 51 | 51 | import std/string |
| 52 | 52 | import std/bytes.{type Builder} |
| 53 | 53 | import std/dict.{type Dict} |
| 54 | | −import std/net.{type TcpListener, type TcpStream} |
| 54 | +import std/net.{ |
| 55 | + type SocketAddress |
| 56 | +, type TcpListener |
| 57 | +, type TcpStream |
| 58 | +, SocketAddress |
| 59 | +} |
| 55 | 60 | import std/ffi.{type Resource} |
| 56 | 61 | import std/html.{type Html} |
| 57 | 62 | import std/process.{type proc, type Pid, type Subject} |
| @@ -199,6 +204,112 @@ pub fn defaults() -> Settings { |
| 199 | 204 | ) |
| 200 | 205 | } |
| 201 | 206 | |
| 207 | +/// The client's address (issue #308): the other end of the connection the |
| 208 | +/// request came on, as the server accepted it. Behind a proxy, that is |
| 209 | +/// the proxy: see `client_address`. `None` for a request built by hand |
| 210 | +/// (in tests). |
| 211 | +/// |
| 212 | +/// The server puts it first among the request's headers as the |
| 213 | +/// pseudo-header `:peer` (`"127.0.0.1:52144"`, `"[::1]:52144"`), which |
| 214 | +/// no client can send: a header name cannot contain `:`. `wire_headers` |
| 215 | +/// leaves it out. |
| 216 | +pub fn peer(req: Request) -> Option<SocketAddress> { |
| 217 | + match req.headers { |
| 218 | + [(":peer", value), ..] -> parse_address(value) |
| 219 | + , _ -> None |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +/// `host:port`, or `[host]:port` for an IPv6 host. |
| 224 | +fn parse_address(value: String) -> Option<SocketAddress> { |
| 225 | + let (host, port) = if string.starts_with(value, "[") { |
| 226 | + match string.split(string.slice(value, 1, string.length(value)), "]:") { |
| 227 | + [host, port] -> (host, port) |
| 228 | + , _ -> ("", "") |
| 229 | + } |
| 230 | + } else { |
| 231 | + match string.split(value, ":") { |
| 232 | + [host, port] -> (host, port) |
| 233 | + , _ -> ("", "") |
| 234 | + } |
| 235 | + } |
| 236 | + match string.to_int(port) { |
| 237 | + Ok(port) -> if host == "" { None } else { Some(SocketAddress(host, port)) } |
| 238 | + , Error(_) -> None |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +/// How the server writes a peer in `:peer`. |
| 243 | +fn address_text(address: SocketAddress) -> String { |
| 244 | + let host = if string.contains(address.host, ":") { |
| 245 | + "[" <> address.host <> "]" |
| 246 | + } else { |
| 247 | + address.host |
| 248 | + } |
| 249 | + host <> ":" <> show(address.port) |
| 250 | +} |
| 251 | + |
| 252 | +/// The headers the client sent, without the server's pseudo-headers |
| 253 | +/// (`:peer`): what to forward or log. |
| 254 | +pub fn wire_headers(req: Request) -> List<(String, String)> { |
| 255 | + use h <- list.filter(req.headers) |
| 256 | + !string.starts_with(h.0, ":") |
| 257 | +} |
| 258 | + |
| 259 | +/// The client's IP address, through the proxies in `trusted` (exact |
| 260 | +/// addresses, as text). When the peer is not trusted, it is the client: |
| 261 | +/// its `X-Forwarded-For`, which anyone can send, is ignored. When it is, |
| 262 | +/// the client is the right-most `X-Forwarded-For` entry that is not |
| 263 | +/// trusted (each proxy appends the address it got the request from), or |
| 264 | +/// the left-most one if all are; with no entries, the peer itself. |
| 265 | +/// `None` without a peer (a request built by hand). |
| 266 | +/// |
| 267 | +/// // Behind one nginx on 10.0.0.2: |
| 268 | +/// http.client_address(req, ["10.0.0.2"]) |
| 269 | +pub fn client_address(req: Request, trusted: List<String>) -> Option<String> { |
| 270 | + match peer(req) { |
| 271 | + None -> None |
| 272 | + , Some(p) -> if !is_trusted(trusted, p.host) { |
| 273 | + Some(p.host) |
| 274 | + } else { |
| 275 | + Some(untrusted_hop(list.reverse(forwarded_for(req)), trusted, p.host)) |
| 276 | + } |
| 277 | + } |
| 278 | +} |
| 279 | + |
| 280 | +/// Every `X-Forwarded-For` entry, in order, across repeated headers. |
| 281 | +fn forwarded_for(req: Request) -> List<String> { |
| 282 | + use h <- list.flat_map(req.headers) |
| 283 | + if string.eq_ignore_ascii_case(h.0, "x-forwarded-for") { |
| 284 | + string.split(h.1, ",") |
| 285 | + |> list.map(string.trim) |
| 286 | + |> list.filter(fn(entry) { entry != "" }) |
| 287 | + } else { |
| 288 | + [] |
| 289 | + } |
| 290 | +} |
| 291 | + |
| 292 | +/// The first of `hops` (right-most first) not in `trusted`, else the |
| 293 | +/// last one seen (`seen`: the peer, before any). |
| 294 | +fn untrusted_hop( |
| 295 | + hops: List<String> |
| 296 | +, trusted: List<String> |
| 297 | +, seen: String |
| 298 | +) -> String { |
| 299 | + match hops { |
| 300 | + [] -> seen |
| 301 | + , [hop, ..rest] -> if is_trusted(trusted, hop) { |
| 302 | + untrusted_hop(rest, trusted, hop) |
| 303 | + } else { |
| 304 | + hop |
| 305 | + } |
| 306 | + } |
| 307 | +} |
| 308 | + |
| 309 | +fn is_trusted(trusted: List<String>, address: String) -> Bool { |
| 310 | + list.any(trusted, fn(t) { t == address }) |
| 311 | +} |
| 312 | + |
| 202 | 313 | /// The value of header `name` (case-insensitive), if present. |
| 203 | 314 | pub fn header(req: Request, name: String) -> Option<String> { |
| 204 | 315 | find_header(req.headers, name) |
| @@ -477,7 +588,11 @@ fn handle_connection( |
| 477 | 588 | fn() { watchdog(conn, me, id, drain, settings, ready, events) } |
| 478 | 589 | ) |
| 479 | 590 | let activity = process.receive_forever(ready) |
| 480 | | − let serving = Serving(conn, activity, drain, settings, handler) |
| 591 | + let peer = match net.peer(conn) { |
| 592 | + Ok(address) -> [(":peer", address_text(address))] |
| 593 | + , Error(_) -> [] |
| 594 | + } |
| 595 | + let serving = Serving(conn, peer, activity, drain, settings, handler) |
| 481 | 596 | serve_requests(serving, bytes.empty(), 0, 0) |
| 482 | 597 | net.close(conn) |
| 483 | 598 | } |
| @@ -556,6 +671,9 @@ type Incoming { |
| 556 | 671 | type Serving { |
| 557 | 672 | Serving( |
| 558 | 673 | conn: Resource<TcpStream> |
| 674 | + // `[(":peer", address)]`, or none if it is not known: put first among |
| 675 | + // every request's headers (`peer`). |
| 676 | + , peer: List<(String, String)> |
| 559 | 677 | , activity: Subject<Activity> |
| 560 | 678 | , drain: Subject<()> |
| 561 | 679 | , settings: Settings |
| @@ -577,6 +695,7 @@ fn serve_requests( |
| 577 | 695 | match read_request(conn, activity, serving.settings.max_body, buffer, at) { |
| 578 | 696 | Ok(Incoming(req, keep_alive, buffer, at)) -> { |
| 579 | 697 | process.send(activity, Handling) |
| 698 | + let req = Request(..req, headers: list.append(serving.peer, req.headers)) |
| 580 | 699 | // HEAD is GET without the body (RFC 9110 §9.3.2): the handler sees |
| 581 | 700 | // a GET, and the body is not sent (a stream fn is not called). |
| 582 | 701 | let with_body = req.method != "HEAD" |