fix: 收紧 agent 出网与数据隔离
SSRF 校验补 DNS 解析与每一跳重校验,重定向手动跟随,超时夹紧、响应体截断 2MB;订单工具改用 userId 隔离并转义正则;沙箱固定拦截 .git/.env/密钥文件;环境变量脱敏改为命名模式兜底并删除 env_set。
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import { tool } from "@langchain/core/tools";
|
||||
import z from "zod";
|
||||
import { DBModel } from "../../../../models/index.js";
|
||||
import { escapeRegex } from "./utils.js";
|
||||
|
||||
function createEscortRecordQueryTool(userInfo) {
|
||||
const userMobile = userInfo?.profile?.mobile;
|
||||
const userId = userInfo?._id;
|
||||
|
||||
return tool(
|
||||
async ({ patientName, status, page = 1, pageSize = 20 }) => {
|
||||
@@ -11,17 +12,18 @@ function createEscortRecordQueryTool(userInfo) {
|
||||
const filter = {};
|
||||
|
||||
if (patientName) {
|
||||
filter["patient.name"] = { $regex: patientName, $options: "i" };
|
||||
filter["patient.name"] = { $regex: escapeRegex(patientName), $options: "i" };
|
||||
}
|
||||
|
||||
// 强制使用当前登录用户的手机号进行查询
|
||||
if (!userMobile) {
|
||||
// 按订单归属用户过滤:不能用手机号作为隔离键,
|
||||
// 因为手机号可由用户自行修改(改到他人号码即可读到他人订单)
|
||||
if (!userId) {
|
||||
return {
|
||||
success: false,
|
||||
error: "用户未登录或手机号缺失,无法查询陪诊记录",
|
||||
error: "用户未登录,无法查询陪诊记录",
|
||||
};
|
||||
}
|
||||
filter["patient.mobile"] = userMobile;
|
||||
filter.userId = userId;
|
||||
|
||||
if (status) {
|
||||
filter.status = status;
|
||||
@@ -54,7 +56,7 @@ function createEscortRecordQueryTool(userInfo) {
|
||||
{
|
||||
name: "escort_record_query",
|
||||
description:
|
||||
"Query escort records from database for the current user. Supports filtering by patient name and appointment status. Returns paginated results sorted by appointment date in descending order. The query is automatically scoped to the current logged-in user's mobile phone number.",
|
||||
"Query escort records from database for the current user. Supports filtering by patient name and appointment status. Returns paginated results sorted by appointment date in descending order. The query is automatically scoped to the current logged-in user.",
|
||||
schema: z.object({
|
||||
patientName: z
|
||||
.string()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { tool } from "@langchain/core/tools";
|
||||
import z from "zod";
|
||||
import { DBModel } from "../../../../models/index.js";
|
||||
import { escapeRegex } from "./utils.js";
|
||||
|
||||
const escortRecordQueryTool = tool(
|
||||
async ({ patientName, mobile, status, page = 1, pageSize = 20 }) => {
|
||||
@@ -8,7 +9,7 @@ const escortRecordQueryTool = tool(
|
||||
const filter = {};
|
||||
|
||||
if (patientName) {
|
||||
filter["patient.name"] = { $regex: patientName, $options: "i" };
|
||||
filter["patient.name"] = { $regex: escapeRegex(patientName), $options: "i" };
|
||||
}
|
||||
|
||||
if (mobile) {
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* 数据库工具的公共小工具
|
||||
*/
|
||||
|
||||
/**
|
||||
* 转义正则元字符,用于把用户/模型输入当作字面量做模糊匹配
|
||||
*
|
||||
* 直接把输入拼进 $regex 有两个问题:用户输入 "(" 会导致查询报错,
|
||||
* 输入嵌套量词(如 (a+)+$)可能触发 ReDoS。这里统一转义并截断长度。
|
||||
*
|
||||
* @param {string} input 原始关键词
|
||||
* @param {number} maxLength 最大参与匹配的长度
|
||||
* @returns {string}
|
||||
*/
|
||||
function escapeRegex(input, maxLength = 64) {
|
||||
if (typeof input !== "string") return "";
|
||||
return input
|
||||
.slice(0, maxLength)
|
||||
.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
|
||||
export { escapeRegex };
|
||||
Reference in New Issue
Block a user