UNPKG

@sentry/core

Version:
31 lines (30 loc) • 1.26 kB
import { isURLObjectRelative, parseStringToURLObject } from "./url.js"; // src/utils/localhost.ts const LOCAL_IPS = ["127.0.0.1", "::1"]; const LOCAL_DOMAINS = ["127.0.0.1", "localhost"]; const HOST_HEADERS = ["host", "x-forwarded-host"]; /** * Whether a request looks like it was served from the developer's own machine. * * Ported from Relay's localhost inbound filter so that spans and events are judged by the same rule. */ function isLocalhostRequest(request, ipAddress) { if (ipAddress && LOCAL_IPS.includes(ipAddress)) return true; const url = request?.url ? parseStringToURLObject(request.url) : void 0; if (url && !isURLObjectRelative(url)) { if (url.protocol === "file:") return true; if (LOCAL_DOMAINS.some((domain) => hostMatchesOrIsSubdomainOf(url.hostname, domain))) return true; } const headers = request?.headers; if (headers) for (const [name, value] of Object.entries(headers)) { if (!HOST_HEADERS.includes(name.toLowerCase())) continue; const host = value?.split(":")[0]; if (host && LOCAL_DOMAINS.includes(host)) return true; } return false; } function hostMatchesOrIsSubdomainOf(host, domain) { return host === domain || host.endsWith(`.${domain}`); } export { isLocalhostRequest }; //# sourceMappingURL=localhost.js.map