skip to content

Production Hardening & Deployment

Security headers, CORS, rate limiting, compression and correct behaviour behind a reverse proxy separate a working Express app from a deployable one. Interviewers ask because Express ships none.

on this pageshow

questions

6

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.
open as a page

An Express 5 API behind a load balancer mounts express-rate-limit, and soon all users get 429 together; why, and what is the right fix?

level: seniorimportance: must knowfreq 52%

basics

~20 s

express-rate-limit keys clients by req.ip, and with Express's default trust proxy false that is the load balancer's address, so every user shares one bucket. Set app.set('trust proxy', 1) or the balancer's subnet, never true, which lets clients forge X-Forwarded-For.

open as a page

In an Express 5 app, what does app.use(helmet()) do by default, and what does it not protect you from?

level: juniorimportance: should knowfreq 55%

basics

~10 s

Helmet is middleware that sets security-related HTTP response headers, such as a strict Content-Security-Policy, Strict-Transport-Security and X-Content-Type-Options: nosniff, and removes X-Powered-By. It only instructs browsers; it does not validate, authenticate or configure CORS.

open as a page

With the cors package in an Express 5 API, how do the origin and credentials options combine for a browser frontend that sends cookies?

level: middleimportance: should knowfreq 50%

basics

~20 s

In an Express 5 API, cors({ credentials: true }) alone still sends Access-Control-Allow-Origin: *, which browsers refuse on cookie-carrying requests. Set origin to the exact frontend origin, an array, a RegExp or a callback, so a matched origin is echoed.

open as a page

After an upgrade to Express 5, express.static serving a built frontend returns 404 for /.well-known/ files; why, and how should its cache options be set?

level: seniorimportance: should knowfreq 30%

basics

~20 s

In Express 5, express.static's dotfiles default 'ignore' also covers hidden directories in the path, so /.well-known/ is treated as missing and ends in 404. Serve that directory from its own mount, and set maxAge with immutable only for fingerprinted assets.

open as a page

As the lead for many Express 5 services, which Express defaults and ecosystem middleware would you make mandatory in a shared bootstrap, and what does that cost?

level: principalimportance: should knowfreq 28%

basics

~20 s

Express defaults to x-powered-by on, env 'development' without NODE_ENV, trust proxy false and no security middleware. A shared bootstrap fixes those and mounts helmet, cors where needed, express-rate-limit and a health route; TLS and bulk compression usually stay at the proxy.

open as a page