fix(security): don't trust loopback socket as local when request arrives via reverse proxy

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
decolua
2026-06-17 11:12:24 +07:00
co-authored by Cursor
parent 2a619655b8
commit da667836cc
2 changed files with 8 additions and 0 deletions
+5
View File
@@ -11,9 +11,14 @@ http.createServer = (...args) => {
if (!handler) return origCreate(...args);
const wrapped = (req, res) => {
const ip = req.socket && req.socket.remoteAddress ? req.socket.remoteAddress : "";
// Forwarding headers present = request arrived via a reverse proxy; loopback
// socket is the proxy hop, not the end-user, so it must not be trusted as local.
const viaProxy = !!(req.headers["x-forwarded-for"] || req.headers["x-real-ip"]);
delete req.headers["x-9r-real-ip"];
delete req.headers["x-forwarded-for"];
delete req.headers["x-9r-via-proxy"];
req.headers["x-9r-real-ip"] = ip;
if (viaProxy) req.headers["x-9r-via-proxy"] = "1";
return handler(req, res);
};
return origCreate(...rest, wrapped);