UNPKG

@nuxt/cli

Version:
78 lines (77 loc) • 3.2 kB
import { isIP } from "node:net"; // src/dev/host-check.ts /** * Hostname of an HTTP `Host` header: lowercased, port stripped, IPv6 brackets * removed. `undefined` for a value that cannot be a hostname at all. */ function parseHostHeader(host) { if (!host) return; const value = host.trim().toLowerCase(); if (value.startsWith("[")) { const end = value.indexOf("]"); return end > 1 ? value.slice(1, end) : void 0; } return value.split(":")[0] || void 0; } /** * Whether a socket's peer address is on this machine: the `127.0.0.0/8` range * or `::1`, in bracketed, zone-suffixed or IPv4-mapped form. */ function isLoopbackAddress(address) { if (!address) return false; let value = address.trim().toLowerCase(); if (value.startsWith("[")) { const end = value.indexOf("]"); if (end < 2) return false; value = value.slice(1, end); } const zone = value.indexOf("%"); if (zone !== -1) value = value.slice(0, zone); if (value.startsWith("::ffff:")) value = value.slice(7); if (value === "::1") return true; if (isIP(value) !== 4) return false; return value.startsWith("127."); } /** * Normalise an `allowedHosts` value: `true` allows any host, an * array contributes its string entries, and anything else contributes nothing. */ function resolveConfiguredAllowedHosts(value) { if (value === true) return true; if (Array.isArray(value)) return value.filter((host) => typeof host === "string"); } /** * The union of `devServer.allowedHosts` and `vite.server.allowedHosts` in a * resolved Nuxt config. `true` in either allows any host. */ function resolveNuxtAllowedHosts(options) { const devServer = resolveConfiguredAllowedHosts((options?.devServer)?.allowedHosts); const vite = resolveConfiguredAllowedHosts(options?.vite?.server?.allowedHosts); if (devServer === true || vite === true) return true; if (!devServer && !vite) return; return [.../* @__PURE__ */ new Set([...devServer ?? [], ...vite ?? []])]; } /** * Whether a request's `Host` header names this dev server or is allowed by the * project, guarding the CLI's own endpoints (the progress stream, the loading * page and the error page) against DNS rebinding, where a hostname the * attacker controls resolves to this machine and a page in the developer's * own browser becomes same-origin with the dev server. * * IP literals are always accepted: rebinding needs a DNS name, and devices on * a permitted network reach a non-loopback bind by address. A missing `Host` * is accepted too, since it cannot come from a browser. An entry with a * leading dot, as in Vite's `server.allowedHosts`, also allows the domain and * all its subdomains. */ function isAllowedHost(host, allowedHosts) { if (!host) return true; const hostname = parseHostHeader(host); if (!hostname) return false; if (hostname === "localhost" || hostname.endsWith(".localhost")) return true; if (isIP(hostname)) return true; if (allowedHosts.has(hostname)) return true; for (const entry of allowedHosts) if (entry.startsWith(".") && (hostname === entry.slice(1) || hostname.endsWith(entry))) return true; return false; } export { resolveNuxtAllowedHosts as i, isLoopbackAddress as n, resolveConfiguredAllowedHosts as r, isAllowedHost as t };