/** * 出网 URL 安全校验(SSRF 防护) * * Agent 的 HTTP 工具处理的 URL 来自模型输出,而模型输入含不可信的用户内容, * 因此可能被诱导去访问环回/内网地址(如本机 127.0.0.1 上的其他服务、云元数据地址)。 * * 两层校验: * 1. checkUrlAllowed —— 同步校验协议与字面量主机(覆盖直接写内网 IP 的情况); * 2. checkUrlAllowedAsync —— 再对域名做 DNS 解析,逐个校验解析结果 * (覆盖「公网域名解析到内网地址」的绕过)。仅做同步校验是不够的。 */ import dns from "dns/promises"; import net from "net"; // 仅允许的协议 const ALLOWED_PROTOCOLS = ["http:", "https:"]; // 本地主机名 const BLOCKED_HOSTNAMES = new Set(["localhost", "localhost.localdomain", "ip6-localhost"]); /** * 是否为内网/保留 IPv4 地址 * @param {string} host 形如 10.0.0.1 的地址 */ function isPrivateIPv4(host) { const parts = host.split("."); if (parts.length !== 4) return false; const nums = parts.map((p) => (/^\d+$/.test(p) ? Number(p) : NaN)); if (nums.some((n) => !Number.isInteger(n) || n < 0 || n > 255)) return false; const [a, b] = nums; if (a === 0 || a === 10 || a === 127) return true; // 当前网络/内网/环回 if (a === 100 && b >= 64 && b <= 127) return true; // 100.64.0.0/10 运营商级 NAT if (a === 169 && b === 254) return true; // 169.254.0.0/16 链路本地(含云 metadata) if (a === 172 && b >= 16 && b <= 31) return true; // 172.16.0.0/12 if (a === 192 && b === 168) return true; // 192.168.0.0/16 if (a === 192 && b === 0) return true; // 192.0.0.0/24 IETF 保留 if (a === 198 && (b === 18 || b === 19)) return true; // 198.18.0.0/15 基准测试 if (a >= 224) return true; // 组播与保留段 return false; } /** * 是否为内网/保留 IPv6 地址 * @param {string} host 不带方括号的 IPv6 地址 */ function isPrivateIPv6(host) { const h = host.toLowerCase(); if (h === "::1" || h === "::") return true; if (/^f[cd]/.test(h)) return true; // 唯一本地 fc00::/7 if (/^fe[89ab]/.test(h)) return true; // 链路本地 fe80::/10 // IPv4 映射地址:URL 会把 ::ffff:127.0.0.1 规范化成 ::ffff:7f00:1 const mapped = h.match(/^::ffff:(\d+\.\d+\.\d+\.\d+)$/); if (mapped) return isPrivateIPv4(mapped[1]); const mappedHex = h.match(/^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/); if (mappedHex) { const hi = parseInt(mappedHex[1], 16); const lo = parseInt(mappedHex[2], 16); return isPrivateIPv4(`${hi >> 8}.${hi & 255}.${lo >> 8}.${lo & 255}`); } // NAT64 64:ff9b::/96 同样内嵌 IPv4 const nat64 = h.match(/^64:ff9b::([0-9a-f]{1,4}):([0-9a-f]{1,4})$/); if (nat64) { const hi = parseInt(nat64[1], 16); const lo = parseInt(nat64[2], 16); return isPrivateIPv4(`${hi >> 8}.${hi & 255}.${lo >> 8}.${lo & 255}`); } return false; } /** * 是否为内网/保留地址(IPv4 或 IPv6 字面量) * @param {string} address IP 地址 */ function isPrivateAddress(address) { return net.isIP(address) === 6 ? isPrivateIPv6(address) : isPrivateIPv4(address); } /** * 同步校验 URL:协议、字面量主机、内网 IP * @param {string} url 待校验的 URL * @returns {string|null} 不允许时返回原因文案,允许时返回 null */ function checkUrlAllowed(url) { let parsed; try { parsed = new URL(url); } catch (e) { return `Invalid URL: ${url}`; } if (!ALLOWED_PROTOCOLS.includes(parsed.protocol)) { return `Blocked protocol: ${parsed.protocol}`; } // URL.hostname 对 IPv6 会带方括号,统一去掉并转小写 const hostname = parsed.hostname.replace(/^\[|\]$/g, "").toLowerCase(); if (BLOCKED_HOSTNAMES.has(hostname)) { return `Blocked host: ${hostname}`; } if (isPrivateAddress(hostname)) { return `Blocked private network address: ${hostname}`; } return null; } /** * 先同步校验,再解析域名并逐个校验解析结果 * @param {string} url 待校验的 URL * @returns {Promise} 不允许时返回原因文案,允许时返回 null */ async function checkUrlAllowedAsync(url) { const blocked = checkUrlAllowed(url); if (blocked) return blocked; const hostname = new URL(url).hostname.replace(/^\[|\]$/g, "").toLowerCase(); if (net.isIP(hostname)) return null; // 字面量地址已在同步阶段校验 let addresses; try { addresses = await dns.lookup(hostname, { all: true }); } catch (e) { return `DNS lookup failed: ${hostname}`; } if (!addresses.length) return `DNS lookup returned no address: ${hostname}`; for (const { address } of addresses) { if (isPrivateAddress(address)) { return `Blocked private network address: ${hostname} -> ${address}`; } } return null; } export { checkUrlAllowed, checkUrlAllowedAsync, isPrivateAddress, isPrivateIPv4, isPrivateIPv6, };