fix: 收紧 agent 出网与数据隔离
SSRF 校验补 DNS 解析与每一跳重校验,重定向手动跟随,超时夹紧、响应体截断 2MB;订单工具改用 userId 隔离并转义正则;沙箱固定拦截 .git/.env/密钥文件;环境变量脱敏改为命名模式兜底并删除 env_set。
This commit is contained in:
@@ -6,7 +6,7 @@ import { FilesystemBackend } from "deepagents";
|
||||
*
|
||||
* 相比原生非 virtualMode 模式的两个安全缺口:
|
||||
* 1. 原生 resolvePath 对绝对路径原样放行(可读磁盘任意文件),此处强制锁定在 rootDir 内;
|
||||
* 2. 沙箱根下的敏感文件(conf.json、.env*、logs、dataDir)默认禁止访问,
|
||||
* 2. 沙箱根下的敏感文件(版本库元数据、.env*、conf.json、logs、dataDir)默认禁止访问,
|
||||
* 防止 LLM 通过 read_file/grep 读取密钥、数据库连接串及其他用户记忆。
|
||||
*
|
||||
* 注意:memory(AGENTS.md)与 skills(SKILL.md)也经由 backend 加载,
|
||||
@@ -17,6 +17,11 @@ import { FilesystemBackend } from "deepagents";
|
||||
* 永远匹配不上(grep 会泄露 conf.json 内容)。敏感路径拦截必须保持在后端层实现。
|
||||
*/
|
||||
export class RestrictedFilesystemBackend extends FilesystemBackend {
|
||||
// 任意层级都拦截的名称:.git 含历史提交(可能残留已轮换的密钥),conf.json 含密钥与库口令
|
||||
static DENY_SEGMENTS = [".git", ".env", "conf.json", "id_rsa", "id_ed25519"];
|
||||
// 按后缀拦截的密钥/证书文件
|
||||
static DENY_SUFFIXES = [".pem", ".key", ".p12", ".pfx", ".keystore"];
|
||||
|
||||
constructor(options = {}) {
|
||||
super(options);
|
||||
this.denyPaths = (options.deny ?? ["conf.json", "logs"]).map(d => String(d).toLowerCase().replace(/\/+$/, ""));
|
||||
@@ -27,8 +32,18 @@ export class RestrictedFilesystemBackend extends FilesystemBackend {
|
||||
const rel = path.relative(this.cwd, fullPath);
|
||||
if (!rel || rel.startsWith("..") || path.isAbsolute(rel)) return false;
|
||||
const norm = rel.split(path.sep).join("/").toLowerCase();
|
||||
// 根目录下的 .env / .env.local 等
|
||||
if (norm === ".env" || norm.startsWith(".env.")) return true;
|
||||
|
||||
// 固定拦截项与调用方传入的 deny 列表并列判断,避免使用方忘记配置
|
||||
for (const seg of norm.split("/")) {
|
||||
if (
|
||||
RestrictedFilesystemBackend.DENY_SEGMENTS.includes(seg) ||
|
||||
seg.startsWith(".env.")
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (RestrictedFilesystemBackend.DENY_SUFFIXES.some(s => norm.endsWith(s))) return true;
|
||||
|
||||
return this.denyPaths.some(d => norm === d || norm.startsWith(d + "/"));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user