fix: strip port from X-Forwarded-For header to get real IP
This commit is contained in:
+7
-1
@@ -221,7 +221,13 @@ function makeBodyAndType(req, clientIp, defaultLook) {
|
||||
const xHeaders = collectXHeaders(req);
|
||||
const { proxyHeadersBlock, proxyHeadersHtml } = proxyMarkup(xHeaders);
|
||||
|
||||
const xff = (req.headers["x-forwarded-for"] || "").split(",")[0].trim();
|
||||
let xff = (req.headers["x-forwarded-for"] || "").split(",")[0].trim();
|
||||
// strip port: [::1]:port -> ::1, 1.2.3.4:port -> 1.2.3.4
|
||||
if (xff.startsWith("[")) {
|
||||
xff = xff.slice(1, xff.includes("]") ? xff.indexOf("]") : xff.length);
|
||||
} else if ((xff.match(/:/g) || []).length === 1) {
|
||||
xff = xff.split(":")[0];
|
||||
}
|
||||
let ipText, ipHtml;
|
||||
if (xff) {
|
||||
ipText = `${xff} (forwarded by proxy ${clientIp})`;
|
||||
|
||||
Reference in New Issue
Block a user