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.comX-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.1or a private interface. Nothing off-VPC should reach the port — the proxy is the only public listener. trust proxyset to the number of hops you control, nevertrue.- HTTPS redirects and HSTS happen at the edge, not in app code.
- Client IP for auth and audit: the rightmost
X-Forwarded-Forentry your edge appended — never a client-supplied one.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Node Runs TypeScript Now. Here's Exactly What It Strips, What It Refuses, and What It Costs
Node 24 and 26 run .ts files with no build step and no flags, by replacing types with whitespace. I ran eleven test cases, the TypeScript 7 compiler and tsx against both versions: enums, parameter properties and type imports without the type keyword all break, type errors run anyway, and...
Node.js vs Deno vs Bun: Picking the Right JavaScript Runtime in 2026
Three JavaScript runtimes. Three different philosophies on what the developer experience should feel like. Here's how they compare on performance, ecosystem, tooling, and the only decision that actually matters for production.
What is Bun? The JavaScript Runtime That Made npm Install Fast
Bun is a JavaScript runtime, package manager, bundler, and test runner in a single binary written in Zig. It's also the fastest thing to happen to JavaScript tooling in years — and it runs your existing Node.js code.
What is Deno? Ryan Dahl's Second Attempt at a JavaScript Runtime
In 2018, Ryan Dahl stood at JSConf EU and apologized for Node.js. Then he built Deno to fix the things he got wrong. Here's what changed, what the security model actually looks like, and whether switching is worth it.