136 lines
5.2 KiB
JavaScript
136 lines
5.2 KiB
JavaScript
"use strict";
|
|
|
|
// Shared trusted-proxy-aware client-IP extraction.
|
|
//
|
|
// This module MIRRORS index.js's private helpers (TRUSTED_PROXY_IPS /
|
|
// normalizeIp / isLoopbackIp / getDirectRemoteIp / isTrustedProxyIp /
|
|
// parseForwardedIpList / getTrustedForwardedFor / getRequestIp, index.js
|
|
// ~lines 2907-3133 — getRequestIp itself at ~3129) and MUST BE KEPT IN SYNC
|
|
// with that copy: index.js keeps
|
|
// its own private implementation on purpose (a separate review checks the two
|
|
// stay equivalent), while nodes.js — which cannot require index.js because
|
|
// index.js requires nodes.js (circular require) — delegates here.
|
|
//
|
|
// Semantics (identical to index.js):
|
|
// - X-Forwarded-For is honored ONLY when the DIRECT socket peer is a
|
|
// trusted proxy. Trusted = loopback (127.0.0.1 / ::1) or an exact entry
|
|
// of the TRUSTED_PROXY_IPS env var (comma-separated, read once at require
|
|
// time). security.json `trusted_subnets` is deliberately NOT part of
|
|
// proxy trust — in index.js its sole consumer is the anti-abuse limiter
|
|
// bypass (isRequestFromTrustedSubnet), so it is not consulted here
|
|
// either. TRUST_PROXY is likewise not consulted: it only configures
|
|
// Express's req.ip, which these helpers never read (req.ip can itself be
|
|
// XFF-derived, i.e. forgeable).
|
|
// - When the direct peer is trusted, the XFF hop chain is walked from the
|
|
// closest proxy BACKWARDS and the first UNTRUSTED address is returned,
|
|
// so a client-supplied leftmost prefix cannot win. Entries that are not
|
|
// valid IP literals are dropped from the chain. If every hop is trusted,
|
|
// the leftmost forwarded entry is returned.
|
|
// - Otherwise the header is ignored entirely and the direct socket address
|
|
// is used; with no socket address at all (e.g. a UNIX socket) the result
|
|
// is "unknown".
|
|
// - IPv6-mapped IPv4 (::ffff:a.b.c.d) is normalized on both the socket and
|
|
// the header side.
|
|
//
|
|
// Testability: every lookup accepts an optional trailing `options` argument
|
|
// ({ trustedProxyIps: string[] | Set<string> }, also aliased as
|
|
// trustedSubnets) that REPLACES the production env-derived trusted set for
|
|
// that call. Omitting options is production behavior, byte-identical to
|
|
// index.js. Like index.js's env-derived set, entries are matched as exact
|
|
// (trimmed) strings against the NORMALIZED peer/hop address — injected
|
|
// entries are not themselves ::ffff:-normalized, mirroring index.js.
|
|
|
|
const net = require("net");
|
|
|
|
// Mirrors index.js TRUSTED_PROXY_IPS: raw trimmed strings (NOT normalized),
|
|
// membership checked against the normalized peer/hop address.
|
|
const TRUSTED_PROXY_IPS = new Set(
|
|
(process.env.TRUSTED_PROXY_IPS || "")
|
|
.split(",")
|
|
.map(s => s.trim())
|
|
.filter(Boolean)
|
|
);
|
|
|
|
function normalizeIp(raw) {
|
|
const ip = String(raw || "").trim();
|
|
if (ip.startsWith("::ffff:")) return ip.slice(7);
|
|
return ip;
|
|
}
|
|
|
|
function isLoopbackIp(ip) {
|
|
return ip === "127.0.0.1" || ip === "::1";
|
|
}
|
|
|
|
// Resolves the trusted-proxy set for one lookup: the explicitly injected set
|
|
// (tests / callers with their own configuration) when provided, otherwise the
|
|
// production env-derived set shared with index.js.
|
|
function resolveTrustedProxyIps(options) {
|
|
const injected = options && (options.trustedProxyIps !== undefined
|
|
? options.trustedProxyIps
|
|
: options.trustedSubnets);
|
|
if (injected === undefined || injected === null) return TRUSTED_PROXY_IPS;
|
|
if (injected instanceof Set) return injected;
|
|
return new Set(
|
|
(Array.isArray(injected) ? injected : [injected])
|
|
.map((entry) => String(entry || "").trim())
|
|
.filter(Boolean)
|
|
);
|
|
}
|
|
|
|
function getDirectRemoteIp(req) {
|
|
return normalizeIp(req.connection?.remoteAddress || req.socket?.remoteAddress || "");
|
|
}
|
|
|
|
function isTrustedProxyIp(ip, options) {
|
|
const normalized = normalizeIp(ip);
|
|
if (!normalized) return false;
|
|
if (isLoopbackIp(normalized)) return true;
|
|
return resolveTrustedProxyIps(options).has(normalized);
|
|
}
|
|
|
|
function parseForwardedIpList(value) {
|
|
const rawValues = Array.isArray(value) ? value : [value];
|
|
return rawValues
|
|
.flatMap((entry) => String(entry || "").split(","))
|
|
.map(normalizeIp)
|
|
.filter((ip) => ip && net.isIP(ip));
|
|
}
|
|
|
|
function getTrustedForwardedFor(req, options) {
|
|
const direct = getDirectRemoteIp(req);
|
|
if (!isTrustedProxyIp(direct, options)) return "";
|
|
|
|
const forwardedIps = parseForwardedIpList(req.headers["x-forwarded-for"]);
|
|
if (!forwardedIps.length) return "";
|
|
|
|
// Walk the hop chain from the closest proxy back towards the client and
|
|
// return the first untrusted IP. This matches how proxy trust should work
|
|
// and prevents client-supplied X-Forwarded-For prefixes from winning.
|
|
const chain = forwardedIps.concat(direct);
|
|
for (let i = chain.length - 1; i >= 0; i -= 1) {
|
|
const hop = chain[i];
|
|
if (!hop) continue;
|
|
if (!isTrustedProxyIp(hop, options)) return hop;
|
|
}
|
|
|
|
return forwardedIps[0] || "";
|
|
}
|
|
|
|
function getRequestIp(req, options) {
|
|
const forwarded = getTrustedForwardedFor(req, options);
|
|
const direct = getDirectRemoteIp(req);
|
|
return forwarded || direct || "unknown";
|
|
}
|
|
|
|
module.exports = {
|
|
TRUSTED_PROXY_IPS,
|
|
normalizeIp,
|
|
isLoopbackIp,
|
|
resolveTrustedProxyIps,
|
|
getDirectRemoteIp,
|
|
isTrustedProxyIp,
|
|
parseForwardedIpList,
|
|
getTrustedForwardedFor,
|
|
getRequestIp,
|
|
};
|