// Which machines the peer relay may reach (docs/notes/security-boundary.md). // // A signed-in browser names other Pipelines machines by URL and this proxy // fetches /login, /files, /artifacts, /updates and /connect from them, so an // unrestricted address would let any signed-in user make this machine request // its own loopback services, link-local cloud metadata or arbitrary hosts. // Allowed by default: tailnet addresses (100.64.0.0/10 and fd7a:115c:a1e0::/48, // minus Tailscale's own 100.100.100.100 service), private LANs (RFC 1918 and // IPv6 unique-local fc00::/7), and public addresses only for *.ts.net names // (Tailscale Funnel) or hosts this machine's own discovery reported. Loopback, // unspecified, link-local, metadata, multicast and reserved addresses are always // refused. PIPELINES_PEER_ALLOW=loopback,public,link-local,metadata (or "any") // widens the policy explicitly. // // A name is resolved once per request and the connection is pinned to the // checked address (the request's `lookup`), so a DNS answer cannot change // between the check and the connect (rebinding). import { lookup as dnsLookup } from "node:dns/promises"; import net from "node:net"; const ALWAYS_REFUSED = new Set(["unspecified", "multicast", "reserved"]); const OPTIONAL = new Set(["loopback", "public", "link-local", "metadata"]); // Hosts the local server's LAN discovery returned (proxyDiscovery), bounded. export const discoveredHosts = new Set(); export function rememberDiscovered(body) { try { for (const server of JSON.parse(body)?.discovered_servers ?? []) { const host = new URL(server.endpoint).hostname.toLowerCase(); if (discoveredHosts.size >= 256) discoveredHosts.delete(discoveredHosts.values().next().value); discoveredHosts.add(host); } } catch {} } function v4(address) { const parts = address.split(".").map(Number); return ((parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3]) >>> 0; } const inV4 = (value, base, bits) => bits === 0 || value >>> (32 - bits) === v4(base) >>> (32 - bits); function classifyV4(address) { const value = v4(address); if (inV4(value, "0.0.0.0", 8)) return "unspecified"; if (inV4(value, "127.0.0.0", 8)) return "loopback"; if (value === v4("169.254.169.254") || value === v4("100.100.100.100")) return "metadata"; if (inV4(value, "169.254.0.0", 16)) return "link-local"; if (inV4(value, "100.64.0.0", 10)) return "tailnet"; if (inV4(value, "10.0.0.0", 8) || inV4(value, "172.16.0.0", 12) || inV4(value, "192.168.0.0", 16)) return "private"; if (inV4(value, "224.0.0.0", 4)) return "multicast"; if (inV4(value, "240.0.0.0", 4)) return "reserved"; return "public"; } // The 8 hextets of an IPv6 literal (already validated by net.isIPv6). function hextets(address) { let text = address.toLowerCase().split("%")[0]; const dotted = /(\d+\.\d+\.\d+\.\d+)$/.exec(text); if (dotted) { const value = v4(dotted[1]); text = text.slice(0, -dotted[1].length) + `${(value >>> 16).toString(16)}:${(value & 0xffff).toString(16)}`; } const [head, tail] = text.split("::"); const left = head ? head.split(":") : []; const right = tail === undefined ? [] : tail ? tail.split(":") : []; const fill = tail === undefined ? [] : Array(8 - left.length - right.length).fill("0"); return [...left, ...fill, ...right].map((part) => parseInt(part, 16)); } function classifyV6(address) { const h = hextets(address); if (h.every((part) => part === 0)) return "unspecified"; if (h.slice(0, 7).every((part) => part === 0) && h[7] === 1) return "loopback"; if (h.slice(0, 5).every((part) => part === 0) && h[5] === 0xffff) return classifyV4(`${h[6] >> 8}.${h[6] & 255}.${h[7] >> 8}.${h[7] & 255}`); if ((h[0] & 0xffc0) === 0xfe80) return "link-local"; if ((h[0] & 0xff00) === 0xff00) return "multicast"; if (h[0] === 0xfd7a && h[1] === 0x115c && h[2] === 0xa1e0) return h.slice(3, 7).every((part) => part === 0) && h[7] === 0x53 ? "metadata" : "tailnet"; // AWS's IPv6 instance metadata endpoint sits inside the unique-local range. if (h[0] === 0xfd00 && h[1] === 0x0ec2) return "metadata"; if ((h[0] & 0xfe00) === 0xfc00) return "private"; return "public"; } export function classify(address) { if (net.isIPv4(address)) return classifyV4(address); if (net.isIPv6(address)) return classifyV6(address); return "reserved"; } export function allowedCategories(env = process.env) { const extra = String(env.PIPELINES_PEER_ALLOW ?? "") .split(",") .map((value) => value.trim().toLowerCase()) .filter(Boolean); const any = extra.includes("any"); return new Set(["tailnet", "private", ...(any ? OPTIONAL : extra.filter((value) => OPTIONAL.has(value)))]); } export class PeerAddressError extends Error {} export class PeerAddressPolicy { constructor({ env = process.env, lookup = dnsLookup, discovered = discoveredHosts } = {}) { this.allowed = allowedCategories(env); this.lookup = lookup; this.discovered = discovered; } permits(hostname, category) { if (ALWAYS_REFUSED.has(category)) return false; if (this.allowed.has(category)) return true; if (category !== "public") return false; return hostname.endsWith(".ts.net") || this.discovered.has(hostname); } // Resolve once, check every answer, and return a `lookup` that pins the // request's connection to the first checked address. async pin(url) { const hostname = url.hostname.replace(/^\[|\]$/g, "").toLowerCase(); const answers = net.isIP(hostname) ? [{ address: hostname, family: net.isIP(hostname) }] : await this.lookup(hostname, { all: true, verbatim: true }); if (!answers.length) throw new PeerAddressError("Server address did not resolve"); for (const { address } of answers) { const category = classify(address); if (!this.permits(hostname, category)) throw new PeerAddressError(`Server address is not allowed (${category})`); } const { address, family } = answers[0]; return { address, family, lookup: (_name, options, callback) => { const done = typeof options === "function" ? options : callback; if (options?.all) done(null, [{ address, family }]); else done(null, address, family); }, }; } }