UNPKG

@sentry/core

Version:
30 lines (29 loc) • 1.48 kB
import { getClientIPAddress as getClientIPAddress$1, ipHeaderNames } from "../vendor/getIpAddress.js"; // src/utils/clientIPAddress.ts const IP_HEADER_NAMES = new Set(ipHeaderNames.map((name) => name.toLowerCase())); /** * Get the IP address of the client sending a request, from its forwarding headers. * * The vendored implementation accepts only bare addresses, so the header values are * normalized first. Some proxies (for example Azure App Service) add the client port, * and RFC 7239 allows a quoted `Forwarded` value and any case for `for=`. */ function getClientIPAddress(headers) { const normalized = {}; for (const [key, value] of Object.entries(headers)) { const name = key.toLowerCase(); if (value == null || !IP_HEADER_NAMES.has(name)) continue; const joined = Array.isArray(value) ? value.join(",") : value; normalized[key] = name === "forwarded" ? normalizeForwardedHeader(joined) : joined.split(",").map((ip) => stripPort(ip.trim())).join(","); } return getClientIPAddress$1(normalized); } function normalizeForwardedHeader(value) { const forPair = value.split(/[,;]/).map((part) => part.trim()).find((part) => part.slice(0, 4).toLowerCase() === "for="); return forPair ? `for=${stripPort(forPair.slice(4).replace(/^"(.*)"$/, "$1"))}` : ""; } function stripPort(ip) { return ip.match(/^\[([^\]]+)\](?::\d+)?$/)?.[1] ?? ip.match(/^(\d{1,3}(?:\.\d{1,3}){3}):\d+$/)?.[1] ?? ip; } export { getClientIPAddress }; //# sourceMappingURL=clientIPAddress.js.map