Skip to content

Commit 6419b10

Browse files
committed
Set Express 'trust proxy' to 1 for rate limiting behind Render's proxy
Render forwards the client IP via X-Forwarded-For through a single reverse proxy. With Express's default trust proxy=false, express-rate-limit throws ERR_ERL_UNEXPECTED_X_FORWARDED_FOR and cannot key requests by client IP. Trusting exactly one proxy hop resolves the error without trusting a spoofable header chain. Verified: a request with X-Forwarded-For to the rate-limited /api/login route no longer throws.
1 parent c6201e2 commit 6419b10

1 file changed

Lines changed: 6 additions & 0 deletions

File tree

server.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ function requireAuth(req, res, next) {
172172
// ─────────────────────────────────────────────────────────────────────────────
173173
const app = express();
174174

175+
// Render (and most PaaS hosts) terminate TLS at a single reverse proxy that
176+
// forwards the client IP in X-Forwarded-For. Trust exactly one proxy hop so
177+
// express-rate-limit can identify clients by real IP without trusting a
178+
// spoofable header chain. See ERR_ERL_UNEXPECTED_X_FORWARDED_FOR.
179+
app.set('trust proxy', 1);
180+
175181
const LOCAL_DEV_ORIGINS = [
176182
'http://localhost:5173',
177183
'http://127.0.0.1:5173',

0 commit comments

Comments
 (0)