KartaWeb.ClientIP (Karta v1.0.0)

The client's address and scheme when Karta runs behind reverse proxies.

A proxy that terminates TLS connects to Karta itself, so conn.remote_ip is the proxy's address and the scheme is http; the proxy names the client in X-Forwarded-For and the scheme in X-Forwarded-Proto. Anyone can send those headers, so they are believed only from the proxies listed in trusted_proxies (CIDR ranges; config :karta, KartaWeb.ClientIP, trusted_proxies: [...], from KARTA_TRUSTED_PROXIES in production). The list is empty by default: then every request's address is the address that connected.

When the connecting address is a trusted proxy:

  • the client is found by reading X-Forwarded-For from the right (the end the nearest proxy appended to), skipping addresses that are trusted proxies themselves; the first other address is the client. An entry that is not a bare IP address stops the search, and the last trusted proxy counts as the client. When every entry is a trusted proxy, the leftmost is the client.
  • X-Forwarded-Proto (its last value) sets the scheme: https or wss (port 443), http or ws (port 80); any other value is ignored. Traefik says wss for a WebSocket request: without it the redirect to HTTPS would answer every upgrade of the admin pages' live connection with 301, and they would fall back to long polling.

Nothing else is taken from forwarded headers: not the host (X-Forwarded-Host), not the port. IPv4-mapped IPv6 addresses (::ffff:192.0.2.1, as a dual-stack listener reports IPv4 clients) become IPv4 addresses, so that limits and logs see one form per client.

Both HTTP endpoints run this plug first, before anything that uses the address or the scheme: the redirect to HTTPS, the limit on API key verifications, the login limits, the network recorded for opens and clicks, and the Received header of messages sent through the APIs.

Summary

Functions

The configured trusted proxies (CIDR strings). Raises when one is not a valid range, so that a mistake stops Karta at boot (validate!/0).

Checks the configured ranges; called when the application starts.

Functions

trusted_proxies()

@spec trusted_proxies() :: [String.t()]

The configured trusted proxies (CIDR strings). Raises when one is not a valid range, so that a mistake stops Karta at boot (validate!/0).

validate!()

@spec validate!() :: :ok

Checks the configured ranges; called when the application starts.