Skip to content

Lesson 4 of 14 · 16m

TLS Ends Before Node Does

A proxy terminates TLS and tells your app the truth in `X-Forwarded-*` headers — trust exactly the hops you control.

Most Node.js apps never see a TLS handshake. An nginx, an ALB, Cloudflare, or a Kubernetes ingress terminates TLS at the edge and speaks plain HTTP/1.1 to your process on 127.0.0.1:3000 or a private VPC port. Everything your app "knows" about the client — their IP, that the request was HTTPS, the original Host — arrives in X-Forwarded-* headers the proxy wrote. Your app trusts them correctly or it is blind.

The headers that carry reality

X-Forwarded-For: 203.0.113.7, 10.0.0.2     ← claimed client, then each proxy hop
X-Forwarded-Proto: https
X-Forwarded-Host: api.example.com

X-Forwarded-For is a list: leftmost is the claimed original client — attacker-writable — and each proxy appends the peer it actually observed. Read it from the right. The last entry is what your trusted edge saw; req.socket.remoteAddress will faithfully report your nginx's private address, which is true and completely useless for rate limiting or audit logs. Chains stack: Cloudflare → ALB → app means XFF has three entries and you trust only what your edge appended. Cloudflare also sends CF-Connecting-IP with the true client — a CDN-specific header worth knowing alongside the generic Forwarded header from RFC 7239, which standardized what X-Forwarded-* had done by convention for a decade. nginx's own convention is proxy_set_header X-Real-IP $remote_addr; — one authoritative client IP for setups that would rather not parse the list.

The classic bug: Secure cookies behind a proxy

Express decides req.secure from the socket — and the socket to nginx is plain HTTP. express-session with cookie: { secure: true } checks req.secure before writing Set-Cookie: behind a proxy with no trust proxy, it silently decides the request isn't HTTPS and the session cookie never reaches the client; req.ip returns the proxy's address. res.cookie itself has no such check — it emits the header unconditionally; the suppression lives in the session middleware, and it is silent, which is why the bug survives code review. app.set("trust proxy", 1) tells Express to believe X-Forwarded-* from the first hop — one line, present in every behind-a-proxy deploy.

express-rate-limit is the same bug from the other direction: it logs ERR_ERL_UNEXPECTED_X_FORWARDED_FOR when it sees the header but trust proxy is off — without it every user shares the proxy's single IP, so one bad client rate-limits your entire site. Trust it too far the other way and any client can send X-Forwarded-For: 1.2.3.4 to mint a fresh identity per request. Trust exactly the hops you control — usually one.

WebSockets ride the same proxy

A ws upgrade is an ordinary GET carrying Upgrade: websocket — through nginx it needs proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; or the handshake dies at the edge with a 400 you will spend an hour blaming on your library. Long-lived sockets then hit proxy_read_timeout — 60 s by default — so a quiet WebSocket dies mid-connection unless heartbeats or a raised timeout keep it warm. HAProxy's PROXY protocol is the binary-header answer for L4 proxies that can't inject HTTP headers at all — it prepends the real client IP before the HTTP bytes.

X-Forwarded-Host and X-Forwarded-Proto matter anywhere the app builds absolute URLs — password-reset links, OAuth callbacks, Location redirects. Get them wrong and the app generates http://10.0.0.2:3000/reset?token=… links that go nowhere or leak tokens through an internal hostname.

Origin lockdown

The edge is only protection if the origin can't be reached directly. If app.internal-ip:3000 answers public traffic, every edge control — WAF, rate limits, mTLS — is decorative. Bind to a private interface and firewall the port to proxy IPs; Cloudflare publishes its ranges for exactly this. The cheap version of the same lockdown: the edge stamps a secret header of your choosing, and the app rejects any request missing it — poor-man's mTLS without the certificate plumbing.

The practice

  • Bind Node to 127.0.0.1 or a private interface. Nothing off-VPC should reach the port — the proxy is the only public listener.
  • trust proxy set to the number of hops you control, never true.
  • HTTPS redirects and HSTS happen at the edge, not in app code.
  • Client IP for auth and audit: the rightmost X-Forwarded-For entry your edge appended — never a client-supplied one.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.