Trusted proxy headers must stop at the last trusted hop
An application that reads a client address from X-Forwarded-For or Forwarded is trusting a header that a user can often supply. The header is useful when a reverse proxy adds it. It is dangerous when the application accepts the first value, the last value, or a complete chain without knowing which hop was trusted.
RFC 7239 defines the Forwarded header so proxy components can disclose information lost during proxying, including the address of the client and the proxy on the user-facing side. It also warns that the header cannot be relied upon unless the proxy chain is controlled and the application knows how to interpret it. [1] The header is a transport fact, not an authentication decision.
Define the trust boundary
Start with a simple diagram. Which proxies terminate TLS, which ones add the header, which ones preserve it, and which ones strip it? The application should trust the header only when the immediate peer is one of the configured proxies. If a request reaches the application directly from a network that can supply the header, the application should ignore it.
Do not assume that every value in a chain came from a trusted proxy. A client can send a forged Forwarded header before the request reaches the edge. A correct proxy should append or replace the header according to policy, not blindly preserve user input. The application should read from the side of the chain that the proxy defines as authoritative, then validate the address format and reject ambiguous values.
Do not use the header for authorization
Client-IP allowlists, rate limits, audit logs, and geographic rules are all affected by the header. None of them should be the only control for an administrative action. If a request appears to come from a trusted network because of a forged header, the attacker may reach a route that was meant to be internal. Use a network-level control or a signed proxy identity in addition to the address.
If the application uses Forwarded for logging, preserve the original value and the normalized derived value separately. Record which proxy made the decision. A log that presents the header as fact without recording its provenance makes incident analysis harder.
Normalize before comparing
Address parsing is full of edge cases. IPv4-mapped IPv6 addresses, compressed IPv6 forms, zone identifiers, and whitespace can make two strings look different while representing the same host. Normalize with a standard library, reject malformed values, and compare parsed addresses rather than raw text. Do not use a regular expression as the only parser for an address family.
If the proxy uses a separate header such as X-Real-IP, define precedence. A request that carries two conflicting headers should be rejected or assigned a conservative value. Do not let a later middleware silently choose a different header than the logging layer.
Test from outside the trusted path
Send a request directly to the application with a forged Forwarded value. Send a request through the proxy with a forged value in the incoming header. Send malformed IPv6 and IPv4-mapped values. Send a header with an unexpected number of hops. Confirm that the application either rejects the request or uses a value derived by the trusted proxy. Verify that the same decision is made by the web framework, the rate limiter, and the audit logger.
Use a canary route that returns the application's derived client address in a non-production environment. Compare it with the proxy's own log. This catches configuration drift after a proxy upgrade or a change in deployment topology.
Keep the boundary explicit in code
Put proxy-header handling in one middleware with a configuration list of trusted proxy networks. Fail closed when the immediate peer is not trusted. Document the expected header name, hop count, and sanitization. If the application is deployed behind a CDN and a load balancer, require both layers to agree.
Trusted headers can make logs and access controls more accurate. They cannot create trust from an untrusted request. The last trusted hop is the only place where the application can safely decide that a forwarded address is meaningful.
References
[1] IETF, "RFC 7239: Forwarded HTTP Extension." https://www.rfc-editor.org/rfc/rfc7239.html
[2] Spring, "Embedded Web Servers: Running Behind a Front-end Proxy Server." https://docs.spring.io/spring-boot/how-to/webserver.html
Top comments (0)