Internals: decision records

ADR-0022: Trusted proxies (X-Forwarded-For, PROXY protocol)

  • Status: Accepted (2026-10-06)

Context

Behind a load balancer every connection comes from the balancer: per-source limits (ADR-0013) would throttle all clients together, access logs and REMOTE_ADDR would show the balancer. Client-supplied X-Forwarded-For must never be believed blindly.

Decision

  • server.trusted_proxies: CIDRs of peers allowed to report the client address. Reloadable.
  • Header mode (any listener): for requests whose TCP peer is trusted, the client is the right-most X-Forwarded-For entry that is not itself a trusted proxy (entries further left may be forged by the client). The chain forwarded upstream is the received chain plus the peer. From untrusted peers the header is ignored and replaced.
  • PROXY protocol (listener.proxy_protocol: true, requires trusted_proxies): v1 and v2 headers, read before TLS/HTTP within header_read_timeout, never past the header. Peers outside trusted_proxies are closed immediately (untrusted_proxy); missing or malformed headers close the connection (proxy_protocol). v1 UNKNOWN, v2 LOCAL and non-TCP families keep the peer address. PROXY listeners admit connections per client after reading the header (the accept loop never waits).
  • The effective client address feeds the per-source request limiter, the access log, RequestContext.remote_addr (PHP REMOTE_ADDR, Node/Python via X-Forwarded-For) and diagnostics. Connections from trusted peers are exempt from the per-source connection limit (a balancer multiplexes many clients); their requests are limited per client.
  • Forwarded (RFC 7239) is not parsed.

1.1: client_address

Status: Accepted (2026-11-14). Adds vendor presets and more headers; nothing above changes.

  • server.client_address has header (x-forwarded-for default, forwarded, cf-connecting-ip, x-real-ip, true-client-ip), presets (cloudflare in 1.1), presets_dir and file. The header follows the trusted group of the peer: trusted_proxies use header, a preset uses its vendor’s header. A peer outside both is the client whatever it sends.
  • A peer in trusted_proxies and in a preset belongs to trusted_proxies (explicit wins).
  • Walking X-Forwarded-For or Forwarded: a hop in trusted_proxies is skipped and the walk continues. The walk ends at an address trusted only through a preset (a vendor edge the operator’s hop appended): the client is the one address in the vendor’s single-value header (CF-Connecting-IP, ruling 3), else the edge itself, never an entry left of the edge (changed after audit run-7 N1; it was a one-step hop whose left entry was the client). A source inside the vendor’s ranges (a Cloudflare Worker) connecting to the load balancer directly writes those entries itself; the vendor header is read before the scrub removes it from the balancer’s request (ruling 7).
  • Single-value headers: absent means the peer is the client; exactly one line with one bare address is that address; anything else is the unknown client ::.
  • Forwarded is parsed (for= only, strict RFC 7239). Only the right-most element of the last line is read on its own, so a client cannot hide the element the proxy appended. At most 32 entries are examined. The proxy must quote and escape client-derived parameters or omit them; X-Forwarded-For is the safer choice. Upstream, ScalWS sends only X-Forwarded-For, starting at the resolved client (:: when unknown).
  • Single-value headers are removed from requests of peers not trusted for them; their underscore spellings (cf_connecting_ip) are removed from every peer, since CGI-style stacks behind a proxy route map them to the same variable (ScalWS’s own PHP runtime already drops underscored names).
  • PROXY protocol stays limited to trusted_proxies. Preset peers and imported networks are exempt from per-source connection and handshake limits like trusted proxies; the cost is that a client coming through such a network (a Cloudflare Worker) has no per-source connection limit.
  • The preset list ships with the release. scalwsctl client-address update (root, TLS, strict validation, --force skips only the coverage check, a failed fetch keeps the old list) writes presets_dir, which must be root-owned, not a link and not group or other writable, as must the file; otherwise the shipped list is used with a warning. No refresh timer in 1.1.
  • Panel mode reads mod_remoteip (cPanel, DirectAdmin) and nginx real_ip (DirectAdmin) and writes /etc/scalws/panel/client-address.yaml, merged through client_address.file: lists are unioned, the file’s header applies only when the operator set neither header nor trusted_proxies, and the file is checked like a site file on every load. RemoteIPHeader CF-Connecting-IP with Cloudflare networks becomes the preset; other proxies go to trusted_proxies; per-site settings, Include, host names, loopback and special-purpose ranges are not carried over.