fix: 收紧 agent 出网与数据隔离
SSRF 校验补 DNS 解析与每一跳重校验,重定向手动跟随,超时夹紧、响应体截断 2MB;订单工具改用 userId 隔离并转义正则;沙箱固定拦截 .git/.env/密钥文件;环境变量脱敏改为命名模式兜底并删除 env_set。
This commit is contained in:
@@ -1,63 +1,58 @@
|
||||
import { tool } from 'langchain';
|
||||
import * as z from 'zod';
|
||||
import { checkUrlAllowed } from './url_guard.js';
|
||||
import { safeFetch } from './http_client.js';
|
||||
|
||||
export const httpGetTool = tool(
|
||||
async ({ url, headers = {}, timeout = 30000 }) => {
|
||||
// SSRF 防护:仅允许 http/https 且目标不得为环回/内网地址
|
||||
const blocked = checkUrlAllowed(url);
|
||||
if (blocked) {
|
||||
return JSON.stringify({ success: false, error: blocked });
|
||||
}
|
||||
|
||||
async ({ url, headers = {}, timeout = 15000 }) => {
|
||||
try {
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
||||
|
||||
const response = await fetch(url, {
|
||||
// 校验(含 DNS)、重定向控制、超时夹紧、响应体截断统一在 safeFetch 内完成
|
||||
const response = await safeFetch(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'User-Agent': 'XAgent-Bot/1.0',
|
||||
...headers
|
||||
},
|
||||
signal: controller.signal
|
||||
timeout
|
||||
});
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
const contentType = response.headers.get('content-type') || '';
|
||||
const contentType = response.headers['content-type'] || '';
|
||||
let body;
|
||||
|
||||
if (contentType.includes('application/json')) {
|
||||
body = await response.json();
|
||||
try {
|
||||
body = JSON.parse(response.text);
|
||||
} catch (e) {
|
||||
// 服务端声明 json 但返回非法内容时退回原文,避免工具直接抛错中断对话
|
||||
body = response.text;
|
||||
}
|
||||
} else if (contentType.includes('text/') || contentType.includes('application/javascript')) {
|
||||
body = await response.text();
|
||||
body = response.text;
|
||||
} else {
|
||||
const buffer = await response.arrayBuffer();
|
||||
body = Buffer.from(buffer).toString('base64');
|
||||
body = response.buffer.toString('base64');
|
||||
}
|
||||
|
||||
return JSON.stringify({
|
||||
success: true,
|
||||
status: response.status,
|
||||
statusText: response.statusText,
|
||||
headers: Object.fromEntries(response.headers.entries()),
|
||||
headers: response.headers,
|
||||
truncated: response.truncated,
|
||||
body
|
||||
});
|
||||
} catch (error) {
|
||||
if (error.name === 'AbortError') {
|
||||
return JSON.stringify({ success: false, error: `Request timed out after ${timeout}ms` });
|
||||
return JSON.stringify({ success: false, error: 'Request timed out' });
|
||||
}
|
||||
return JSON.stringify({ success: false, error: error.message });
|
||||
}
|
||||
},
|
||||
{
|
||||
name: 'http_get',
|
||||
description: 'Send HTTP GET request and return response content. Supports custom headers and timeout settings.',
|
||||
description: 'Send HTTP GET request and return response content. Response body is capped at 2MB.',
|
||||
schema: z.object({
|
||||
url: z.string().describe('Request URL'),
|
||||
headers: z.record(z.string(), z.string()).optional().describe('Request headers'),
|
||||
timeout: z.number().optional().describe('Timeout in milliseconds, default 30000')
|
||||
timeout: z.number().optional().describe('Timeout in milliseconds, clamped to 1s-15s')
|
||||
})
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user