How to Restrict Host and Trust Forwarded Headers Behind a Reverse Proxy
Behind a reverse proxy, an application needs two separate rules: which hostnames it serves and which sources it accepts Forwarded and X-Forwarded-* information from. Having a proxy does not make request headers trustworthy. We will explain how to align application and proxy settings, then verify the result in a fully fictional environment using domains you own and synthetic data. We will also check that an absolute link uses the address configured in the application.
Where the Trust Boundary Lies
The client chooses Host. The client also controls Forwarded and X-Forwarded-* unless your own proxy sets them. Accepting a hostname and trusting request information therefore require separate rules: the first restricts the names served, while the second defines acceptable sources for proxy headers.
Start by documenting which proxies belong to your infrastructure and which of them the application should trust. Agree on which headers the deployment uses and which values the proxy generates. Saying ‘we run behind a proxy’ is too vague for configuration and verification: you need a specific list of your own trusted proxies and a header-handling rule for your chosen stack.
Explicitly List the Hosts You Serve
The application should accept only the hostnames it serves and reject other requests. Specify these names explicitly. The wildcard value * allows any host, so it is unsuitable for restricting the list. Configure the default virtual host on the reverse proxy or load balancer separately: it must not serve the application.
Consider a fully fictional product called ‘Leaflet.’ In an isolated test environment, the team uses two test domains it owns: the main product address and an additional interface address. Both are on the allowlist. A third test hostname owned by the team is deliberately left off the list. It will be used to verify rejection without contacting anyone else’s systems.
Remove Client-Supplied Proxy Headers at the External Boundary
The external proxy must remove client-supplied Forwarded and X-Forwarded-* headers. The application should use these values only when they arrive from its own proxies. Align these requirements with the application’s trust settings: an allowed Host alone does not make the other headers trustworthy.
For Leaflet’s test environment, the team documents the expected behavior in advance: which client headers are removed and which new values its own proxy sets, if needed. It then compares observations at the proxy’s output and the application’s input against that description. The criterion is the origin of the values used, rather than simply the presence of Forwarded in a request.
Build Absolute Links from Configuration
Absolute URLs should be generated from application configuration rather than request data. For the fictional environment, set the base address explicitly and provide a harmless test resource whose link can be returned in a response. This keeps the check limited to address generation.
Use each of the two allowed test hosts in turn. In each case, compare the returned absolute link with the configured address. The additional allowed Host must not replace the configured base address. Apply the same criterion when sending synthetic values in client-supplied proxy headers.
Author-Proposed Acceptance Checks in Your Own Test Environment
The following are checks proposed by the authors, not an official testing procedure. Run them only in the fictional environment described above, using domains you own and synthetic data. Before starting, define the expected test-route result and the rejection contract for your chosen stack.
- Normal request: access an allowed host through your own proxy. Compare the contents of the test-route result with the expected result. A successful HTTP response alone is insufficient.
- Unknown Host: use your own test hostname that is not on the list. The request must be rejected and must not receive the application’s normal result. Separately verify that the default virtual host does not serve the application.
- Client headers: send Forwarded and the X-Forwarded-* headers used in your deployment with synthetic markers. Observations at the proxy’s output and the application’s input must confirm that client-supplied values were removed. New proxy values, if generated, must match its configuration; client markers must not become trusted application data.
- Absolute link: request the link again with another allowed Host and synthetic proxy headers. The link’s address must match the application configuration.
Fixes and Priorities
Start with an explicit host list and rejection of other names, including the behavior of the default virtual host. Then align the removal of client-supplied proxy headers with the application’s trust in its own proxies. Next, check the source of absolute URLs. After each fix, repeat the relevant check along with the normal request.
If the route being checked belongs to a REST API, unexpected or invalid input content must be rejected. Error responses must not disclose stack traces or other internal technical details. When checking rejection, record both the absence of the normal result and the response content: an HTTP status code alone does not confirm that all criteria have been met.
What the Results Confirm and Where to Get Help
These checks are limited to the list of accepted hosts, the origin of proxy headers, and the source of the absolute link’s address in the configuration tested. They provide no statistical guarantee of product security. DNS, TLS, caching, email delivery, and the lifecycle of password-reset links are outside this article’s scope.
If you need help checking these rules, you can start with a free audit of your website or the web part of your product from CheckMyApp. A useful outcome is an agreed list of hosts, a clear rule for trusting your own proxy, and observable acceptance criteria for your web product.