fix: 收紧 agent 出网与数据隔离

SSRF 校验补 DNS 解析与每一跳重校验,重定向手动跟随,超时夹紧、响应体截断 2MB;订单工具改用 userId 隔离并转义正则;沙箱固定拦截 .git/.env/密钥文件;环境变量脱敏改为命名模式兜底并删除 env_set。
This commit is contained in:
lik
2026-10-24 22:04:59 +08:00
committed by trae-solo-agent
parent 5e10096fea
commit 1774239553
9 changed files with 325 additions and 112 deletions
+17 -22
View File
@@ -1,15 +1,9 @@
import { tool } from "@langchain/core/tools";
import z from "zod";
import { checkUrlAllowed } from "./url_guard.js";
import { safeFetch } from "./http_client.js";
const httpPostTool = tool(
async ({ url, body, headers = {}, contentType = 'json', timeout = 30000 }) => {
// SSRF 防护:仅允许 http/https 且目标不得为环回/内网地址
const blocked = checkUrlAllowed(url);
if (blocked) {
return { success: false, error: blocked };
}
async ({ url, body, headers = {}, contentType = 'json', timeout = 15000 }) => {
let requestBody;
const requestHeaders = {
'User-Agent': 'XAgent-Bot/1.0',
@@ -38,50 +32,51 @@ const httpPostTool = tool(
}
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
const response = await fetch(url, {
// 校验(含 DNS)、超时夹紧、响应体截断统一在 safeFetch 内完成;POST 不跟随重定向
const response = await safeFetch(url, {
method: 'POST',
headers: requestHeaders,
body: requestBody,
signal: controller.signal
timeout
});
clearTimeout(timeoutId);
const responseContentType = response.headers.get('content-type') || '';
const responseContentType = response.headers['content-type'] || '';
let responseBody;
if (responseContentType.includes('application/json')) {
responseBody = await response.json();
try {
responseBody = JSON.parse(response.text);
} catch (e) {
responseBody = response.text;
}
} else {
responseBody = await response.text();
responseBody = response.text;
}
return {
success: true,
status: response.status,
statusText: response.statusText,
headers: Object.fromEntries(response.headers.entries()),
headers: response.headers,
truncated: response.truncated,
body: responseBody
};
} catch (error) {
if (error.name === 'AbortError') {
return { success: false, error: `Request timed out after ${timeout}ms` };
return { success: false, error: 'Request timed out' };
}
return { success: false, error: error.message };
}
},
{
name: "http_post",
description: "Send HTTP POST request. Supports JSON and form data.",
description: "Send HTTP POST request. Supports JSON and form data. Response body is capped at 2MB.",
schema: z.object({
url: z.string().describe("Request URL"),
body: z.union([z.string(), z.object({})]).describe("Request body, can be string or JSON object"),
headers: z.record(z.string()).optional().describe("Request headers"),
contentType: z.enum(['json', 'form', 'text', 'raw']).optional().describe("Content type, default json"),
timeout: z.number().optional().describe("Timeout in milliseconds, default 30000")
timeout: z.number().optional().describe("Timeout in milliseconds, clamped to 1s-15s")
}),
},
);