CCyberwiki

Reverse proxy

Reverse proxy
TypeConcept, server setup
Also known asGateway (in the HTTP standard), front-end server
Common softwarenginx, Caddy, CDNs, cloud load balancers
StandardRFC 9110, section 3.7

A reverse proxy is a server that takes requests from the internet in place of an application and passes each one on to the application over a second connection. The browser treats it as the site's server, and the application sees it as the client. The HTTP standard lists caching and spreading load over several servers among its uses.1 nginx, Caddy, a CDN or a cloud load balancer usually fills the role. The second connection need not be encrypted: Caddy ends TLS for visitors and by default talks to the app in plain HTTP.2

Forward and reverse

HTTP has three kinds of intermediary between a browser and the origin server that produces the answer: proxy, gateway and tunnel. A forward proxy is picked by the client. A reverse proxy, which RFC 9110 calls a gateway, works for the server side instead: it answers the browser as if it were the origin and forwards the request inward to one or more servers behind it.1 Each hop is its own connection, so three intermediaries in a row carry one request over four connections.1

What the application no longer sees

The application's connection now comes from the proxy, so the source address of that connection is the proxy's.3 The visitor's address, the host name typed in the browser and the scheme travel on as request headers instead:

Table 1. Headers a reverse proxy uses to pass on what the app cannot see
HeaderCarries
X-Forwarded-ForThe client address, then the address of each proxy, comma-separated, with the most recent proxy on the right3
X-Forwarded-HostThe original Host value, the name the client asked for2
X-Forwarded-Protohttps or http, as the client connected4
ForwardedThe standardised version of X-Forwarded-For, less often used3
Hostnginx replaces it by default with the address named in proxy_pass; Caddy passes the client's value through unchanged52

A framework reads these headers only when told a proxy is in front. In Express the setting is trust proxy; once it is on, req.ip, req.hostname and req.protocol come from the forwarded headers rather than from the connection.4

Trusting forwarded headers

Any client can send its own X-Forwarded-For. A proxy that appends to the header keeps whatever the client wrote at the front: the nginx variable $proxy_add_x_forwarded_for is the incoming value with the connecting address added at the end.5 A client at 198.51.100.7 that sends X-Forwarded-For: 10.0.0.1 arrives at the app as 10.0.0.1, 198.51.100.7. Code that takes the leftmost entry sees an internal address the attacker chose, and a rate limit keyed on it starts again whenever the value changes. Express with trust proxy set to true takes the leftmost entry, and its documentation warns that the last proxy must then overwrite X-Forwarded-For, X-Forwarded-Host and X-Forwarded-Proto.4

Only the entries written by the operator's own proxies can be believed, read from the right. With one proxy, that is the rightmost address; with several, the reader skips known proxy addresses from the right and takes the first that is not on the list.3 Express does this when trust proxy is given addresses such as 'loopback' instead of true.4 nginx behind a CDN does it with set_real_ip_from listing the CDN's ranges and real_ip_recursive on; these come from the realip module, which a build from source includes only with --with-http_realip_module.6 Caddy throws away incoming X-Forwarded-* values unless the sender is in trusted_proxies.2 Spoofed values defeat rate limiting and IP allow lists, and can exhaust memory.3

Headers that must stop at the proxy

A framework can use a request header as an internal signal, and the proxy is the place to drop copies sent from outside. On 21 March 2025 Next.js disclosed that a request carrying x-middleware-subrequest could skip middleware, so an Authorization check placed there never ran; versions 15.2.3, 14.2.25, 13.5.9 and 12.3.5 fixed it, with a CVSS score of 9.1.7 Next.js sets the header itself, when middleware fetches from the app, to mark that middleware has already run and to prevent recursion.8 Self-hosted apps run with next start were affected; Vercel and Netlify route requests in a separate system and were not.8 For apps that could not upgrade, the advised workaround was to keep outside requests with that header from reaching the application.7 In nginx, a header set to an empty string is not forwarded at all.5

Reaching the app around the proxy

A proxy checks only the traffic that passes through it. If the app's own port, such as 3000, also answers from the internet, a client can connect to it directly with any forwarded headers it likes, and then no entry in X-Forwarded-For can be trusted, even for traffic that did come through the proxy.3 Binding the app to the loopback address 127.0.0.1 or a private network closes that path; ports published by Docker need extra care, covered in Docker published ports and firewalls.

When the proxy is a CDN, the origin server's public address is the way around it. Cloudflare's options are an allow list of its own address ranges at the origin, a secret header that the proxy adds and the server checks, client certificates through Authenticated Origin Pulls, and Cloudflare Tunnel, which connects outward from the server so that it needs no public address. It also lists ways the address leaks: DNS history from before the move, SPF and TXT records, and bounce messages from a mail service on the same machine.9

When proxy and app read a request differently

The proxy and the application parse the same bytes separately. In HTTP request smuggling they disagree on where one request ends on a shared HTTP/1 connection, because the length can be given by Content-Length or by Transfer-Encoding; the end of an attacker's request is then read as the start of the next visitor's.10 HTTP/2 used from end to end has a single way to state the length and is immune; an HTTP/2 front end that translates to HTTP/1 for the back end brings the problem back.10 A proxy that routes by whatever Host header it receives can be pointed at internal addresses, the subject of Host header attacks, and of server-side request forgery when the target is the cloud metadata service.

Example: nginx in front of a Node.js app

nginx as the only proxy, on the same server as an app that listens on 127.0.0.1:3000:

location / {
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    # nginx is the first hop: overwrite, do not append
    proxy_set_header X-Forwarded-For $remote_addr;
    # an empty value means the header is not forwarded
    proxy_set_header x-middleware-subrequest "";
}

The Express app then trusts only the loopback address as a proxy:

app.set('trust proxy', 'loopback');

Prompt for a coding agent

This app runs behind a reverse proxy. Find every place the code reads the client IP, host name or scheme (req.ip, X-Forwarded-For, X-Real-IP, X-Forwarded-Host, X-Forwarded-Proto, Host) and check that it trusts only values written by our own proxy: configure trust proxy (or the framework equivalent) with the proxy's address, never true. In the proxy config, overwrite X-Forwarded-For on the first hop instead of appending, drop framework-internal headers such as x-middleware-subrequest, and make sure the app port listens on 127.0.0.1 only. List each file and line you changed.

In SecFlash

SF-SEC-619 on the Security page reports a proxy that forwarded a request whose Host header named the cloud metadata address and got a success back. SF-SRV-103 on the Server page reports a port other than web and SSH that accepts connections from outside, such as an app port left open beside the proxy.

See also

References

  1. a b c Fielding, R., Nottingham, M. and Reschke, J. (June 2022). "RFC 9110: HTTP Semantics", section 3.7 Intermediaries. IETF.
  2. a b c d Caddy. "reverse_proxy". Caddy documentation.
  3. a b c d e f MDN contributors. "X-Forwarded-For header". MDN Web Docs.
  4. a b c d Express. "Express behind proxies". Express documentation.
  5. a b c F5. "Module ngx_http_proxy_module". nginx documentation.
  6. back F5. "Module ngx_http_realip_module". nginx documentation.
  7. a b Vercel (21 March 2025). "Authorization Bypass in Next.js Middleware" (GHSA-f82v-jwr5-mffw, CVE-2025-29927). GitHub Security Advisories.
  8. a b Vercel (25 March 2025). "Postmortem on Next.js Middleware bypass". Vercel Blog.
  9. back Cloudflare. "Protect your origin server". Cloudflare Fundamentals docs.
  10. a b PortSwigger. "HTTP request smuggling". Web Security Academy.