Class TrustedProxies

java.lang.Object
com.jsgalactic.axiom.security.TrustedProxies

public final class TrustedProxies extends Object
Decides a request's client address and scheme, believing X-Forwarded-For and X-Forwarded-Proto only when they were set by configured, trusted proxies.

 static final TrustedProxies PROXIES = TrustedProxies.of("10.0.0.0/8", "::1");
 var origin = PROXIES.resolve(ctx.request());    // empty only for in-memory requests
 origin.map(ClientOrigin::address)...
 

Rules. Forwarding headers are client input and anyone can send them, so:

  • When the transport peer (Request.remoteAddress()) is not a trusted proxy, the client is the peer and the scheme is that of the connection (Request.scheme(): https on a TLS listener, otherwise http); forwarding headers are ignored entirely.
  • When the peer is trusted, X-Forwarded-For is read from right to left, the order in which proxies appended to it. Entries that are trusted proxies are skipped; the first untrusted entry is the client. If every entry is trusted, the leftmost one is the client. The walk stops at the first entry that is not an IP address literal (for example unknown or a host name), and after 32 entries: the client is then the last address that was vouched for by a trusted proxy. Addresses are never resolved through DNS. Entries may carry a port (192.0.2.1:4711, [2001:db8::1]:4711), which is ignored.
  • When the peer is trusted, X-Forwarded-Proto supplies the scheme if it is exactly one value, http or https (any case); otherwise the scheme is that of the connection (Request.scheme()).
  • X-Forwarded-Host supplies the host (and its port, if any) when it is exactly one value that is a DNS name, an IPv4 literal or a bracketed IPv6 literal; X-Forwarded-Port supplies the port, 1 to 65535, when the host carries none. Lists, names that are not valid host names (spaces, paths, credentials, @) and out-of-range ports are ignored.

Header family. By default the X-Forwarded-* family is read. reading(ForwardedHeaders) selects the RFC 7239 Forwarded header instead; exactly one family is ever read, so mixing a spoofed header of one with a genuine header of the other cannot happen. Forwarded is parsed strictly (tokens and quoted strings; an element with a repeated or malformed parameter is invalid; no whitespace around = or ;), then walked right to left with the same trust rule and the same 32-hop bound as above. The for value must be an IPv4 literal or a quoted, bracketed IPv6 literal, with an optional port; obfuscated identifiers (_hidden), unknown and names stop the walk. proto and host are taken from the element that vouches for the client address (the one whose for became the client), because that proxy describes what the client sent. A header with an unterminated quoted string is ignored as a whole.

Trust only proxies that overwrite or append to these headers; a trusted proxy that passes a client's forwarding headers through unchanged lets the client choose its address and host. The host and port are client-influenced input as soon as the proxy does not set them itself: never use them for security decisions such as password-reset links without an allow-list.

Immutable and thread-safe; configure once and share.

  • Method Details

    • none

      public static TrustedProxies none()
      Trusts no proxy: the client is always the transport peer.
      Returns:
      the empty configuration
    • of

      public static TrustedProxies of(String... ranges)
      Trusts proxies in the given address ranges.
      Parameters:
      ranges - IP address literals (10.0.0.7, ::1) or CIDR ranges ( 10.0.0.0/8, fd00::/8) whose host bits are zero
      Returns:
      the configuration
      Throws:
      IllegalArgumentException - for a range that is not an IP literal or a CIDR range
    • reading

      public TrustedProxies reading(ForwardedHeaders headers)
      Returns a copy that reads the given header family instead of the default ForwardedHeaders.X_FORWARDED. Only that family is read; the other is ignored.
      Parameters:
      headers - the family the trusted proxies set
      Returns:
      a copy with the same trusted ranges
    • isTrusted

      public boolean isTrusted(InetAddress address)
      Reports whether an address is a trusted proxy.
      Parameters:
      address - address to check
      Returns:
      true if it lies in a configured range
    • resolve

      public Optional<ClientOrigin> resolve(Request request)
      Decides the request's client address and scheme by the rules above.
      Parameters:
      request - the request
      Returns:
      the origin, or empty when the request has no transport peer (in-memory requests)