Skip to content

Trust proxy and client IPs

In short

The threat

Behind a proxy the client’s address arrives in a header the client can write too; read it wrong and the client picks its own IP, or every client shares the proxy’s.

The rule

Count exactly as many hops as you run proxies, from the right, or trust the proxies by address; true is refused.

In the package
http/ip.ts, configured as trustedHops in the adapters.

Every per-IP limit and every audit row depends on the client’s IP. Behind a reverse proxy, a load balancer or a CDN, the socket address is the proxy’s, and the client’s address arrives in X-Forwarded-For, a header each proxy appends to. The client can send that header too, with any entries it likes, and the proxies keep them.

  • Read the header from the wrong end and the client picks its own IP: a fresh rate-limit bucket on every request, and any address it likes in your audit trail.
  • Ignore the header behind a proxy and every client has the proxy’s address: one bucket for everyone, so one attacker who uses up the login limit blocks everyone’s login.
Trust exactly as many hops as you run proxies: one too few gives every client the proxy's address, one too many lets the client choose its own.

One proxy sits in front of the app. The request arrives with X-Forwarded-For 192.0.2.66, 198.51.100.23 from the socket address 203.0.113.9. The client wrote 192.0.2.66 itself; the proxy appended 198.51.100.23, the address it saw the request come from; 203.0.113.9 is the proxy. clientIp puts the socket address after the header and steps back trustedHops entries from the right. With 0 trusted hops it picks 203.0.113.9, the proxy, one bucket for everyone. With 1 trusted hops it picks 198.51.100.23, the client, as the proxy saw it. With 2 trusted hops it picks 192.0.2.66, the client's own words. Only the count that matches the proxies you run stops before the entries the client wrote.

Text version

Trust the proxies you run and nothing past them, either by count or by address.

clientIp takes the socket address, the header and the number of proxies in front of the app (trustedHops). It puts the socket address after the header’s entries and steps back that many entries from the right. Entries to the left of the ones your proxies wrote are the client’s own words and are never picked. With 0 trusted hops the header is ignored and the socket address is the client IP.

An example, with one proxy in front of the app:

X-Forwarded-For: 192.0.2.66, 198.51.100.23
socket address: 203.0.113.9

The client wrote 192.0.2.66 itself. The proxy, at 203.0.113.9, appended 198.51.100.23, the address it saw the request come from.

Count Which address, and why
With 1 trusted hop the client IP is 198.51.100.23 Right.
With 0 trusted hops it is 203.0.113.9 The proxy, shared by every client.
With 2 trusted hops it is 192.0.2.66 The address the client chose.

Some gateways write the client’s source port into the header after its address, and a misconfigured proxy can write anything. The source port changes per connection, so taken as written it would give every connection its own per-IP bucket. Whichever way the entry is picked, by count or by list, clientIp strips a port (203.0.113.9:51234 is 203.0.113.9), brackets ([2001:db8::1]:443 is 2001:db8::1) and an IPv6 zone id (fe80::1%eth0 is fe80::1), and what is left must be exactly one IPv4 or IPv6 address, an IPv4-mapped one included. An entry that is not (not-an-ip, a port out of range, a bracketed IPv4 address) is never returned: the socket address stands in for it, or unknown when there is none (an empty socket address counts as none, in every branch). An IPv6 address without brackets is taken whole, because its last group cannot be told apart from a port: with a port glued on, it is either read as a different address or refused. No rule can strip that port safely, because the entry with the port glued on is often a valid address of its own, so clientIp keeps taking it whole. If your gateway writes IPv6 entries with a port and no brackets, fix the gateway to bracket them or to leave the port out.

With a list, an entry that is not an address is never trusted, so the walk stops there and the socket address is used; entries further left are never read.

A count higher than the proxies you run reaches into the entries the client wrote, as the last case shows. When the chain is shorter than the count, the leftmost entry is used, and that is the client’s too. Without a socket address (a fetch adapter given no peer address) the count picks the same entry as with one: the socket address only fills in when the header is empty.

parseTrustProxy reads a TRUST_PROXY-style setting:

  • an all-digit value is a hop count;
  • false or an empty value means none;
  • true, which trusts every hop and so lets the client choose its IP, throws a configuration error;
  • other values, such as an address list, are passed through, for resolveTrustedProxies below.

When your proxies have known addresses, give trustedHops a list instead of a count: addresses, CIDR ranges (IPv4 or IPv6, address and prefix length), and the Express presets loopback (127.0.0.1/8, ::1/128), linklocal (169.254.0.0/16, fe80::/10) and uniquelocal (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, fc00::/7), the same ranges Express gives them.

X-Forwarded-For is then read only when the socket address is inside the list, and clientIp walks it from the right, skipping every address inside the list; the first one outside it is the client, as Express does. This is stronger than a count: a client that reaches the app directly, around the proxy, is never taken for one.

Socket addresses are matched as the server reports them. An IPv4-mapped socket address (an IPv4 address in IPv6 form, as a dual-stack server reports it) matches the IPv4 ranges, and the zone id a socket adds to a link-local IPv6 peer (after a percent sign) is dropped before matching, so linklocal works.

A list that would trust everything is refused. resolveTrustedProxies('loopback, uniquelocal') checks a list and expands its presets, and refuses at boot an unknown name, a malformed range, and a range that holds every address (a prefix length of zero, or an IPv6 range that holds every IPv4-mapped address), which would trust every hop just as true does. On the request path a list that would be refused trusts nothing: clientIp never throws for it.

An edge header, only through a trusted hop

Section titled “An edge header, only through a trusted hop”

Some edges put the client’s address in a single-address header of their own, such as cf-connecting-ip. Name it in clientIpHeader (off by default) and clientIp takes it, but only when:

  • trustedHops is a proxy list,
  • the socket address is inside it,
  • and the value is exactly one valid IP.

A missing header, a list, an address with a port or anything else that is not one address falls back to the X-Forwarded-For rule. A hop count never looks at the socket address, so a client that reaches the app directly would be taken for the edge and could write the header itself: clientIpHeader with a hop count is refused at boot. X-Forwarded-For itself is refused as clientIpHeader.

The adapters make the setting impossible to forget

Section titled “The adapters make the setting impossible to forget”

The NestJS and tRPC adapters require trustedHops, a whole number from 0 to 16 or a proxy list, take clientIpHeader as an option (only with a proxy list), and read the IP only through clientIp, never from the framework’s own req.ip.

In NestJS, trustedHopsFromEnv(process.env) reads TRUST_PROXY: a hop count, or a comma-separated list such as loopback, uniquelocal, returned with its presets expanded; unset is 0 in development and refused when NODE_ENV is production, so a deploy that forgot it fails at boot.

configureTrustProxy sets Express’s trust proxy to the same count or list, so req.ip in your own code agrees with the flows (Express does not read clientIpHeader). A request with no address at all gets the IP unknown, a single bucket shared by all such requests.

Limit Keyed on A client that can choose its IP
The per-IP door limits (login allows 10 attempts per minute from one IP by default) The IP Gets fresh ones on every request
The per-account-and-IP lockout budget (5 attempts per 15 minutes) The IP Gets fresh ones on every request
The per-account budget (10 attempts per 15 minutes from all sources) The address Stays bounded: guessing at one account is capped

What a spoofed IP buys is the rest: sign-ups and reset requests from one machine past their per-IP limits, password guessing spread thinly over many accounts, and any address it likes in your audit trail and session lists.

Count the proxies between the internet and the app that append to X-Forwarded-For: a CDN in front of a load balancer in front of the app is two. Then check it rather than trust the diagram:

  • From outside your network, send a request with a made-up X-Forwarded-For: 192.0.2.1.
  • Log the raw header and the socket address on the server.
  • Confirm that the IP clientIp returns is your own public address: not 192.0.2.1, and not one of your proxies.

A proxy that replaces the header instead of appending to it, or one that adds nothing, changes the count, which is why the check matters more than the count of boxes. Run it again after any change to your hosting.

The IP a plain Node server hands the flows. The adapters do the same for you; the NestJS guide has the TRUST_PROXY setup.

trust-proxy.ts
import type { IncomingMessage } from 'node:http';
import { clientIp } from '@noctcore/auth';
import { config } from '../build/node-server/config';
/**
* The IP every per-IP limit and audit row uses. Never the raw header, and
* never a framework's own guess: the hop count or proxy list decides.
*/
export function requestIp(req: IncomingMessage): string {
return (
clientIp({
remoteAddress: req.socket.remoteAddress,
forwardedFor: req.headers['x-forwarded-for'],
// From TRUST_PROXY: a hop count, or proxy addresses and ranges.
trustedHops: config.trustedHops,
}) ?? 'unknown'
);
}

The guide has no page on proxies or X-Forwarded-For.

Its passwords page warns against strict IP-based rate limits, which block people on shared networks and are bypassed through proxy networks; the package agrees, keeps its per-IP door limits generous and limits guessing per account (see Brute force and lockout).

Its sessions page advises against binding a session to an IP, because addresses change on mobile networks and VPNs; the package records the IP with a session for the session list but never refuses a session because the IP changed.

Test Attack Stopped by
A011 Spoofing the client IP http/ip.ts
A025 Choosing your own client IP through the Nest adapter auth-nestjs/request.ts, auth-nestjs/trust-proxy.ts
A081 Choosing your own IP with a spoofed edge header http/ip.ts
A082 Posing as a trusted proxy http/ip.ts, http/ip-ranges.ts
A105 A forwarded entry with a port, or that is not an address http/ip.ts