fix: 收紧 agent 出网与数据隔离
SSRF 校验补 DNS 解析与每一跳重校验,重定向手动跟随,超时夹紧、响应体截断 2MB;订单工具改用 userId 隔离并转义正则;沙箱固定拦截 .git/.env/密钥文件;环境变量脱敏改为命名模式兜底并删除 env_set。
This commit is contained in:
@@ -3,9 +3,16 @@
|
||||
*
|
||||
* Agent 的 HTTP 工具处理的 URL 来自模型输出,而模型输入含不可信的用户内容,
|
||||
* 因此可能被诱导去访问环回/内网地址(如本机 127.0.0.1 上的其他服务、云元数据地址)。
|
||||
* 这里集中做白名单式校验:仅允许 http/https,且目标不能是环回/内网/链路本地地址。
|
||||
*
|
||||
* 两层校验:
|
||||
* 1. checkUrlAllowed —— 同步校验协议与字面量主机(覆盖直接写内网 IP 的情况);
|
||||
* 2. checkUrlAllowedAsync —— 再对域名做 DNS 解析,逐个校验解析结果
|
||||
* (覆盖「公网域名解析到内网地址」的绕过)。仅做同步校验是不够的。
|
||||
*/
|
||||
|
||||
import dns from "dns/promises";
|
||||
import net from "net";
|
||||
|
||||
// 仅允许的协议
|
||||
const ALLOWED_PROTOCOLS = ["http:", "https:"];
|
||||
|
||||
@@ -28,15 +35,56 @@ function isPrivateIPv4(host) {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验 URL 是否允许出网访问
|
||||
* 是否为内网/保留 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
|
||||
*/
|
||||
export function checkUrlAllowed(url) {
|
||||
function checkUrlAllowed(url) {
|
||||
let parsed;
|
||||
try {
|
||||
parsed = new URL(url);
|
||||
@@ -54,21 +102,45 @@ export function checkUrlAllowed(url) {
|
||||
if (BLOCKED_HOSTNAMES.has(hostname)) {
|
||||
return `Blocked host: ${hostname}`;
|
||||
}
|
||||
if (isPrivateIPv4(hostname)) {
|
||||
if (isPrivateAddress(hostname)) {
|
||||
return `Blocked private network address: ${hostname}`;
|
||||
}
|
||||
if (hostname.includes(":")) {
|
||||
// IPv6:环回 ::1 / 未指定 :: / 唯一本地 fc00::、fd00:: / 链路本地 fe80::
|
||||
if (hostname === "::1" || hostname === "::") {
|
||||
return `Blocked private network address: ${hostname}`;
|
||||
}
|
||||
if (/^f[cd]/.test(hostname)) {
|
||||
return `Blocked private network address: ${hostname}`;
|
||||
}
|
||||
if (/^fe[89ab]/.test(hostname)) {
|
||||
return `Blocked private network address: ${hostname}`;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 先同步校验,再解析域名并逐个校验解析结果
|
||||
* @param {string} url 待校验的 URL
|
||||
* @returns {Promise<string|null>} 不允许时返回原因文案,允许时返回 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,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user