mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user