Skip to content

Proxies & Client Info ​

Inside an action, request exposes the effective connection details:

MethodExampleDescription
request.remote_ip"203.0.113.7"The client's IP address
request.scheme"https"http or https
request.ssl?trueWhether the scheme is https
request.host"api.example.com"Host name, without the port
request.port443Port
request.protocol"https://"Scheme with ://
request.base_url"https://api.example.com"Scheme, host and port — handy for building absolute URLs

Behind a proxy ​

Without configuration, Runcobo trusts no proxy. Values come from the TCP connection and the Host header, and every X-Forwarded-* header is ignored — otherwise any client could claim any IP by sending a header.

When your app runs behind a load balancer, list the proxies you trust:

src/main.cr
crystal
Runcobo.start(
  proxy_config: Runcobo::ProxyConfig.new(
    trusted_proxies: ["10.0.0.0/8", "127.0.0.1", "::1"],
  ),
)

Entries are IPv4 or IPv6 addresses, or CIDR ranges. Invalid entries raise at startup.

When a request comes from a trusted address, Runcobo uses:

HeaderUsed for
X-Forwarded-Forremote_ip: walks the list from the right, skipping trusted proxies, and stops at the first untrusted address
X-Forwarded-Protoscheme (http or https only)
X-Forwarded-Hosthost and port
X-Forwarded-Portport

When a header contains several comma-separated values, the last one (added by the nearest proxy) is used. Malformed values are ignored rather than trusted.

Single-address headers ​

Some providers put the client IP in a header of their own, such as CF-Connecting-IP or Fly-Client-IP. Tell Runcobo to read it — still only from trusted proxies:

crystal
Runcobo::ProxyConfig.new(
  trusted_proxies: ["10.0.0.0/8"],
  client_ip_header: "Fly-Client-IP",
)

The configuration belongs to the server it's passed to; nothing is set globally.

Only trust what's in front of you

List only addresses that your own infrastructure controls. Trusting 0.0.0.0/0 lets any client spoof its IP address and scheme.

Released under the MIT License.