skip to content

In Express 5, what does the trust proxy setting change, and how do false, true, a hop count and a subnet list derive req.ip?

level: middleimportance: must knowfreq 62%

answer

  1. default trusts nobody
  2. socket address is hop one
  3. X-Forwarded-For read right to left
  4. leftmost entry is client-writable

basics

~20 s

Express's trust proxy setting decides which X-Forwarded-* headers to believe. At the default false, req.ip is the socket peer, usually the balancer; true takes the client-writable leftmost X-Forwarded-For entry; a hop count or subnet list walks right to left past trusted proxies.

solid answer

~40 s

`trust proxy` defaults to `false`: Express treats the app as facing the client, so `req.ip` is `req.socket.remoteAddress`, which behind a load balancer is the balancer. `true` makes `req.ip` the **leftmost** `X-Forwarded-For` entry, which any client can write. A number `n` trusts `n` hops: the socket address is the first hop and the rest are read from `X-Forwarded-For` right to left, so `app.set('trust proxy', 1)` behind one balancer yields the address that balancer appended. A subnet list such as `'loopback, uniquelocal'` or explicit addresses skips trusted proxies from the right and stops at the first untrusted one; a function decides per address. The same setting drives `req.ips`, `req.protocol` and so `req.secure` from `X-Forwarded-Proto`, and `req.hostname` from `X-Forwarded-Host`.

go deeper

for a junior

Recall that the default is false, so behind a proxy req.ip is the proxy's address, and that the setting is changed with app.set('trust proxy', ...).

for a middle

Explain hop one as the socket address, X-Forwarded-For read right to left, and why true picks the client-writable leftmost entry.

for a senior

Show you can pick the value for a real topology, CDN plus balancer or a private subnet, verify it with a req.ip echo, and spot the different-length-path trap.

for a principal

Treat trust proxy as platform-owned configuration tied to the network topology, and decide who changes it when the edge changes.

## The problem the setting solves Behind a reverse proxy or load balancer, the TCP connection that reaches the Express app comes **from the proxy**, not from the user. The proxy passes the original facts along in request headers it adds: `X-Forwarded-For` (the chain of client and proxy addresses), `X-Forwarded-Proto` (whether the user spoke `https` or `http`) and `X-Forwarded-Host`. Any client can send those headers too, so Express does not believe them unless told whom to trust. That is what the **`trust proxy` application setting** is: a rule for which addresses in the chain are proxies you operate. Express implements it with the `proxy-addr` package. Two facts anchor everything: - the address of the direct peer, `req.socket.remoteAddress`, is always **hop one** and cannot be forged by the client; - `X-Forwarded-For` is read **from right to left**, because each proxy appends the address it received the connection from, so the rightmost entries were written by the proxies closest to you. ## The values and what req.ip becomes Take an app behind one load balancer at `10.0.0.5`. A client at `203.0.113.7` sends a forged `X-Forwarded-For: 198.51.100.9`; the balancer appends the real peer, so the app sees `X-Forwarded-For: 198.51.100.9, 203.0.113.7` on a socket from `10.0.0.5`. | `trust proxy` value | Rule | `req.ip` in the example | |---|---|---| | `false` (default) | App faces the client directly | `10.0.0.5`, the balancer | | `true` | Trust everything; client is the **leftmost** entry | `198.51.100.9`, the forged value | | `1` | Trust one hop: the socket peer | `203.0.113.7`, the real client | | `'uniquelocal'` or `'10.0.0.0/8'` | Skip trusted addresses from the right | `203.0.113.7` | | `(ip, i) => ...` | Your function decides per address | whatever it allows | Pre-configured subnet names are `loopback` (`127.0.0.1/8`, `::1/128`), `linklocal` (`169.254.0.0/16`, `fe80::/10`) and `uniquelocal` (the private ranges `10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16` and `fc00::/7`); they can be combined as a comma-separated string or an array, mixed with literal addresses. ## What else the setting changes Enabling it affects more than `req.ip`: - **`req.ips`** lists the addresses of the trusted chain, farthest first, without the socket address (empty at the default `false`); - **`req.protocol`** reads the first `X-Forwarded-Proto` value when the socket peer is trusted, so **`req.secure`** becomes `true` for users who arrived over HTTPS at a TLS-terminating balancer; - **`req.hostname`** comes from `X-Forwarded-Host`. Anything built on those properties inherits the setting: rate limiters keyed by `req.ip`, access logs, redirect builders that use `req.protocol`, and cookie middleware that only sets a secure cookie when `req.secure` is true (the session-store material covers that case). ## Choosing a value safely 1. **Never use `true` in front of the internet** unless your last proxy overwrites `X-Forwarded-For`, `X-Forwarded-Host` and `X-Forwarded-Proto`; the Express guide warns that otherwise the client can supply any value. 2. **Count hops honestly.** A CDN in front of a balancer is two appending proxies, so `2`. The guide warns that a hop count is unsafe if there are several paths of different lengths into the app, because a client on a shorter path is fewer hops away. 3. **Prefer a subnet list when the proxy addresses are stable**, because it trusts *who* the proxies are rather than how many there are. 4. **Verify it** with a temporary route that returns `req.ip`, called from outside, and compare it with your public address. Express reads only the `X-Forwarded-*` headers here; the standardised `Forwarded` header is not consulted, so a proxy that sends only `Forwarded` needs custom handling. ## Details that catch people out - **It is evaluated per request.** `req.ip`, `req.ips` and `req.protocol` are getters that consult the compiled trust function each time they are read, so the setting only has to be in place before traffic arrives; there is no ordering relationship with middleware. - **Sub-apps inherit it.** An Express app mounted inside another with `app.use('/admin', adminApp)` picks up the parent's `trust proxy` unless it sets its own. - **Development hides the bug.** Locally there is no proxy, so `req.ip` looks right with the default and the mistake only shows up once a balancer is in front. - **The proxy has to cooperate.** Trusting a hop is only safe if that proxy appends the real peer to `X-Forwarded-For` and sets `X-Forwarded-Proto` itself rather than passing a client's value through. ## Version note This setting behaves the same way in Express 4 and Express 5; the upgrade did not change it.

  • How do you find the right trust proxy value for a deployment?
    Count the proxies that append to `X-Forwarded-For` between the user and the app, for example two for a CDN plus a balancer, or list their subnets. Then add a temporary route that returns `req.ip`, call it from outside and compare with your public address. Prefer subnets when proxy addresses are stable, because a hop count breaks if some traffic can reach the app through a shorter path.
  • Which request properties besides req.ip change when trust proxy is enabled?
    `req.ips` exposes the trusted chain; `req.protocol` takes the first `X-Forwarded-Proto` value when the socket peer is trusted, which flips `req.secure` to true for HTTPS users behind a TLS-terminating balancer; and `req.hostname` comes from `X-Forwarded-Host`. Anything reading those, from redirects to secure-cookie logic, depends on the setting.

A building receptionist reads the visitor badge clipped on by the front desk. Trusting one hop means believing the badge the front desk wrote; trusting everything means believing whatever the visitor scribbled on the badge before reaching the desk.

  • Setting trust proxy to true is the safe choice behind any load balancer.
  • With trust proxy enabled, req.ip is always the leftmost X-Forwarded-For entry.
  • app.set('trust proxy', 1) trusts the first address listed in X-Forwarded-For.
  • trust proxy only affects req.ip; req.protocol and req.hostname ignore it.
  • Express reads the standard Forwarded header once trust proxy is set.